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