View Javadoc

1   /*
2   * The contents of this file are subject to the BT "ZEUS" Open Source 
3   * Licence (L77741), Version 1.0 (the "Licence"); you may not use this file 
4   * except in compliance with the Licence. You may obtain a copy of the Licence
5   * from $ZEUS_INSTALL/licence.html or alternatively from
6   * http://www.labs.bt.com/projects/agents/zeus/licence.htm
7   * 
8   * Except as stated in Clause 7 of the Licence, software distributed under the
9   * Licence is distributed WITHOUT WARRANTY OF ANY KIND, either express or 
10  * implied. See the Licence for the specific language governing rights and 
11  * limitations under the Licence.
12  * 
13  * The Original Code is within the package zeus.*.
14  * The Initial Developer of the Original Code is British Telecommunications
15  * public limited company, whose registered office is at 81 Newgate Street, 
16  * London, EC1A 7AJ, England. Portions created by British Telecommunications 
17  * public limited company are Copyright 1996-9. All Rights Reserved.
18  * 
19  * THIS NOTICE MUST BE INCLUDED ON ANY COPY OF THIS FILE
20  */
21  
22  
23  package zeus.actors.intrays;
24  import zeus.actors.*;
25  import zeus.util.*;
26  import zeus.concepts.*;
27  import zeus.actors.event.*;
28  import java.util.*;
29  import zeus.actors.factories.*;
30  import zeus.actors.outtrays.*;
31  // the fact that I am importing *everything* makes me think that this is 
32  // in the wrong package...
33  
34  /***
35      zeus.actors.fipa.FIPA_Mailbox extends the normal zeus.actors.MailBox.
36      In fact it is almost the same. The only changes are that the PostMan[] postman
37      and Server server objects are overwritten with FIPA_PostMan and FIPA_Server 
38      instances. Since these are functionally equivalent to PostMan and Server 
39      everything else can be let be. 
40      */ 
41  public class Zeus_ACC_MailBox extends zeus.actors.MailBox { 
42    
43  
44    
45    private FIPA_AddressBook addressBook;
46    protected Queue       fipaOut = new Queue("fipaOut");
47    protected Zeus_ACC_Server fipaServer; 
48    
49    public Zeus_ACC_MailBox(AgentContext context) {
50      //super (context); 
51      Assert.notNull(context);
52      this.context = context;
53      context.set(this);
54  
55      Address addr;
56      Performative msg;
57  
58      // setup event-monitor db
59      for(int i = 0; i < eventMonitor.length; i++ )
60         eventMonitor[i] = new HSet();
61      addressBook = new FIPA_AddressBook();
62      context.set(addressBook);
63      // HERE is the FIPA_Server 
64  
65      fipaServer = new Zeus_ACC_Server(context,this);
66  
67      server = new Server(context,this,inMail);
68      myAddress = server.getAddress();
69      
70  
71      // HERE is the FIPA_PostMan
72      postman = new PostMan[3];
73      // normal postmen..
74      postman[0] = new PostMan(this,outMail,ccMail,myAddress);
75      // this one is not a FIPA_PostMan, cause is is just
76      // sending mail to the visualiser!
77      postman[1] = new PostMan(this,ccMail,myAddress);
78      // knock
79      // knock! Ha Ha HA .
80     
81      postman[2] = new FIPA_PostMan (this, fipaOut,ccMail, myAddress); 
82      // Register with Name Servers
83      String key = context.newId();
84      String[] pattern = { "type", "inform", "in-reply-to", key };
85  
86      context.MsgHandler().addRule(new MessageRuleImpl(context.newId("Rule"),
87  	       pattern,MessageActionImpl.EXECUTE_ONCE,this,"register"));
88  
89      for(int i = 0; i < context.nameservers().size(); i++ ) {
90         addr = (Address)context.nameservers().elementAt(i);
91         context.AddressBook().add(addr);
92  
93         msg = new Performative("request");
94         msg.setReceiver(addr.getName());
95         msg.setReplyWith(key);
96         msg.setContent("register");
97         sendMsg(msg);
98      }   
99   
100   		//$$ zeus_ACC_MailBox1.move(0,0);
101 }
102   
103   
104   public FIPA_PostMan getFIPA_PostMan () { 
105     return (FIPA_PostMan) this.postman[2]; 
106   }
107   
108   
109   public Zeus_ACC_Server getZeus_ACC_Server() { 
110     return fipaServer;
111   }
112   
113   
114   public InTray getInTray () { 
115     return fipaServer; 
116   }
117   
118   
119   public boolean is_FIPA (String agent_address_string) {
120         if (addressBook.lookupFIPA(agent_address_string)!=null) { 
121             return true; 
122         }
123         return false; 
124         // should return true if this address is a FIPA address
125   }
126   
127   
128   /***
129     lookup the address of a FIPA agent from the name that is being
130     used for it in the Zeus agency. 
131     */
132   public FIPA_AID_Address FIPA_Lookup(String agent_address_string){
133     return (addressBook.lookupAlias(agent_address_string)); 
134   }
135   
136   
137  public String addressSought(String agent) {
138     // First we should clear 'asTable' of entries older than a predefined
139     // age so that our agent should query known nameservers for the
140     // receiver's address. This way, a receiver that went off-line and
141     // later comes online would be found.
142 
143     String name;
144     KeyValue data;
145     double now = context.now();
146     Enumeration enum = asTable.keys();
147     while( enum.hasMoreElements() ) {
148       name = (String) enum.nextElement();
149       data = (KeyValue)asTable.get(name);
150       if ( now-data.value >= context.getAddressBookRefresh() ) { // LL 040500 1.03b 
151         asTable.remove(name);
152          context.MsgHandler().removeRule(data.key);
153       }
154     }
155     data = (KeyValue)asTable.get(agent);
156 
157     if (data == null) {
158       // try contacting known nameservers to find agent's address
159       Performative query;
160       Address addr;
161 
162       String key = context.newId();
163       String[] pattern = { "type", "inform", "in-reply-to", key };
164       context.MsgHandler().addRule(new MessageRuleImpl(key,pattern,
165          MessageActionImpl.EXECUTE_ONCE,this,"addressReceived")
166       );
167       for(int i = 0; i < context.nameservers().size(); i++ ) {
168 
169          addr = (Address)context.nameservers().elementAt(i);
170          query = new Performative("query-ref");
171          query.setReceiver(addr.getName());
172          query.setReplyWith(key);
173          query.setContent("address_of " + agent);
174          sendMsg(query);
175       }
176 
177       // add receiver to list of agents whose addresses
178       // are being looked for
179       now = context.now();
180       if ( !context.nameservers().isEmpty() ) {
181          now += context.getAddressTimeout();
182          asTable.put(agent,new KeyValue(key,now));
183          return key;
184       }
185       else {
186          return null;
187       }
188     }
189 
190     else if ( data.value > context.now()){
191       return data.key;}
192     else{
193       return null;}
194   }
195 
196   public void addressReceived(Performative msg) {
197     String key = msg.getInReplyTo();
198     Address  address = ZeusParser.address(msg.getContent());
199     add(address);
200     asTable.remove(address.getName());
201     for(int i = 0; i < postman.length; i++ )
202        postman[i].addressReceived(key);
203   }   
204     
205 	
206 }