add an ID for the server requests
authorNiki Roo <niki@nikiroo.be>
Tue, 30 Apr 2019 21:01:54 +0000 (23:01 +0200)
committerNiki Roo <niki@nikiroo.be>
Tue, 30 Apr 2019 21:01:54 +0000 (23:01 +0200)
src/be/nikiroo/utils/serial/server/Server.java
src/be/nikiroo/utils/serial/server/ServerObject.java
src/be/nikiroo/utils/serial/server/ServerString.java
src/be/nikiroo/utils/test_code/SerialServerTest.java

index ed27557e4cd56e0c8335c1d0ef05cc5ca84b68f4..932a705136d5a550df5dc5a7d8992b5e313c37fe 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();
@@ -376,6 +377,33 @@ abstract class Server implements Runnable {
                tracer.error(e);
        }
 
+       /**
+        * Return the next ID to use.
+        * 
+        * @return the next ID
+        */
+       protected synchronized long getNextId() {
+               return id++;
+       }
+
+       /**
+        * Method called when
+        * {@link ServerObject#onRequest(ConnectActionServerObject, Object, long)}
+        * has successfully finished.
+        * <p>
+        * Can be used to know how much data was transmitted.
+        * 
+        * @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
+        */
+       @SuppressWarnings("unused")
+       protected void onRequestDone(long id, long bytesReceived, long bytesSent) {
+       }
+
        /**
         * Create a {@link Socket}.
         * 
index 0315f90c47f74b19d557e6a029e065da89dd1911..f79776696cdc2e6c5fae13e6ed0f733c814268c2 100644 (file)
@@ -67,11 +67,12 @@ abstract public class ServerObject extends Server {
                return new ConnectActionServerObject(s, key) {
                        @Override
                        public void action() throws Exception {
+                               long id = getNextId();
                                try {
                                        for (Object data = rec(); true; data = rec()) {
                                                Object rep = null;
                                                try {
-                                                       rep = onRequest(this, data);
+                                                       rep = onRequest(this, data, id);
                                                        if (isClosing()) {
                                                                return;
                                                        }
@@ -83,6 +84,7 @@ abstract public class ServerObject extends Server {
                                        }
                                } catch (NullPointerException e) {
                                        // Client has no data any more, we quit
+                                       onRequestDone(id, getBytesReceived(), getBytesSent());
                                }
                        }
 
@@ -102,6 +104,9 @@ abstract public class ServerObject extends Server {
         *            the client action
         * @param data
         *            the data sent by the client (which can be NULL)
+        * @param id
+        *            an ID to identify this request (will also be re-used for
+        *            {@link ServerObject#onRequestDone(long, long, long)}.
         * 
         * @return the answer to return to the client (which can be NULL)
         * 
@@ -109,5 +114,5 @@ abstract public class ServerObject extends Server {
         *             in case of an exception, the error will only be logged
         */
        abstract protected Object onRequest(ConnectActionServerObject action,
-                       Object data) throws Exception;
+                       Object data, long id) throws Exception;
 }
index a6e7a04f80dbb9d0d0e9784f5ada3d3c1b82cfcc..6fe4ad0df1a6aeb4d37aaba4de6a5f5bdf1ce306 100644 (file)
@@ -67,10 +67,11 @@ abstract public class ServerString extends Server {
                return new ConnectActionServerString(s, key) {
                        @Override
                        public void action() throws Exception {
+                               long id = getNextId();
                                for (String data = rec(); data != null; data = rec()) {
                                        String rep = null;
                                        try {
-                                               rep = onRequest(this, data);
+                                               rep = onRequest(this, data, id);
                                                if (isClosing()) {
                                                        return;
                                                }
@@ -81,9 +82,10 @@ abstract public class ServerString extends Server {
                                        if (rep == null) {
                                                rep = "";
                                        }
-
                                        send(rep);
                                }
+
+                               onRequestDone(id, getBytesReceived(), getBytesSent());
                        }
 
                        @Override
@@ -103,6 +105,9 @@ abstract public class ServerString extends Server {
         *            the client action
         * @param data
         *            the data sent by the client
+        * @param id
+        *            an ID to identify this request (will also be re-used for
+        *            {@link ServerObject#onRequestDone(long, long, long)}.
         * 
         * @return the answer to return to the client
         * 
@@ -110,5 +115,5 @@ abstract public class ServerString extends Server {
         *             in case of an exception, the error will only be logged
         */
        abstract protected String onRequest(ConnectActionServerString action,
-                       String data) throws Exception;
+                       String data, long id) throws Exception;
 }
index 9c346fd5829b880622dd05307513720f816213b3..956499a40f3e43cac07998ffa5f9c20764b7bc70 100644 (file)
@@ -51,8 +51,8 @@ class SerialServerTest extends TestLauncher {
                                ServerString server = new ServerString(this.getName(), 0, key) {
                                        @Override
                                        protected String onRequest(
-                                                       ConnectActionServerString action, String data)
-                                                       throws Exception {
+                                                       ConnectActionServerString action, String data,
+                                                       long id) throws Exception {
                                                return null;
                                        }
 
@@ -111,8 +111,8 @@ class SerialServerTest extends TestLauncher {
                                ServerString server = new ServerString(this.getName(), 0, key) {
                                        @Override
                                        protected String onRequest(
-                                                       ConnectActionServerString action, String data)
-                                                       throws Exception {
+                                                       ConnectActionServerString action, String data,
+                                                       long id) throws Exception {
                                                sent[0] = data;
                                                return "pong";
                                        }
@@ -172,8 +172,8 @@ class SerialServerTest extends TestLauncher {
                                ServerString server = new ServerString(this.getName(), 0, key) {
                                        @Override
                                        protected String onRequest(
-                                                       ConnectActionServerString action, String data)
-                                                       throws Exception {
+                                                       ConnectActionServerString action, String data,
+                                                       long id) throws Exception {
                                                sent[0] = data;
                                                action.send("pong");
                                                sent[1] = action.rec();
@@ -238,8 +238,8 @@ class SerialServerTest extends TestLauncher {
                                ServerString server = new ServerString(this.getName(), 0, key) {
                                        @Override
                                        protected String onRequest(
-                                                       ConnectActionServerString action, String data)
-                                                       throws Exception {
+                                                       ConnectActionServerString action, String data,
+                                                       long id) throws Exception {
                                                sent[Integer.parseInt(data)] = data;
                                                return "" + (Integer.parseInt(data) * 2);
                                        }
@@ -310,8 +310,8 @@ class SerialServerTest extends TestLauncher {
                                ServerObject server = new ServerObject(this.getName(), 0, key) {
                                        @Override
                                        protected Object onRequest(
-                                                       ConnectActionServerObject action, Object data)
-                                                       throws Exception {
+                                                       ConnectActionServerObject action, Object data,
+                                                       long id) throws Exception {
                                                return null;
                                        }
 
@@ -369,8 +369,8 @@ class SerialServerTest extends TestLauncher {
                                ServerObject server = new ServerObject(this.getName(), 0, key) {
                                        @Override
                                        protected Object onRequest(
-                                                       ConnectActionServerObject action, Object data)
-                                                       throws Exception {
+                                                       ConnectActionServerObject action, Object data,
+                                                       long id) throws Exception {
                                                sent[0] = data;
                                                return "pong";
                                        }
@@ -430,8 +430,8 @@ class SerialServerTest extends TestLauncher {
                                ServerObject server = new ServerObject(this.getName(), 0, key) {
                                        @Override
                                        protected Object onRequest(
-                                                       ConnectActionServerObject action, Object data)
-                                                       throws Exception {
+                                                       ConnectActionServerObject action, Object data,
+                                                       long id) throws Exception {
                                                sent[0] = data;
                                                action.send("pong");
                                                sent[1] = action.rec();
@@ -496,8 +496,8 @@ class SerialServerTest extends TestLauncher {
                                ServerObject server = new ServerObject(this.getName(), 0, key) {
                                        @Override
                                        protected Object onRequest(
-                                                       ConnectActionServerObject action, Object data)
-                                                       throws Exception {
+                                                       ConnectActionServerObject action, Object data,
+                                                       long id) throws Exception {
                                                sent[0] = data;
                                                return new Object[] { "ACK" };
                                        }
@@ -567,8 +567,8 @@ class SerialServerTest extends TestLauncher {
                                ServerObject server = new ServerObject(this.getName(), 0, key) {
                                        @Override
                                        protected Object onRequest(
-                                                       ConnectActionServerObject action, Object data)
-                                                       throws Exception {
+                                                       ConnectActionServerObject action, Object data,
+                                                       long id) throws Exception {
                                                sent[(Integer) data] = data;
                                                return ((Integer) data) * 2;
                                        }