1 package be
.nikiroo
.utils
.serial
.server
;
3 import java
.io
.IOException
;
4 import java
.net
.ServerSocket
;
5 import java
.net
.Socket
;
6 import java
.net
.UnknownHostException
;
7 import java
.util
.ArrayList
;
10 import javax
.net
.ssl
.SSLServerSocket
;
11 import javax
.net
.ssl
.SSLServerSocketFactory
;
12 import javax
.net
.ssl
.SSLSocket
;
13 import javax
.net
.ssl
.SSLSocketFactory
;
15 import be
.nikiroo
.utils
.TraceHandler
;
18 * This class implements a simple server that can listen for connections and
19 * send/receive objects.
21 * Note: this {@link Server} has to be discarded after use (cannot be started
26 abstract class Server
implements Runnable
{
27 static private final String
[] ANON_CIPHERS
= getAnonCiphers();
29 private final String name
;
30 private final boolean ssl
;
31 private final Object lock
= new Object();
32 private final Object counterLock
= new Object();
34 private ServerSocket ss
;
37 private boolean started
;
38 private boolean exiting
= false;
41 private long bytesReceived
;
42 private long bytesSent
;
44 private TraceHandler tracer
= new TraceHandler();
47 * Create a new {@link ConnectActionServer} to handle a request.
50 * the socket to service
54 abstract ConnectActionServer
createConnectActionServer(Socket s
);
57 * Create a new server that will start listening on the network when
58 * {@link Server#start()} is called.
61 * the port to listen on, or 0 to assign any unallocated port
62 * found (which can later on be queried via
63 * {@link Server#getPort()}
65 * use a SSL connection (or not)
68 * in case of I/O error
69 * @throws UnknownHostException
70 * if the IP address of the host could not be determined
71 * @throws IllegalArgumentException
72 * if the port parameter is outside the specified range of valid
73 * port values, which is between 0 and 65535, inclusive
75 public Server(int port
, boolean ssl
) throws IOException
{
76 this((String
) null, port
, ssl
);
80 * Create a new server that will start listening on the network when
81 * {@link Server#start()} is called.
84 * the server name (only used for debug info and traces)
86 * the port to listen on
88 * use a SSL connection (or not)
91 * in case of I/O error
92 * @throws UnknownHostException
93 * if the IP address of the host could not be determined
94 * @throws IllegalArgumentException
95 * if the port parameter is outside the specified range of valid
96 * port values, which is between 0 and 65535, inclusive
98 public Server(String name
, int port
, boolean ssl
) throws IOException
{
102 this.ss
= createSocketServer(port
, ssl
);
104 if (this.port
== 0) {
105 this.port
= this.ss
.getLocalPort();
110 * The traces handler for this {@link Server}.
112 * @return the traces handler
114 public TraceHandler
getTraceHandler() {
119 * The traces handler for this {@link Server}.
122 * the new traces handler
124 public void setTraceHandler(TraceHandler tracer
) {
125 if (tracer
== null) {
126 tracer
= new TraceHandler(false, false, false);
129 this.tracer
= tracer
;
133 * The name of this {@link Server} if any.
135 * Used for traces and debug purposes only.
137 * @return the name or NULL
139 public String
getName() {
144 * Return the assigned port.
146 * @return the assigned port
148 public int getPort() {
153 * The total amount of bytes received.
155 * @return the amount of bytes received
157 public long getBytesReceived() {
158 return bytesReceived
;
162 * The total amount of bytes sent.
164 * @return the amount of bytes sent
166 public long getBytesSent() {
171 * Start the server (listen on the network for new connections).
173 * Can only be called once.
175 * This call is asynchronous, and will just start a new {@link Thread} on
176 * itself (see {@link Server#run()}).
178 public void start() {
179 new Thread(this).start();
183 * Start the server (listen on the network for new connections).
185 * Can only be called once.
187 * You may call it via {@link Server#start()} for an asynchronous call, too.
191 ServerSocket ss
= null;
192 boolean alreadyStarted
= false;
193 synchronized (lock
) {
195 if (!started
&& ss
!= null) {
198 alreadyStarted
= started
;
202 if (alreadyStarted
) {
203 tracer
.error(name
+ ": cannot start server on port " + port
204 + ", it is already started");
209 tracer
.error(name
+ ": cannot start server on port " + port
210 + ", it has already been used");
215 tracer
.trace(name
+ ": server starting on port " + port
+ " ("
216 + (ssl ?
"SSL" : "plain text") + ")");
218 while (started
&& !exiting
) {
220 final Socket s
= ss
.accept();
221 new Thread(new Runnable() {
224 ConnectActionServer action
= null;
226 action
= createConnectActionServer(s
);
230 if (action
!= null) {
231 bytesReceived
+= action
.getBytesReceived();
232 bytesSent
+= action
.getBytesSent();
239 // Will be covered by @link{Server#stop(long)} for timeouts
240 while (counter
> 0) {
243 } catch (Exception e
) {
250 } catch (Exception e
) {
260 tracer
.trace(name
+ ": client terminated on port " + port
);
265 * Will stop the server, synchronously and without a timeout.
268 tracer
.trace(name
+ ": stopping server");
276 * the maximum timeout to wait for existing actions to complete,
277 * or 0 for "no timeout"
279 * wait for the server to be stopped before returning
280 * (synchronous) or not (asynchronous)
282 public void stop(final long timeout
, final boolean wait
) {
286 new Thread(new Runnable() {
296 * Stop the server (synchronous).
299 * the maximum timeout to wait for existing actions to complete,
300 * or 0 for "no timeout"
302 private void stop(long timeout
) {
303 tracer
.trace(name
+ ": server stopping on port " + port
);
304 synchronized (lock
) {
305 if (started
&& !exiting
) {
309 new ConnectActionClientObject(createSocket(null, port
, ssl
))
312 while (ss
!= null && timeout
> 0 && timeout
> time
) {
316 } catch (Exception e
) {
318 counter
= 0; // will stop the main thread
325 // return only when stopped
326 while (started
|| exiting
) {
329 } catch (InterruptedException e
) {
335 * Change the number of currently serviced actions.
338 * the number to increase or decrease
340 * @return the current number after this operation
342 private int count(int change
) {
343 synchronized (counterLock
) {
350 * This method will be called on errors.
352 * By default, it will only call the trace handler (so you may want to call
353 * super {@link Server#onError} if you override it).
358 protected void onError(Exception e
) {
363 * Create a {@link Socket}.
366 * the host to connect to
368 * the port to connect to
370 * TRUE for SSL mode (or FALSE for plain text mode)
372 * @return the {@link Socket}
374 * @throws IOException
375 * in case of I/O error
376 * @throws UnknownHostException
377 * if the host is not known
378 * @throws IllegalArgumentException
379 * if the port parameter is outside the specified range of valid
380 * port values, which is between 0 and 65535, inclusive
382 static Socket
createSocket(String host
, int port
, boolean ssl
)
386 s
= SSLSocketFactory
.getDefault().createSocket(host
, port
);
387 if (s
instanceof SSLSocket
) {
388 // Should always be the case
389 ((SSLSocket
) s
).setEnabledCipherSuites(ANON_CIPHERS
);
392 s
= new Socket(host
, port
);
399 * Create a {@link ServerSocket}.
402 * the port to accept connections on
404 * TRUE for SSL mode (or FALSE for plain text mode)
406 * @return the {@link ServerSocket}
408 * @throws IOException
409 * in case of I/O error
410 * @throws UnknownHostException
411 * if the IP address of the host could not be determined
412 * @throws IllegalArgumentException
413 * if the port parameter is outside the specified range of valid
414 * port values, which is between 0 and 65535, inclusive
416 static ServerSocket
createSocketServer(int port
, boolean ssl
)
420 ss
= SSLServerSocketFactory
.getDefault().createServerSocket(port
);
421 if (ss
instanceof SSLServerSocket
) {
422 // Should always be the case
423 ((SSLServerSocket
) ss
).setEnabledCipherSuites(ANON_CIPHERS
);
426 ss
= new ServerSocket(port
);
433 * Return all the supported ciphers that do not use authentication.
435 * @return the list of such supported ciphers
437 public static String
[] getAnonCiphers() {
438 List
<String
> anonCiphers
= new ArrayList
<String
>();
439 for (String cipher
: ((SSLSocketFactory
) SSLSocketFactory
.getDefault())
440 .getSupportedCipherSuites()) {
441 if (cipher
.contains("_anon_")) {
442 anonCiphers
.add(cipher
);
446 return anonCiphers
.toArray(new String
[] {});