diff options
author | Florian Dold <florian.dold@gmail.com> | 2013-10-21 20:33:44 +0000 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2013-10-21 20:33:44 +0000 |
commit | 47c175cbeb83db37b222e61bee9944a79f078e2f (patch) | |
tree | 4fa74747915200b569bc35818462573bd6f6ce8c | |
parent | 35df3d81f0f5b76b728b3be8ddaa95ecdbbd0e5e (diff) | |
download | gnunet-java-47c175cbeb83db37b222e61bee9944a79f078e2f.tar.gz gnunet-java-47c175cbeb83db37b222e61bee9944a79f078e2f.zip |
- removed some auto-generated IDE comments
- implemented Construct-parsing of strings with size field
9 files changed, 339 insertions, 29 deletions
diff --git a/src/main/java/org/gnunet/arm/ResultHandler.java b/src/main/java/org/gnunet/arm/ResultHandler.java index 05c34f9..b355bd8 100644 --- a/src/main/java/org/gnunet/arm/ResultHandler.java +++ b/src/main/java/org/gnunet/arm/ResultHandler.java | |||
@@ -20,12 +20,5 @@ | |||
20 | 20 | ||
21 | package org.gnunet.arm; | 21 | package org.gnunet.arm; |
22 | 22 | ||
23 | /** | ||
24 | * Created with IntelliJ IDEA. | ||
25 | * User: dold | ||
26 | * Date: 10/7/13 | ||
27 | * Time: 4:00 PM | ||
28 | * To change this template use File | Settings | File Templates. | ||
29 | */ | ||
30 | public interface ResultHandler { | 23 | public interface ResultHandler { |
31 | } | 24 | } |
diff --git a/src/main/java/org/gnunet/construct/Construct.java b/src/main/java/org/gnunet/construct/Construct.java index 2cedbdc..ac3c804 100644 --- a/src/main/java/org/gnunet/construct/Construct.java +++ b/src/main/java/org/gnunet/construct/Construct.java | |||
@@ -20,6 +20,7 @@ | |||
20 | 20 | ||
21 | package org.gnunet.construct; | 21 | package org.gnunet.construct; |
22 | 22 | ||
23 | import com.google.common.base.Preconditions; | ||
23 | import org.gnunet.construct.parsers.*; | 24 | import org.gnunet.construct.parsers.*; |
24 | import org.grothoff.Runabout; | 25 | import org.grothoff.Runabout; |
25 | import org.slf4j.Logger; | 26 | import org.slf4j.Logger; |
@@ -139,7 +140,7 @@ public class Construct { | |||
139 | private static Parser getParser(Class<? extends Message> c, | 140 | private static Parser getParser(Class<? extends Message> c, |
140 | final ParserGenerator pg) { | 141 | final ParserGenerator pg) { |
141 | 142 | ||
142 | 143 | Preconditions.checkNotNull(c); | |
143 | SequenceParser parser = new SequenceParser(); | 144 | SequenceParser parser = new SequenceParser(); |
144 | 145 | ||
145 | if (!Modifier.isPublic(c.getModifiers())) { | 146 | if (!Modifier.isPublic(c.getModifiers())) { |
@@ -354,6 +355,23 @@ public class Construct { | |||
354 | } | 355 | } |
355 | } | 356 | } |
356 | 357 | ||
358 | public void visit(VariableSizeString vss) { | ||
359 | try { | ||
360 | parser = new VariableSizeStringParser(vss.terminationType(), c.getField(vss | ||
361 | .lengthField()), field); | ||
362 | |||
363 | } catch (SecurityException e) { | ||
364 | throw new AssertionError( | ||
365 | String.format( | ||
366 | "VariableSizeString: length field '%s' not declared public", | ||
367 | vss.lengthField())); | ||
368 | } catch (NoSuchFieldException e) { | ||
369 | throw new AssertionError(String.format( | ||
370 | "VariableSizeString: length field '%s' does not exist in class %s", | ||
371 | vss.lengthField(), c)); | ||
372 | } | ||
373 | } | ||
374 | |||
357 | 375 | ||
358 | public void visit(VariableSizeIntegerArray a) { | 376 | public void visit(VariableSizeIntegerArray a) { |
359 | try { | 377 | try { |
diff --git a/src/main/java/org/gnunet/construct/StringTerminationType.java b/src/main/java/org/gnunet/construct/StringTerminationType.java new file mode 100644 index 0000000..6638493 --- /dev/null +++ b/src/main/java/org/gnunet/construct/StringTerminationType.java | |||
@@ -0,0 +1,28 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2012, 2013 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | package org.gnunet.construct; | ||
22 | |||
23 | |||
24 | public enum StringTerminationType { | ||
25 | NONE, | ||
26 | ZERO_INCLUDED, | ||
27 | ZERO_EXCLUDED | ||
28 | } | ||
diff --git a/src/main/java/org/gnunet/construct/VariableSizeString.java b/src/main/java/org/gnunet/construct/VariableSizeString.java new file mode 100644 index 0000000..d9bc9be --- /dev/null +++ b/src/main/java/org/gnunet/construct/VariableSizeString.java | |||
@@ -0,0 +1,60 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2012, 2013 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /* | ||
22 | This file is part of GNUnet. | ||
23 | (C) 2011, 2012 Christian Grothoff (and other contributing authors) | ||
24 | |||
25 | GNUnet is free software; you can redistribute it and/or modify | ||
26 | it under the terms of the GNU General Public License as published | ||
27 | by the Free Software Foundation; either version 3, or (at your | ||
28 | option) any later version. | ||
29 | |||
30 | GNUnet is distributed in the hope that it will be useful, but | ||
31 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
32 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
33 | General Public License for more details. | ||
34 | |||
35 | You should have received a copy of the GNU General Public License | ||
36 | along with GNUnet; see the file COPYING. If not, write to the | ||
37 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
38 | Boston, MA 02111-1307, USA. | ||
39 | */ | ||
40 | |||
41 | package org.gnunet.construct; | ||
42 | |||
43 | import java.lang.annotation.ElementType; | ||
44 | import java.lang.annotation.Retention; | ||
45 | import java.lang.annotation.RetentionPolicy; | ||
46 | import java.lang.annotation.Target; | ||
47 | |||
48 | /** | ||
49 | * An array of messages, where the length of the array is specified in a | ||
50 | * field of the containing message. | ||
51 | * | ||
52 | * @author Florian Dold | ||
53 | * | ||
54 | */ | ||
55 | @Retention(RetentionPolicy.RUNTIME) | ||
56 | @Target(ElementType.FIELD) | ||
57 | public @interface VariableSizeString { | ||
58 | String lengthField(); | ||
59 | StringTerminationType terminationType(); | ||
60 | } | ||
diff --git a/src/main/java/org/gnunet/construct/parsers/VariableSizeStringParser.java b/src/main/java/org/gnunet/construct/parsers/VariableSizeStringParser.java new file mode 100644 index 0000000..39dd7a4 --- /dev/null +++ b/src/main/java/org/gnunet/construct/parsers/VariableSizeStringParser.java | |||
@@ -0,0 +1,169 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2012, 2013 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /* | ||
22 | This file is part of GNUnet. | ||
23 | (C) 2011, 2012 Christian Grothoff (and other contributing authors) | ||
24 | |||
25 | GNUnet is free software; you can redistribute it and/or modify | ||
26 | it under the terms of the GNU General Public License as published | ||
27 | by the Free Software Foundation; either version 3, or (at your | ||
28 | option) any later version. | ||
29 | |||
30 | GNUnet is distributed in the hope that it will be useful, but | ||
31 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
32 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
33 | General Public License for more details. | ||
34 | |||
35 | You should have received a copy of the GNU General Public License | ||
36 | along with GNUnet; see the file COPYING. If not, write to the | ||
37 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
38 | Boston, MA 02111-1307, USA. | ||
39 | */ | ||
40 | |||
41 | package org.gnunet.construct.parsers; | ||
42 | |||
43 | import org.gnunet.construct.Message; | ||
44 | import org.gnunet.construct.ReflectUtil; | ||
45 | import org.gnunet.construct.StringTerminationType; | ||
46 | |||
47 | import java.lang.reflect.Array; | ||
48 | import java.lang.reflect.Field; | ||
49 | import java.nio.ByteBuffer; | ||
50 | import java.util.List; | ||
51 | |||
52 | public class VariableSizeStringParser implements Parser { | ||
53 | private final Field targetField; | ||
54 | private ReflectUtil.NumField sizeField; | ||
55 | private StringTerminationType terminationType; | ||
56 | |||
57 | |||
58 | public VariableSizeStringParser(StringTerminationType terminationType, | ||
59 | Field sizeField, Field arrayField) { | ||
60 | targetField = arrayField; | ||
61 | this.sizeField = new ReflectUtil.NumField(sizeField); | ||
62 | this.terminationType = terminationType; | ||
63 | } | ||
64 | |||
65 | @Override | ||
66 | public int getSize(final Message src) { | ||
67 | final String str = (String) ReflectUtil.justGet(src, targetField); | ||
68 | |||
69 | if (str == null) { | ||
70 | throw new RuntimeException("string"); | ||
71 | } | ||
72 | switch (terminationType) { | ||
73 | case NONE: | ||
74 | return str.length(); | ||
75 | case ZERO_EXCLUDED: | ||
76 | return str.length() + 1; | ||
77 | case ZERO_INCLUDED: | ||
78 | return str.length() + 1; | ||
79 | default: | ||
80 | throw new AssertionError(); | ||
81 | } | ||
82 | } | ||
83 | |||
84 | @Override | ||
85 | public int parse(final ByteBuffer srcBuf, int frameOffset, Message frameObj, final Message dstObj, List<Field> | ||
86 | frameSizePath) { | ||
87 | final int size = (int) sizeField.get(dstObj); | ||
88 | final int strSize; | ||
89 | final int skipSize; | ||
90 | |||
91 | switch (terminationType) { | ||
92 | case NONE: | ||
93 | strSize = size; | ||
94 | skipSize = 0; | ||
95 | break; | ||
96 | case ZERO_EXCLUDED: | ||
97 | strSize = size; | ||
98 | skipSize = 1; | ||
99 | break; | ||
100 | case ZERO_INCLUDED: | ||
101 | strSize = size-1; | ||
102 | skipSize = 1; | ||
103 | break; | ||
104 | default: | ||
105 | throw new AssertionError(); | ||
106 | } | ||
107 | |||
108 | byte[] data = new byte[strSize]; | ||
109 | srcBuf.get(data); | ||
110 | srcBuf.position(srcBuf.position() + skipSize); | ||
111 | ReflectUtil.justSet(dstObj, targetField, new String(data)); | ||
112 | return size; | ||
113 | } | ||
114 | |||
115 | @Override | ||
116 | public int write(final ByteBuffer dstBuf, final Message src) { | ||
117 | int n; | ||
118 | final String str = (String) ReflectUtil.justGet(src, targetField); | ||
119 | |||
120 | if (str == null) { | ||
121 | throw new RuntimeException("string must not be null"); | ||
122 | } | ||
123 | |||
124 | byte[] data = str.getBytes(); | ||
125 | |||
126 | switch (terminationType) { | ||
127 | case NONE: | ||
128 | dstBuf.put(data); | ||
129 | n = data.length; | ||
130 | break; | ||
131 | case ZERO_INCLUDED: | ||
132 | dstBuf.put(data); | ||
133 | dstBuf.put((byte) 0); | ||
134 | n = data.length + 1; | ||
135 | break; | ||
136 | case ZERO_EXCLUDED: | ||
137 | dstBuf.put(data); | ||
138 | dstBuf.put((byte) 0); | ||
139 | n = data.length + 1; | ||
140 | break; | ||
141 | default: | ||
142 | throw new AssertionError(); | ||
143 | } | ||
144 | return n; | ||
145 | } | ||
146 | |||
147 | @Override | ||
148 | public void patch(Message m, int frameSize, List<Field> frameSizePath, Message frameObj) { | ||
149 | int size; | ||
150 | final String str = (String) ReflectUtil.justGet(m, targetField); | ||
151 | |||
152 | if (str == null) { | ||
153 | throw new RuntimeException("string must not be null"); | ||
154 | } | ||
155 | |||
156 | if (terminationType == StringTerminationType.ZERO_INCLUDED) { | ||
157 | size = str.length() + 1; | ||
158 | } else { | ||
159 | size = str.length(); | ||
160 | } | ||
161 | sizeField.set(m, size); | ||
162 | } | ||
163 | |||
164 | @Override | ||
165 | public int getStaticSize() { | ||
166 | return 0; | ||
167 | } | ||
168 | |||
169 | } | ||
diff --git a/src/main/java/org/gnunet/dht/ClientGetMessage.java b/src/main/java/org/gnunet/dht/ClientGetMessage.java index cd317fb..71f26bc 100644 --- a/src/main/java/org/gnunet/dht/ClientGetMessage.java +++ b/src/main/java/org/gnunet/dht/ClientGetMessage.java | |||
@@ -24,13 +24,6 @@ import org.gnunet.construct.*; | |||
24 | import org.gnunet.util.GnunetMessage; | 24 | import org.gnunet.util.GnunetMessage; |
25 | import org.gnunet.util.HashCode; | 25 | import org.gnunet.util.HashCode; |
26 | 26 | ||
27 | /** | ||
28 | * Created with IntelliJ IDEA. | ||
29 | * User: dold | ||
30 | * Date: 5/2/12 | ||
31 | * Time: 7:05 PM | ||
32 | * To change this template use File | Settings | File Templates. | ||
33 | */ | ||
34 | @UnionCase(143) | 27 | @UnionCase(143) |
35 | public class ClientGetMessage implements GnunetMessage.Body { | 28 | public class ClientGetMessage implements GnunetMessage.Body { |
36 | /** | 29 | /** |
diff --git a/src/main/java/org/gnunet/dht/ClientResultMessage.java b/src/main/java/org/gnunet/dht/ClientResultMessage.java index fab614f..3a4ac96 100644 --- a/src/main/java/org/gnunet/dht/ClientResultMessage.java +++ b/src/main/java/org/gnunet/dht/ClientResultMessage.java | |||
@@ -26,13 +26,6 @@ import org.gnunet.util.GnunetMessage; | |||
26 | import org.gnunet.util.HashCode; | 26 | import org.gnunet.util.HashCode; |
27 | import org.gnunet.util.PeerIdentity; | 27 | import org.gnunet.util.PeerIdentity; |
28 | 28 | ||
29 | /** | ||
30 | * Created with IntelliJ IDEA. | ||
31 | * User: dold | ||
32 | * Date: 5/2/12 | ||
33 | * Time: 7:06 PM | ||
34 | * To change this template use File | Settings | File Templates. | ||
35 | */ | ||
36 | @UnionCase(145) | 29 | @UnionCase(145) |
37 | public class ClientResultMessage implements GnunetMessage.Body { | 30 | public class ClientResultMessage implements GnunetMessage.Body { |
38 | @UInt32 | 31 | @UInt32 |
diff --git a/src/main/java/org/gnunet/statistics/StatisticsWatcher.java b/src/main/java/org/gnunet/statistics/StatisticsWatcher.java index d3fb645..b6c5f9e 100644 --- a/src/main/java/org/gnunet/statistics/StatisticsWatcher.java +++ b/src/main/java/org/gnunet/statistics/StatisticsWatcher.java | |||
@@ -1,12 +1,5 @@ | |||
1 | package org.gnunet.statistics; | 1 | package org.gnunet.statistics; |
2 | 2 | ||
3 | /** | ||
4 | * Created with IntelliJ IDEA. | ||
5 | * User: dold | ||
6 | * Date: 8/24/13 | ||
7 | * Time: 5:56 PM | ||
8 | * To change this template use File | Settings | File Templates. | ||
9 | */ | ||
10 | public interface StatisticsWatcher { | 3 | public interface StatisticsWatcher { |
11 | public void onReceive(String subsystem, String name, long value); | 4 | public void onReceive(String subsystem, String name, long value); |
12 | public void onTimeout(); | 5 | public void onTimeout(); |
diff --git a/src/test/java/org/gnunet/construct/VariableStringTest.java b/src/test/java/org/gnunet/construct/VariableStringTest.java new file mode 100644 index 0000000..35e3f13 --- /dev/null +++ b/src/test/java/org/gnunet/construct/VariableStringTest.java | |||
@@ -0,0 +1,63 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2012, 2013 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | package org.gnunet.construct; | ||
22 | |||
23 | import org.junit.Assert; | ||
24 | import org.junit.Test; | ||
25 | |||
26 | public class VariableStringTest { | ||
27 | public static class VariableStringMessage implements Message { | ||
28 | @UInt32 | ||
29 | public int len1; | ||
30 | @UInt32 | ||
31 | public int len2; | ||
32 | @UInt32 | ||
33 | public int len3; | ||
34 | @VariableSizeString(lengthField = "len1", terminationType = StringTerminationType.NONE) | ||
35 | public String str1; | ||
36 | @VariableSizeString(lengthField = "len2", terminationType = StringTerminationType.ZERO_EXCLUDED) | ||
37 | public String str2; | ||
38 | @VariableSizeString(lengthField = "len3", terminationType = StringTerminationType.ZERO_INCLUDED) | ||
39 | public String str3; | ||
40 | @UInt8 | ||
41 | public int sentinel; | ||
42 | } | ||
43 | |||
44 | @Test | ||
45 | public void test_variable_string() { | ||
46 | VariableStringMessage m = new VariableStringMessage(); | ||
47 | m.str1 = "foo"; | ||
48 | m.str2 = "quux"; | ||
49 | m.str3 = "42"; | ||
50 | m.sentinel = 123; | ||
51 | Construct.patch(m); | ||
52 | Assert.assertEquals(3, m.len1); | ||
53 | Assert.assertEquals(4, m.len2); | ||
54 | Assert.assertEquals(3, m.len3); | ||
55 | byte[] data = Construct.toBinary(m); | ||
56 | Assert.assertEquals(3+5+3+3*4+1, data.length); | ||
57 | VariableStringMessage m2 = Construct.parseAs(data, VariableStringMessage.class); | ||
58 | Assert.assertEquals("foo", m2.str1); | ||
59 | Assert.assertEquals("quux", m2.str2); | ||
60 | Assert.assertEquals("42", m2.str3); | ||
61 | Assert.assertEquals(123, m2.sentinel); | ||
62 | } | ||
63 | } | ||