1 |
//---PACKAGE DECLARATION--- |
2 |
|
3 |
//---IMPORTS--- |
4 |
import java.io.ObjectInputStream; |
5 |
import java.io.IOException; |
6 |
import java.net.Socket; |
7 |
|
8 |
/** |
9 |
* Handles agents that connect directly to the agent station |
10 |
* as opposed to agents from peers stations. |
11 |
* |
12 |
* @author $Author: tdb1 $ |
13 |
* @version $Id: TemplateClass.java,v 1.11 2001/03/22 21:50:41 tdb1 Exp $ |
14 |
*/ |
15 |
class DirectAgentHandler extends Handler { |
16 |
|
17 |
//---FINAL / FINAL STATIC ATTRIBUTES--- |
18 |
|
19 |
/** |
20 |
* The current CVS revision of this class |
21 |
*/ |
22 |
public static final String REVISION = "$Revision: 1.11 $"; |
23 |
|
24 |
//---STATIC METHODS--- |
25 |
|
26 |
//---CONSTRUCTORS--- |
27 |
|
28 |
public DirectAgentHandler(Socket socket) throws IOException { |
29 |
super(socket); |
30 |
_logger.write(toString(), Logger.SYSINIT, "created"); |
31 |
} |
32 |
|
33 |
//---PUBLIC METHODS--- |
34 |
|
35 |
public void run() { |
36 |
_logger.write(toString(), Logger.SYSINIT, "started"); |
37 |
try { |
38 |
// receive agent bytecode |
39 |
|
40 |
// TODO |
41 |
|
42 |
// receive agent state (serialized object) |
43 |
ObjectInputStream objectStream = new ObjectInputStream(_socket.getInputStream()); |
44 |
Agent agent = (Agent) objectStream.readObject(); |
45 |
AgentStation.getInstance().addAgent(agent); |
46 |
|
47 |
_logger.write(toString(), Logger.DEBUG, "Agent state received"); |
48 |
_logger.write(toString(), Logger.SYSMSG, "Agent name - " + agent.getName()); |
49 |
_logger.write(toString(), Logger.DEBUG, agent.getName() + "'s transmission count - " + agent.getTransmissionCount()); |
50 |
|
51 |
_logger.write(toString(), Logger.SYSMSG, "Welcoming agent"); |
52 |
agent.onArrival(AgentStation.getInstance()); |
53 |
|
54 |
_logger.write(toString(), Logger.SYSMSG, "Starting agent thread"); |
55 |
|
56 |
Thread thread = new Thread(agent); |
57 |
thread.start(); |
58 |
|
59 |
objectStream.close(); |
60 |
|
61 |
} catch (IOException e) { |
62 |
_logger.write(toString(), Logger.ERROR, "I/O ERROR - " + e); |
63 |
} catch (ClassNotFoundException e) { |
64 |
_logger.write(toString(), Logger.ERROR, "ERROR - possible error transferring agent bytecode - " + e); |
65 |
} |
66 |
|
67 |
shutdownSocket(); |
68 |
_logger.write(toString(), Logger.SYSINIT, "finished"); |
69 |
} |
70 |
|
71 |
/** |
72 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
73 |
* method to provide clean logging (every class should have this). |
74 |
* |
75 |
* This uses the uk.ac.ukc.iscream.util.FormatName class |
76 |
* to format the toString() |
77 |
* |
78 |
* @return the name of this class and its CVS revision |
79 |
*/ |
80 |
public String toString() { |
81 |
return FormatName.getName( |
82 |
_name, |
83 |
getClass().getName(), |
84 |
REVISION); |
85 |
} |
86 |
|
87 |
//---PRIVATE/PROTECTED METHODS--- |
88 |
|
89 |
//---ACCESSOR/MUTATOR METHODS--- |
90 |
|
91 |
//---ATTRIBUTES--- |
92 |
|
93 |
/** |
94 |
* This is the friendly identifier of the |
95 |
* component this class is running in. |
96 |
* eg, a Filter may be called "filter1", |
97 |
* If this class does not have an owning |
98 |
* component, a name from the configuration |
99 |
* can be placed here. This name could also |
100 |
* be changed to null for utility classes. |
101 |
*/ |
102 |
private String _name = "DirectAgentHandler"; |
103 |
|
104 |
/** |
105 |
* This holds a reference to the |
106 |
* system logger that is being used. |
107 |
*/ |
108 |
private Logger _logger = Logger.getInstance(); |
109 |
|
110 |
//---STATIC ATTRIBUTES--- |
111 |
|
112 |
} |