1 |
< |
import java.io.*; |
2 |
< |
import java.net.*; |
3 |
< |
import java.util.*; |
1 |
> |
//---PACKAGE DECLARATION--- |
2 |
|
|
3 |
< |
class BasicAgent extends Agent implements Serializable { |
3 |
> |
//---IMPORTS--- |
4 |
> |
import java.io.ObjectOutputStream; |
5 |
> |
import java.io.IOException; |
6 |
> |
import java.net.Socket; |
7 |
> |
import java.net.UnknownHostException; |
8 |
> |
import java.util.Iterator; |
9 |
> |
import java.util.ArrayList; |
10 |
> |
import java.util.Random; |
11 |
> |
|
12 |
> |
/** |
13 |
> |
* A basic implementation of the Agent abstract class |
14 |
> |
* |
15 |
> |
* @author $Author$ |
16 |
> |
* @version $Id$ |
17 |
> |
*/ |
18 |
> |
class BasicAgent extends Agent { |
19 |
> |
|
20 |
> |
//---FINAL / FINAL STATIC ATTRIBUTES--- |
21 |
> |
|
22 |
> |
/** |
23 |
> |
* The current CVS revision of this class |
24 |
> |
*/ |
25 |
> |
public static final String REVISION = "$Revision$"; |
26 |
> |
|
27 |
> |
//---STATIC METHODS--- |
28 |
> |
|
29 |
> |
/** |
30 |
> |
* A bootstrap mechanism for an agent. |
31 |
> |
* Essentially used to fire an agent direct into an agent station. |
32 |
> |
* |
33 |
> |
* @param args command line arguements |
34 |
> |
* |
35 |
> |
* @throws UnknownHostException if it can't find the agent station |
36 |
> |
* @throws IOException if there is a problem firing the agent |
37 |
> |
*/ |
38 |
|
public static void main(String[] args) throws IOException, UnknownHostException { |
39 |
< |
System.out.println("Agent Started."); |
39 |
> |
System.out.println("Agent Bootstrapping Started."); |
40 |
|
if (args.length != 3) { |
41 |
|
throw new RuntimeException("Must specify an agent name and agent station hostname and port number on the command line!"); |
42 |
|
} |
43 |
|
Agent instance = new BasicAgent(args[0]); |
12 |
– |
System.out.println("Transmission count - " + instance.getTransmissionCount()); |
44 |
|
Socket s = new Socket(args[1], Integer.parseInt(args[2])); |
45 |
|
ObjectOutputStream p = new ObjectOutputStream(s.getOutputStream()); |
46 |
< |
p.writeObject(instance); |
47 |
< |
p.flush(); |
48 |
< |
|
49 |
< |
s.close(); |
19 |
< |
System.out.println("Agent finished."); |
46 |
> |
p.writeObject(instance); |
47 |
> |
p.flush(); |
48 |
> |
s.close(); |
49 |
> |
System.out.println("Agent " + args[0] + " injected into " + args[1] + ":" + args[2]); |
50 |
|
} |
51 |
+ |
|
52 |
+ |
//---CONSTRUCTORS--- |
53 |
|
|
54 |
|
public BasicAgent(String name) { |
55 |
|
_name = name; |
56 |
|
} |
57 |
+ |
|
58 |
+ |
//---PUBLIC METHODS--- |
59 |
|
|
60 |
< |
public void onArrival(Station station) { |
61 |
< |
_transmissionCount++; |
28 |
< |
_station = station; |
29 |
< |
say("Just arrived at - " + _station.getName()); |
30 |
< |
} |
31 |
< |
|
60 |
> |
// *** AGENT LIFETIME *** |
61 |
> |
|
62 |
|
public void run() { |
63 |
|
while(_running) { |
64 |
|
try { |
74 |
|
agent.greet(this); |
75 |
|
} |
76 |
|
} |
77 |
+ |
say("Requesting to move on..."); |
78 |
+ |
ArrayList peers = _station.getAllPeers(); |
79 |
+ |
if (peers.size() > 1) { |
80 |
+ |
PeerHandler dstPeer = (PeerHandler) peers.get(new Random().nextInt(peers.size() - 1)); |
81 |
+ |
_station.sendAgent(this, dstPeer); |
82 |
+ |
} else if (peers.size() == 1) { |
83 |
+ |
PeerHandler dstPeer = (PeerHandler) peers.get(0); |
84 |
+ |
_station.sendAgent(this, dstPeer); |
85 |
+ |
} |
86 |
|
} |
87 |
|
} |
88 |
+ |
|
89 |
+ |
//*** AGENT SERVICES *** |
90 |
+ |
|
91 |
+ |
public void onArrival(Station station) { |
92 |
+ |
_transmissionCount++; |
93 |
+ |
_station = station; |
94 |
+ |
_running = true; |
95 |
+ |
say("Just arrived at - " + _station.getName()); |
96 |
+ |
} |
97 |
|
|
98 |
|
public void greet(Agent agent) { |
99 |
|
say(agent.getName() + " just said hi to me"); |
100 |
|
} |
101 |
|
|
102 |
|
private void say(String text) { |
103 |
< |
System.out.println(getName() + ": " + text); |
103 |
> |
System.out.println("*** [" + getName() + "] " + text); |
104 |
|
} |
105 |
|
|
106 |
|
public String getName() { |
110 |
|
public long getTransmissionCount() { |
111 |
|
return _transmissionCount; |
112 |
|
} |
113 |
+ |
|
114 |
+ |
/** |
115 |
+ |
* Used to shutdown this thread |
116 |
+ |
*/ |
117 |
+ |
public void shutdown() { |
118 |
+ |
say("shutting down"); |
119 |
+ |
_station = null; |
120 |
+ |
_running = false; |
121 |
+ |
} |
122 |
|
|
123 |
< |
private Station _station; |
123 |
> |
//*** OTHER PUBLIC METHODS *** |
124 |
> |
|
125 |
> |
//---PRIVATE/PROTECTED METHODS--- |
126 |
> |
|
127 |
> |
/** |
128 |
> |
* Overridden for debugging purposes |
129 |
> |
* to see when an instance of this class |
130 |
> |
* is destroyed |
131 |
> |
*/ |
132 |
> |
protected void finalize() throws Throwable { |
133 |
> |
say("dead - just been gc'd"); |
134 |
> |
} |
135 |
> |
|
136 |
> |
//---ACCESSOR/MUTATOR METHODS--- |
137 |
> |
|
138 |
> |
//---ATTRIBUTES--- |
139 |
> |
|
140 |
> |
/** |
141 |
> |
* The name of the agent |
142 |
> |
*/ |
143 |
|
private String _name; |
144 |
< |
private long _transmissionCount = 0; |
144 |
> |
|
145 |
> |
/** |
146 |
> |
* A reference to the current AgentStation this agent is at |
147 |
> |
*/ |
148 |
> |
private Station _station; |
149 |
> |
|
150 |
> |
/** |
151 |
> |
* A "running" used by the main loop in the run() method to determine if |
152 |
> |
* it should continue running this thread. |
153 |
> |
*/ |
154 |
|
private boolean _running = true; |
155 |
< |
} |
155 |
> |
|
156 |
> |
/** |
157 |
> |
* The number of times this agent has been transmitted between stations |
158 |
> |
*/ |
159 |
> |
private long _transmissionCount = 0; |
160 |
> |
|
161 |
> |
//---STATIC ATTRIBUTES--- |
162 |
> |
|
163 |
> |
} |