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-2001. All Rights Reserved.
18      *
19      * THIS NOTICE MUST BE INCLUDED ON ANY COPY OF THIS FILE
20      */
21      packageong> zeus.concepts;
22      import zeus.util.*;
23      import java.util.*;
24  
25      /***
26          FIPA_AID_Address is an implementation of the Address interface that is used to
27          hold addresses of agents that have identified themselves with FIPA format aid's
28          <p>
29          ISSUES <p>
30          Resolvers: we don't do them yet, and we might need to.
31          Personally I hate the idea....
32  
33          */
34      public class FIPA_AID_Address implements Address {
35  
36  
37      private String name = null;
38      private String host = null;
39      private String port = new String ("900");
40      private String type = new String ("FIPA_AID");
41      private Vector addresses = new Vector();
42      private String alias = null;
43      private boolean forwardSet = false;
44  
45      public FIPA_AID_Address (FIPA_AID_Address copy) {
46          this.name = copy.getName();
47          this.host = copy.getHost();
48          this.port = String.valueOf(copy.getPort());
49          this.type = copy.getType();
50          this.addresses = copy.getAddresses();
51          this.alias = copy.getAlias();
52          this.forwardSet = copy.getForwardingRuleSet();
53      }
54  
55  
56  
57  
58      public FIPA_AID_Address(String address) {
59             this(ZeusParser.fipaAddress(address));
60  
61  
62                  /*StringTokenizer tokens = new StringTokenizer(address,"@ \n");
63                  try {
64                  /*    tokens.nextToken();
65                      tokens.nextToken();
66                      this.setName ((String) tokens.nextToken());
67                      this.setHost ((String) tokens.nextToken());
68                      tokens.nextToken();
69                      tokens.nextToken();
70                      System.out.println("host = " + this.host);
71                      String restStr = new String();
72                      while (tokens.hasMoreElements()) { restStr+= tokens.nextElement();}
73                      StringTokenizer ntokens = new StringTokenizer(restStr);
74                      while (ntokens.hasMoreTokens()) {
75                          String addr = ntokens.nextToken();
76                          System.out.println("adding address: " + addr);
77                          addresses.addElement(addr);
78                      }*/
79  
80  
81      }
82  
83  
84      public FIPA_AID_Address (String name, String host) {
85          this.host = host;
86          this.name = name;
87      }
88  
89          /***
90              return the name of the agent (ie. the acc bit of acc@fipa.bt.com)
91              */
92      public String getName() {
93          return this.name;
94      }
95  
96          /***
97              return the host name (ie. the fipa.bt.com bit of acc@fipa.bt.com)
98              */
99      public String getHost(){
100         return this.host;
101     }
102 
103 
104     /***
105         this method always returns "FIPA_AID"
106         */
107     public String getType() {
108         return ("FIPA_AID");
109     }
110 
111 
112     /***
113         this method always returns 900
114         */
115     public int getPort(){
116         return (900);
117     }
118 
119 
120     /***
121         set the value of the alias for this address that is being used at this time,
122         if the value is already set then return the current value. <p>
123         <u> if a value is set already this method will not reset it </u>
124         <P> note, if you want to set an alias with a current value then you should use
125         <i> void resetAlias(String newVal) </i>
126         @see resetAlias
127         */
128     public String setAlias (String possibleValue) {
129     if (this.alias==null)
130             this.alias = possibleValue;
131     return this.alias;
132 
133     }
134 
135 
136     /***
137         reset the value of the alias for this address regardless of its
138         current state
139         */
140     public void resetAlias (String newVal) {
141         this.alias = newVal;
142     }
143 
144 
145     /***
146         retrieve the value of the alias for this address
147         */
148     public String getAlias () {
149         return this.alias;
150     }
151 
152 
153     /***
154         if this address has the same name value and the same host value return true
155         */
156     public boolean equals(Address addr) {
157         return name.equals(addr.getName()) &&
158                 host.equals(addr.getHost());
159 
160 
161     }
162 
163 
164 
165     /***
166         if the addresses share a host name return true;
167         also return true if the addresses sequence of this address share a common
168         host name and transport<p>
169         WARNING: I am not sure if this is the right behaviour.
170         */
171     public boolean sameAddress(Address addr) {
172         return host.equals(addr.getHost());
173     }
174 
175 
176     /***
177         return a zeus address - ie. one that can be handled by Zeus internally.
178         */
179     public String toString () {
180         return( "(" +
181                 ":name " + name + " " +
182                 ":host \"" + host + "\" " +
183                 ":port " + port + " " +
184                 ":type " + type +
185                 ")"
186                 );
187 
188     }
189 
190 
191     /***
192         return a String version in FIPA_AID format
193         */
194     public String toFIPAString () {
195         return ("agent-identifier  " +
196                 " :name " + getName() +"@" + getHost() +
197                 " :addresses (sequence " + allAddresses() + ")");
198     }
199 
200 
201     /***
202         addAddress could be confusing to the unwary - this method is used to store
203         one of the list of addresses that fipa uses ie. <p>
204         <i> :addresses (sequence iiop://foo.com/ACC http://foo.com/ACC ) </I> <p>
205         Personally I think that this is all a big mistake, forced on FIPA by the
206         use of agent containers and platforms, which are a big mistake. Still, this
207         is the way the wind is blowing....
208         */
209     public void addAddress(String address) {
210         addresses.addElement(address);
211     }
212 
213 
214     /*** all addresses just spits out the content of the addresses vector in a
215     nicely formatted way */
216     private String allAddresses () {
217         Enumeration allAddresses = addresses.elements();
218         String retVal = new String();
219         while (allAddresses.hasMoreElements()) {
220             retVal += (String) allAddresses.nextElement() +" ";
221 
222         }
223         return (retVal);
224     }
225 
226 
227     public Iterator iterator() {
228         return addresses.iterator();
229     }
230 
231      /***
232         getAddresses is protected because I don't want to expose the vector but
233         need access to copy it internally. Use allAddresses() or iterator() as an
234         alternatives
235         */
236     protected Vector getAddresses () {
237         return this.addresses;
238     }
239 
240 
241     public void setName (String name) {
242         this.name = name;
243     }
244 
245 
246     public void setHost (String host) {
247         this.host = host;
248     }
249 
250 
251 
252     /***
253         forwarding rules used for zeus housekeeping
254         */
255     public boolean getForwardingRuleSet () {
256         return forwardSet;
257     }
258 
259 
260     /***
261         forwarding rules used for zeus housekeeping
262         */
263     public void setForwardingRuleSet (boolean val ) {
264         forwardSet = val;
265     }
266 
267 
268     /***
269         return this zeus.concepts.FIPA_AID_Address rendered as a FIPA.AgentID.
270         This is might be used by zeus.actors.service.Transport implementors in order to
271         create valid FIPA_99 messages <p>
272         ISSUES:<p>
273         No resolvers or userDefinedProperties yet...
274         (29/01/01 - set resolvers and udp's to empty arrays)
275         02/02/01 removed addresses - is this why FIPA-os acc's can't forward things????
276         */
277     public FIPA.AgentID getAgentID () {
278         FIPA.AgentID retVal = new FIPA.AgentID();
279         retVal.name = this.name+"@"+this.host;
280         retVal.addresses = new String[addresses.size()+1];
281         retVal.resolvers = new FIPA.AgentID [0];
282     // retVal.resolvers[0] = new FIPA.AgentID();
283         retVal.userDefinedProperties  = new FIPA.Property[0];
284     //  retVal.userDefinedProperties[0] = new FIPA.Property();
285         int count = 0; // was 1
286     Enumeration elems = addresses.elements();
287 
288     // indended recivers seems to be the problem - try fipaos-suniiop-ext ??
289     //retVal.addresses[0] = "fipaos-suniiop-ext";
290         // hack for FIPA-OS
291         //retVal.addresses[0] = "rmi://"+ host + ":3000"+"/"+ name; // was in
292         while (elems.hasMoreElements()) {
293         retVal.addresses[count] = (String) elems.nextElement();
294             count++;}
295         // resolvers and userDefinedProperties are left as null ... for now
296         return (retVal);
297 
298 
299 
300 
301 
302     }
303 
304 
305 
306     /***
307         return this as XML as per XC00084C
308         */
309     public String toXML () {
310             String retVal = new String("\t\t\t<agent-identifier>\n");
311             retVal+= "\t\t\t\t<name>" + getName() + "@" + getHost() +"</name>\n";
312             retVal+= "\t\t\t\t<addresses>\n";
313             Iterator addresses = iterator();
314             if (SystemProps.FIPA_IMPLEMENTED_TIGHTLY) { // I dispair
315              while (addresses.hasNext()) {
316                 String current = (String) addresses.next();
317                 retVal+= "\t\t\t\t\t<url>";
318                 retVal += current +"</url>\n"; }
319                 }
320                 /*else {  // no, it really is depressing.
321                 // hours, and hours OF MY TIME (soon I will be dead).
322                 while (addresses.hasNext()) {
323                     retVal += "\t\t\t\t\t <url>" + addresses.next() +"</url>";
324                     // not to mention the millions of ecus and dollars spent on
325                     // building other tool kits. I am sick of this....
326                 }
327                           */
328                 else {
329                         String current = new String();
330                         while (addresses.hasNext()) {
331                                 current = (String) addresses.next();
332                                 }
333                      	retVal += "\t\t\t\t\t <url>" + current +"</url>";
334             retVal+="\n\t\t\t\t</addresses>\n";
335             retVal += "\t\t\t</agent-identifier>\n";
336                 }// end else
337          return retVal;
338          
339          // end everything
340          // I wish... ;-) 
341     }
342 
343     /***
344      * allow the type to be manipulated
345      */
346     public void setType(String type) {
347         this.type = type; 
348     }    
349 
350 
351 
352 
353     }
354