Add 'src/be/nikiroo/utils/' from commit '46add0670fdee4bd936a13fe2448c5e20a7ffd0a'
[fanfix.git] / src / be / nikiroo / utils / serial / server / Server.java
index ed27557e4cd56e0c8335c1d0ef05cc5ca84b68f4..04701590e1c669c7d2dd143e7c9d3e921052b5b5 100644 (file)
@@ -18,6 +18,7 @@ import be.nikiroo.utils.TraceHandler;
  */
 abstract class Server implements Runnable {
        protected final String key;
+       protected long id = 0;
 
        private final String name;
        private final Object lock = new Object();
@@ -323,8 +324,7 @@ abstract class Server implements Runnable {
                                exiting = true;
 
                                try {
-                                       new ConnectActionClientObject(new Socket((String) null,
-                                                       port), key).connect();
+                                       getConnectionToMe().connect();
                                        long time = 0;
                                        while (ss != null && timeout > 0 && timeout > time) {
                                                Thread.sleep(10);
@@ -348,6 +348,20 @@ abstract class Server implements Runnable {
                }
        }
 
+       /**
+        * Return a connection to this server (used by the Exit code to send an exit
+        * message).
+        * 
+        * @return the connection
+        * 
+        * @throws UnknownHostException
+        *             the host should always be NULL (localhost)
+        * @throws IOException
+        *             in case of I/O error
+        */
+       abstract protected ConnectActionClient getConnectionToMe()
+                       throws UnknownHostException, IOException;
+
        /**
         * Change the number of currently serviced actions.
         * 
@@ -377,46 +391,29 @@ abstract class Server implements Runnable {
        }
 
        /**
-        * Create a {@link Socket}.
-        * 
-        * @param host
-        *            the host to connect to
-        * @param port
-        *            the port to connect to
-        * 
-        * @return the {@link Socket}
+        * Return the next ID to use.
         * 
-        * @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
+        * @return the next ID
         */
-       @Deprecated
-       static Socket createSocket(String host, int port) throws IOException {
-               return new Socket(host, port);
+       protected synchronized long getNextId() {
+               return id++;
        }
 
        /**
-        * Create a {@link ServerSocket}.
-        * 
-        * @param port
-        *            the port to accept connections on
-        * 
-        * @return the {@link ServerSocket}
+        * Method called when
+        * {@link ServerObject#onRequest(ConnectActionServerObject, Object, long)}
+        * has successfully finished.
+        * <p>
+        * Can be used to know how much data was transmitted.
         * 
-        * @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
+        * @param id
+        *            the ID used to identify the request
+        * @param bytesReceived
+        *            the bytes received during the request
+        * @param bytesSent
+        *            the bytes sent during the request
         */
-       @Deprecated
-       static ServerSocket createSocketServer(int port) throws IOException {
-               return new ServerSocket(port);
+       @SuppressWarnings("unused")
+       protected void onRequestDone(long id, long bytesReceived, long bytesSent) {
        }
 }