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") + ")");
219 for (String cipher
: getAnonCiphers()) {
220 if (!ciphers
.isEmpty()) {
226 while (started
&& !exiting
) {
228 final Socket s
= ss
.accept();
229 new Thread(new Runnable() {
232 ConnectActionServer action
= null;
234 action
= createConnectActionServer(s
);
238 if (action
!= null) {
239 bytesReceived
+= action
.getBytesReceived();
240 bytesSent
+= action
.getBytesSent();
247 // Will be covered by @link{Server#stop(long)} for timeouts
248 while (counter
> 0) {
251 } catch (Exception e
) {
258 } catch (Exception e
) {
268 tracer
.trace(name
+ ": client terminated on port " + port
);
273 * Will stop the server, synchronously and without a timeout.
276 tracer
.trace(name
+ ": stopping server");
284 * the maximum timeout to wait for existing actions to complete,
285 * or 0 for "no timeout"
287 * wait for the server to be stopped before returning
288 * (synchronous) or not (asynchronous)
290 public void stop(final long timeout
, final boolean wait
) {
294 new Thread(new Runnable() {
304 * Stop the server (synchronous).
307 * the maximum timeout to wait for existing actions to complete,
308 * or 0 for "no timeout"
310 private void stop(long timeout
) {
311 tracer
.trace(name
+ ": server stopping on port " + port
);
312 synchronized (lock
) {
313 if (started
&& !exiting
) {
317 new ConnectActionClientObject(createSocket(null, port
, ssl
))
320 while (ss
!= null && timeout
> 0 && timeout
> time
) {
324 } catch (Exception e
) {
326 counter
= 0; // will stop the main thread
333 // return only when stopped
334 while (started
|| exiting
) {
337 } catch (InterruptedException e
) {
343 * Change the number of currently serviced actions.
346 * the number to increase or decrease
348 * @return the current number after this operation
350 private int count(int change
) {
351 synchronized (counterLock
) {
358 * This method will be called on errors.
360 * By default, it will only call the trace handler (so you may want to call
361 * super {@link Server#onError} if you override it).
366 protected void onError(Exception e
) {
371 * Create a {@link Socket}.
374 * the host to connect to
376 * the port to connect to
378 * TRUE for SSL mode (or FALSE for plain text mode)
380 * @return the {@link Socket}
382 * @throws IOException
383 * in case of I/O error
384 * @throws UnknownHostException
385 * if the host is not known
386 * @throws IllegalArgumentException
387 * if the port parameter is outside the specified range of valid
388 * port values, which is between 0 and 65535, inclusive
390 static Socket
createSocket(String host
, int port
, boolean ssl
)
394 s
= SSLSocketFactory
.getDefault().createSocket(host
, port
);
395 if (s
instanceof SSLSocket
) {
396 // Should always be the case
397 ((SSLSocket
) s
).setEnabledCipherSuites(ANON_CIPHERS
);
400 s
= new Socket(host
, port
);
407 * Create a {@link ServerSocket}.
410 * the port to accept connections on
412 * TRUE for SSL mode (or FALSE for plain text mode)
414 * @return the {@link ServerSocket}
416 * @throws IOException
417 * in case of I/O error
418 * @throws UnknownHostException
419 * if the IP address of the host could not be determined
420 * @throws IllegalArgumentException
421 * if the port parameter is outside the specified range of valid
422 * port values, which is between 0 and 65535, inclusive
424 static ServerSocket
createSocketServer(int port
, boolean ssl
)
428 ss
= SSLServerSocketFactory
.getDefault().createServerSocket(port
);
429 if (ss
instanceof SSLServerSocket
) {
430 // Should always be the case
431 ((SSLServerSocket
) ss
).setEnabledCipherSuites(ANON_CIPHERS
);
434 ss
= new ServerSocket(port
);
441 * Return all the supported ciphers that do not use authentication.
443 * @return the list of such supported ciphers
445 public static String
[] getAnonCiphers() {
446 List
<String
> anonCiphers
= new ArrayList
<String
>();
447 for (String cipher
: ((SSLSocketFactory
) SSLSocketFactory
.getDefault())
448 .getSupportedCipherSuites()) {
449 if (cipher
.contains("_anon_")) {
450 anonCiphers
.add(cipher
);
454 return anonCiphers
.toArray(new String
[] {});