| 1 |
//---PACKAGE DECLARATION--- |
| 2 |
|
| 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: ajm4 $ |
| 16 |
* @version $Id: BasicAgent.java,v 1.2 2001/05/04 02:04:35 ajm4 Exp $ |
| 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: 1.2 $"; |
| 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 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]); |
| 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 |
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 |
// *** AGENT LIFETIME *** |
| 61 |
|
| 62 |
public void run() { |
| 63 |
while(_running) { |
| 64 |
try { |
| 65 |
Thread.sleep(10000); |
| 66 |
} catch (InterruptedException e) { |
| 67 |
} |
| 68 |
ArrayList agents = _station.getAllAgents(); |
| 69 |
Iterator i = agents.iterator(); |
| 70 |
while(i.hasNext()) { |
| 71 |
Agent agent = (Agent) i.next(); |
| 72 |
if (agent != (Agent) this) { |
| 73 |
say("Saying hi to " + agent.getName()); |
| 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); |
| 104 |
} |
| 105 |
|
| 106 |
public String getName() { |
| 107 |
return _name; |
| 108 |
} |
| 109 |
|
| 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 |
//*** 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 |
|
| 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 |
|
| 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 |
} |