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 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
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 }