1 |
//---PACKAGE DECLARATION--- |
2 |
package uk.ac.ukc.iscream.filter; |
3 |
|
4 |
//---IMPORTS--- |
5 |
import uk.ac.ukc.iscream.core.*; |
6 |
import uk.ac.ukc.iscream.filter.*; |
7 |
import java.net.Socket; |
8 |
import java.net.ServerSocket; |
9 |
import java.io.OutputStream; |
10 |
import java.io.IOException; |
11 |
import java.net.InetAddress; |
12 |
import java.net.UnknownHostException; |
13 |
import org.omg.CORBA.*; |
14 |
import org.omg.CosNaming.*; |
15 |
|
16 |
/** |
17 |
* A socket listener to listen for new hosts registering with the system. |
18 |
* When a host makes a connection, the connecton is past to an instance |
19 |
* of the HostInit class, which handles further communication. |
20 |
* |
21 |
* @author $Author: tdb1 $ |
22 |
* @version $Id: TCPReader.java,v 1.1 2000/11/27 00:43:17 tdb1 Exp $ |
23 |
*/ |
24 |
class TCPReader extends Thread { |
25 |
|
26 |
//---FINAL ATTRIBUTES--- |
27 |
|
28 |
/** |
29 |
* The current CVS revision of this class |
30 |
*/ |
31 |
public final String REVISION = "$Revision: 1.1 $"; |
32 |
|
33 |
//---STATIC METHODS--- |
34 |
|
35 |
//---CONSTRUCTORS--- |
36 |
|
37 |
/** |
38 |
* Constructs a new listener |
39 |
* |
40 |
* @param logger a reference to the logger we are using |
41 |
* @param configManager a reference to the ConfigurationManager we are using |
42 |
* @param port The port that the server will listen on. |
43 |
*/ |
44 |
public TCPReader(Logger logger, ConfigurationManager configManager, int port, Filter parent) { |
45 |
_logger = logger; |
46 |
_configManager = configManager; |
47 |
_port = port; |
48 |
_parent = parent; |
49 |
_logger.write(toString(), Logger.SYSINIT, "started"); |
50 |
} |
51 |
|
52 |
|
53 |
|
54 |
//---PUBLIC METHODS--- |
55 |
|
56 |
/** |
57 |
* The run() method is the main loop for this thread, and we |
58 |
* will remain in here until such a point as something goes |
59 |
* wrong with the listening. After initially setting up the |
60 |
* ServerSocket we go round a while loop receiving connections |
61 |
* and then passing them off to other processes to deal with. |
62 |
*/ |
63 |
public void run(){ |
64 |
ServerSocket listenPort=null; |
65 |
// We use this boolean so we can break out of the while loop if we want |
66 |
boolean run = true; |
67 |
try{ |
68 |
// Setup the ServerSocket so that clients can connect |
69 |
listenPort = new ServerSocket(_port); |
70 |
} |
71 |
catch(IOException e){ |
72 |
} |
73 |
// Log what machine/port we're listening on |
74 |
try{ |
75 |
_logger.write(toString(), Logger.SYSMSG, "TCPReader listening on " |
76 |
+InetAddress.getLocalHost().getHostName() |
77 |
+"/"+InetAddress.getLocalHost().getHostAddress() |
78 |
+" port "+listenPort.getLocalPort()); |
79 |
} |
80 |
catch(UnknownHostException e){ |
81 |
_logger.write(toString(), Logger.SYSMSG, "TCPReader listening on UnknownHost " |
82 |
+"port "+listenPort.getLocalPort()); |
83 |
} |
84 |
// Loop round constantly until we decide to stop |
85 |
while(run){ |
86 |
Socket hostSocket=null; |
87 |
try{ |
88 |
_logger.write(toString(), Logger.SYSMSG, "Waiting for Connection"); |
89 |
// This will block until a host connects - at which point we get a Socket |
90 |
hostSocket = listenPort.accept(); |
91 |
_logger.write(toString(), Logger.SYSMSG, "Connection accepted from: " + hostSocket.toString()); |
92 |
} |
93 |
catch(IOException e){ |
94 |
// Something went wrong with the ServerSocket, so we'll stop listening |
95 |
run=false; |
96 |
} |
97 |
// If we've stopped on the line above we won't want to try this ! |
98 |
if(run){ |
99 |
try { |
100 |
// Setup the HostInit so it can carry on communications with the host |
101 |
TCPReaderInit init = new TCPReaderInit(hostSocket, _configManager, _logger, _parent); |
102 |
// and start it |
103 |
init.start(); |
104 |
} catch (IOException e) { |
105 |
_logger.write(toString(), Logger.ERROR, e.toString()); |
106 |
} |
107 |
} |
108 |
} |
109 |
// Best log the fact that we're stopping |
110 |
_logger.write(toString(), Logger.FATAL, "Fatal error, shutdown pending"); |
111 |
} |
112 |
|
113 |
/** |
114 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
115 |
* method to provide clean logging (every class should have this). |
116 |
* |
117 |
* @return the name of this class and its CVS revision |
118 |
*/ |
119 |
public String toString() { |
120 |
return this.getClass().getName() + "(" + REVISION.substring(11, REVISION.length() - 2) + ")"; |
121 |
} |
122 |
|
123 |
//---PRIVATE METHODS--- |
124 |
|
125 |
//---ACCESSOR/MUTATOR METHODS--- |
126 |
|
127 |
//---ATTRIBUTES--- |
128 |
|
129 |
/** |
130 |
* A reference to the logger the system is using |
131 |
*/ |
132 |
Logger _logger; |
133 |
|
134 |
/** |
135 |
* A reference to the configurator the system is using |
136 |
*/ |
137 |
ConfigurationManager _configManager; |
138 |
|
139 |
/** |
140 |
* The port on which the server should listen. |
141 |
*/ |
142 |
private int _port; |
143 |
|
144 |
private Filter _parent; |
145 |
|
146 |
//---STATIC ATTRIBUTES--- |
147 |
|
148 |
} |