gnunet-svn
[Top][All Lists]
Advanced

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

[GNUnet-SVN] r30389 - in gnunet-java/src: main/java/org/gnunet/arm main/


From: gnunet
Subject: [GNUnet-SVN] r30389 - in gnunet-java/src: main/java/org/gnunet/arm main/java/org/gnunet/construct main/java/org/gnunet/construct/parsers main/java/org/gnunet/dht main/java/org/gnunet/statistics test/java/org/gnunet/construct
Date: Mon, 21 Oct 2013 22:33:45 +0200

Author: dold
Date: 2013-10-21 22:33:44 +0200 (Mon, 21 Oct 2013)
New Revision: 30389

Added:
   gnunet-java/src/main/java/org/gnunet/construct/StringTerminationType.java
   gnunet-java/src/main/java/org/gnunet/construct/VariableSizeString.java
   
gnunet-java/src/main/java/org/gnunet/construct/parsers/VariableSizeStringParser.java
   gnunet-java/src/test/java/org/gnunet/construct/VariableStringTest.java
Modified:
   gnunet-java/src/main/java/org/gnunet/arm/ResultHandler.java
   gnunet-java/src/main/java/org/gnunet/construct/Construct.java
   gnunet-java/src/main/java/org/gnunet/dht/ClientGetMessage.java
   gnunet-java/src/main/java/org/gnunet/dht/ClientResultMessage.java
   gnunet-java/src/main/java/org/gnunet/statistics/StatisticsWatcher.java
Log:
- removed some auto-generated IDE comments
- implemented Construct-parsing of strings with size field


Modified: gnunet-java/src/main/java/org/gnunet/arm/ResultHandler.java
===================================================================
--- gnunet-java/src/main/java/org/gnunet/arm/ResultHandler.java 2013-10-21 
19:13:51 UTC (rev 30388)
+++ gnunet-java/src/main/java/org/gnunet/arm/ResultHandler.java 2013-10-21 
20:33:44 UTC (rev 30389)
@@ -20,12 +20,5 @@
 
 package org.gnunet.arm;
 
-/**
- * Created with IntelliJ IDEA.
- * User: dold
- * Date: 10/7/13
- * Time: 4:00 PM
- * To change this template use File | Settings | File Templates.
- */
 public interface ResultHandler {
 }

Modified: gnunet-java/src/main/java/org/gnunet/construct/Construct.java
===================================================================
--- gnunet-java/src/main/java/org/gnunet/construct/Construct.java       
2013-10-21 19:13:51 UTC (rev 30388)
+++ gnunet-java/src/main/java/org/gnunet/construct/Construct.java       
2013-10-21 20:33:44 UTC (rev 30389)
@@ -20,6 +20,7 @@
 
 package org.gnunet.construct;
 
+import com.google.common.base.Preconditions;
 import org.gnunet.construct.parsers.*;
 import org.grothoff.Runabout;
 import org.slf4j.Logger;
@@ -139,7 +140,7 @@
     private static Parser getParser(Class<? extends Message> c,
                                     final ParserGenerator pg) {
 
-
+        Preconditions.checkNotNull(c);
         SequenceParser parser = new SequenceParser();
 
         if (!Modifier.isPublic(c.getModifiers())) {
@@ -354,7 +355,24 @@
             }
         }
 
+        public void visit(VariableSizeString vss) {
+            try {
+                parser = new VariableSizeStringParser(vss.terminationType(), 
c.getField(vss
+                        .lengthField()), field);
 
+            } catch (SecurityException e) {
+                throw new AssertionError(
+                        String.format(
+                                "VariableSizeString: length field '%s' not 
declared public",
+                                vss.lengthField()));
+            } catch (NoSuchFieldException e) {
+                throw new AssertionError(String.format(
+                        "VariableSizeString: length field '%s' does not exist 
in class %s",
+                        vss.lengthField(), c));
+            }
+        }
+
+
         public void visit(VariableSizeIntegerArray a) {
             try {
                 parser = new 
VariableSizeIntegerArrayParser(c.getField(a.lengthField()), field, a.signed(), 
a.bitSize() / 8);

Added: gnunet-java/src/main/java/org/gnunet/construct/StringTerminationType.java
===================================================================
--- gnunet-java/src/main/java/org/gnunet/construct/StringTerminationType.java   
                        (rev 0)
+++ gnunet-java/src/main/java/org/gnunet/construct/StringTerminationType.java   
2013-10-21 20:33:44 UTC (rev 30389)
@@ -0,0 +1,28 @@
+/*
+ This file is part of GNUnet.
+  (C) 2012, 2013 Christian Grothoff (and other contributing authors)
+
+  GNUnet is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published
+  by the Free Software Foundation; either version 3, or (at your
+  option) any later version.
+
+  GNUnet is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with GNUnet; see the file COPYING.  If not, write to the
+  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+  Boston, MA 02111-1307, USA.
+ */
+
+package org.gnunet.construct;
+
+
+public enum StringTerminationType {
+    NONE,
+    ZERO_INCLUDED,
+    ZERO_EXCLUDED
+}

Copied: gnunet-java/src/main/java/org/gnunet/construct/VariableSizeString.java 
(from rev 30348, 
gnunet-java/src/main/java/org/gnunet/construct/VariableSizeArray.java)
===================================================================
--- gnunet-java/src/main/java/org/gnunet/construct/VariableSizeString.java      
                        (rev 0)
+++ gnunet-java/src/main/java/org/gnunet/construct/VariableSizeString.java      
2013-10-21 20:33:44 UTC (rev 30389)
@@ -0,0 +1,60 @@
+/*
+ This file is part of GNUnet.
+  (C) 2012, 2013 Christian Grothoff (and other contributing authors)
+
+  GNUnet is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published
+  by the Free Software Foundation; either version 3, or (at your
+  option) any later version.
+
+  GNUnet is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with GNUnet; see the file COPYING.  If not, write to the
+  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+  Boston, MA 02111-1307, USA.
+ */
+
+/*
+ This file is part of GNUnet.
+ (C) 2011, 2012 Christian Grothoff (and other contributing authors)
+
+ GNUnet is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published
+ by the Free Software Foundation; either version 3, or (at your
+ option) any later version.
+
+ GNUnet is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNUnet; see the file COPYING.  If not, write to the
+ Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA.
+ */
+
+package org.gnunet.construct;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * An array of messages, where the length of the array is specified in a
+ * field of the containing message.
+ * 
+ * @author Florian Dold
+ * 
+ */
address@hidden(RetentionPolicy.RUNTIME)
address@hidden(ElementType.FIELD)
+public @interface VariableSizeString {
+    String lengthField();
+    StringTerminationType terminationType();
+}

Copied: 
gnunet-java/src/main/java/org/gnunet/construct/parsers/VariableSizeStringParser.java
 (from rev 30348, 
gnunet-java/src/main/java/org/gnunet/construct/parsers/VariableSizeArrayParser.java)
===================================================================
--- 
gnunet-java/src/main/java/org/gnunet/construct/parsers/VariableSizeStringParser.java
                                (rev 0)
+++ 
gnunet-java/src/main/java/org/gnunet/construct/parsers/VariableSizeStringParser.java
        2013-10-21 20:33:44 UTC (rev 30389)
@@ -0,0 +1,169 @@
+/*
+ This file is part of GNUnet.
+  (C) 2012, 2013 Christian Grothoff (and other contributing authors)
+
+  GNUnet is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published
+  by the Free Software Foundation; either version 3, or (at your
+  option) any later version.
+
+  GNUnet is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with GNUnet; see the file COPYING.  If not, write to the
+  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+  Boston, MA 02111-1307, USA.
+ */
+
+/*
+ This file is part of GNUnet.
+ (C) 2011, 2012 Christian Grothoff (and other contributing authors)
+
+ GNUnet is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published
+ by the Free Software Foundation; either version 3, or (at your
+ option) any later version.
+
+ GNUnet is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNUnet; see the file COPYING.  If not, write to the
+ Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA.
+ */
+
+package org.gnunet.construct.parsers;
+
+import org.gnunet.construct.Message;
+import org.gnunet.construct.ReflectUtil;
+import org.gnunet.construct.StringTerminationType;
+
+import java.lang.reflect.Array;
+import java.lang.reflect.Field;
+import java.nio.ByteBuffer;
+import java.util.List;
+
+public class VariableSizeStringParser implements Parser {
+    private final Field targetField;
+    private ReflectUtil.NumField sizeField;
+    private StringTerminationType terminationType;
+
+
+    public VariableSizeStringParser(StringTerminationType terminationType,
+                                    Field sizeField, Field arrayField) {
+        targetField = arrayField;
+        this.sizeField = new ReflectUtil.NumField(sizeField);
+        this.terminationType = terminationType;
+    }
+
+    @Override
+    public int getSize(final Message src) {
+        final String str = (String) ReflectUtil.justGet(src, targetField);
+
+        if (str == null) {
+            throw new RuntimeException("string");
+        }
+        switch (terminationType) {
+            case NONE:
+                return str.length();
+            case ZERO_EXCLUDED:
+                return str.length() + 1;
+            case ZERO_INCLUDED:
+                return str.length() + 1;
+            default:
+                throw new AssertionError();
+        }
+    }
+
+    @Override
+    public int parse(final ByteBuffer srcBuf, int frameOffset, Message 
frameObj, final Message dstObj, List<Field>
+            frameSizePath) {
+        final int size = (int) sizeField.get(dstObj);
+        final int strSize;
+        final int skipSize;
+
+        switch (terminationType) {
+            case NONE:
+                strSize = size;
+                skipSize = 0;
+                break;
+            case ZERO_EXCLUDED:
+                strSize = size;
+                skipSize = 1;
+                break;
+            case ZERO_INCLUDED:
+                strSize = size-1;
+                skipSize = 1;
+                break;
+            default:
+                throw new AssertionError();
+        }
+
+        byte[] data = new byte[strSize];
+        srcBuf.get(data);
+        srcBuf.position(srcBuf.position() + skipSize);
+        ReflectUtil.justSet(dstObj, targetField, new String(data));
+        return size;
+    }
+
+    @Override
+    public int write(final ByteBuffer dstBuf, final Message src) {
+        int n;
+        final String str = (String) ReflectUtil.justGet(src, targetField);
+
+        if (str == null) {
+            throw new RuntimeException("string must not be null");
+        }
+
+        byte[] data = str.getBytes();
+
+        switch (terminationType) {
+            case NONE:
+                dstBuf.put(data);
+                n = data.length;
+                break;
+            case ZERO_INCLUDED:
+                dstBuf.put(data);
+                dstBuf.put((byte) 0);
+                n = data.length + 1;
+                break;
+            case ZERO_EXCLUDED:
+                dstBuf.put(data);
+                dstBuf.put((byte) 0);
+                n = data.length + 1;
+                break;
+            default:
+                throw new AssertionError();
+        }
+        return n;
+    }
+
+    @Override
+    public void patch(Message m, int frameSize, List<Field> frameSizePath, 
Message frameObj) {
+        int size;
+        final String str = (String) ReflectUtil.justGet(m, targetField);
+
+        if (str == null) {
+            throw new RuntimeException("string must not be null");
+        }
+
+        if (terminationType == StringTerminationType.ZERO_INCLUDED) {
+            size = str.length() + 1;
+        } else {
+            size = str.length();
+        }
+        sizeField.set(m, size);
+    }
+
+    @Override
+    public int getStaticSize() {
+        return 0;
+    }
+
+}

Modified: gnunet-java/src/main/java/org/gnunet/dht/ClientGetMessage.java
===================================================================
--- gnunet-java/src/main/java/org/gnunet/dht/ClientGetMessage.java      
2013-10-21 19:13:51 UTC (rev 30388)
+++ gnunet-java/src/main/java/org/gnunet/dht/ClientGetMessage.java      
2013-10-21 20:33:44 UTC (rev 30389)
@@ -24,13 +24,6 @@
 import org.gnunet.util.GnunetMessage;
 import org.gnunet.util.HashCode;
 
-/**
-* Created with IntelliJ IDEA.
-* User: dold
-* Date: 5/2/12
-* Time: 7:05 PM
-* To change this template use File | Settings | File Templates.
-*/
 @UnionCase(143)
 public class ClientGetMessage implements GnunetMessage.Body {
     /**

Modified: gnunet-java/src/main/java/org/gnunet/dht/ClientResultMessage.java
===================================================================
--- gnunet-java/src/main/java/org/gnunet/dht/ClientResultMessage.java   
2013-10-21 19:13:51 UTC (rev 30388)
+++ gnunet-java/src/main/java/org/gnunet/dht/ClientResultMessage.java   
2013-10-21 20:33:44 UTC (rev 30389)
@@ -26,13 +26,6 @@
 import org.gnunet.util.HashCode;
 import org.gnunet.util.PeerIdentity;
 
-/**
-* Created with IntelliJ IDEA.
-* User: dold
-* Date: 5/2/12
-* Time: 7:06 PM
-* To change this template use File | Settings | File Templates.
-*/
 @UnionCase(145)
 public class ClientResultMessage implements GnunetMessage.Body {
     @UInt32

Modified: gnunet-java/src/main/java/org/gnunet/statistics/StatisticsWatcher.java
===================================================================
--- gnunet-java/src/main/java/org/gnunet/statistics/StatisticsWatcher.java      
2013-10-21 19:13:51 UTC (rev 30388)
+++ gnunet-java/src/main/java/org/gnunet/statistics/StatisticsWatcher.java      
2013-10-21 20:33:44 UTC (rev 30389)
@@ -1,12 +1,5 @@
 package org.gnunet.statistics;
 
-/**
- * Created with IntelliJ IDEA.
- * User: dold
- * Date: 8/24/13
- * Time: 5:56 PM
- * To change this template use File | Settings | File Templates.
- */
 public interface StatisticsWatcher {
     public void onReceive(String subsystem, String name, long value);
     public void onTimeout();

Added: gnunet-java/src/test/java/org/gnunet/construct/VariableStringTest.java
===================================================================
--- gnunet-java/src/test/java/org/gnunet/construct/VariableStringTest.java      
                        (rev 0)
+++ gnunet-java/src/test/java/org/gnunet/construct/VariableStringTest.java      
2013-10-21 20:33:44 UTC (rev 30389)
@@ -0,0 +1,63 @@
+/*
+ This file is part of GNUnet.
+  (C) 2012, 2013 Christian Grothoff (and other contributing authors)
+
+  GNUnet is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published
+  by the Free Software Foundation; either version 3, or (at your
+  option) any later version.
+
+  GNUnet is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with GNUnet; see the file COPYING.  If not, write to the
+  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+  Boston, MA 02111-1307, USA.
+ */
+
+package org.gnunet.construct;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class VariableStringTest {
+    public static class VariableStringMessage implements Message {
+        @UInt32
+        public int len1;
+        @UInt32
+        public int len2;
+        @UInt32
+        public int len3;
+        @VariableSizeString(lengthField = "len1", terminationType = 
StringTerminationType.NONE)
+        public String str1;
+        @VariableSizeString(lengthField = "len2", terminationType = 
StringTerminationType.ZERO_EXCLUDED)
+        public String str2;
+        @VariableSizeString(lengthField = "len3", terminationType = 
StringTerminationType.ZERO_INCLUDED)
+        public String str3;
+        @UInt8
+        public int sentinel;
+    }
+
+    @Test
+    public void test_variable_string() {
+        VariableStringMessage m = new VariableStringMessage();
+        m.str1 = "foo";
+        m.str2 = "quux";
+        m.str3 = "42";
+        m.sentinel = 123;
+        Construct.patch(m);
+        Assert.assertEquals(3, m.len1);
+        Assert.assertEquals(4, m.len2);
+        Assert.assertEquals(3, m.len3);
+        byte[] data = Construct.toBinary(m);
+        Assert.assertEquals(3+5+3+3*4+1, data.length);
+        VariableStringMessage m2 = Construct.parseAs(data, 
VariableStringMessage.class);
+        Assert.assertEquals("foo", m2.str1);
+        Assert.assertEquals("quux", m2.str2);
+        Assert.assertEquals("42", m2.str3);
+        Assert.assertEquals(123, m2.sentinel);
+    }
+}




reply via email to

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