1 |
< |
import java.io.*; |
2 |
< |
import java.net.*; |
3 |
< |
import java.rmi.RMISecurityManager; |
4 |
< |
import java.util.*; |
1 |
> |
//---PACKAGE DECLARATION--- |
2 |
|
|
3 |
+ |
//---IMPORTS--- |
4 |
+ |
import java.net.Socket; |
5 |
+ |
import java.io.IOException; |
6 |
+ |
import java.util.ArrayList; |
7 |
+ |
|
8 |
+ |
/** |
9 |
+ |
* An implementation of an agent station |
10 |
+ |
* |
11 |
+ |
* @author $Author$ |
12 |
+ |
* @version $Id$ |
13 |
+ |
*/ |
14 |
|
class AgentStation extends Station { |
15 |
+ |
|
16 |
+ |
//---FINAL / FINAL STATIC ATTRIBUTES--- |
17 |
+ |
|
18 |
+ |
/** |
19 |
+ |
* The current CVS revision of this class |
20 |
+ |
*/ |
21 |
+ |
public static final String REVISION = "$Revision$"; |
22 |
|
|
23 |
< |
public static final String REVISION = "v0.0.1"; |
9 |
< |
public static final int LISTEN_PORT = 32497; |
23 |
> |
public static final int DIRECT_LISTEN_PORT = 32497; |
24 |
|
|
25 |
< |
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 |
> |
public static final int PEER_LISTEN_PORT = 32498; |
26 |
|
|
27 |
< |
public void startListening() throws IOException, UnknownHostException, ClassNotFoundException { |
28 |
< |
ServerSocket listener = new ServerSocket(LISTEN_PORT); |
29 |
< |
while (_listening) { |
30 |
< |
System.out.println("Listening..."); |
31 |
< |
Socket conn = listener.accept(); |
32 |
< |
System.out.println("Got connection..."); |
33 |
< |
ObjectInputStream p = new ObjectInputStream(conn.getInputStream()); |
34 |
< |
System.out.println("Got hook to stream..."); |
35 |
< |
Agent agent = (Agent)p.readObject(); |
36 |
< |
_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."); |
27 |
> |
//---STATIC METHODS--- |
28 |
> |
|
29 |
> |
/** |
30 |
> |
* Returns a reference to the singleton agent station in use |
31 |
> |
* |
32 |
> |
* @throws RuntimeException if not yet initialised |
33 |
> |
*/ |
34 |
> |
public static AgentStation getInstance() throws RuntimeException { |
35 |
> |
if (_instance == null) { |
36 |
> |
throw new RuntimeException("Cannot obtain instance to uninitialised AgentStation!"); |
37 |
|
} |
38 |
+ |
return _instance; |
39 |
|
} |
40 |
+ |
|
41 |
+ |
/** |
42 |
+ |
* Constructs and returns the singleton AgentStation class |
43 |
+ |
* |
44 |
+ |
* @throws RuntimeException if already initialised |
45 |
+ |
*/ |
46 |
+ |
public static AgentStation initialise(String name, String descriptiveName, String initialPeerHostname, int initialPeerPort) { |
47 |
+ |
if (_instance != null) { |
48 |
+ |
throw new RuntimeException("Already initialised!"); |
49 |
+ |
} |
50 |
+ |
_instance = new AgentStation(name, descriptiveName, initialPeerHostname, initialPeerPort); |
51 |
+ |
return _instance; |
52 |
+ |
} |
53 |
|
|
54 |
+ |
//---CONSTRUCTORS--- |
55 |
+ |
|
56 |
+ |
/** |
57 |
+ |
* Starts up the agent station. |
58 |
+ |
* This is a private method to ensure singleton pattern. |
59 |
+ |
*/ |
60 |
+ |
private AgentStation(String name, String descriptiveName, String initialPeerHostname, int initialPeerPort) { |
61 |
+ |
_name = name; |
62 |
+ |
_descriptiveName = descriptiveName; |
63 |
+ |
|
64 |
+ |
DirectAgentHandlerFactory directAgentHandlerFactory = new DirectAgentHandlerFactory(); |
65 |
+ |
PeerHandlerFactory peerHandlerFactory = new PeerHandlerFactory(); |
66 |
+ |
|
67 |
+ |
Listener directAgentListener = new Listener(DIRECT_LISTEN_PORT, directAgentHandlerFactory); |
68 |
+ |
Listener peerListener = new Listener(PEER_LISTEN_PORT, peerHandlerFactory); |
69 |
+ |
|
70 |
+ |
directAgentListener.start(); |
71 |
+ |
peerListener.start(); |
72 |
+ |
|
73 |
+ |
// TESTING ONLY - Have one peer connection |
74 |
+ |
if (initialPeerHostname != null) { |
75 |
+ |
try { |
76 |
+ |
_logger.write(toString(), Logger.DEBUG, "Establishing peer connection with - " + initialPeerHostname + ":" + initialPeerPort); |
77 |
+ |
Socket socket = new Socket(initialPeerHostname, initialPeerPort); |
78 |
+ |
|
79 |
+ |
PeerHandler peerHandler = new PeerHandler(socket); |
80 |
+ |
addPeer(peerHandler); |
81 |
+ |
peerHandler.start(); |
82 |
+ |
} catch (IOException e) { |
83 |
+ |
_logger.write(toString(), Logger.ERROR, "I/O ERROR - " + e); |
84 |
+ |
} |
85 |
+ |
} |
86 |
+ |
_logger.write(toString(), Logger.SYSINIT, "created"); |
87 |
+ |
} |
88 |
+ |
|
89 |
+ |
//---PUBLIC METHODS--- |
90 |
+ |
|
91 |
+ |
//*** STATION SERVICES TO AGENTS *** |
92 |
+ |
|
93 |
|
public ArrayList getAllAgents() { |
94 |
|
return _agentList; |
95 |
|
} |
96 |
|
|
97 |
+ |
public ArrayList getAllPeers() { |
98 |
+ |
return _peerList; |
99 |
+ |
} |
100 |
+ |
|
101 |
|
public String getName() { |
102 |
< |
return _name; |
102 |
> |
return _name + " - " + _descriptiveName; |
103 |
|
} |
104 |
|
|
105 |
< |
private String _name = "Listening Post Alpha76"; |
106 |
< |
private boolean _listening = true; |
105 |
> |
public void sendAgent(Agent agent, PeerHandler peerHandler) { |
106 |
> |
// NOTE - no support for restarting agent on a failed send. |
107 |
> |
agent.shutdown(); |
108 |
> |
((PeerHandler) _peerList.get(_peerList.indexOf(peerHandler))).sendAgent(agent); |
109 |
> |
} |
110 |
> |
|
111 |
> |
//*** OTHER PUBLIC METHODS *** |
112 |
> |
|
113 |
> |
public void addAgent(Agent agent) { |
114 |
> |
_logger.write(toString(), Logger.DEBUG, "registered agent"); |
115 |
> |
_agentList.add(agent); |
116 |
> |
} |
117 |
> |
|
118 |
> |
public void removeAgent(Agent agent) { |
119 |
> |
_agentList.remove(_agentList.indexOf(agent)); |
120 |
> |
_logger.write(toString(), Logger.DEBUG, "deregistered agent"); |
121 |
> |
} |
122 |
> |
|
123 |
> |
public void addPeer(PeerHandler peerHandler) { |
124 |
> |
_logger.write(toString(), Logger.DEBUG, "registered peer"); |
125 |
> |
_peerList.add(peerHandler); |
126 |
> |
} |
127 |
> |
|
128 |
> |
public void removePeer(PeerHandler peerHandler) { |
129 |
> |
_peerList.remove(peerHandler); |
130 |
> |
_logger.write(toString(), Logger.DEBUG, "deregistered peer"); |
131 |
> |
} |
132 |
> |
|
133 |
> |
/** |
134 |
> |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
135 |
> |
* method to provide clean logging (every class should have this). |
136 |
> |
* |
137 |
> |
* This uses the uk.ac.ukc.iscream.util.FormatName class |
138 |
> |
* to format the toString() |
139 |
> |
* |
140 |
> |
* @return the name of this class and its CVS revision |
141 |
> |
*/ |
142 |
> |
public String toString() { |
143 |
> |
return FormatName.getName( |
144 |
> |
_name, |
145 |
> |
getClass().getName(), |
146 |
> |
REVISION); |
147 |
> |
} |
148 |
> |
|
149 |
> |
//---PRIVATE/PROTECTED METHODS--- |
150 |
> |
|
151 |
> |
//---ACCESSOR/MUTATOR METHODS--- |
152 |
> |
|
153 |
> |
//---ATTRIBUTES--- |
154 |
> |
|
155 |
> |
/** |
156 |
> |
* This is the friendly identifier of the |
157 |
> |
* component this class is running in. |
158 |
> |
* eg, a Filter may be called "filter1", |
159 |
> |
* If this class does not have an owning |
160 |
> |
* component, a name from the configuration |
161 |
> |
* can be placed here. This name could also |
162 |
> |
* be changed to null for utility classes. |
163 |
> |
*/ |
164 |
> |
private String _name; |
165 |
> |
|
166 |
> |
/** |
167 |
> |
* This holds a reference to the |
168 |
> |
* system logger that is being used. |
169 |
> |
*/ |
170 |
> |
private Logger _logger = Logger.getInstance(); |
171 |
> |
|
172 |
> |
private String _descriptiveName; |
173 |
> |
|
174 |
|
private ArrayList _agentList = new ArrayList(); |
175 |
+ |
|
176 |
+ |
private ArrayList _peerList = new ArrayList(); |
177 |
+ |
|
178 |
+ |
//---STATIC ATTRIBUTES--- |
179 |
+ |
|
180 |
+ |
/** |
181 |
+ |
* Holds the reference to the singleton instance of his class |
182 |
+ |
*/ |
183 |
+ |
private static AgentStation _instance; |
184 |
+ |
|
185 |
+ |
|
186 |
|
} |