1 |
import java.io.*; |
2 |
import java.net.*; |
3 |
import java.rmi.RMISecurityManager; |
4 |
import java.util.*; |
5 |
|
6 |
class AgentStation extends Station { |
7 |
|
8 |
public static final String REVISION = "v0.0.1"; |
9 |
public static final int LISTEN_PORT = 32497; |
10 |
|
11 |
public static void main(String[] args) throws IOException, UnknownHostException, ClassNotFoundException { |
12 |
System.out.println("- Agent Station " + REVISION + "-"); |
13 |
System.out.println("Boot sequence start."); |
14 |
//System.getProperties().setProperty("java.security.policy", "/home/cut/ajm4/java/java.policy"); |
15 |
//if (System.getSecurityManager() == null) { |
16 |
// System.out.println("Setting security manager..."); |
17 |
// System.setSecurityManager(new RMISecurityManager()); |
18 |
//} |
19 |
AgentStation station = new AgentStation(); |
20 |
|
21 |
station.startListening(); |
22 |
|
23 |
System.out.println("Boot sequence end."); |
24 |
} |
25 |
|
26 |
public void startListening() throws IOException, UnknownHostException, ClassNotFoundException { |
27 |
ServerSocket listener = new ServerSocket(LISTEN_PORT); |
28 |
while (_listening) { |
29 |
System.out.println("Listening..."); |
30 |
Socket conn = listener.accept(); |
31 |
System.out.println("Got connection..."); |
32 |
ObjectInputStream p = new ObjectInputStream(conn.getInputStream()); |
33 |
System.out.println("Got hook to stream..."); |
34 |
Agent agent = (Agent)p.readObject(); |
35 |
_agentList.add(agent); |
36 |
System.out.println("Got agent..."); |
37 |
System.out.println("Closing connection..."); |
38 |
conn.close(); |
39 |
System.out.println("Telling agent that its journey is complete..."); |
40 |
agent.onArrival(this); |
41 |
System.out.println("New agent name - " + agent.getName()); |
42 |
System.out.println(agent.getName() + "'s transmission count - " + agent.getTransmissionCount()); |
43 |
System.out.println("Attempting to run locally..."); |
44 |
Thread thread = new Thread(agent); |
45 |
thread.start(); |
46 |
System.out.println("Done."); |
47 |
} |
48 |
} |
49 |
|
50 |
public ArrayList getAllAgents() { |
51 |
return _agentList; |
52 |
} |
53 |
|
54 |
public String getName() { |
55 |
return _name; |
56 |
} |
57 |
|
58 |
private String _name = "Listening Post Alpha76"; |
59 |
private boolean _listening = true; |
60 |
private ArrayList _agentList = new ArrayList(); |
61 |
} |