From 0802e98432d1fde4650f02bc657b0862d269a8ed Mon Sep 17 00:00:00 2001
From: Sven Gestegard Robertz <sven.robertz@cs.lth.se>
Date: Mon, 25 May 2015 10:50:36 +0200
Subject: [PATCH] Csharp adapted to protocol with intentions. (Ignoring them)

---
 .../lth/control/labcomm2014/DecoderChannel.cs | 48 ++++++++++++++-----
 .../lth/control/labcomm2014/EncoderChannel.cs | 10 +++-
 2 files changed, 44 insertions(+), 14 deletions(-)

diff --git a/lib/csharp/se/lth/control/labcomm2014/DecoderChannel.cs b/lib/csharp/se/lth/control/labcomm2014/DecoderChannel.cs
index a56a604..6d11ba9 100644
--- a/lib/csharp/se/lth/control/labcomm2014/DecoderChannel.cs
+++ b/lib/csharp/se/lth/control/labcomm2014/DecoderChannel.cs
@@ -31,7 +31,7 @@ namespace se.lth.control.labcomm2014 {
         } break;
         case Constant.SAMPLE_DEF: {
           int index = decodePacked32();
-          String name = decodeString();
+          String name = decodeIntentions();
           int signature_length = decodePacked32();
           byte[] signature = new byte[signature_length];
           ReadBytes(signature, signature_length);
@@ -39,17 +39,17 @@ namespace se.lth.control.labcomm2014 {
         } break;
         case Constant.SAMPLE_REF: {
           int index = decodePacked32();
-          String name = decodeString();
+          String name = decodeIntentions();
           int signature_length = decodePacked32();
           byte[] signature = new byte[signature_length];
           ReadBytes(signature, signature_length);
 	  ref_registry.add(index, name, signature);
         } break;
-        case Constant.TYPE_DEF: 
+        case Constant.TYPE_DEF:
         case Constant.TYPE_BINDING: {
             for(int i=0; i<length;i++){
                 decodeByte();
-            }                
+            }
         } break;
         default: {
           DecoderRegistry.Entry e = def_registry.get(tag);
@@ -77,7 +77,7 @@ namespace se.lth.control.labcomm2014 {
       }
     }
 
-    public void register(SampleDispatcher dispatcher, 
+    public void register(SampleDispatcher dispatcher,
 			 SampleHandler handler) {
       def_registry.add(dispatcher, handler);
     }
@@ -92,7 +92,7 @@ namespace se.lth.control.labcomm2014 {
 	int count = stream.Read(result, offset, length - offset);
 	if (count <= 0)
 	  throw new EndOfStreamException(
-	    String.Format("End of stream reached with {0} bytes left to read", 
+	    String.Format("End of stream reached with {0} bytes left to read",
 			  length - offset));
 	offset += count;
       }
@@ -115,7 +115,7 @@ namespace se.lth.control.labcomm2014 {
     public byte decodeByte() {
       return (byte)ReadInt(1);
     }
-  
+
     public short decodeShort() {
       return (short)ReadInt(2);
     }
@@ -123,11 +123,11 @@ namespace se.lth.control.labcomm2014 {
     public int decodeInt() {
       return (int)ReadInt(4);
     }
-    
+
     public long decodeLong() {
       return (long)ReadInt(8);
     }
-    
+
 
     [StructLayout(LayoutKind.Explicit)]
     private struct Int32SingleUnion {
@@ -141,7 +141,7 @@ namespace se.lth.control.labcomm2014 {
       u.AsInt = (int)ReadInt(4);
       return u.AsFloat;
     }
-    
+
     public double decodeDouble() {
       return BitConverter.Int64BitsToDouble(ReadInt(8));
     }
@@ -155,7 +155,7 @@ namespace se.lth.control.labcomm2014 {
 
     public int decodePacked32() {
       Int64 res = 0;
-      bool cont = true; 
+      bool cont = true;
 
       do {
         Int64 c = decodeByte();
@@ -166,6 +166,30 @@ namespace se.lth.control.labcomm2014 {
       return (int) (res & 0xffffffff);
     }
 
+  private byte[] decodeBytes() {
+    int len = decodePacked32();
+    byte[] result = new byte[len];
+
+    for(int i=0; i<len; i++) {
+      result[i] = decodeByte();
+    }
+    return result;
+  }
+
+  private String decodeIntentions() {
+    int numIntentions = decodePacked32();
+    string name = "";
+    for(int i = 0; i<numIntentions; i++) {
+      byte[] key = decodeBytes();
+      byte[] val = decodeBytes();
+
+      if(key.Length == 0) {
+        name = Encoding.UTF8.GetString(val, 0, val.Length);
+      }
+    }
+    return name;
+  }
+
     public Type decodeSampleRef() {
       int index = (int)ReadInt(4);
       try {
@@ -178,4 +202,4 @@ namespace se.lth.control.labcomm2014 {
 
   }
 
-} 
+}
diff --git a/lib/csharp/se/lth/control/labcomm2014/EncoderChannel.cs b/lib/csharp/se/lth/control/labcomm2014/EncoderChannel.cs
index 20536e4..b8f98e1 100644
--- a/lib/csharp/se/lth/control/labcomm2014/EncoderChannel.cs
+++ b/lib/csharp/se/lth/control/labcomm2014/EncoderChannel.cs
@@ -26,7 +26,7 @@ namespace se.lth.control.labcomm2014 {
       int index = def_registry.add(dispatcher);
       begin(Constant.SAMPLE_DEF);
       encodePacked32(index);
-      encodeString(dispatcher.getName());
+      encodeIntentions(dispatcher.getName());
       byte[] signature = dispatcher.getSignature();
       encodePacked32(signature.Length);
       for (int i = 0 ; i < signature.Length ; i++) {
@@ -39,7 +39,7 @@ namespace se.lth.control.labcomm2014 {
       int index = ref_registry.add(dispatcher);
       begin(Constant.SAMPLE_REF);
       encodePacked32(index);
-      encodeString(dispatcher.getName());
+      encodeIntentions(dispatcher.getName());
       byte[] signature = dispatcher.getSignature();
       encodePacked32(signature.Length);
       for (int i = 0 ; i < signature.Length ; i++) {
@@ -132,6 +132,12 @@ namespace se.lth.control.labcomm2014 {
       WritePacked32(bytes, value);
     }
 
+    private void encodeIntentions(String name) {
+      encodePacked32(1); // one intention field
+      encodePacked32(0); // empty key: name
+      encodeString(name);
+    }
+
     public void encodeSampleRef(Type value) {
       int index = 0;
       try {
-- 
GitLab