ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/experimental/agents/Listener.java
Revision: 1.1
Committed: Fri May 4 02:04:35 2001 UTC (23 years, 6 months ago) by ajm
Branch: MAIN
CVS Tags: HEAD
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.io.IOException;
5 import java.net.Socket;
6 import java.net.ServerSocket;
7 import java.net.UnknownHostException;
8 import java.net.InetAddress;
9
10 /**
11 * Used as a generic listener class. Acts as a handler factory for connections
12 * by creating handlers using the assigned handler factory. This allows
13 * this class to be used as a generic listening object.
14 *
15 * @author $Author: tdb1 $
16 * @version $Id: TemplateClass.java,v 1.11 2001/03/22 21:50:41 tdb1 Exp $
17 */
18 class Listener extends Thread {
19
20 //---FINAL / FINAL STATIC ATTRIBUTES---
21
22 /**
23 * The current CVS revision of this class
24 */
25 public static final String REVISION = "$Revision: 1.11 $";
26
27 //---STATIC METHODS---
28
29 //---CONSTRUCTORS---
30
31 public Listener(int port, HandlerFactory handlerFactory) {
32 _port = port;
33 _handlerFactory = handlerFactory;
34 _name = handlerFactory.getName();
35 setName(toString());
36 _logger.write(toString(), Logger.SYSINIT, "created");
37 }
38
39 //---PUBLIC METHODS---
40
41 public void run() {
42 _logger.write(toString(), Logger.SYSINIT, "started");
43 ServerSocket listenSocket = null;
44
45 try {
46 listenSocket = new ServerSocket(_port);
47 _logger.write(toString(), Logger.SYSMSG, "Server listening on "
48 + InetAddress.getLocalHost().getHostName()
49 + "/" + InetAddress.getLocalHost().getHostAddress()
50 + " port " + listenSocket.getLocalPort());
51
52 }
53 catch(UnknownHostException e){
54 _logger.write(toString(), Logger.SYSMSG, "Server listening on UnknownHost "
55 + "port " + listenSocket.getLocalPort());
56 }
57 catch(IOException e){
58 _logger.write(toString(), Logger.FATAL, "IO Error, can't start AgentListener :"+e);
59 _running = false;
60 }
61 catch(NumberFormatException e){
62 _logger.write(toString(), Logger.FATAL, "Invalid port configuration found :"+e);
63 _running = false;
64 }
65
66 while(_running) {
67 Socket hostSocket = null;
68 try{
69 _logger.write(toString(), Logger.SYSMSG, "Waiting for Connection");
70 // This will block until a host connects - at which point we get a Socket
71 hostSocket = listenSocket.accept();
72 _logger.write(toString(), Logger.SYSMSG, "Connection accepted from: " + hostSocket.toString());
73 }
74 catch(IOException e){
75 // Something went wrong with the ServerSocket, so we'll stop listening
76 _logger.write(toString(), Logger.FATAL, "Fatal error, shutdown pending");
77 _running = false;
78 }
79 // If we've stopped on the line above we won't want to try this !
80 if(_running){
81 try {
82 // Setup the handler so it can carry on communications with the connection
83 Handler handler = _handlerFactory.newHandler(hostSocket);
84 // and start it
85 handler.start();
86 } catch (IOException e) {
87 _logger.write(toString(), Logger.ERROR, e.toString());
88 }
89 }
90 }
91 // Best log the fact that we're stopping
92 _logger.write(toString(), Logger.SYSINIT, "finished");
93 }
94
95 /**
96 * Used to shutdown this thread
97 */
98 public void shutdown() {
99 _running = false;
100 }
101
102 /**
103 * Overrides the {@link java.lang.Object#toString() Object.toString()}
104 * method to provide clean logging (every class should have this).
105 *
106 * This uses the uk.ac.ukc.iscream.util.FormatName class
107 * to format the toString()
108 *
109 * @return the name of this class and its CVS revision
110 */
111 public String toString() {
112 return FormatName.getName(
113 _name,
114 getClass().getName(),
115 REVISION);
116 }
117
118 //---PRIVATE/PROTECTED METHODS---
119
120 //---ACCESSOR/MUTATOR METHODS---
121
122 //---ATTRIBUTES---
123
124 /**
125 * This is the friendly identifier of the
126 * component this class is running in.
127 * eg, a Filter may be called "filter1",
128 * If this class does not have an owning
129 * component, a name from the configuration
130 * can be placed here. This name could also
131 * be changed to null for utility classes.
132 */
133 private String _name = null;
134
135 /**
136 * This holds a reference to the
137 * system logger that is being used.
138 */
139 private Logger _logger = Logger.getInstance();
140
141 /**
142 * A "running" used by the main loop in the run() method to determine if
143 * it should continue running this thread.
144 */
145 private boolean _running = true;
146
147 /**
148 * The handler factory that will be used to create
149 * threads to handle inbound connections
150 */
151 private HandlerFactory _handlerFactory;
152
153 /**
154 * The server port to listen on
155 */
156 private int _port;
157
158 //---STATIC ATTRIBUTES---
159
160 }