ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/experimental/agents/AgentStation.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: +170 -46 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

# Content
1 //---PACKAGE DECLARATION---
2
3 //---IMPORTS---
4 import java.net.Socket;
5 import java.io.IOException;
6 import java.util.ArrayList;
7
8 /**
9 * An implementation of an agent station
10 *
11 * @author $Author: tdb1 $
12 * @version $Id: TemplateClass.java,v 1.11 2001/03/22 21:50:41 tdb1 Exp $
13 */
14 class AgentStation extends Station {
15
16 //---FINAL / FINAL STATIC ATTRIBUTES---
17
18 /**
19 * The current CVS revision of this class
20 */
21 public static final String REVISION = "$Revision: 1.11 $";
22
23 public static final int DIRECT_LISTEN_PORT = 32497;
24
25 public static final int PEER_LISTEN_PORT = 32498;
26
27 //---STATIC METHODS---
28
29 /**
30 * Returns a reference to the singleton agent station in use
31 *
32 * @throws RuntimeException if not yet initialised
33 */
34 public static AgentStation getInstance() throws RuntimeException {
35 if (_instance == null) {
36 throw new RuntimeException("Cannot obtain instance to uninitialised AgentStation!");
37 }
38 return _instance;
39 }
40
41 /**
42 * Constructs and returns the singleton AgentStation class
43 *
44 * @throws RuntimeException if already initialised
45 */
46 public static AgentStation initialise(String name, String descriptiveName, String initialPeerHostname, int initialPeerPort) {
47 if (_instance != null) {
48 throw new RuntimeException("Already initialised!");
49 }
50 _instance = new AgentStation(name, descriptiveName, initialPeerHostname, initialPeerPort);
51 return _instance;
52 }
53
54 //---CONSTRUCTORS---
55
56 /**
57 * Starts up the agent station.
58 * This is a private method to ensure singleton pattern.
59 */
60 private AgentStation(String name, String descriptiveName, String initialPeerHostname, int initialPeerPort) {
61 _name = name;
62 _descriptiveName = descriptiveName;
63
64 DirectAgentHandlerFactory directAgentHandlerFactory = new DirectAgentHandlerFactory();
65 PeerHandlerFactory peerHandlerFactory = new PeerHandlerFactory();
66
67 Listener directAgentListener = new Listener(DIRECT_LISTEN_PORT, directAgentHandlerFactory);
68 Listener peerListener = new Listener(PEER_LISTEN_PORT, peerHandlerFactory);
69
70 directAgentListener.start();
71 peerListener.start();
72
73 // TESTING ONLY - Have one peer connection
74 if (initialPeerHostname != null) {
75 try {
76 _logger.write(toString(), Logger.DEBUG, "Establishing peer connection with - " + initialPeerHostname + ":" + initialPeerPort);
77 Socket socket = new Socket(initialPeerHostname, initialPeerPort);
78
79 PeerHandler peerHandler = new PeerHandler(socket);
80 addPeer(peerHandler);
81 } catch (IOException e) {
82 _logger.write(toString(), Logger.ERROR, "I/O ERROR - " + e);
83 }
84 }
85 _logger.write(toString(), Logger.SYSINIT, "created");
86 }
87
88 //---PUBLIC METHODS---
89
90 //*** STATION SERVICES TO AGENTS ***
91
92 public ArrayList getAllAgents() {
93 return _agentList;
94 }
95
96 public ArrayList getAllPeers() {
97 return _peerList;
98 }
99
100 public String getName() {
101 return _name + " - " + _descriptiveName;
102 }
103
104 public void sendAgent(Agent agent, PeerHandler peerHandler) {
105 // NOTE - no support for restarting agent on a failed send.
106 agent.shutdown();
107 ((PeerHandler) _peerList.get(_peerList.indexOf(peerHandler))).sendAgent(agent);
108 }
109
110 //*** OTHER PUBLIC METHODS ***
111
112 public void addAgent(Agent agent) {
113 _logger.write(toString(), Logger.DEBUG, "registered agent");
114 _agentList.add(agent);
115 }
116
117 public void removeAgent(Agent agent) {
118 _agentList.remove(_agentList.indexOf(agent));
119 _logger.write(toString(), Logger.DEBUG, "deregistered agent");
120 }
121
122 public void addPeer(PeerHandler peerHandler) {
123 _logger.write(toString(), Logger.DEBUG, "registered peer");
124 _peerList.add(peerHandler);
125 }
126
127 public void removePeer(PeerHandler peerHandler) {
128 _peerList.remove(peerHandler);
129 _logger.write(toString(), Logger.DEBUG, "deregistered peer");
130 }
131
132 /**
133 * Overrides the {@link java.lang.Object#toString() Object.toString()}
134 * method to provide clean logging (every class should have this).
135 *
136 * This uses the uk.ac.ukc.iscream.util.FormatName class
137 * to format the toString()
138 *
139 * @return the name of this class and its CVS revision
140 */
141 public String toString() {
142 return FormatName.getName(
143 _name,
144 getClass().getName(),
145 REVISION);
146 }
147
148 //---PRIVATE/PROTECTED METHODS---
149
150 //---ACCESSOR/MUTATOR METHODS---
151
152 //---ATTRIBUTES---
153
154 /**
155 * This is the friendly identifier of the
156 * component this class is running in.
157 * eg, a Filter may be called "filter1",
158 * If this class does not have an owning
159 * component, a name from the configuration
160 * can be placed here. This name could also
161 * be changed to null for utility classes.
162 */
163 private String _name;
164
165 /**
166 * This holds a reference to the
167 * system logger that is being used.
168 */
169 private Logger _logger = Logger.getInstance();
170
171 private String _descriptiveName;
172
173 private ArrayList _agentList = new ArrayList();
174
175 private ArrayList _peerList = new ArrayList();
176
177 //---STATIC ATTRIBUTES---
178
179 /**
180 * Holds the reference to the singleton instance of his class
181 */
182 private static AgentStation _instance;
183
184
185 }