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