ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/experimental/agents/AgentStation.java
Revision: 1.3
Committed: Sun May 6 19:01:48 2001 UTC (23 years, 6 months ago) by ajm
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +4 -3 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.net.Socket;
5     import java.io.IOException;
6     import java.util.ArrayList;
7    
8     /**
9     * An implementation of an agent station
10     *
11 ajm 1.3 * @author $Author: ajm4 $
12     * @version $Id: AgentStation.java,v 1.2 2001/05/04 02:04:35 ajm4 Exp $
13 ajm 1.2 */
14 ajm 1.1 class AgentStation extends Station {
15 ajm 1.2
16     //---FINAL / FINAL STATIC ATTRIBUTES---
17    
18     /**
19     * The current CVS revision of this class
20     */
21 ajm 1.3 public static final String REVISION = "$Revision: 1.2 $";
22 ajm 1.2
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 ajm 1.1
54 ajm 1.2 //---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 ajm 1.1
79 ajm 1.2 PeerHandler peerHandler = new PeerHandler(socket);
80     addPeer(peerHandler);
81 ajm 1.3 peerHandler.start();
82 ajm 1.2 } catch (IOException e) {
83     _logger.write(toString(), Logger.ERROR, "I/O ERROR - " + e);
84     }
85     }
86     _logger.write(toString(), Logger.SYSINIT, "created");
87 ajm 1.1 }
88 ajm 1.2
89     //---PUBLIC METHODS---
90    
91     //*** STATION SERVICES TO AGENTS ***
92 ajm 1.1
93     public ArrayList getAllAgents() {
94     return _agentList;
95     }
96    
97 ajm 1.2 public ArrayList getAllPeers() {
98     return _peerList;
99     }
100    
101 ajm 1.1 public String getName() {
102 ajm 1.2 return _name + " - " + _descriptiveName;
103 ajm 1.1 }
104    
105 ajm 1.2 public void sendAgent(Agent agent, PeerHandler peerHandler) {
106     // NOTE - no support for restarting agent on a failed send.
107     agent.shutdown();
108     ((PeerHandler) _peerList.get(_peerList.indexOf(peerHandler))).sendAgent(agent);
109     }
110    
111     //*** OTHER PUBLIC METHODS ***
112    
113     public void addAgent(Agent agent) {
114     _logger.write(toString(), Logger.DEBUG, "registered agent");
115     _agentList.add(agent);
116     }
117    
118     public void removeAgent(Agent agent) {
119     _agentList.remove(_agentList.indexOf(agent));
120     _logger.write(toString(), Logger.DEBUG, "deregistered agent");
121     }
122    
123     public void addPeer(PeerHandler peerHandler) {
124     _logger.write(toString(), Logger.DEBUG, "registered peer");
125     _peerList.add(peerHandler);
126     }
127    
128     public void removePeer(PeerHandler peerHandler) {
129     _peerList.remove(peerHandler);
130     _logger.write(toString(), Logger.DEBUG, "deregistered peer");
131     }
132    
133     /**
134     * Overrides the {@link java.lang.Object#toString() Object.toString()}
135     * method to provide clean logging (every class should have this).
136     *
137     * This uses the uk.ac.ukc.iscream.util.FormatName class
138     * to format the toString()
139     *
140     * @return the name of this class and its CVS revision
141     */
142     public String toString() {
143     return FormatName.getName(
144     _name,
145     getClass().getName(),
146     REVISION);
147     }
148    
149     //---PRIVATE/PROTECTED METHODS---
150    
151     //---ACCESSOR/MUTATOR METHODS---
152    
153     //---ATTRIBUTES---
154    
155     /**
156     * This is the friendly identifier of the
157     * component this class is running in.
158     * eg, a Filter may be called "filter1",
159     * If this class does not have an owning
160     * component, a name from the configuration
161     * can be placed here. This name could also
162     * be changed to null for utility classes.
163     */
164     private String _name;
165    
166     /**
167     * This holds a reference to the
168     * system logger that is being used.
169     */
170     private Logger _logger = Logger.getInstance();
171    
172     private String _descriptiveName;
173    
174 ajm 1.1 private ArrayList _agentList = new ArrayList();
175 ajm 1.2
176     private ArrayList _peerList = new ArrayList();
177    
178     //---STATIC ATTRIBUTES---
179    
180     /**
181     * Holds the reference to the singleton instance of his class
182     */
183     private static AgentStation _instance;
184    
185    
186 ajm 1.1 }