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.concepts;
24  
25  import java.util.*;
26  import zeus.actors.outtrays.*;
27  /***
28      FIPA_AddressBook is an extention of the addressbook that can cope with fipa
29      AID addresses, the intention is that this should be used to implement an 
30      ACC agent that can act as a gateway from Zeus agencies to the FIPA world. <p> 
31      The objective of this class is to provide something that can store agent aliases 
32      so that when the ACC gets a message that is addressed to <i> testForwardACCiioptbtoledofuturesbtcom</i> 
33      it can lookup the appropriate aid to construct a fipa performative which can be 
34      sent to the target agent
35      */
36  public class FIPA_AddressBook extends zeus.concepts.AddressBook {
37      
38      private Hashtable FIPA_Addresses= new Hashtable(); 
39  
40      
41      /*** 
42          add an address to the book
43          */
44      public boolean add(FIPA_AID_Address fipa_address) { 
45          FIPA_Addresses.put (fipa_address.getAlias(), fipa_address); // was getName
46          return true; 
47          }
48          
49      
50      
51     /***
52          find a proper address from a name of an agent 
53          */
54      public FIPA_AID_Address lookupFIPA(String name) { 
55          Enumeration allNames = FIPA_Addresses.keys(); 
56          while (allNames.hasMoreElements()) { 
57              String current = (String) allNames.nextElement();
58              if (current.equals(name)) {
59                  return ((FIPA_AID_Address)FIPA_Addresses.get(current)); }
60          }
61          //improve this by raising an exception? 
62          return (null); 
63          }
64          
65      
66      /***
67          find a proper address from a name of an agent 
68          */
69      public FIPA_AID_Address lookupAlias(String alias) { 
70          Enumeration allAddresses = FIPA_Addresses.elements(); 
71          while (allAddresses.hasMoreElements()) { 
72              try {
73                  FIPA_AID_Address current = (FIPA_AID_Address) allAddresses.nextElement(); 
74                  String currentAlias = current.getAlias();
75            //      System.out.println("currentA =" + currentAlias + " alias = " +alias);
76                  if (currentAlias.equals(alias)) {
77                      return current; }}
78                  catch (ClassCastException cce) {
79                      // ignor this - if a class cast occurs it is because 
80                      // a Address is being cast to a FIPA_AID_Address, which is expected 
81                      // and irrelevant
82                  }
83          }
84          //improve this by raising an exception? 
85          return (null); 
86          }
87          
88      
89      
90      /***
91      determine if the address for this agent  is in the address book, return 
92      the correct address object if it is...
93      */
94      public FIPA_AID_Address checkAddress(Address addr ) { 
95        String name = makeAlias(addr); // added getHost (could be a problem...)
96        FIPA_AID_Address lookedUp = lookupFIPA(name); 
97        if (lookedUp!= null) 
98              return lookedUp;
99          else
100           try {  
101             return (FIPA_AID_Address) addr; }
102             catch (Exception e) { 
103                 e.printStackTrace (); 
104                 System.out.println("Probably class cast problem in checkAddress"); 
105                 System.out.println("You may have tried to check to see if a Zeus format"); 
106                 System.out.println("address is a FIPA_AID_Address - sorry, this causes an Exception "); 
107                 return null; 
108             }
109             
110     }
111     
112     /***
113      *utility method to be used to reconsitute local platform name from an 
114      *external name/host combo
115      */
116     public static String makeAlias (Address addr) { 
117         String host = addr.getHost(); 
118         host.replace(':','X');
119         host.replace('/','X'); 
120         return (addr.getName() + host); 
121     }
122         
123         
124     
125     
126 }