ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/experimental/agents/BasicAgent.java
Revision: 1.3
Committed: Sun May 6 19:01:48 2001 UTC (22 years, 11 months ago) by ajm
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +15 -5 lines
Log Message:
Now has some VERY crude byte code passing mechanisms in place.

Currently it does it a very silly and non standard way, this will change.  But hey, it works ;p

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 ajm 1.3 * @author $Author: ajm4 $
16     * @version $Id: BasicAgent.java,v 1.2 2001/05/04 02:04:35 ajm4 Exp $
17 ajm 1.2 */
18     class BasicAgent extends Agent {
19    
20     //---FINAL / FINAL STATIC ATTRIBUTES---
21    
22     /**
23     * The current CVS revision of this class
24     */
25 ajm 1.3 public static final String REVISION = "$Revision: 1.2 $";
26 ajm 1.2
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 ajm 1.1 }
87     }
88 ajm 1.2
89     //*** AGENT SERVICES ***
90    
91     public void onArrival(Station station) {
92     _transmissionCount++;
93     _station = station;
94 ajm 1.3 _running = true;
95 ajm 1.2 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 ajm 1.3 say("shutting down");
119 ajm 1.2 _station = null;
120     _running = false;
121     }
122 ajm 1.1
123 ajm 1.2 //*** OTHER PUBLIC METHODS ***
124 ajm 1.3
125     //---PRIVATE/PROTECTED METHODS---
126 ajm 1.2
127 ajm 1.3 /**
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 ajm 1.2
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 ajm 1.1 private Station _station;
149 ajm 1.2
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 ajm 1.1 private long _transmissionCount = 0;
160 ajm 1.2
161     //---STATIC ATTRIBUTES---
162    
163     }