working
[nikiroo-utils.git] / src / be / nikiroo / utils / serial / server / ConnectActionServer.java
index 60f8db85db4f6f278a9b1f50ef1d41de92b0540e..d0ddb92acd253cb5befda49760c5803e379e1688 100644 (file)
@@ -2,8 +2,6 @@ package be.nikiroo.utils.serial.server;
 
 import java.net.Socket;
 
-import be.nikiroo.utils.Version;
-
 /**
  * Base class used for the server basic handling.
  * <p>
@@ -13,6 +11,8 @@ import be.nikiroo.utils.Version;
  * @author niki
  */
 abstract class ConnectActionServer {
+       private boolean closing;
+
        /**
         * The underlying {@link ConnectAction}.
         * <p>
@@ -20,41 +20,26 @@ abstract class ConnectActionServer {
         */
        protected ConnectAction action;
 
-       /**
-        * Create a new {@link ConnectActionServer} with the current application
-        * version (see {@link Version#getCurrentVersion()}) as the server version.
-        * 
-        * @param s
-        *            the socket to bind to
-        */
-       public ConnectActionServer(Socket s) {
-               this(s, Version.getCurrentVersion());
-       }
-
        /**
         * Create a new {@link ConnectActionServer}.
         * 
         * @param s
         *            the socket to bind to
-        * @param version
-        *            the server version
+        * @param key
+        *            an optional key to encrypt all the communications (if NULL,
+        *            everything will be sent in clear text)
         */
-       public ConnectActionServer(Socket s, Version version) {
-               action = new ConnectAction(s, true, version) {
+       public ConnectActionServer(Socket s, String key) {
+               action = new ConnectAction(s, true, key) {
                        @Override
-                       protected void action(Version clientVersion) throws Exception {
-                               ConnectActionServer.this.action(clientVersion);
+                       protected void action() throws Exception {
+                               ConnectActionServer.this.action();
                        }
 
                        @Override
                        protected void onError(Exception e) {
                                ConnectActionServer.this.onError(e);
                        }
-
-                       @Override
-                       protected Version negotiateVersion(Version clientVersion) {
-                               return ConnectActionServer.this.negotiateVersion(clientVersion);
-                       }
                };
        }
 
@@ -77,6 +62,34 @@ 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.
+        * 
+        * @return TRUE when it is
+        */
+       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.
         * 
@@ -92,20 +105,17 @@ abstract class ConnectActionServer {
         * @return the amount of bytes sent
         */
        public long getBytesSent() {
-               return action.getBytesSent();
+               return action.getBytesWritten();
        }
 
        /**
         * Method that will be called when an action is performed on the server.
         * 
-        * @param clientVersion
-        *            the client version
-        * 
         * @throws Exception
         *             in case of I/O error
         */
        @SuppressWarnings("unused")
-       public void action(Version clientVersion) throws Exception {
+       public void action() throws Exception {
        }
 
        /**
@@ -118,19 +128,4 @@ abstract class ConnectActionServer {
         */
        protected void onError(@SuppressWarnings("unused") Exception e) {
        }
-
-       /**
-        * Method called when we negotiate the version with the client.
-        * <p>
-        * Will return the actual server version by default.
-        * 
-        * @param clientVersion
-        *            the client version
-        * 
-        * @return the version to send to the client
-        */
-       protected Version negotiateVersion(
-                       @SuppressWarnings("unused") Version clientVersion) {
-               return action.getVersion();
-       }
 }
\ No newline at end of file