1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
32
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
51 Assert.notNull(context);
52 this.context = context;
53 context.set(this);
54
55 Address addr;
56 Performative msg;
57
58
59 for(int i = 0; i < eventMonitor.length; i++ )
60 eventMonitor[i] = new HSet();
61 addressBook = new FIPA_AddressBook();
62 context.set(addressBook);
63
64
65 fipaServer = new Zeus_ACC_Server(context,this);
66
67 server = new Server(context,this,inMail);
68 myAddress = server.getAddress();
69
70
71
72 postman = new PostMan[3];
73
74 postman[0] = new PostMan(this,outMail,ccMail,myAddress);
75
76
77 postman[1] = new PostMan(this,ccMail,myAddress);
78
79
80
81 postman[2] = new FIPA_PostMan (this, fipaOut,ccMail, myAddress);
82
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
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
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
139
140
141
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() ) {
151 asTable.remove(name);
152 context.MsgHandler().removeRule(data.key);
153 }
154 }
155 data = (KeyValue)asTable.get(agent);
156
157 if (data == null) {
158
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
178
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 }