--- experimental/agents/BasicAgent.java 2001/04/23 19:45:18 1.1 +++ experimental/agents/BasicAgent.java 2001/05/04 02:04:35 1.2 @@ -1,34 +1,64 @@ -import java.io.*; -import java.net.*; -import java.util.*; +//---PACKAGE DECLARATION--- -class BasicAgent extends Agent implements Serializable { +//---IMPORTS--- +import java.io.ObjectOutputStream; +import java.io.IOException; +import java.net.Socket; +import java.net.UnknownHostException; +import java.util.Iterator; +import java.util.ArrayList; +import java.util.Random; + +/** + * A basic implementation of the Agent abstract class + * + * @author $Author: ajm $ + * @version $Id: BasicAgent.java,v 1.2 2001/05/04 02:04:35 ajm Exp $ + */ +class BasicAgent extends Agent { + +//---FINAL / FINAL STATIC ATTRIBUTES--- + + /** + * The current CVS revision of this class + */ + public static final String REVISION = "$Revision: 1.2 $"; + +//---STATIC METHODS--- + + /** + * A bootstrap mechanism for an agent. + * Essentially used to fire an agent direct into an agent station. + * + * @param args command line arguements + * + * @throws UnknownHostException if it can't find the agent station + * @throws IOException if there is a problem firing the agent + */ public static void main(String[] args) throws IOException, UnknownHostException { - System.out.println("Agent Started."); + System.out.println("Agent Bootstrapping Started."); if (args.length != 3) { throw new RuntimeException("Must specify an agent name and agent station hostname and port number on the command line!"); } Agent instance = new BasicAgent(args[0]); - System.out.println("Transmission count - " + instance.getTransmissionCount()); Socket s = new Socket(args[1], Integer.parseInt(args[2])); ObjectOutputStream p = new ObjectOutputStream(s.getOutputStream()); - p.writeObject(instance); - p.flush(); - - s.close(); - System.out.println("Agent finished."); + p.writeObject(instance); + p.flush(); + s.close(); + System.out.println("Agent " + args[0] + " injected into " + args[1] + ":" + args[2]); } + +//---CONSTRUCTORS--- public BasicAgent(String name) { _name = name; } + +//---PUBLIC METHODS--- - public void onArrival(Station station) { - _transmissionCount++; - _station = station; - say("Just arrived at - " + _station.getName()); - } - + // *** AGENT LIFETIME *** + public void run() { while(_running) { try { @@ -44,15 +74,33 @@ class BasicAgent extends Agent implements Serializable agent.greet(this); } } + say("Requesting to move on..."); + ArrayList peers = _station.getAllPeers(); + if (peers.size() > 1) { + PeerHandler dstPeer = (PeerHandler) peers.get(new Random().nextInt(peers.size() - 1)); + _station.sendAgent(this, dstPeer); + } else if (peers.size() == 1) { + PeerHandler dstPeer = (PeerHandler) peers.get(0); + _station.sendAgent(this, dstPeer); + } + say("No peers to move to...staying here."); } } + + //*** AGENT SERVICES *** + + public void onArrival(Station station) { + _transmissionCount++; + _station = station; + say("Just arrived at - " + _station.getName()); + } public void greet(Agent agent) { say(agent.getName() + " just said hi to me"); } private void say(String text) { - System.out.println(getName() + ": " + text); + System.out.println("*** [" + getName() + "] " + text); } public String getName() { @@ -62,9 +110,44 @@ class BasicAgent extends Agent implements Serializable public long getTransmissionCount() { return _transmissionCount; } + + /** + * Used to shutdown this thread + */ + public void shutdown() { + _station = null; + _running = false; + } - private Station _station; + //*** OTHER PUBLIC METHODS *** + +//---PRIVATE/PROTECTED METHODS--- + +//---ACCESSOR/MUTATOR METHODS--- + +//---ATTRIBUTES--- + + /** + * The name of the agent + */ private String _name; - private long _transmissionCount = 0; + + /** + * A reference to the current AgentStation this agent is at + */ + private Station _station; + + /** + * A "running" used by the main loop in the run() method to determine if + * it should continue running this thread. + */ private boolean _running = true; -} \ No newline at end of file + + /** + * The number of times this agent has been transmitted between stations + */ + private long _transmissionCount = 0; + +//---STATIC ATTRIBUTES--- + +}