1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
73
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
81
82
83
84
85
86
87
88
89
90
91
92
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 }