Improve TraceHandler, fix server system + tests:
[nikiroo-utils.git] / src / be / nikiroo / utils / serial / ConnectAction.java
index ec49a1dd7f3c235c32614c0c04bee9db902e1166..2a25c5b1c62e9f6a15778e200b04603637fa747a 100644 (file)
@@ -35,12 +35,14 @@ abstract class ConnectAction {
 
        public void connectAsync() {
                new Thread(new Runnable() {
+                       @Override
                        public void run() {
                                connect();
                        }
                }).start();
        }
 
+       // connect, do the action (sync)
        public void connect() {
                try {
                        in = new BufferedReader(new InputStreamReader(s.getInputStream(),
@@ -80,7 +82,11 @@ abstract class ConnectAction {
                        NoSuchMethodException, ClassNotFoundException {
                synchronized (lock) {
                        String rep = sendString(new Exporter().append(data).toString(true));
-                       return new Importer().read(rep).getValue();
+                       if (rep != null) {
+                               return new Importer().read(rep).getValue();
+                       }
+
+                       return null;
                }
        }
 
@@ -94,12 +100,23 @@ abstract class ConnectAction {
                return new Importer().read(str).getValue();
        }
 
-       protected void onClientVersionReceived(Version clientVersion) {
-
+       /**
+        * Handler called when the client {@link Version} is received.
+        * 
+        * @param clientVersion
+        *            the client {@link Version}
+        */
+       protected void onClientVersionReceived(
+                       @SuppressWarnings("unused") Version clientVersion) {
        }
 
-       protected void onError(Exception e) {
-
+       /**
+        * Handler called when an unexpected error occurs in the code.
+        * 
+        * @param e
+        *            the exception that occurred
+        */
+       protected void onError(@SuppressWarnings("unused") Exception e) {
        }
 
        // \n included in line, but not in rep (also, server never get anything)
@@ -111,10 +128,10 @@ abstract class ConnectAction {
                        if (server) {
                                out.flush();
                                return null;
-                       } else {
-                               contentToSend = true;
-                               return flushString();
                        }
+
+                       contentToSend = true;
+                       return flushString();
                }
        }
 
@@ -139,9 +156,9 @@ abstract class ConnectAction {
                                }
 
                                return line;
-                       } else {
-                               return null;
                        }
+
+                       return null;
                }
        }
 }
\ No newline at end of file