Version 3.1.6: fix Bridge, Serialiser, Progress:
[nikiroo-utils.git] / src / be / nikiroo / utils / serial / server / Server.java
index 36806d6b61924bd60d5d0d1d2c6afca68d878893..2022469a0296387f90bc5aca40005d4b2981849c 100644 (file)
@@ -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
@@ -283,19 +304,6 @@ abstract class Server implements Runnable {
                }
        }
 
-       /**
-        * This method will be called on errors.
-        * <p>
-        * 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.
+        * <p>
+        * 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,6 +346,11 @@ 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 {
@@ -351,6 +377,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
         */
        static ServerSocket createSocketServer(int port, boolean ssl)
                        throws IOException {