1 |
//---PACKAGE DECLARATION--- |
2 |
package uk.org.iscream.filter; |
3 |
|
4 |
//---IMPORTS--- |
5 |
import uk.org.iscream.core.*; |
6 |
import uk.org.iscream.componentmanager.*; |
7 |
import uk.org.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.org.iscream.util.*; |
15 |
|
16 |
/** |
17 |
* Reads TCP Heartbeats from the host applications. |
18 |
* |
19 |
* @author $Author: tdb1 $ |
20 |
* @version $Id: TCPReader.java,v 1.10 2001/03/13 02:19:46 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.10 $"; |
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 |
// set the Thread name |
43 |
setName("filter.TCPReader"); |
44 |
|
45 |
_port = port; |
46 |
_queue = queue; |
47 |
_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 |
_logger.write(toString(), Logger.DEBUG, "Waiting for Connection"); |
87 |
// This will block until a host connects - at which point we get a Socket |
88 |
hostSocket = listenPort.accept(); |
89 |
_logger.write(toString(), Logger.DEBUG, "Connection accepted from: " + hostSocket.toString()); |
90 |
} |
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 |
// Setup the TCPReaderInit and start it |
99 |
TCPReaderInit init = new TCPReaderInit(hostSocket, _queue); |
100 |
// 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 |
* This uses the uk.org.iscream.util.NameFormat class |
116 |
* to format the toString() |
117 |
* |
118 |
* @return the name of this class and its CVS revision |
119 |
*/ |
120 |
public String toString() { |
121 |
return FormatName.getName( |
122 |
_name, |
123 |
getClass().getName(), |
124 |
REVISION); |
125 |
} |
126 |
|
127 |
//---PRIVATE METHODS--- |
128 |
|
129 |
//---ACCESSOR/MUTATOR METHODS--- |
130 |
|
131 |
//---ATTRIBUTES--- |
132 |
|
133 |
/** |
134 |
* 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 |
*/ |
148 |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
149 |
|
150 |
/** |
151 |
* The port on which the server should listen. |
152 |
*/ |
153 |
private int _port; |
154 |
|
155 |
/** |
156 |
* A reference to our Queue |
157 |
*/ |
158 |
private Queue _queue; |
159 |
|
160 |
//---STATIC ATTRIBUTES--- |
161 |
|
162 |
} |