ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/experimental/agents/BasicAgent.java
Revision: 1.1
Committed: Mon Apr 23 19:45:18 2001 UTC (23 years ago) by ajm
Branch: MAIN
Log Message:
Sketchy ideas for starting the "agents" project.

File Contents

# Content
1 import java.io.*;
2 import java.net.*;
3 import java.util.*;
4
5 class BasicAgent extends Agent implements Serializable {
6 public static void main(String[] args) throws IOException, UnknownHostException {
7 System.out.println("Agent Started.");
8 if (args.length != 3) {
9 throw new RuntimeException("Must specify an agent name and agent station hostname and port number on the command line!");
10 }
11 Agent instance = new BasicAgent(args[0]);
12 System.out.println("Transmission count - " + instance.getTransmissionCount());
13 Socket s = new Socket(args[1], Integer.parseInt(args[2]));
14 ObjectOutputStream p = new ObjectOutputStream(s.getOutputStream());
15 p.writeObject(instance);
16 p.flush();
17
18 s.close();
19 System.out.println("Agent finished.");
20 }
21
22 public BasicAgent(String name) {
23 _name = name;
24 }
25
26 public void onArrival(Station station) {
27 _transmissionCount++;
28 _station = station;
29 say("Just arrived at - " + _station.getName());
30 }
31
32 public void run() {
33 while(_running) {
34 try {
35 Thread.sleep(10000);
36 } catch (InterruptedException e) {
37 }
38 ArrayList agents = _station.getAllAgents();
39 Iterator i = agents.iterator();
40 while(i.hasNext()) {
41 Agent agent = (Agent) i.next();
42 if (agent != (Agent) this) {
43 say("Saying hi to " + agent.getName());
44 agent.greet(this);
45 }
46 }
47 }
48 }
49
50 public void greet(Agent agent) {
51 say(agent.getName() + " just said hi to me");
52 }
53
54 private void say(String text) {
55 System.out.println(getName() + ": " + text);
56 }
57
58 public String getName() {
59 return _name;
60 }
61
62 public long getTransmissionCount() {
63 return _transmissionCount;
64 }
65
66 private Station _station;
67 private String _name;
68 private long _transmissionCount = 0;
69 private boolean _running = true;
70 }