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