Browse Source

fixed parsing of unknown pkgs

Martin Kunz 5 years ago
parent
commit
eb68ca62cf

+ 32 - 11
src/main/java/at/acdp/urweb/sclient/SecondaryClient.java

@@ -2,6 +2,7 @@ package at.acdp.urweb.sclient;
 
 import at.acdp.urweb.CountDataInputStream;
 import at.acdp.urweb.sclient.data.*;
+import io.undertow.util.FileUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -11,8 +12,6 @@ import java.nio.charset.StandardCharsets;
 
 public class SecondaryClient {
     private static final Logger log = LoggerFactory.getLogger(SecondaryClient.class);
-
-
     private final String ip;
     private final int port;
     private volatile boolean _running = true;
@@ -29,12 +28,18 @@ public class SecondaryClient {
         this.rt = new Socket(ip, port);
         this.os = rt.getOutputStream();
         this.in = new CountDataInputStream(rt.getInputStream());
+//        byte[] data=new byte[4096];
+//        this.in.readFully(data);
+//        FileOutputStream fos = new FileOutputStream("data2.txt");
+//        fos.write(data);
+//        fos.close();
+
         VersionMessage vm = new VersionMessage();
         vm.read(in, -1);
         return vm;
     }
 
-    public int readReply(CountDataInputStream di, boolean isSub) throws IOException {
+    public int readReply(CountDataInputStream di) throws IOException {
         int beforeCount=di.getCount();
         int size = di.readInt(); //4
         int pType = di.readByte() & 0xff; //+1=5
@@ -45,6 +50,24 @@ public class SecondaryClient {
             case MessageType.ROBOT_STATE:
                 readRobotState(di, size);
                 break;
+            default:
+                log.warn("unknown ptype: "+pType+", size: "+size);
+                byte[] pack=new byte[size-5];
+                di.readFully(pack);
+        }
+        int afterCount=di.getCount();
+        int diff=afterCount-beforeCount-size;
+        if(diff!=0) {
+            log.warn("size mismatch: " +diff + "package type: "+pType);
+        }
+        return size;
+    }
+
+    public int readSub(CountDataInputStream di) throws IOException {
+        int beforeCount=di.getCount();
+        int size = di.readInt(); //4
+        int pType = di.readByte() & 0xff; //+1=5
+        switch (pType) {
             case PackageType.ROBOT_MODE_DATA:
                 var md=readRobotModeData(di, size);
                 log.info(md.toString());
@@ -150,22 +173,20 @@ public class SecondaryClient {
 
     void readRobotMessage(CountDataInputStream di, int size) throws IOException {
         long ts = di.readLong();
-        char source = (char) (di.readByte() & 0xFF);
-        char robotMessageType = (char) (di.readByte() & 0xFF);
+        byte source = di.readByte();
+        int robotMessageType = di.readUnsignedByte();
         switch (robotMessageType) {
             default:
-                log.warn("unknown msg" + (int) robotMessageType);
-                //byte[] buf=new byte[size-11];
-                byte[] buf = new byte[size - 10];
+                log.info("unknown msg: " + (int) robotMessageType);
+                byte[] buf = new byte[size - 15];
                 di.readFully(buf);
-                System.out.println(buf);
         }
     }
 
     void readRobotState(CountDataInputStream di, int size) throws IOException {
         int remaining=size-5;
         while(remaining>0)
-            remaining-=readReply(di, true);
+            remaining-=readSub(di);
     }
 
     CartesianInfo readCartesianInfo(CountDataInputStream di, int size) throws IOException {
@@ -222,6 +243,6 @@ public class SecondaryClient {
     }
 
     public void readPackage() throws IOException {
-        readReply(in, false);
+        readReply(in);
     }
 }

+ 7 - 2
src/main/java/at/acdp/urweb/sclient/data/VersionMessage.java

@@ -20,12 +20,13 @@ public class VersionMessage implements IRead {
     public String buildDate;
 
     public void read(CountDataInputStream in, int size) throws IOException {
+        int before=in.getCount();
         int msgSize = in.readInt();
-        msgType = in.readByte() & 0xff;
+        msgType = in.readUnsignedByte();
         tstamp = in.readLong();
         source = in.readByte();
         robotMsgType = in.readByte();
-        int projectNameSize = in.readByte() & 0xff;
+        int projectNameSize = in.readUnsignedByte();
         byte[] nameBytes = new byte[projectNameSize];
         in.readFully(nameBytes);
         name = new String(nameBytes);
@@ -36,6 +37,10 @@ public class VersionMessage implements IRead {
         byte[] buildDateBytes = new byte[msgSize - (16 + projectNameSize + 10)];
         in.readFully(buildDateBytes);
         buildDate = new String(buildDateBytes);
+        int diff=in.getCount()-before;
+        if(diff!=msgSize) {
+            throw new RuntimeException("Versionmessage length mismatch: "+diff);
+        }
     }
 
     @Override