ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/experimental/agents/BasicAgent.java
Revision: 1.2
Committed: Fri May 4 02:04:35 2001 UTC (23 years, 6 months ago) by ajm
Branch: MAIN
Changes since 1.1: +104 -21 lines
Log Message:
Loads of new framework but far from complete.

The BasicAgent class is as it says, a basic agent, with ability to be run from the command line to fire it into an agentstation.

AgentStations now have support for adding and removing agents and support for multiple listeners.  Also initial support for peer agent stations.  Attempted support at peer -> peer agent transfer, but run into problems.

Still no bytecode transfer, but location and method has been figured out.

The AgentSystem class is now the bootstrap class for an agent station.  It brings up the AgentStation (which is now singleton) and the Logger (also singleton - using standard i-scream logging techniques).  It is possible to specify a peer station that the booting agent station should connect to.

Initial TODO:

	agent class loader
	agent bytecode transfer
	solve peer -> peer problems - possibly by creating extra socket to send agent, but shouldn't really be needed

File Contents

# User Rev Content
1 ajm 1.2 //---PACKAGE DECLARATION---
2 ajm 1.1
3 ajm 1.2 //---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: tdb1 $
16     * @version $Id: TemplateClass.java,v 1.11 2001/03/22 21:50:41 tdb1 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.11 $";
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 ajm 1.1 public static void main(String[] args) throws IOException, UnknownHostException {
39 ajm 1.2 System.out.println("Agent Bootstrapping Started.");
40 ajm 1.1 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 ajm 1.2 p.writeObject(instance);
47     p.flush();
48     s.close();
49     System.out.println("Agent " + args[0] + " injected into " + args[1] + ":" + args[2]);
50 ajm 1.1 }
51 ajm 1.2
52     //---CONSTRUCTORS---
53 ajm 1.1
54     public BasicAgent(String name) {
55     _name = name;
56     }
57 ajm 1.2
58     //---PUBLIC METHODS---
59    
60     // *** AGENT LIFETIME ***
61 ajm 1.1
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 ajm 1.2 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     say("No peers to move to...staying here.");
87 ajm 1.1 }
88     }
89 ajm 1.2
90     //*** AGENT SERVICES ***
91    
92     public void onArrival(Station station) {
93     _transmissionCount++;
94     _station = station;
95     say("Just arrived at - " + _station.getName());
96     }
97 ajm 1.1
98     public void greet(Agent agent) {
99     say(agent.getName() + " just said hi to me");
100     }
101    
102     private void say(String text) {
103 ajm 1.2 System.out.println("*** [" + getName() + "] " + text);
104 ajm 1.1 }
105    
106     public String getName() {
107     return _name;
108     }
109    
110     public long getTransmissionCount() {
111     return _transmissionCount;
112     }
113 ajm 1.2
114     /**
115     * Used to shutdown this thread
116     */
117     public void shutdown() {
118     _station = null;
119     _running = false;
120     }
121 ajm 1.1
122 ajm 1.2 //*** OTHER PUBLIC METHODS ***
123    
124     //---PRIVATE/PROTECTED METHODS---
125    
126     //---ACCESSOR/MUTATOR METHODS---
127    
128     //---ATTRIBUTES---
129    
130     /**
131     * The name of the agent
132     */
133     private String _name;
134    
135     /**
136     * A reference to the current AgentStation this agent is at
137     */
138 ajm 1.1 private Station _station;
139 ajm 1.2
140     /**
141     * A "running" used by the main loop in the run() method to determine if
142     * it should continue running this thread.
143     */
144     private boolean _running = true;
145    
146     /**
147     * The number of times this agent has been transmitted between stations
148     */
149 ajm 1.1 private long _transmissionCount = 0;
150 ajm 1.2
151     //---STATIC ATTRIBUTES---
152    
153     }