Add 'src/be/nikiroo/utils/' from commit '46add0670fdee4bd936a13fe2448c5e20a7ffd0a'
[fanfix.git] / src / be / nikiroo / utils / serial / server / ConnectActionServer.java
index 60f8db85db4f6f278a9b1f50ef1d41de92b0540e..350d3fe40a1320b68f555458a43c3ceec11e8c25 100644 (file)
@@ -13,6 +13,8 @@ import be.nikiroo.utils.Version;
  * @author niki
  */
 abstract class ConnectActionServer {
+       private boolean closing;
+
        /**
         * The underlying {@link ConnectAction}.
         * <p>
@@ -21,14 +23,16 @@ abstract class ConnectActionServer {
        protected ConnectAction action;
 
        /**
-        * Create a new {@link ConnectActionServer} with the current application
-        * version (see {@link Version#getCurrentVersion()}) as the server version.
+        * Create a new {@link ConnectActionServer}, using the current version.
         * 
         * @param s
         *            the socket to bind to
+        * @param key
+        *            an optional key to encrypt all the communications (if NULL,
+        *            everything will be sent in clear text)
         */
-       public ConnectActionServer(Socket s) {
-               this(s, Version.getCurrentVersion());
+       public ConnectActionServer(Socket s, String key) {
+               this(s, key, Version.getCurrentVersion());
        }
 
        /**
@@ -36,11 +40,14 @@ abstract class 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)
+        * @param serverVersion
+        *            the version of this server,that will be sent to the client
         */
-       public ConnectActionServer(Socket s, Version version) {
-               action = new ConnectAction(s, true, version) {
+       public ConnectActionServer(Socket s, String key, Version serverVersion) {
+               action = new ConnectAction(s, true, key, serverVersion) {
                        @Override
                        protected void action(Version clientVersion) throws Exception {
                                ConnectActionServer.this.action(clientVersion);
@@ -77,6 +84,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,14 +127,14 @@ 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
+        *            the version of the client connected to this server
         * 
         * @throws Exception
         *             in case of I/O error