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  
24  /*
25   * @(#)ANServer.java 1.00
26   */
27  
28  package zeus.agents;
29  
30  import java.util.*;
31  import java.io.*;
32  
33  import zeus.util.*;
34  import zeus.concepts.Address;
35  import zeus.concepts.ZeusParser;
36  import zeus.actors.*;
37  
38  /***
39   * The implementation of the Zeus Agent Name Server (ANS). An agent society
40   * must possess at least one ANS, in order to maintain a registry of known agents,
41   * enabling agent identities to be mapped to their logical network location.
42   * This is necessary because to ensure location independence agents only know
43   * the names of their acquaintances and not their locations. <p>
44   *
45   * It is unlikely that users will need to change or call directly any of the
46   * methods of this class.
47   */
48  
49  public class ANServer extends BasicAgent
50  {
51  
52    public ANServer(String name, Vector nameservers, Clock clock) {
53      super(SystemProps.getProperty("agent.names.nameserver"),
54            name,nameservers,clock);
55    }
56  
57    protected static void version() {
58      System.err.println("ANServer version: " +
59                         SystemProps.getProperty("version.id"));
60      System.exit(0);
61    }
62  
63    protected static void usage() {
64      System.err.println("Usage: java ANServer <name> " +
65             "[-s <dns_file>] [-t time_grain] " +
66             "[-f <addressoutfile>] [-gui ViewerProg] " +
67                         "[-e ExternalProg] [-debug] [-h] [-v]");
68      System.exit(0);
69    }
70  
71    public static void main(String[] arg) {
72      // debug classpath problems for lamers. 
73      // added by simon 21/08/00
74      try {
75           Class c = Class.forName("java.lang.Object"); 
76      }
77      catch (ClassNotFoundException cnfe) { 
78         System.out.println("Java cannot find java.lang.Object.\n This indicates that the rt.jar file is not in your classpath.\n Ensure that $java_install_dir//jre//rt.jar is present in the classpath and then continue");
79              cnfe.printStackTrace();}
80   /*   try {
81          // obscure zeus class picked to try it 
82           Class c = Class.forName("zeus.gui.help.HelpWindow"); 
83      }
84      catch (ClassNotFoundException cnfe) { 
85         System.out.println("Java cannot find a zeus class.\n This indicates that the zeus.jar file is not in your classpath.\n Ensure that zeus_install_dir//lib//zeus.jar is present in the classpath and then continue");
86              cnfe.printStackTrace();}   */
87   /*   try {
88           Class c = Class.forName("gnu.regexp.REException"); 
89      }
90      catch (ClassNotFoundException cnfe) { 
91         System.out.println("Java cannot find a utility object.\n This indicates that the gnu-regexp.jar file is not in your classpath.\n Ensure that $zeus_install_dir//lib//gnu-regexp.jar is present in the classpath and then continue");
92              cnfe.printStackTrace();}*/
93      Vector nameservers = new Vector();
94      String dns_file = null, dout = null;
95      String time_grain = null;
96      String gui = null;
97      String external = null;
98  
99      if ( arg.length < 1 )  usage();
100     else
101       for( int i = 1; i < arg.length; i++ ) {
102         if ( arg[i].equals("-s") && ++i < arg.length )
103           dns_file = arg[i];
104         else if ( arg[i].equals("-t") && ++i < arg.length )
105           time_grain = arg[i];
106         else if ( arg[i].equals("-e") && ++i < arg.length )
107           external = arg[i];
108         else if ( arg[i].equals("-f") && ++i < arg.length )
109           dout = arg[i];
110         else if ( arg[i].equals("-gui") && ++i < arg.length )
111           gui = arg[i];
112         else if ( arg[i].equals("-debug") ) {
113           Core.debug = true;
114         Core.setDebuggerOutputFile(arg[0] + ".log");
115         }
116     else if ( arg[i].equals("-h") )
117       usage();
118     else if ( arg[i].equals("-v") )
119       version();
120     else
121       usage();
122     }
123 
124     try {
125       if ( dns_file != null ) {
126          nameservers = ZeusParser.addressList(new FileInputStream(dns_file));
127       if ( nameservers == null || nameservers.isEmpty() )
128       throw new IOException();
129       }
130 
131       Clock clock = null;
132       if ( time_grain != null ) {
133         double time_incr = (Double.valueOf(time_grain)).doubleValue();
134         long t = System.currentTimeMillis();
135         clock = new Clock(t, (long)(60000*time_incr));
136       }
137       ANServer ans = new ANServer(arg[0],nameservers,clock);
138 
139       if ( dout != null ) {
140         PrintWriter out = new PrintWriter(new FileWriter(dout));
141         out.println(ans.getAgentContext().MailBox().getAddress());
142         out.flush();
143         out.close();
144       }
145 
146       Class c;
147       AgentContext context = ans.getAgentContext();
148 
149       if ( gui != null ) {
150          c = Class.forName(gui);
151          BasicAgentUI ui = (BasicAgentUI) c.newInstance();
152          context.set(ui);
153          ui.set(context);
154       }
155       if ( external != null ) {
156          c = Class.forName(external);
157          ZeusExternal user_prog = (ZeusExternal)c.newInstance();
158          context.set(user_prog);
159          user_prog.exec(ans.getAgentContext());
160       }
161       
162  
163 
164     }
165     catch(Exception e) {
166       e.printStackTrace();
167       System.exit(0);
168     }
169   }
170 }