1 |
tdb |
1.1 |
//---PACKAGE DECLARATION--- |
2 |
tdb |
1.2 |
package uk.ac.ukc.iscream.filter; |
3 |
tdb |
1.1 |
|
4 |
|
|
//---IMPORTS--- |
5 |
|
|
import uk.ac.ukc.iscream.core.*; |
6 |
tdb |
1.7 |
import uk.ac.ukc.iscream.componentmanager.*; |
7 |
tdb |
1.1 |
import uk.ac.ukc.iscream.filter.*; |
8 |
|
|
import java.net.Socket; |
9 |
|
|
import java.net.ServerSocket; |
10 |
|
|
import java.io.OutputStream; |
11 |
|
|
import java.io.IOException; |
12 |
|
|
import java.net.InetAddress; |
13 |
|
|
import java.net.UnknownHostException; |
14 |
ajm |
1.4 |
import uk.ac.ukc.iscream.util.*; |
15 |
tdb |
1.1 |
|
16 |
|
|
/** |
17 |
tdb |
1.8 |
* Reads TCP Heartbeats from the host applications. |
18 |
tdb |
1.1 |
* |
19 |
tdb |
1.7 |
* @author $Author: tdb1 $ |
20 |
tdb |
1.10 |
* @version $Id: TCPReader.java,v 1.9 2001/02/01 00:18:42 tdb1 Exp $ |
21 |
tdb |
1.1 |
*/ |
22 |
|
|
class TCPReader extends Thread { |
23 |
|
|
|
24 |
|
|
//---FINAL ATTRIBUTES--- |
25 |
|
|
|
26 |
|
|
/** |
27 |
|
|
* The current CVS revision of this class |
28 |
|
|
*/ |
29 |
tdb |
1.10 |
public final String REVISION = "$Revision: 1.9 $"; |
30 |
tdb |
1.1 |
|
31 |
|
|
//---STATIC METHODS--- |
32 |
|
|
|
33 |
|
|
//---CONSTRUCTORS--- |
34 |
|
|
|
35 |
|
|
/** |
36 |
tdb |
1.8 |
* Constructs a new TCPReader |
37 |
tdb |
1.1 |
* |
38 |
tdb |
1.8 |
* @param queue A reference to our Queue |
39 |
|
|
* @param port The port that the TCPReader will listen on |
40 |
tdb |
1.1 |
*/ |
41 |
tdb |
1.6 |
public TCPReader(int port, Queue queue) { |
42 |
tdb |
1.10 |
// set the Thread name |
43 |
|
|
setName("filter.TCPReader"); |
44 |
|
|
|
45 |
tdb |
1.1 |
_port = port; |
46 |
tdb |
1.6 |
_queue = queue; |
47 |
tdb |
1.1 |
_logger.write(toString(), Logger.SYSINIT, "started"); |
48 |
|
|
} |
49 |
|
|
|
50 |
|
|
|
51 |
|
|
|
52 |
|
|
//---PUBLIC METHODS--- |
53 |
|
|
|
54 |
|
|
/** |
55 |
|
|
* The run() method is the main loop for this thread, and we |
56 |
|
|
* will remain in here until such a point as something goes |
57 |
|
|
* wrong with the listening. After initially setting up the |
58 |
|
|
* ServerSocket we go round a while loop receiving connections |
59 |
|
|
* and then passing them off to other processes to deal with. |
60 |
|
|
*/ |
61 |
|
|
public void run(){ |
62 |
|
|
ServerSocket listenPort=null; |
63 |
|
|
// We use this boolean so we can break out of the while loop if we want |
64 |
|
|
boolean run = true; |
65 |
|
|
try{ |
66 |
|
|
// Setup the ServerSocket so that clients can connect |
67 |
|
|
listenPort = new ServerSocket(_port); |
68 |
|
|
} |
69 |
|
|
catch(IOException e){ |
70 |
|
|
} |
71 |
|
|
// Log what machine/port we're listening on |
72 |
|
|
try{ |
73 |
|
|
_logger.write(toString(), Logger.SYSMSG, "TCPReader listening on " |
74 |
|
|
+InetAddress.getLocalHost().getHostName() |
75 |
|
|
+"/"+InetAddress.getLocalHost().getHostAddress() |
76 |
|
|
+" port "+listenPort.getLocalPort()); |
77 |
|
|
} |
78 |
|
|
catch(UnknownHostException e){ |
79 |
|
|
_logger.write(toString(), Logger.SYSMSG, "TCPReader listening on UnknownHost " |
80 |
|
|
+"port "+listenPort.getLocalPort()); |
81 |
|
|
} |
82 |
|
|
// Loop round constantly until we decide to stop |
83 |
|
|
while(run){ |
84 |
|
|
Socket hostSocket=null; |
85 |
|
|
try{ |
86 |
tdb |
1.9 |
_logger.write(toString(), Logger.DEBUG, "Waiting for Connection"); |
87 |
tdb |
1.1 |
// This will block until a host connects - at which point we get a Socket |
88 |
|
|
hostSocket = listenPort.accept(); |
89 |
tdb |
1.9 |
_logger.write(toString(), Logger.DEBUG, "Connection accepted from: " + hostSocket.toString()); |
90 |
tdb |
1.1 |
} |
91 |
|
|
catch(IOException e){ |
92 |
|
|
// Something went wrong with the ServerSocket, so we'll stop listening |
93 |
|
|
run=false; |
94 |
|
|
} |
95 |
|
|
// If we've stopped on the line above we won't want to try this ! |
96 |
|
|
if(run){ |
97 |
|
|
try { |
98 |
tdb |
1.8 |
// Setup the TCPReaderInit and start it |
99 |
tdb |
1.6 |
TCPReaderInit init = new TCPReaderInit(hostSocket, _queue); |
100 |
tdb |
1.1 |
// and start it |
101 |
|
|
init.start(); |
102 |
|
|
} catch (IOException e) { |
103 |
|
|
_logger.write(toString(), Logger.ERROR, e.toString()); |
104 |
|
|
} |
105 |
|
|
} |
106 |
|
|
} |
107 |
|
|
// Best log the fact that we're stopping |
108 |
|
|
_logger.write(toString(), Logger.FATAL, "Fatal error, shutdown pending"); |
109 |
|
|
} |
110 |
|
|
|
111 |
|
|
/** |
112 |
|
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
113 |
|
|
* method to provide clean logging (every class should have this). |
114 |
|
|
* |
115 |
ajm |
1.5 |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
116 |
|
|
* to format the toString() |
117 |
|
|
* |
118 |
tdb |
1.1 |
* @return the name of this class and its CVS revision |
119 |
|
|
*/ |
120 |
|
|
public String toString() { |
121 |
ajm |
1.5 |
return FormatName.getName( |
122 |
|
|
_name, |
123 |
|
|
getClass().getName(), |
124 |
|
|
REVISION); |
125 |
tdb |
1.1 |
} |
126 |
|
|
|
127 |
|
|
//---PRIVATE METHODS--- |
128 |
|
|
|
129 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
130 |
|
|
|
131 |
|
|
//---ATTRIBUTES--- |
132 |
|
|
|
133 |
|
|
/** |
134 |
ajm |
1.5 |
* This is the friendly identifier of the |
135 |
|
|
* component this class is running in. |
136 |
|
|
* eg, a Filter may be called "filter1", |
137 |
|
|
* If this class does not have an owning |
138 |
|
|
* component, a name from the configuration |
139 |
|
|
* can be placed here. This name could also |
140 |
|
|
* be changed to null for utility classes. |
141 |
|
|
*/ |
142 |
|
|
private String _name = FilterMain.NAME; |
143 |
|
|
|
144 |
|
|
/** |
145 |
|
|
* This holds a reference to the |
146 |
|
|
* system logger that is being used. |
147 |
tdb |
1.1 |
*/ |
148 |
ajm |
1.5 |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
149 |
tdb |
1.1 |
|
150 |
|
|
/** |
151 |
|
|
* The port on which the server should listen. |
152 |
|
|
*/ |
153 |
|
|
private int _port; |
154 |
|
|
|
155 |
tdb |
1.8 |
/** |
156 |
|
|
* A reference to our Queue |
157 |
|
|
*/ |
158 |
tdb |
1.6 |
private Queue _queue; |
159 |
tdb |
1.1 |
|
160 |
|
|
//---STATIC ATTRIBUTES--- |
161 |
|
|
|
162 |
|
|
} |