Commits

Michael Caron  committed c34fab4

fixed bug in reading from network

  • Participants
  • Parent commits 4055c50
  • Tags Version 0.7.4

Comments (0)

Files changed (2)

     <groupId>com.intelix</groupId>
     <artifactId>NetLib</artifactId>
     <packaging>jar</packaging>
-    <version>0.7.3</version>
+    <version>0.7.4</version>
     <name>NetLib</name>
 
     <description>Networking library for Intelix HW</description>

File src/main/java/com/intelix/net/Connection.java

 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.net.SocketTimeoutException;
 import java.util.Arrays;
 import java.util.List;
 import java.util.logging.Level;
         byte[] backupBuff = null;
 
         Logger.getLogger(getClass().getName()).fine(">>> NET(ReadOne): about to start reading");
-        while (((c == null) && (bytesRead > 0)) || (!started)) {
-            Logger.getLogger(getClass().getName()).fine(">>> NET(ReadOne): Cycling outer loop");
-            
-            bytesRead = in.read(this.bufferIn);
-            
-            Logger.getLogger(getClass().getName()).fine(">>> NET(ReadOne): read " + bytesRead + " bytes.");
-            if (started = (bytesRead > 0))
-            {
-                Logger.getLogger(getClass().getName()).fine(">>> NET(ReadOne): started processing bytes");
-                int position = 0;
-                while (position < bytesRead) {
-                    Logger.getLogger(getClass().getName()).fine(">>> NET(ReadOne): Cycling inner loop");
-                    Logger.getLogger(getClass().getName()).fine(">>> NET(ReadOne): position:" + position);
-                    try {
-                        byte[] temp = null;
-                        if (backupBuff != null) {
-                            Logger.getLogger(getClass().getName()).fine(">>> NET(ReadOne): copying backup buffer");
-                            temp = Arrays.copyOf(backupBuff, backupBuff.length + this.bufferIn.length);
-                            System.arraycopy(this.bufferIn, 0, temp, backupBuff.length, this.bufferIn.length);
-                        } else {
-                            temp = this.bufferIn;
+        int MAX_TRIES = 3;
+        int tries = 0;
+        while (((c == null) && (bytesRead > 0)) || ( (!started) && tries < MAX_TRIES) ){
+            try {
+                Logger.getLogger(getClass().getName()).fine(">>> NET(ReadOne): Cycling outer loop");
+
+                bytesRead = in.read(this.bufferIn);
+
+                Logger.getLogger(getClass().getName()).fine(">>> NET(ReadOne): read " + bytesRead + " bytes.");
+                if (started = (bytesRead > 0))
+                {
+                    Logger.getLogger(getClass().getName()).fine(">>> NET(ReadOne): started processing bytes");
+                    int position = 0;
+                    while (position < bytesRead) {
+                        Logger.getLogger(getClass().getName()).fine(">>> NET(ReadOne): Cycling inner loop");
+                        Logger.getLogger(getClass().getName()).fine(">>> NET(ReadOne): position:" + position);
+                        try {
+                            byte[] temp = null;
+                            if (backupBuff != null) {
+                                Logger.getLogger(getClass().getName()).fine(">>> NET(ReadOne): copying backup buffer");
+                                temp = Arrays.copyOf(backupBuff, backupBuff.length + this.bufferIn.length);
+                                System.arraycopy(this.bufferIn, 0, temp, backupBuff.length, this.bufferIn.length);
+                            } else {
+                                temp = this.bufferIn;
+                            }
+                            Logger.getLogger(getClass().getName()).fine(">>> NET(ReadOne): backup buffer size:" + temp.length);
+                            backupBuff = null;
+
+                            c = new Command();
+                            position = c.decode(temp, position);
+                            Logger.getLogger(getClass().getName()).fine(">>> NET(ReadOne): position after decode:" + position);
+                        } catch (InsufficientDataException ex) {
+                            Logger.getLogger(getClass().getName()).fine(">>> NET(ReadOne): Have to cycle again, didn't get enough data.");
+                            backupBuff = new byte[bytesRead - position];
+                            System.arraycopy(this.bufferIn, position, backupBuff, 0, backupBuff.length);
+                            c = null;
                         }
-                        Logger.getLogger(getClass().getName()).fine(">>> NET(ReadOne): backup buffer size:" + temp.length);
-                        backupBuff = null;
-
-                        c = new Command();
-                        position = c.decode(temp, position);
-                        Logger.getLogger(getClass().getName()).fine(">>> NET(ReadOne): position after decode:" + position);
-                    } catch (InsufficientDataException ex) {
-                        Logger.getLogger(getClass().getName()).fine(">>> NET(ReadOne): Have to cycle again, didn't get enough data.");
-                        backupBuff = new byte[bytesRead - position];
-                        System.arraycopy(this.bufferIn, position, backupBuff, 0, backupBuff.length);
-                        c = null;
                     }
                 }
             }
+            catch (SocketTimeoutException ex)
+            {
+                Logger.getLogger(getClass().getName()).info("Didn't read anything, trying again. Tries=" + tries);
+                tries++;
+            }
+        }
+        if (tries >= MAX_TRIES)
+        {
+            throw new IOException("Reached max tries... time for the caller to decide what to do.");
         }
 
         return c;