gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r18224 - in gnunet-java: . src/org/gnunet/construct src/org


From: gnunet
Subject: [GNUnet-SVN] r18224 - in gnunet-java: . src/org/gnunet/construct src/org/gnunet/messages test/org/gnunet/construct
Date: Sun, 20 Nov 2011 22:26:35 +0100

Author: dold
Date: 2011-11-20 22:26:35 +0100 (Sun, 20 Nov 2011)
New Revision: 18224

Modified:
   gnunet-java/ISSUES
   gnunet-java/src/org/gnunet/construct/Construct.java
   gnunet-java/src/org/gnunet/construct/FieldLocation.java
   gnunet-java/src/org/gnunet/construct/FixedSizeArrayParser.java
   gnunet-java/src/org/gnunet/construct/ObjectParser.java
   gnunet-java/src/org/gnunet/construct/RefLocation.java
   gnunet-java/src/org/gnunet/construct/SignedIntegerParser.java
   gnunet-java/src/org/gnunet/construct/UnsignedIntegerParser.java
   gnunet-java/src/org/gnunet/messages/SimpleTestMessage.java
   gnunet-java/test/org/gnunet/construct/ConstructTest.java
Log:
fixed serialization, added tests

Modified: gnunet-java/ISSUES
===================================================================
--- gnunet-java/ISSUES  2011-11-20 19:41:29 UTC (rev 18223)
+++ gnunet-java/ISSUES  2011-11-20 21:26:35 UTC (rev 18224)
@@ -44,14 +44,12 @@
 
 
 * exception handling in statically used classes (scheduler)
- * solution: singleton?
+ * solution: singleton? => static {} - blocks?
  
  
  
  
  
- 
- 
  * advantages of the "new" parsing/unparsing package
   * parsers are decoupled from annotations
     * => parsers could be pre-compiled

Modified: gnunet-java/src/org/gnunet/construct/Construct.java
===================================================================
--- gnunet-java/src/org/gnunet/construct/Construct.java 2011-11-20 19:41:29 UTC 
(rev 18223)
+++ gnunet-java/src/org/gnunet/construct/Construct.java 2011-11-20 21:26:35 UTC 
(rev 18224)
@@ -89,14 +89,16 @@
         String charset() default "UTF-8";
     }
 
-    public <T> T parse(Parser p, byte[] data, int offset) {
+    public static <T> T parseWith(Parser p, byte[] data, int offset) {
         RefLocation rl = new RefLocation();
         Location[] locs = { rl };
         p.parse(data, offset, locs);
         return (T) rl.get();
     }
 
-    public ObjectParser makeParser(Class cls) {
+    public static ObjectParser makeParser(Class cls) {
+        // TODO: caching
+
         ObjectParser op = new ObjectParser(cls);
 
         for (Field f : cls.getFields()) {
@@ -111,6 +113,14 @@
                     op.add(new UnsignedIntegerParser(a.value()), f);
                 } else if (ann instanceof TotalSize) {
                     op.addSizeField(f);
+                } else if (ann instanceof FixedSizeArray) {
+                    throw new UnsupportedOperationException(
+                            "not yet implemented");
+                    // look at anns[i+1]
+                    // [...]
+                } else {
+                    throw new RuntimeException("unknown construct annotation: "
+                            + ann.getClass());
                 }
             }
         }
@@ -132,12 +142,10 @@
      * @throws RuntimeException
      *             (ugh)
      */
-    // how to deal with combined annotations?
-    public static <T> T parse_as(byte[] data, int offset, Class<T> c) {
-        
+    public static <T> T parse(byte[] data, int offset, Class<T> cls) {
+        ObjectParser p = makeParser(cls);
 
-        return null;
-
+        return Construct.parseWith(p, data, offset);
     }
 
     /**
@@ -145,7 +153,7 @@
      * the object are expected to be annotated with annotations from the
      * construct package.
      * 
-     * @param o
+     * @param obj
      *            object to serialize
      * @param data
      *            where to write the binary object data
@@ -153,8 +161,9 @@
      *            where to start writing data
      * @return number of bytes written to data, -1 on error
      */
-    public static int write_to(Object o, ByteBuffer buf) {
-        return -1;
+    public static int writeTo(Object obj, byte[] data, int offset) {
+        ObjectParser p = makeParser(obj.getClass());
+        return p.unparse(data, offset, RefLocation.makeLocationsWith(obj));
     }
 
     /**
@@ -164,7 +173,14 @@
      *            object to serialize
      * @return number of bytes required, -1 on error
      */
-    public static int compute_binary_size(Object o) {
-        return -1;
+    public static int estimateSize(Object obj) {
+        ObjectParser p = makeParser(obj.getClass());
+        return p.estimateSize(RefLocation.makeLocationsWith(obj));
     }
+    
+    public static byte[] unparse(Object obj) {
+        byte[] data = new byte[estimateSize(obj)];
+        writeTo(obj, data, 0);
+        return data;
+    }
 }

Modified: gnunet-java/src/org/gnunet/construct/FieldLocation.java
===================================================================
--- gnunet-java/src/org/gnunet/construct/FieldLocation.java     2011-11-20 
19:41:29 UTC (rev 18223)
+++ gnunet-java/src/org/gnunet/construct/FieldLocation.java     2011-11-20 
21:26:35 UTC (rev 18224)
@@ -32,8 +32,10 @@
         try {
             return f.get(o);
         } catch (IllegalArgumentException e) {
+            // TODO Auto-generated catch block
             throw new RuntimeException();
         } catch (IllegalAccessException e) {
+            // TODO Auto-generated catch block
             throw new RuntimeException();
         }
     }

Modified: gnunet-java/src/org/gnunet/construct/FixedSizeArrayParser.java
===================================================================
--- gnunet-java/src/org/gnunet/construct/FixedSizeArrayParser.java      
2011-11-20 19:41:29 UTC (rev 18223)
+++ gnunet-java/src/org/gnunet/construct/FixedSizeArrayParser.java      
2011-11-20 21:26:35 UTC (rev 18224)
@@ -1,5 +1,7 @@
 package org.gnunet.construct;
 
+import java.lang.reflect.Array;
+
 public class FixedSizeArrayParser implements Parser {
     
     private Parser p;
@@ -12,22 +14,53 @@
 
     @Override
     public int parse(byte[] src_data, int offset, Location[] dst) {
-        // TODO Auto-generated method stub
-        return 0;
+        assert dst.length == 1;
+        Location loc = dst[0];
+        
+        assert loc.getType().isArray();
+        
+        Object array = Array.newInstance(loc.getType(), size);
+        
+        for (int i = 0; i < size; ++i) {
+            RefLocation rl = new RefLocation();
+            Location[] locs = {rl};
+            offset += p.parse(src_data, offset, locs);
+            Array.set(array, i, rl.get());
+        }
+        
+        
+        return estimateSize(dst);
     }
 
     @Override
     public int unparse(byte[] dst_data, int offset, Location[] src) {
-        // TODO Auto-generated method stub
-        return 0;
+        assert src.length == 1;
+        
+        Object arr = src[0].get();
+        int parsed = 0;
+        
+        for (int i = 0; i < size; ++i) {
+            RefLocation rl = new RefLocation();
+            rl.put(Array.get(arr, i));
+            Location[] locs = {rl};
+            parsed += p.unparse(dst_data, offset, locs);
+            offset += p.parse(dst_data, offset, locs);
+        }
+        
+        return parsed;
     }
 
     @Override
     public int estimateSize(Location[] args) {
-        Location[] a = {};
-        return p.estimateSize(a) * size;
+        assert args.length == 1;
+        int accum = 0;
+        Object arr = args[0].get();
+        for (int i = 0; i < size; ++i) {
+            RefLocation rl = new RefLocation();
+            rl.put(Array.get(arr, i));
+            Location[] locs = {rl};
+            accum += p.estimateSize(locs);
+        }
+        return accum;
     }
-    
-
-
 }

Modified: gnunet-java/src/org/gnunet/construct/ObjectParser.java
===================================================================
--- gnunet-java/src/org/gnunet/construct/ObjectParser.java      2011-11-20 
19:41:29 UTC (rev 18223)
+++ gnunet-java/src/org/gnunet/construct/ObjectParser.java      2011-11-20 
21:26:35 UTC (rev 18224)
@@ -75,7 +75,7 @@
         for (ParserRecord pr : this.parser_records) {
             // create locations from record
             Location[] locs = getLocs(pr.args, obj);
-            int x = pr.p.parse(data, offset, locs);
+            int x = pr.p.parse(data, offset + parsed, locs);
             assert x >= 0;
             parsed += x;
         }
@@ -102,6 +102,7 @@
         int parsed = 0;
 
         for (ParserRecord pr : this.parser_records) {
+            
             parsed += pr.p
                     .unparse(data, offset + parsed, getLocs(pr.args, obj));
         }

Modified: gnunet-java/src/org/gnunet/construct/RefLocation.java
===================================================================
--- gnunet-java/src/org/gnunet/construct/RefLocation.java       2011-11-20 
19:41:29 UTC (rev 18223)
+++ gnunet-java/src/org/gnunet/construct/RefLocation.java       2011-11-20 
21:26:35 UTC (rev 18224)
@@ -26,5 +26,12 @@
     public Class getType() {
         return cls;
     }
+    
+    public static Location[] makeLocationsWith(Object obj) {
+        RefLocation rl = new RefLocation(obj.getClass());
+        rl.put(obj);
+        return new Location[]{rl};
+    }
 
+
 }

Modified: gnunet-java/src/org/gnunet/construct/SignedIntegerParser.java
===================================================================
--- gnunet-java/src/org/gnunet/construct/SignedIntegerParser.java       
2011-11-20 19:41:29 UTC (rev 18223)
+++ gnunet-java/src/org/gnunet/construct/SignedIntegerParser.java       
2011-11-20 21:26:35 UTC (rev 18224)
@@ -39,10 +39,10 @@
             } else if (loc.getType().equals(Long.TYPE)) {
                 loc.put(num.longValue());
                 bit_size = 64;
-            } else if (loc.getType().equals(Short.class)) {
+            } else if (loc.getType().equals(Short.TYPE)) {
                 loc.put(num.shortValue());
                 bit_size = 16;
-            } else if (loc.getType().equals(Byte.class)) {
+            } else if (loc.getType().equals(Byte.TYPE)) {
                 loc.put(num.byteValue());
                 bit_size = 8;
             } else {
@@ -60,9 +60,27 @@
     @Override
     public int unparse(byte[] dst_data, int offset, Location[] src) {
         assert src.length == 1;
-        Location loc = src[0];
+        Object obj = src[0].get();
+        BigInteger num;
         
-        return 0;
+        if (obj instanceof BigInteger) {
+            num = (BigInteger) obj;
+        } else if (obj instanceof Number) {
+            num = BigInteger.valueOf(((Number) obj).longValue());
+        } else {
+            throw new RuntimeException("invalid number type");
+        }
+        
+        // +1 for the sign
+        if ((num.bitLength() + 1) > byte_size * 8) {
+            throw new RuntimeException("value to large for serialization");
+        }
+        
+        byte[] buf = num.toByteArray();
+        
+        System.arraycopy(buf, 0, dst_data, offset, byte_size);
+        
+        return byte_size;
     }
 
     @Override

Modified: gnunet-java/src/org/gnunet/construct/UnsignedIntegerParser.java
===================================================================
--- gnunet-java/src/org/gnunet/construct/UnsignedIntegerParser.java     
2011-11-20 19:41:29 UTC (rev 18223)
+++ gnunet-java/src/org/gnunet/construct/UnsignedIntegerParser.java     
2011-11-20 21:26:35 UTC (rev 18224)
@@ -1,6 +1,7 @@
 package org.gnunet.construct;
 
 import java.math.BigInteger;
+import java.util.Arrays;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -61,9 +62,39 @@
 
     @Override
     public int unparse(byte[] dst_data, int offset, Location[] src) {
-        // TODO Auto-generated method stub
-        return 0;
+        assert src.length == 1;
+        Object obj = src[0].get();
+        BigInteger num;
+        
+        if (obj instanceof BigInteger) {
+            num = (BigInteger) obj;
+        } else if (obj instanceof Number) {
+            num = BigInteger.valueOf(((Number) obj).longValue());
+        } else {
+            throw new RuntimeException("invalid number type");
+        }
+        
+        if (num.signum() == -1) {
+            throw new RuntimeException("negative value not allowed for 
unsigned integer");
+        }
+        
+        if (num.bitLength() > byte_size*8) {
+            throw new RuntimeException("value to large for serialization");
+        }
+        
+        byte[] buf = num.toByteArray();
+        
+        
+        int dst_pos = offset + (byte_size - buf.length);
+        
+        System.arraycopy(buf, 0, dst_data, dst_pos, buf.length);
+        
+        Arrays.fill(dst_data, offset, dst_pos, (byte) 0);
+        
+        return byte_size;
     }
+    
+    
 
 
     @Override

Modified: gnunet-java/src/org/gnunet/messages/SimpleTestMessage.java
===================================================================
--- gnunet-java/src/org/gnunet/messages/SimpleTestMessage.java  2011-11-20 
19:41:29 UTC (rev 18223)
+++ gnunet-java/src/org/gnunet/messages/SimpleTestMessage.java  2011-11-20 
21:26:35 UTC (rev 18224)
@@ -1,5 +1,6 @@
 package org.gnunet.messages;
 
+import org.gnunet.construct.Construct.SignedInteger;
 import org.gnunet.construct.Construct.UnsignedInteger;
 import org.gnunet.construct.ObjectParser;
 import org.gnunet.construct.UnsignedIntegerParser;
@@ -7,23 +8,8 @@
 public class SimpleTestMessage {
     
     @UnsignedInteger(2)
-    public short v;
+    public short v1;
     
-    public static ObjectParser getParser() {
-        ObjectParser op = new ObjectParser(SimpleTestMessage.class);
-        
-        try {
-            op.add(new UnsignedIntegerParser(2), 
SimpleTestMessage.class.getField("v"));
-        } catch (SecurityException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        } catch (NoSuchFieldException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        }
-        
-        
-        return op;
-    }
-
+    @SignedInteger(2)
+    public short v2;
 }

Modified: gnunet-java/test/org/gnunet/construct/ConstructTest.java
===================================================================
--- gnunet-java/test/org/gnunet/construct/ConstructTest.java    2011-11-20 
19:41:29 UTC (rev 18223)
+++ gnunet-java/test/org/gnunet/construct/ConstructTest.java    2011-11-20 
21:26:35 UTC (rev 18224)
@@ -2,33 +2,40 @@
 
 import org.gnunet.messages.MessageHeader;
 import org.gnunet.messages.SimpleTestMessage;
+import org.junit.Assert;
 import org.junit.Test;
 
 public class ConstructTest {
 
-       @Test
-       public void test_SimpleTestMessage_1() {
-           
-           ObjectParser op = SimpleTestMessage.getParser();
-           
-           /*
-           SimpleTestMessage stm = new SimpleTestMessage();
-           stm.v = 0xAB;
-           op.unparse(data, offset, dest)
-           
-           */
-           
-           RefLocation loc = new RefLocation();
-           Location[] locs = {loc};
-           
-           byte[] a = {0x01, 0x01};
-           
-           op.parse(a, 0, locs);
-           
-           SimpleTestMessage m = (SimpleTestMessage) loc.get();
-           
-           System.out.println(""+m.v);  
-           
-       }
+    @Test
+    public void test_SimpleTestMessage_1() {
+        ObjectParser op = Construct.makeParser(SimpleTestMessage.class);
+        byte[] a = { 0x02, 0x01, 0x00, 0x1c };
+        SimpleTestMessage m = Construct.parseWith(op, a, 0);
 
+        Assert.assertTrue(m.v1 == ((0x02 << 8) | 0x01));
+        Assert.assertTrue(m.v2 == 0x1c);
+
+        Assert.assertEquals(4, Construct.estimateSize(m));
+    }
+
+    @Test
+    public void test_SimpleTestMessage_2() {
+        SimpleTestMessage m1 = new SimpleTestMessage();
+        
+        m1.v1 = 42;
+        m1.v2 = 420;
+
+        byte[] data = Construct.unparse(m1);
+        
+        SimpleTestMessage m2 = Construct.parse(data, 0, 
SimpleTestMessage.class);
+        
+        Assert.assertEquals(m1.v1, m2.v1);
+        
+        Assert.assertEquals(m1.v2, m2.v2);
+        
+        
+
+    }
+
 }




reply via email to

[Prev in Thread] Current Thread [Next in Thread]