ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/experimental/agents/BasicAgent.java
(Generate patch)

Comparing experimental/agents/BasicAgent.java (file contents):
Revision 1.1 by ajm, Mon Apr 23 19:45:18 2001 UTC vs.
Revision 1.2 by ajm, Fri May 4 02:04:35 2001 UTC

# Line 1 | Line 1
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 {
# Line 44 | Line 74 | class BasicAgent extends Agent implements Serializable
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 +            say("No peers to move to...staying here.");
87          }
88      }
89 +    
90 +    //*** AGENT SERVICES ***    
91 +    
92 +    public void onArrival(Station station) {
93 +        _transmissionCount++;
94 +        _station = station;
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() {
# Line 62 | Line 110 | class BasicAgent extends Agent implements Serializable
110      public long getTransmissionCount() {
111          return _transmissionCount;
112      }
113 +
114 +    /**
115 +     * Used to shutdown this thread
116 +     */
117 +    public void shutdown() {
118 +        _station = null;
119 +        _running = false;
120 +    }
121      
122 <    private Station _station;
122 >    //*** 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 <    private long _transmissionCount = 0;
134 >
135 >    /**
136 >     * A reference to the current AgentStation this agent is at
137 >     */    
138 >    private Station _station;
139 >    
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 < }
145 >    
146 >    /**
147 >     * The number of times this agent has been transmitted between stations
148 >     */
149 >    private long _transmissionCount = 0;
150 >
151 > //---STATIC ATTRIBUTES---
152 >
153 > }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines