X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Fserial%2Fserver%2FServer.java;h=afceaf95323ec349825372c858b65fc95f185761;hb=3fb0b4a71a70c154a5eeb41b89add4ae6be5a7a1;hp=36806d6b61924bd60d5d0d1d2c6afca68d878893;hpb=79ce1a4973eba079ba819ba841d906de42f38e40;p=nikiroo-utils.git diff --git a/src/be/nikiroo/utils/serial/server/Server.java b/src/be/nikiroo/utils/serial/server/Server.java index 36806d6..afceaf9 100644 --- a/src/be/nikiroo/utils/serial/server/Server.java +++ b/src/be/nikiroo/utils/serial/server/Server.java @@ -3,6 +3,7 @@ package be.nikiroo.utils.serial.server; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; +import java.net.UnknownHostException; import java.util.ArrayList; import java.util.List; @@ -62,6 +63,11 @@ abstract class Server implements Runnable { * * @throws IOException * in case of I/O error + * @throws UnknownHostException + * if the IP address of the host could not be determined + * @throws IllegalArgumentException + * if the port parameter is outside the specified range of valid + * port values, which is between 0 and 65535, inclusive */ public Server(int port, boolean ssl) throws IOException { this((String) null, port, ssl); @@ -80,6 +86,11 @@ abstract class Server implements Runnable { * * @throws IOException * in case of I/O error + * @throws UnknownHostException + * if the IP address of the host could not be determined + * @throws IllegalArgumentException + * if the port parameter is outside the specified range of valid + * port values, which is between 0 and 65535, inclusive */ public Server(String name, int port, boolean ssl) throws IOException { this.name = name; @@ -180,12 +191,22 @@ abstract class Server implements Runnable { } try { - tracer.trace(name + ": server starting on port " + port); + tracer.trace(name + ": server starting on port " + port + " (" + + (ssl ? "SSL" : "plain text") + ")"); while (started && !exiting) { count(1); - Socket s = ss.accept(); - createConnectActionServer(s).connectAsync(); + final Socket s = ss.accept(); + new Thread(new Runnable() { + @Override + public void run() { + try { + createConnectActionServer(s).connect(); + } finally { + count(-1); + } + } + }).start(); } // Will be covered by @link{Server#stop(long)} for timeouts @@ -272,30 +293,17 @@ abstract class Server implements Runnable { } } } + } - // only return when stopped - while (started || exiting) { - try { - Thread.sleep(10); - } catch (InterruptedException e) { - } + // return only when stopped + while (started || exiting) { + try { + Thread.sleep(10); + } catch (InterruptedException e) { } } } - /** - * This method will be called on errors. - *

- * By default, it will only call the trace handler (so you may want to call - * super {@link Server#onError} if you override it). - * - * @param e - * the error - */ - protected void onError(Exception e) { - tracer.error(e); - } - /** * Change the number of currently serviced actions. * @@ -304,13 +312,26 @@ abstract class Server implements Runnable { * * @return the current number after this operation */ - int count(int change) { + private int count(int change) { synchronized (counterLock) { counter += change; return counter; } } + /** + * This method will be called on errors. + *

+ * By default, it will only call the trace handler (so you may want to call + * super {@link Server#onError} if you override it). + * + * @param e + * the error + */ + protected void onError(Exception e) { + tracer.error(e); + } + /** * Create a {@link Socket}. * @@ -325,13 +346,21 @@ abstract class Server implements Runnable { * * @throws IOException * in case of I/O error + * @throws UnknownHostException + * if the host is not known + * @throws IllegalArgumentException + * if the port parameter is outside the specified range of valid + * port values, which is between 0 and 65535, inclusive */ static Socket createSocket(String host, int port, boolean ssl) throws IOException { Socket s; if (ssl) { s = SSLSocketFactory.getDefault().createSocket(host, port); - ((SSLSocket) s).setEnabledCipherSuites(ANON_CIPHERS); + if (s instanceof SSLSocket) { + // Should always be the case + ((SSLSocket) s).setEnabledCipherSuites(ANON_CIPHERS); + } } else { s = new Socket(host, port); } @@ -351,13 +380,21 @@ abstract class Server implements Runnable { * * @throws IOException * in case of I/O error + * @throws UnknownHostException + * if the IP address of the host could not be determined + * @throws IllegalArgumentException + * if the port parameter is outside the specified range of valid + * port values, which is between 0 and 65535, inclusive */ static ServerSocket createSocketServer(int port, boolean ssl) throws IOException { ServerSocket ss; if (ssl) { ss = SSLServerSocketFactory.getDefault().createServerSocket(port); - ((SSLServerSocket) ss).setEnabledCipherSuites(ANON_CIPHERS); + if (ss instanceof SSLServerSocket) { + // Should always be the case + ((SSLServerSocket) ss).setEnabledCipherSuites(ANON_CIPHERS); + } } else { ss = new ServerSocket(port); }