From: Niki Roo Date: Tue, 30 Apr 2019 21:01:54 +0000 (+0200) Subject: add an ID for the server requests X-Git-Url: http://git.nikiroo.be/?p=fanfix.git;a=commitdiff_plain;h=ae0ca537218b08a53929a5f2545269fc99e2b0b1 add an ID for the server requests --- diff --git a/src/be/nikiroo/utils/serial/server/Server.java b/src/be/nikiroo/utils/serial/server/Server.java index ed27557..932a705 100644 --- a/src/be/nikiroo/utils/serial/server/Server.java +++ b/src/be/nikiroo/utils/serial/server/Server.java @@ -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. + *

+ * 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}. * diff --git a/src/be/nikiroo/utils/serial/server/ServerObject.java b/src/be/nikiroo/utils/serial/server/ServerObject.java index 0315f90..f797766 100644 --- a/src/be/nikiroo/utils/serial/server/ServerObject.java +++ b/src/be/nikiroo/utils/serial/server/ServerObject.java @@ -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; } diff --git a/src/be/nikiroo/utils/serial/server/ServerString.java b/src/be/nikiroo/utils/serial/server/ServerString.java index a6e7a04..6fe4ad0 100644 --- a/src/be/nikiroo/utils/serial/server/ServerString.java +++ b/src/be/nikiroo/utils/serial/server/ServerString.java @@ -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; } diff --git a/src/be/nikiroo/utils/test_code/SerialServerTest.java b/src/be/nikiroo/utils/test_code/SerialServerTest.java index 9c346fd..956499a 100644 --- a/src/be/nikiroo/utils/test_code/SerialServerTest.java +++ b/src/be/nikiroo/utils/test_code/SerialServerTest.java @@ -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; }