View Javadoc

1   
2   /*
3   * The contents of this file are subject to the BT "ZEUS" Open Source 
4   * Licence (L77741), Version 1.0 (the "Licence"); you may not use this file 
5   * except in compliance with the Licence. You may obtain a copy of the Licence
6   * from $ZEUS_INSTALL/licence.html or alternatively from
7   * http://www.labs.bt.com/projects/agents/zeus/licence.htm
8   * 
9   * Except as stated in Clause 7 of the Licence, software distributed under the
10  * Licence is distributed WITHOUT WARRANTY OF ANY KIND, either express or 
11  * implied. See the Licence for the specific language governing rights and 
12  * limitations under the Licence.
13  * 
14  * The Original Code is within the package zeus.*.
15  * The Initial Developer of the Original Code is British Telecommunications
16  * public limited company, whose registered office is at 81 Newgate Street, 
17  * London, EC1A 7AJ, England. Portions created by British Telecommunications 
18  * public limited company are Copyright 1996-2001. All Rights Reserved.
19  * 
20  * THIS NOTICE MUST BE INCLUDED ON ANY COPY OF THIS FILE
21  */
22  package zeus.agents;
23  import zeus.actors.*;
24  import zeus.concepts.*; 
25  import java.net.*; 
26  /*** 
27      FIPA_AMS_Services is used by the ACC agent to wrap the Nameserver 
28      so that it can be accessed exernally. 
29      */
30  public class FIPA_Services { 
31     
32      protected AgentContext context = null; 
33      protected String name = null; 
34      protected String host = null;
35      protected String type = null; 
36      
37      
38         
39            /***
40            set the agent using this to handle messages that are ".*receiver df@hap.*content (register"
41          by calling the handleRegister method in this class
42          */
43      public void setRegister () { 
44          MsgHandler handler = context.getMsgHandler();
45  	    String msg_pattern[] = {"receiver",type,"content","//A(.*)//((//s*)register(.*)//Z"};
46  	    handler.addRule(new MessageRuleImpl(context.newId("Rule"),msg_pattern, this, "handleRegister"));
47  	}
48  	
49  	
50  	
51  	
52  	
53  	public void setDeregister () { 
54  	    MsgHandler handler = context.getMsgHandler();
55  	    String msg_pattern[] = {"receiver",type,"content","//A(.*)deregister(.*)//Z"};
56  	    handler.addRule(new MessageRuleImpl(context.newId("Rule"),msg_pattern, this, "handleDeregister"));
57  	}
58  	
59  	
60  	
61  	
62  	public void setModify () { 
63  	    MsgHandler handler = context.getMsgHandler();
64  	    String msg_pattern[] = {"receiver",type,"content","//A(.*)modify(.*)//Z"};
65  	    handler.addRule(new MessageRuleImpl(context.newId("Rule"),msg_pattern, this, "handleModify"));
66  	}
67  	
68  		
69  		
70  	public void setSearch() { 
71  	    MsgHandler handler = context.getMsgHandler();
72  	    String msg_pattern[] = {"receiver",type,"content","//A(.*)search(.*)//Z"};
73  	    handler.addRule(new MessageRuleImpl(context.newId("Rule"),msg_pattern, this, "handleSearch"));
74  	}
75  	
76  		
77  	    /***
78          set the name of the agent to df@host - if the host has been set, else
79          set it to df@localhost
80          */
81      public void setName () { 
82          name = new String (this.type + "@"); 
83          if (host == null) { 
84              try {
85                  InetAddress ip = InetAddress.getLocalHost();
86                  String localhost = ip.getHostAddress();
87                  name += localhost; }
88                  catch (Exception e) { 
89                      System.out.println("network configuration problems in " + type); 
90                      System.out.println("Exception thrown : " ); 
91                      e.printStackTrace(); 
92                      System.out.println("setting name to " + type + "@127.0.0.1"); 
93                      name += "127.0.0.1"; }
94              } else {
95                  name += host; 
96              }
97              
98      }
99      
100     
101     /***
102         set the name of the df to some arbitary value @param name
103         This method should not normally be used.
104         */
105     public void setName (String name) { 
106         this.name = name; 
107     }
108     
109     
110     /***
111         set the hap part of the df@hap name of the df to some value 
112         other than the default df@localhost (where local host is the ip address of 
113         this machine
114     */
115     public void setHost (String host) { 
116         this.host = host; 
117     }
118 	
119 	
120 	 /***
121         send a registration to the nameservers that we are using. <br>
122         @param sender is the name of the alias to use
123         */
124     protected void registerAlias (String name) {
125      // String name = address.getName();
126       MailBox mbox = context.getMailBox();
127       AddressBook addressBook = context.getAddressBook();
128       for(int i = 0; i < context.nameservers().size(); i++ ) {
129         Address addr = (Address)context.nameservers().elementAt(i);
130         addressBook.add(addr);
131         Performative msg = new Performative("request");
132         msg.setReceiver(addr.getName());
133         msg.setReplyWith(name);
134         msg.setContent("register");
135         msg.setSender (name);
136         mbox.sendMsg(msg);
137         System.out.println("tried to send " + msg.toString());
138     }
139         System.out.println("finished attempted registration"); 
140   }
141 
142 
143 public void send (Performative perf){ 
144         MailBox mbox = context.getMailBox(); 
145         mbox.sendMsg(perf); 
146     }
147     
148 
149 }