1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package zeus.agents;
23
24 import zeus.actors.outtrays.*;
25 import zeus.actors.factories.*;
26 import zeus.actors.intrays.*;
27 import java.util.*;
28 import java.io.*;
29 import zeus.util.*;
30 import zeus.concepts.*;
31 import zeus.actors.*;
32 import zeus.agents.*;
33
34 import zeus.actors.rtn.Engine;
35 import zeus.rete.ReteEngine;
36
37 import zeus.concepts.xmlobject.acc.Contacts;
38 import zeus.concepts.xmlobject.acc.Contact;
39 import zeus.concepts.xmlobject.acc.ContactsParser;
40
41
42 /***
43 * ACC is a Zeus agent that has been set up to act as a FIPA ACC.
44 * This means that the ACC agent is the point of contact between agents in
45 * a Zeus naming domain and agents in a FIPA naming domain
46 */
47 public class ACC implements ZeusExternal {
48
49 protected static void version() {
50 System.err.println("ZeusAgent - ACC version: 1.2");
51
52 }
53
54
55 protected static void usage() {
56 System.err.println("Usage: java ACC -s <dns_file> [-gui ViewerProg] ] [-debug] [-fipaNames fipNameFileName] [-transports transportConfigFile");
57 System.exit(0);
58 }
59
60
61 public static void main(String[] arg) {
62 String name = "ACC";
63 Vector cmdLineFact = new Vector(10);
64
65 BasicAgent agent;
66 String external = null;
67 String dns_file = null;
68 String resource = null;
69 String gui = null;
70 String ontology_file = null;
71 Vector nameservers = null;
72 Bindings b;
73 FileInputStream stream = null;
74 ZeusExternal user_prog = null;
75 String fipaNames = "contacts.xml";
76 String transports = null;
77 String filename = null;
78
79
80 for( int j = 0; j < arg.length; j++ ) {
81 if ( arg[j].equals("-s") && ++j < arg.length )
82 dns_file = arg[j];
83 if (arg[j].equals("-gui") && ++j < arg.length)
84 gui = arg[j];
85 if (arg[j].equals("-fipaNames") && ++j < arg.length) {
86 fipaNames = arg[j];
87 }
88 if (arg[j].equals("-transports") && ++j < arg.length)
89 transports = arg[j];
90 if (arg[j].equals("-o") && ++j <arg.length)
91 filename = arg[j];
92 else if ( arg[j].equals("-debug") ) {
93 Core.debug = true;
94 Core.setDebuggerOutputFile("ACC.log");
95 }
96
97 }
98
99 b = new Bindings(name);
100
101
102 if ( dns_file == null ) {
103 System.err.println("Domain nameserver file must be specified with -s option");
104 usage();
105 }
106
107 try {
108 nameservers = ZeusParser.addressList(new FileInputStream(dns_file));
109 System.out.println ("namerservers = " + nameservers);
110 if ( nameservers == null || nameservers.isEmpty() )
111 throw new IOException();
112
113 ACC acc = new ACC();
114 if (transports != null) {
115 acc.initialiseTransports(transports);}
116
117 agent = new ACCAgent(name,name,nameservers);
118
119 AgentContext context = agent.getAgentContext();
120
121 OntologyDb db = new OntologyDb(context.GenSym());
122 context.set(db);
123 int status = 0;
124 if (filename == null) {
125 status = db.openFile(new File("ACC.ont"));}
126 else
127 status = db.openFile(new File(filename));
128 new OrganisationDb(context);
129 new ResourceDb(context);
130 new Engine(context);
131 new TaskDb(context);
132
133 new Planner(context,4,40);
134 new ExecutionMonitor(context);
135
136
137 new ProtocolDb(context);
138
139
140
141
142
143 Class c;
144
145 if ( resource != null ) {
146 c = Class.forName(resource);
147 ExternalDb oracle = (ExternalDb) c.newInstance();
148 context.set(oracle);
149 oracle.set(context);
150 }
151 if ( gui != null ) {
152 c = Class.forName(gui);
153 ZeusAgentUI ui = (ZeusAgentUI)c.newInstance();
154 context.set(ui);
155 ui.set(context);
156 }
157
158
159
160
161 ProtocolInfo info;
162
163
164
165 AbilityDbItem item;
166
167
168
169
170
171
172 context.set(acc);
173 acc.exec(context);
174
175
176
177 if (fipaNames != null ) {
178 System.out.println("attempting to register names");
179 acc.fipaNames = fipaNames;
180 acc.loadFIPAAliases();
181 }
182
183
184
185 FIPA_DF_Services fdf = new FIPA_DF_Services();
186 System.out.println("calling fdf");
187 fdf.exec(context);
188
189
190 FIPA_AMS_Services fams = new FIPA_AMS_Services();
191 System.out.println("calling fams");
192 fams.exec(context);
193 }
194 catch (Exception e) {
195 e.printStackTrace(); }
196 }
197
198
199 String fipaNames = null;
200 AgentContext context = null;
201 MsgHandler msg = null;
202
203 public void exec(AgentContext context) {
204 this.context = context;
205
206
207
208 msg = context.MsgHandler();
209 setMessageRules();
210
211 }
212
213
214 public void setMessageRules() {
215 String FIPA_forward_request[] = {"type", "request", "content", "//A(action acc(//s*)(forward (//s*)(.*)))//Z"};
216 msg.addRule(new MessageRuleImpl(context.newId("Rule"), FIPA_forward_request, this, "forward_message"));
217 String FIPA_inform_new_contact[] = {"type", "inform", "content", "//A(<contact//s)(.*)(/>)//Z"};
218 msg.addRule(new MessageRuleImpl(context.newId("Rule"), FIPA_inform_new_contact, this, "add_new_contact"));
219 }
220
221
222 public void forward_message(Performative perf) {
223 System.out.println(perf.toString());
224 }
225
226 public File contactsDb = null;
227 public Contacts contactsRoot = new Contacts();
228
229
230
231
232
233
234 protected synchronized void loadFIPAAliases() {
235 List allContacts = null;
236 try {
237 contactsDb = new File (fipaNames);
238
239 buildTree();
240
241 allContacts = contactsRoot.getContacts();
242 }
243 catch (Exception e) {
244 e.printStackTrace();
245 }
246
247 if (allContacts != null) {
248
249 Zeus_ACC_MailBox mbox = (Zeus_ACC_MailBox) context.getMailBox();
250 Zeus_ACC_Server server = mbox.getZeus_ACC_Server();
251
252
253 for (ListIterator i = allContacts.listIterator(); i.hasNext(); ) {
254 Contact contact = (Contact)i.next();
255 System.out.println(contact.toString());
256 String zeusName = contact.getZeusName();
257 FIPA_AID_Address FIPAAddress = new FIPA_AID_Address(contact.getFipaAddress());
258 System.out.println("ZEUS Name = " + zeusName + ", FIPA Address = " + FIPAAddress.toString());
259
260
261
262
263
264
265
266 server.setFIPAAlias(zeusName, FIPAAddress);
267 }
268 }
269 else {
270 System.out.println("Failed to load FIPA aliases into address book");
271 }
272 }
273
274
275
276
277
278
279 protected synchronized void saveFIPAAliases() {
280 try {
281 marshalTree();
282 System.out.println("FIPA Aliases file saved successfully!");
283 }
284 catch (Exception e) {
285 System.out.println("Error saving FIPA Aliases to file");
286 e.printStackTrace();
287 }
288 }
289
290
291 public void add_new_contact(Performative perf) {
292
293
294 Performative p = new Performative();
295 p.setInReplyTo(perf.getReplyWith());
296 p.setReceiver(perf.getSender());
297
298 Contact newContact = new Contact();
299 String msgContent = perf.getContent();
300 System.out.println("Msg content: " + msgContent);
301 try {
302
303 ContactsParser parser = new ContactsParser(msgContent);
304 newContact = parser.getContact();
305
306 List existingContacts = contactsRoot.getContacts();
307 for (ListIterator i = existingContacts.listIterator(); i.hasNext(); ) {
308 Contact c = (Contact)i.next();
309 if (c.getZeusName().equalsIgnoreCase(newContact.getZeusName())) {
310
311 i.remove();
312 }
313 }
314 existingContacts.add(newContact);
315
316 saveFIPAAliases();
317
318 loadFIPAAliases();
319
320 p.setType("inform");
321 p.setContent("((done (" + msgContent + ")))");
322 }
323 catch (Exception e) {
324 System.out.println("Error unmarshalling XML msg content to Java object");
325 e.printStackTrace();
326
327 p.setType("failure");
328 p.setContent(msgContent);
329 }
330
331 p.send(context);
332 }
333
334
335 /***
336 * call the methods necessary to set up the agents transports using a different set of ports.
337 *
338 */
339 protected void initialiseTransports(String transports) {
340 String thisLine;
341 String input = new String();
342 try {
343 FileInputStream fileStream = new FileInputStream(transports);
344 DataInputStream toReadFrom = new DataInputStream(fileStream);
345 while(toReadFrom.available() > 0) {
346 System.out.println("reading");
347 thisLine = toReadFrom.readLine();
348 input += "\n" + thisLine;
349 System.out.println(thisLine);
350
351 }
352 System.out.println(input);
353
354 }
355 catch (Exception e) {
356 e.printStackTrace(); }
357
358
359 StringHashtable allTransports = ZeusParser.Transports(input);
360 Enumeration keys = allTransports.keys();
361 while(keys.hasMoreElements()) {
362 System.out.println("key = " + (String) keys.nextElement());
363 }
364 SystemProps.setTransports(allTransports);
365
366 }
367
368
369
370
371 /***
372 * Traverses the XML document and creates the Java classes to
373 * represent it
374 */
375 private synchronized void buildTree() {
376 try {
377 ContactsParser parser = new ContactsParser(contactsDb);
378 contactsRoot = parser.getContacts();
379 }
380 catch(FileNotFoundException f) {
381
382 }
383 }
384
385
386 /***
387 * Writes XML content from the Java classes.
388 */
389 private synchronized void marshalTree() {
390 ContactsParser parser = new ContactsParser();
391 parser.setRootObject(contactsRoot);
392 parser.write(contactsDb);
393 }
394
395 }