server: allow closing conn from server action
authorNiki Roo <niki@nikiroo.be>
Mon, 22 Apr 2019 09:13:48 +0000 (11:13 +0200)
committerNiki Roo <niki@nikiroo.be>
Mon, 22 Apr 2019 09:13:48 +0000 (11:13 +0200)
src/be/nikiroo/utils/serial/server/ConnectActionServer.java
src/be/nikiroo/utils/serial/server/ServerObject.java
src/be/nikiroo/utils/serial/server/ServerString.java

index 60f8db85db4f6f278a9b1f50ef1d41de92b0540e..10d3440e7af4a4a09e5b7cdf06151ab15ac98f66 100644 (file)
@@ -13,6 +13,8 @@ import be.nikiroo.utils.Version;
  * @author niki
  */
 abstract class ConnectActionServer {
+       private boolean closing;
+
        /**
         * The underlying {@link ConnectAction}.
         * <p>
@@ -77,6 +79,32 @@ abstract class ConnectActionServer {
                }).start();
        }
 
+       /**
+        * Stop the client/server connection on behalf of the server (usually, the
+        * client connects then is allowed to send as many requests as it wants; in
+        * some cases, though, the server may wish to forcefully close the
+        * connection and can do via this value, when it is set to TRUE).
+        * <p>
+        * Example of usage: the client failed an authentication check, cut the
+        * connection here and now.
+        */
+       public boolean isClosing() {
+               return closing;
+       }
+
+       /**
+        * Can be called to stop the client/server connection on behalf of the
+        * server (usually, the client connects then is allowed to send as many
+        * requests as it wants; in some cases, though, the server may wish to
+        * forcefully close the connection and can do so by calling this method).
+        * <p>
+        * Example of usage: the client failed an authentication check, cut the
+        * connection here and now.
+        */
+       public void close() {
+               closing = true;
+       }
+
        /**
         * The total amount of bytes received.
         * 
index 53606ed420a671ce34826fe0d1380dac9501d05d..6d3d53935208d713ef251d3ec8bf7b5ce54c85bf 100644 (file)
@@ -72,6 +72,9 @@ abstract public class ServerObject extends Server {
                                                Object rep = null;
                                                try {
                                                        rep = onRequest(this, clientVersion, data);
+                                                       if (isClosing()) {
+                                                               return;
+                                                       }
                                                } catch (Exception e) {
                                                        onError(e);
                                                }
index 3185c6f450f0cda09c4b66d7b1924d917c027417..89a7b28c6042418be95348191ad79794adb826fe 100644 (file)
@@ -71,6 +71,9 @@ abstract public class ServerString extends Server {
                                        String rep = null;
                                        try {
                                                rep = onRequest(this, clientVersion, data);
+                                               if (isClosing()) {
+                                                       return;
+                                               }
                                        } catch (Exception e) {
                                                onError(e);
                                        }