1 |
ajm |
1.2 |
//---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 |
tdb |
1.3 |
import uk.ac.ukc.iscream.componentmanager.*; |
8 |
ajm |
1.2 |
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 |
|
|
* Creates a local implementation of the ClientHandler interface |
17 |
|
|
* and registers this with the ClientInterfaceServant. It then |
18 |
|
|
* returns a "reference" to this ClientHandler back to the client. |
19 |
|
|
* |
20 |
tdb |
1.3 |
* @author $Author: ajm4 $ |
21 |
|
|
* @version $Id: ClientListener.java,v 1.2 2000/12/13 12:57:38 ajm4 Exp $ |
22 |
ajm |
1.2 |
*/ |
23 |
|
|
class ClientListener extends Thread { |
24 |
|
|
|
25 |
|
|
//---FINAL ATTRIBUTES--- |
26 |
|
|
|
27 |
|
|
/** |
28 |
|
|
* The current CVS revision of this class |
29 |
|
|
*/ |
30 |
tdb |
1.3 |
public final String REVISION = "$Revision: 1.2 $"; |
31 |
ajm |
1.2 |
|
32 |
|
|
//---STATIC METHODS--- |
33 |
|
|
|
34 |
|
|
//---CONSTRUCTORS--- |
35 |
|
|
|
36 |
|
|
/** |
37 |
|
|
* Constructs a new ClientListener |
38 |
|
|
* |
39 |
|
|
* @param port The port that the server will listen on. |
40 |
|
|
* @param clit A reference to the ClientInterfaceServant. |
41 |
|
|
*/ |
42 |
|
|
public ClientListener(int port, ClientInterfaceServant cli) { |
43 |
|
|
_port = port; |
44 |
|
|
_cli = cli; |
45 |
|
|
_logger.write(toString(), Logger.SYSINIT, "started"); |
46 |
|
|
} |
47 |
|
|
|
48 |
|
|
//---PUBLIC METHODS--- |
49 |
|
|
|
50 |
|
|
/** |
51 |
|
|
* The run() method is the main loop for this thread, and we |
52 |
|
|
* will remain in here until such a point as something goes |
53 |
|
|
* wrong with the listening. After initially setting up the |
54 |
|
|
* ServerSocket we go round a while loop receiving connections |
55 |
|
|
* and then passing them off to other processes to deal with. |
56 |
|
|
*/ |
57 |
|
|
public void run(){ |
58 |
|
|
ServerSocket listenPort=null; |
59 |
|
|
// We use this boolean so we can break out of the while loop if we want |
60 |
|
|
boolean run = true; |
61 |
|
|
try{ |
62 |
|
|
// Setup the ServerSocket so that clients can connect |
63 |
|
|
listenPort = new ServerSocket(_port); |
64 |
|
|
} |
65 |
|
|
catch(IOException e){ |
66 |
|
|
} |
67 |
|
|
// Log what machine/port we're listening on |
68 |
|
|
try{ |
69 |
|
|
_logger.write(toString(), Logger.SYSMSG, "ClientListener listening on " |
70 |
|
|
+InetAddress.getLocalHost().getHostName() |
71 |
|
|
+"/"+InetAddress.getLocalHost().getHostAddress() |
72 |
|
|
+" port "+listenPort.getLocalPort()); |
73 |
|
|
} |
74 |
|
|
catch(UnknownHostException e){ |
75 |
|
|
_logger.write(toString(), Logger.SYSMSG, "ClientListener listening on UnknownHost " |
76 |
|
|
+"port "+listenPort.getLocalPort()); |
77 |
|
|
} |
78 |
|
|
// Loop round constantly until we decide to stop |
79 |
|
|
while(run){ |
80 |
|
|
Socket hostSocket=null; |
81 |
|
|
try{ |
82 |
|
|
_logger.write(toString(), Logger.SYSMSG, "Waiting for Connection"); |
83 |
|
|
// This will block until a host connects - at which point we get a Socket |
84 |
|
|
hostSocket = listenPort.accept(); |
85 |
|
|
_logger.write(toString(), Logger.SYSMSG, "Connection accepted from: " + hostSocket.toString()); |
86 |
|
|
} |
87 |
|
|
catch(IOException e){ |
88 |
|
|
// Something went wrong with the ServerSocket, so we'll stop listening |
89 |
|
|
run=false; |
90 |
|
|
} |
91 |
|
|
// If we've stopped on the line above we won't want to try this ! |
92 |
|
|
if(run){ |
93 |
|
|
try { |
94 |
|
|
// Setup the HostInit so it can carry on communications with the host |
95 |
|
|
ClientHandler handler = new TCPClientHandler(hostSocket, _cli); |
96 |
|
|
// register it |
97 |
|
|
_cli.register(handler); |
98 |
|
|
// then we can leave here.... I trust handler won't go out of scope ! |
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 = ClientInterfaceMain.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 the ClientInterfaceServant. |
154 |
|
|
*/ |
155 |
|
|
private ClientInterfaceServant _cli; |
156 |
|
|
|
157 |
|
|
//---STATIC ATTRIBUTES--- |
158 |
|
|
|
159 |
|
|
} |