[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[GNUnet-SVN] r18594 - in gnunet-java: . .externalToolBuilders .settings
From: |
gnunet |
Subject: |
[GNUnet-SVN] r18594 - in gnunet-java: . .externalToolBuilders .settings src/org/gnunet src/org/gnunet/construct src/org/gnunet/construct/parsers src/org/gnunet/exceptions src/org/gnunet/messages src/org/gnunet/service src/org/gnunet/util test/org/gnunet/construct |
Date: |
Thu, 15 Dec 2011 08:31:39 +0100 |
Author: dold
Date: 2011-12-15 08:31:39 +0100 (Thu, 15 Dec 2011)
New Revision: 18594
Added:
gnunet-java/.externalToolBuilders/
gnunet-java/.externalToolBuilders/Build_GNUnet.launch
gnunet-java/.factorypath
gnunet-java/.settings/org.eclipse.jdt.apt.core.prefs
gnunet-java/build.xml
gnunet-java/src/org/gnunet/construct/MessageId.java
gnunet-java/src/org/gnunet/exceptions/
gnunet-java/src/org/gnunet/exceptions/InterfaceViolationException.java
gnunet-java/src/org/gnunet/exceptions/InternalLogicError.java
gnunet-java/src/org/gnunet/exceptions/MessageFormatException.java
gnunet-java/src/org/gnunet/messages/RelativeTimeMessage.java
gnunet-java/src/org/gnunet/util/TransmitHandler.java
Removed:
gnunet-java/src/org/gnunet/messages/RelativeTimeNBO.java
Modified:
gnunet-java/.classpath
gnunet-java/.project
gnunet-java/.settings/org.eclipse.jdt.core.prefs
gnunet-java/ISSUES
gnunet-java/src/org/gnunet/construct/Construct.java
gnunet-java/src/org/gnunet/construct/Fill.java
gnunet-java/src/org/gnunet/construct/FixedSizeArray.java
gnunet-java/src/org/gnunet/construct/Int16.java
gnunet-java/src/org/gnunet/construct/Int32.java
gnunet-java/src/org/gnunet/construct/Int64.java
gnunet-java/src/org/gnunet/construct/Int8.java
gnunet-java/src/org/gnunet/construct/Integer.java
gnunet-java/src/org/gnunet/construct/UInt16.java
gnunet-java/src/org/gnunet/construct/UInt32.java
gnunet-java/src/org/gnunet/construct/UInt64.java
gnunet-java/src/org/gnunet/construct/UInt8.java
gnunet-java/src/org/gnunet/construct/VariableSizeArray.java
gnunet-java/src/org/gnunet/construct/ZeroTerminatedString.java
gnunet-java/src/org/gnunet/construct/parsers/ByteFillParser.java
gnunet-java/src/org/gnunet/construct/parsers/FillParser.java
gnunet-java/src/org/gnunet/construct/parsers/IntegerParser.java
gnunet-java/src/org/gnunet/construct/parsers/NestedParser.java
gnunet-java/src/org/gnunet/construct/parsers/VariableSizeArrayParser.java
gnunet-java/src/org/gnunet/messages/QueryMessage.java
gnunet-java/src/org/gnunet/service/StatisticsService.java
gnunet-java/src/org/gnunet/util/Client.java
gnunet-java/src/org/gnunet/util/MessageHandler.java
gnunet-java/src/org/gnunet/util/RelativeTime.java
gnunet-java/src/org/gnunet/util/Scheduler.java
gnunet-java/test/org/gnunet/construct/ConstructTest.java
Log:
many changes
Modified: gnunet-java/.classpath
===================================================================
--- gnunet-java/.classpath 2011-12-14 13:57:53 UTC (rev 18593)
+++ gnunet-java/.classpath 2011-12-15 07:31:39 UTC (rev 18594)
@@ -6,5 +6,10 @@
<classpathentry kind="con"
path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="lib" path="lib/slf4j-api-1.6.4.jar"/>
<classpathentry kind="lib" path="lib/slf4j-jdk14-1.6.4.jar"/>
+ <classpathentry kind="src" path=".apt_generated">
+ <attributes>
+ <attribute name="optional" value="true"/>
+ </attributes>
+ </classpathentry>
<classpathentry kind="output" path="bin"/>
</classpath>
Added: gnunet-java/.externalToolBuilders/Build_GNUnet.launch
===================================================================
--- gnunet-java/.externalToolBuilders/Build_GNUnet.launch
(rev 0)
+++ gnunet-java/.externalToolBuilders/Build_GNUnet.launch 2011-12-15
07:31:39 UTC (rev 18594)
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<launchConfiguration type="org.eclipse.ant.AntBuilderLaunchConfigurationType">
+<booleanAttribute key="editedByExternalToolsMainTab" value="true"/>
+<booleanAttribute key="org.eclipse.ant.ui.ATTR_TARGETS_UPDATED" value="true"/>
+<booleanAttribute key="org.eclipse.ant.ui.DEFAULT_VM_INSTALL" value="false"/>
+<booleanAttribute key="org.eclipse.debug.ui.ATTR_LAUNCH_IN_BACKGROUND"
value="false"/>
+<stringAttribute key="org.eclipse.jdt.launching.CLASSPATH_PROVIDER"
value="org.eclipse.ant.ui.AntClasspathProvider"/>
+<booleanAttribute key="org.eclipse.jdt.launching.DEFAULT_CLASSPATH"
value="true"/>
+<booleanAttribute key="org.eclipse.ui.externaltools.ATTR_BUILDER_ENABLED"
value="false"/>
+<stringAttribute key="org.eclipse.ui.externaltools.ATTR_RUN_BUILD_KINDS"
value="full,incremental,"/>
+<booleanAttribute key="org.eclipse.ui.externaltools.ATTR_TRIGGERS_CONFIGURED"
value="true"/>
+</launchConfiguration>
Added: gnunet-java/.factorypath
===================================================================
--- gnunet-java/.factorypath (rev 0)
+++ gnunet-java/.factorypath 2011-12-15 07:31:39 UTC (rev 18594)
@@ -0,0 +1,3 @@
+<factorypath>
+ <factorypathentry kind="WKSPJAR" id="/gnunet-java/lib/gnj-ap.jar"
enabled="true" runInBatchMode="false"/>
+</factorypath>
Modified: gnunet-java/.project
===================================================================
--- gnunet-java/.project 2011-12-14 13:57:53 UTC (rev 18593)
+++ gnunet-java/.project 2011-12-15 07:31:39 UTC (rev 18594)
@@ -10,6 +10,16 @@
<arguments>
</arguments>
</buildCommand>
+ <buildCommand>
+
<name>org.eclipse.ui.externaltools.ExternalToolBuilder</name>
+ <triggers>full,incremental,</triggers>
+ <arguments>
+ <dictionary>
+ <key>LaunchConfigHandle</key>
+
<value><project>/.externalToolBuilders/Build_GNUnet.launch</value>
+ </dictionary>
+ </arguments>
+ </buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
Added: gnunet-java/.settings/org.eclipse.jdt.apt.core.prefs
===================================================================
--- gnunet-java/.settings/org.eclipse.jdt.apt.core.prefs
(rev 0)
+++ gnunet-java/.settings/org.eclipse.jdt.apt.core.prefs 2011-12-15
07:31:39 UTC (rev 18594)
@@ -0,0 +1,5 @@
+#Mon Dec 12 08:32:26 CET 2011
+eclipse.preferences.version=1
+org.eclipse.jdt.apt.aptEnabled=true
+org.eclipse.jdt.apt.genSrcDir=.apt_generated
+org.eclipse.jdt.apt.reconcileEnabled=true
Modified: gnunet-java/.settings/org.eclipse.jdt.core.prefs
===================================================================
--- gnunet-java/.settings/org.eclipse.jdt.core.prefs 2011-12-14 13:57:53 UTC
(rev 18593)
+++ gnunet-java/.settings/org.eclipse.jdt.core.prefs 2011-12-15 07:31:39 UTC
(rev 18594)
@@ -1,4 +1,4 @@
-#Sun Nov 06 13:14:08 CET 2011
+#Mon Dec 12 08:32:27 CET 2011
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
@@ -7,8 +7,23 @@
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.doc.comment.support=enabled
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.problem.invalidJavadoc=warning
+org.eclipse.jdt.core.compiler.problem.invalidJavadocTags=disabled
+org.eclipse.jdt.core.compiler.problem.invalidJavadocTagsDeprecatedRef=disabled
+org.eclipse.jdt.core.compiler.problem.invalidJavadocTagsNotVisibleRef=disabled
+org.eclipse.jdt.core.compiler.problem.invalidJavadocTagsVisibility=public
+org.eclipse.jdt.core.compiler.problem.missingJavadocComments=ignore
+org.eclipse.jdt.core.compiler.problem.missingJavadocCommentsOverriding=disabled
+org.eclipse.jdt.core.compiler.problem.missingJavadocCommentsVisibility=public
+org.eclipse.jdt.core.compiler.problem.missingJavadocTagDescription=return_tag
+org.eclipse.jdt.core.compiler.problem.missingJavadocTags=ignore
+org.eclipse.jdt.core.compiler.problem.missingJavadocTagsMethodTypeParameters=disabled
+org.eclipse.jdt.core.compiler.problem.missingJavadocTagsOverriding=disabled
+org.eclipse.jdt.core.compiler.problem.missingJavadocTagsVisibility=public
+org.eclipse.jdt.core.compiler.processAnnotations=enabled
org.eclipse.jdt.core.compiler.source=1.6
org.eclipse.jdt.core.formatter.align_type_members_on_columns=false
org.eclipse.jdt.core.formatter.alignment_for_arguments_in_allocation_expression=16
Modified: gnunet-java/ISSUES
===================================================================
--- gnunet-java/ISSUES 2011-12-14 13:57:53 UTC (rev 18593)
+++ gnunet-java/ISSUES 2011-12-15 07:31:39 UTC (rev 18594)
@@ -47,10 +47,37 @@
(* RelativeTime/AbsoluteTime: are wrappers are really necessary? do we need to
distinguish
relative time from absolute time statically? most java APIs just use long)
-* should MessageHandler be a runabout?
+* should MessageHandler be a runabout? => yes
(* idea: gnunet-java uses a lot of reflection, should we use apt (annotation
processing tool)
- to do some basic static checks for runabouts and maybe construct
annotations?)
\ No newline at end of file
+ to do some basic static checks for runabouts and maybe construct
annotations?)
+
+
+
+
+== Meeting: Thu Dec 15 ==
+
+ * big problem: no unix domain socket implementation supports select
+ * possible solutions:
+ * write our own implementation
+ * hard/impossible to integrate with the java api for sockets/selectors
+ * => use one thread that selects on all UDSs and writes to a pipe
+ * don't use unix domain sockets
+ * use threads to receive from UDSs
+ * write java daemon to multiplex UDSs over one TCP socket
+
+
+ * IDE: switching to IntelliJ community edition (available under Apache 2
license)
+
+
+ * project structure
+ * where to put annotation processors?
+ * should work with ant and eclipse
+ * dependencies: ann.proc <-> annotation classes
+
+
+
+
\ No newline at end of file
Added: gnunet-java/build.xml
===================================================================
Modified: gnunet-java/src/org/gnunet/construct/Construct.java
===================================================================
--- gnunet-java/src/org/gnunet/construct/Construct.java 2011-12-14 13:57:53 UTC
(rev 18593)
+++ gnunet-java/src/org/gnunet/construct/Construct.java 2011-12-15 07:31:39 UTC
(rev 18594)
@@ -17,6 +17,7 @@
import org.gnunet.construct.parsers.SequenceParser;
import org.gnunet.construct.parsers.StringParser;
import org.gnunet.construct.parsers.VariableSizeArrayParser;
+import org.gnunet.exceptions.InterfaceViolationException;
import org.gnunet.messages.Message;
import org.grothoff.Runabout;
@@ -25,13 +26,8 @@
*
* @author Christian Grothoff
*
- * @TODO serialization runabout, size computation runabout, some more basic
- * annotations (int16, int32, uint64, etc.)
- * @TODO error handling (exceptions?), documentation
- * @TODO performance evaluation
*/
public class Construct {
-
private static HashMap<Class<? extends Message>, Parser> parserCache = new
HashMap<Class<? extends Message>, Parser>();
@@ -47,8 +43,6 @@
* @param c
* desired object type to return
* @return instance of the desired object type
- * @throws RuntimeException
- * (ugh)
*/
public static <T extends Message> T parseAs(byte[] data, int offset,
Class<T> c) {
@@ -57,9 +51,10 @@
try {
m = (T) c.newInstance();
} catch (InstantiationException e) {
- throw new RuntimeException();
+ throw new InterfaceViolationException("Cannot instantiate
Message");
} catch (IllegalAccessException e) {
- throw new RuntimeException();
+ throw new InterfaceViolationException(
+ "Cannot instantiate Message (illegal access)");
}
getParser(c).parse(data, offset, 0, m);
@@ -68,8 +63,7 @@
}
/**
- * Create a Parser for a sub-class of Message.
- * The result is always cached.
+ * Create a Parser for a sub-class of Message. The result is always cached.
*
* @param c
* @return
@@ -87,7 +81,7 @@
return p;
}
- public static Parser getParser(Class<? extends Message> c,
+ private static Parser getParser(Class<? extends Message> c,
ParserGenerator pg) {
SequenceParser parser = new SequenceParser();
@@ -112,35 +106,46 @@
return parser;
}
- public static class ParserGenerator extends Runabout {
+ private static class ParserGenerator extends Runabout {
+ // the field we are currently generating a parser for
Field field;
+ // all annotations on the field
Annotation[] annotations;
+ // the index of the annotation we are supposed to process right now
int annotationsIdx;
+ // the message class for which the parser is generated
Class c;
+ // the parser we are actually generating, used by the caller, set as
+ // return value of
+ // the runabout invocation
FieldParser parser;
+ // where are we currently, seen from the root message object
+ List<Field> path = new LinkedList<Field>();
+
+ // path of the object that has a total size field
List<Field> total_size_path;
- List<Field> path = new LinkedList<Field>();
-
private ParserGenerator() {
}
- public void visitDefault(Object o) {
- throw new RuntimeException("unexpected annotation");
- }
-
public void visit(FrameSize ts) {
total_size_path = new LinkedList<Field>(path);
total_size_path.add(field);
+ if (annotationsIdx != 0) {
+ throw new InterfaceViolationException(
+ "FrameSize must be the first annotation on a Field");
+ }
+
annotationsIdx++;
if (annotationsIdx >= annotations.length) {
- throw new RuntimeException();
+ throw new InterfaceViolationException(
+ "FrameSize must be followed by an numeric parser");
}
visitAppropriate(annotations[annotationsIdx]);
@@ -205,7 +210,7 @@
public void visit(ByteFill bf) {
if (total_size_path == null) {
- throw new RuntimeException(
+ throw new InterfaceViolationException(
"no total size found before variable size element");
}
@@ -235,12 +240,16 @@
.lengthField()), f);
} catch (SecurityException e) {
- throw new RuntimeException();
+ throw new InterfaceViolationException(
+ String.format(
+ "VariableSizeArray: length field '%s' not
declared public",
+ vsa.lengthField()));
} catch (NoSuchFieldException e) {
- throw new RuntimeException(e);
+ throw new InterfaceViolationException(String.format(
+ "VariableSizeArray: length field '%s' does not exist",
+ vsa.lengthField()));
}
}
-
}
/**
@@ -284,69 +293,88 @@
p.patchSizeFields(m, p.getSize(m));
}
- public enum NumFieldType {
- BIGNUM, BYTE_PRIM, SHORT_PRIM, INT_PRIM, LONG_PRIM, CHAR_PRIM
- }
+ // the following are utility methods for the java reflection api
- public static NumFieldType getNumFieldType(Field f) {
- if (f.getType().equals(Long.TYPE)) {
- return NumFieldType.LONG_PRIM;
- } else if (f.getType().equals(java.lang.Integer.TYPE)) {
- return NumFieldType.INT_PRIM;
- } else if (f.getType().equals(Short.TYPE)) {
- return NumFieldType.SHORT_PRIM;
- } else if (f.getType().equals(Byte.TYPE)) {
- return NumFieldType.BYTE_PRIM;
- } else if (f.getType().equals(Character.TYPE)) {
- return NumFieldType.CHAR_PRIM;
- } else if (f.getType().equals(BigInteger.class)) {
- return NumFieldType.BIGNUM;
- } else {
- throw new RuntimeException("target type not supported");
+ public static class ReflectionUtil {
+ /**
+ * assign an enum value to each numeric type we want to serialize in
+ * order do switch statements on field types
+ */
+ public enum NumFieldType {
+ BIGNUM, BYTE_PRIM, SHORT_PRIM, INT_PRIM, LONG_PRIM, CHAR_PRIM
}
- }
-
-
- public static Object followFieldPath(List<Field> fl, Object obj, int
depth) {
- for (int i = 0; i < depth; ++i) {
+
+ /**
+ * Get the corresponding NumFieldType for a Field if possible
+ *
+ * @param f
+ * @return
+ */
+ public static NumFieldType getNumFieldType(Field f) {
+ if (f.getType().equals(Long.TYPE)) {
+ return NumFieldType.LONG_PRIM;
+ } else if (f.getType().equals(java.lang.Integer.TYPE)) {
+ return NumFieldType.INT_PRIM;
+ } else if (f.getType().equals(Short.TYPE)) {
+ return NumFieldType.SHORT_PRIM;
+ } else if (f.getType().equals(Byte.TYPE)) {
+ return NumFieldType.BYTE_PRIM;
+ } else if (f.getType().equals(Character.TYPE)) {
+ return NumFieldType.CHAR_PRIM;
+ } else if (f.getType().equals(BigInteger.class)) {
+ return NumFieldType.BIGNUM;
+ } else {
+ throw new InterfaceViolationException(
+ "expected numeric type, got: " + f.getType());
+ }
+ }
+
+ public static void setNumField(Object obj, NumFieldType ft, Field f,
long val) {
try {
- obj = fl.get(i).get(obj);
+ switch (ft) {
+ case LONG_PRIM:
+ f.setLong(obj, val);
+ break;
+ case INT_PRIM:
+ f.setInt(obj, (int) val);
+ break;
+ case SHORT_PRIM:
+ f.setShort(obj, (short) val);
+ break;
+ case BYTE_PRIM:
+ f.setByte(obj, (byte) val);
+ break;
+ case CHAR_PRIM:
+ f.setChar(obj, (char) val);
+ break;
+ case BIGNUM:
+ f.set(obj, BigInteger.valueOf(val));
+ break;
+ }
} catch (IllegalArgumentException e) {
- throw new RuntimeException();
+ throw new InterfaceViolationException("cannot access field");
} catch (IllegalAccessException e) {
- throw new RuntimeException();
+ throw new InterfaceViolationException("cannot access field");
}
}
- return obj;
- }
-
- public static Object followFieldPath(List<Field> fl, Object obj) {
- return followFieldPath(fl, obj, fl.size());
- }
-
- public static void setNumField(Field f, NumFieldType t, Object obj, long
val) {
- try {
- switch (t) {
- case LONG_PRIM:
- f.setLong(obj, val);
- break;
- case SHORT_PRIM:
- f.setShort(obj, (short) val);
- break;
- case BYTE_PRIM:
- f.setLong(obj, (byte) val);
- break;
- case INT_PRIM:
- f.setInt(obj, (int) val);
- break;
- default:
- throw new RuntimeException("invalid type");
+ public static Object followFieldPath(List<Field> fl, Object obj,
+ int depth) {
+ for (int i = 0; i < depth; ++i) {
+ try {
+ obj = fl.get(i).get(obj);
+ } catch (IllegalArgumentException e) {
+ throw new RuntimeException();
+ } catch (IllegalAccessException e) {
+ throw new RuntimeException();
+ }
}
- } catch (IllegalArgumentException e) {
- throw new RuntimeException(e);
- } catch (IllegalAccessException e) {
- throw new RuntimeException();
+ return obj;
}
+
+ public static Object followFieldPath(List<Field> fl, Object obj) {
+ return followFieldPath(fl, obj, fl.size());
+ }
}
+
}
Modified: gnunet-java/src/org/gnunet/construct/Fill.java
===================================================================
--- gnunet-java/src/org/gnunet/construct/Fill.java 2011-12-14 13:57:53 UTC
(rev 18593)
+++ gnunet-java/src/org/gnunet/construct/Fill.java 2011-12-15 07:31:39 UTC
(rev 18594)
@@ -6,7 +6,7 @@
import java.lang.annotation.Target;
/**
- * Fills an array of messages with the rest of the message in the buffer.
+ * An array of messages filling the rest of the frame
*
* @author Florian Dold
*
Modified: gnunet-java/src/org/gnunet/construct/FixedSizeArray.java
===================================================================
--- gnunet-java/src/org/gnunet/construct/FixedSizeArray.java 2011-12-14
13:57:53 UTC (rev 18593)
+++ gnunet-java/src/org/gnunet/construct/FixedSizeArray.java 2011-12-15
07:31:39 UTC (rev 18594)
@@ -6,7 +6,8 @@
import java.lang.annotation.Target;
/**
- * Parse an array of messages with its size fixed at the creation time of the
parser.
+ * An array of messages with static size.
+ *
* @author Florian Dold
*
*/
Modified: gnunet-java/src/org/gnunet/construct/Int16.java
===================================================================
--- gnunet-java/src/org/gnunet/construct/Int16.java 2011-12-14 13:57:53 UTC
(rev 18593)
+++ gnunet-java/src/org/gnunet/construct/Int16.java 2011-12-15 07:31:39 UTC
(rev 18594)
@@ -5,6 +5,12 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
+/**
+ * Synonym for Integer(byteSize=2, signed=true)
+ *
+ * @author Florian Dold
+ *
+ */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Int16 {
Modified: gnunet-java/src/org/gnunet/construct/Int32.java
===================================================================
--- gnunet-java/src/org/gnunet/construct/Int32.java 2011-12-14 13:57:53 UTC
(rev 18593)
+++ gnunet-java/src/org/gnunet/construct/Int32.java 2011-12-15 07:31:39 UTC
(rev 18594)
@@ -5,6 +5,12 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
+/**
+ * Synonym for Integer(byteSize=4, signed=true)
+ *
+ * @author Florian Dold
+ *
+ */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Int32 {
Modified: gnunet-java/src/org/gnunet/construct/Int64.java
===================================================================
--- gnunet-java/src/org/gnunet/construct/Int64.java 2011-12-14 13:57:53 UTC
(rev 18593)
+++ gnunet-java/src/org/gnunet/construct/Int64.java 2011-12-15 07:31:39 UTC
(rev 18594)
@@ -5,6 +5,13 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
+/**
+ * Synonym for Integer(byteSize=8, signed=true)
+ *
+ * @author Florian Dold
+ *
+ */
+
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Int64 {
Modified: gnunet-java/src/org/gnunet/construct/Int8.java
===================================================================
--- gnunet-java/src/org/gnunet/construct/Int8.java 2011-12-14 13:57:53 UTC
(rev 18593)
+++ gnunet-java/src/org/gnunet/construct/Int8.java 2011-12-15 07:31:39 UTC
(rev 18594)
@@ -5,6 +5,14 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
+
+/**
+ * Synonym for Integer(byteSize=1, signed=true)
+ *
+ * @author Florian Dold
+ *
+ */
+
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Int8 {
Modified: gnunet-java/src/org/gnunet/construct/Integer.java
===================================================================
--- gnunet-java/src/org/gnunet/construct/Integer.java 2011-12-14 13:57:53 UTC
(rev 18593)
+++ gnunet-java/src/org/gnunet/construct/Integer.java 2011-12-15 07:31:39 UTC
(rev 18594)
@@ -5,6 +5,14 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
+/**
+ * An integer with static size. May be signed or unsigned.
+ *
+ * Allowed target fields: byte, short, int, long, BigInteger
+ *
+ * @author Florian Dold
+ *
+ */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Integer {
Added: gnunet-java/src/org/gnunet/construct/MessageId.java
===================================================================
--- gnunet-java/src/org/gnunet/construct/MessageId.java
(rev 0)
+++ gnunet-java/src/org/gnunet/construct/MessageId.java 2011-12-15 07:31:39 UTC
(rev 18594)
@@ -0,0 +1,18 @@
+package org.gnunet.construct;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Assigns an id to a a message.
+ *
+ * @author Florian Dold
+ *
+ */
address@hidden(RetentionPolicy.SOURCE)
address@hidden(ElementType.TYPE)
+public @interface MessageId {
+
+}
Modified: gnunet-java/src/org/gnunet/construct/UInt16.java
===================================================================
--- gnunet-java/src/org/gnunet/construct/UInt16.java 2011-12-14 13:57:53 UTC
(rev 18593)
+++ gnunet-java/src/org/gnunet/construct/UInt16.java 2011-12-15 07:31:39 UTC
(rev 18594)
@@ -5,6 +5,14 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
+
+/**
+ * Synonym for Integer(byteSize=2, signed=false)
+ *
+ * @author Florian Dold
+ *
+ */
+
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface UInt16 {
Modified: gnunet-java/src/org/gnunet/construct/UInt32.java
===================================================================
--- gnunet-java/src/org/gnunet/construct/UInt32.java 2011-12-14 13:57:53 UTC
(rev 18593)
+++ gnunet-java/src/org/gnunet/construct/UInt32.java 2011-12-15 07:31:39 UTC
(rev 18594)
@@ -5,6 +5,13 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
+/**
+ * Synonym for Integer(byteSize=4, signed=false)
+ *
+ * @author Florian Dold
+ *
+ */
+
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface UInt32 {
Modified: gnunet-java/src/org/gnunet/construct/UInt64.java
===================================================================
--- gnunet-java/src/org/gnunet/construct/UInt64.java 2011-12-14 13:57:53 UTC
(rev 18593)
+++ gnunet-java/src/org/gnunet/construct/UInt64.java 2011-12-15 07:31:39 UTC
(rev 18594)
@@ -5,6 +5,13 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
+/**
+ * Synonym for Integer(byteSize=8, signed=false)
+ *
+ * @author Florian Dold
+ *
+ */
+
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface UInt64 {
Modified: gnunet-java/src/org/gnunet/construct/UInt8.java
===================================================================
--- gnunet-java/src/org/gnunet/construct/UInt8.java 2011-12-14 13:57:53 UTC
(rev 18593)
+++ gnunet-java/src/org/gnunet/construct/UInt8.java 2011-12-15 07:31:39 UTC
(rev 18594)
@@ -5,6 +5,14 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
+
+/**
+ * Synonym for Integer(byteSize=1, signed=false)
+ *
+ * @author Florian Dold
+ *
+ */
+
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface UInt8 {
Modified: gnunet-java/src/org/gnunet/construct/VariableSizeArray.java
===================================================================
--- gnunet-java/src/org/gnunet/construct/VariableSizeArray.java 2011-12-14
13:57:53 UTC (rev 18593)
+++ gnunet-java/src/org/gnunet/construct/VariableSizeArray.java 2011-12-15
07:31:39 UTC (rev 18594)
@@ -5,12 +5,12 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
-
/**
- * Parse an array of messages, where the length of the array is specified in a
field of the containing message.
+ * An array of messages, where the length of the array is specified in a
+ * field of the containing message.
*
* @author Florian Dold
- *
+ *
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
Modified: gnunet-java/src/org/gnunet/construct/ZeroTerminatedString.java
===================================================================
--- gnunet-java/src/org/gnunet/construct/ZeroTerminatedString.java
2011-12-14 13:57:53 UTC (rev 18593)
+++ gnunet-java/src/org/gnunet/construct/ZeroTerminatedString.java
2011-12-15 07:31:39 UTC (rev 18594)
@@ -4,7 +4,7 @@
import java.lang.annotation.RetentionPolicy;
/**
- * Parse and unparse a zero-terminated string with the specified encoding.
+ * A zero-terminated string with the specified encoding.
* The default encoding is UTF-8.
*
* @author Florian Dold
Modified: gnunet-java/src/org/gnunet/construct/parsers/ByteFillParser.java
===================================================================
--- gnunet-java/src/org/gnunet/construct/parsers/ByteFillParser.java
2011-12-14 13:57:53 UTC (rev 18593)
+++ gnunet-java/src/org/gnunet/construct/parsers/ByteFillParser.java
2011-12-15 07:31:39 UTC (rev 18594)
@@ -5,7 +5,7 @@
import java.util.List;
import org.gnunet.construct.Construct;
-import org.gnunet.construct.Construct.NumFieldType;
+import org.gnunet.construct.Construct.ReflectionUtil;
import org.gnunet.messages.Message;
/**
@@ -16,22 +16,23 @@
*/
public class ByteFillParser extends FieldParser {
private List<Field> totalSizePath;
-
- private NumFieldType totalSizeFieldType;
+ private ReflectionUtil.NumFieldType totalSizeFieldType;
+
public ByteFillParser(List<Field> totalSizePath, Field field) {
super(field);
this.totalSizePath = totalSizePath;
- totalSizeFieldType =
Construct.getNumFieldType(totalSizePath.get(totalSizePath.size() -1 ));
+ totalSizeFieldType = Construct.ReflectionUtil
+ .getNumFieldType(totalSizePath.get(totalSizePath.size() - 1));
}
@Override
public int getSize(final Message src) {
return Array.getLength(getFieldValue(src));
}
-
+
private int getSizeFieldValue(Message m) {
- Object obj = Construct.followFieldPath(totalSizePath, m);
+ Object obj = Construct.ReflectionUtil.followFieldPath(totalSizePath,
m);
return ((Number) obj).intValue();
}
@@ -56,17 +57,18 @@
return a.length;
}
-
/**
- * Currently every parser that uses a total size field sets the total size
field.
- * This works, but is very redundant.
+ * Currently every parser that uses a total size field sets the total size
+ * field. This works, but is very redundant.
*/
@Override
public void patchSizeFields(Message m, int frameSize) {
- Object obj = Construct.followFieldPath(totalSizePath, m,
totalSizePath.size() - 1);
+ Object obj = ReflectionUtil.followFieldPath(totalSizePath, m,
+ totalSizePath.size() - 1);
Field f = totalSizePath.get(totalSizePath.size() - 1);
- Construct.setNumField(f, totalSizeFieldType, obj, frameSize);
+ ReflectionUtil.setNumField(obj, totalSizeFieldType, f,
+ frameSize);
}
}
Modified: gnunet-java/src/org/gnunet/construct/parsers/FillParser.java
===================================================================
--- gnunet-java/src/org/gnunet/construct/parsers/FillParser.java
2011-12-14 13:57:53 UTC (rev 18593)
+++ gnunet-java/src/org/gnunet/construct/parsers/FillParser.java
2011-12-15 07:31:39 UTC (rev 18594)
@@ -5,7 +5,7 @@
import java.util.List;
import org.gnunet.construct.Construct;
-import org.gnunet.construct.Construct.NumFieldType;
+import org.gnunet.construct.Construct.ReflectionUtil;
import org.gnunet.messages.Message;
/**
@@ -19,13 +19,13 @@
private List<Field> totalSizePath;
- private NumFieldType totalSizeFieldType;
+ private ReflectionUtil.NumFieldType totalSizeFieldType;
public FillParser(Parser p, List<Field> totalSizePath, Field field) {
super(field);
elemParser = p;
this.totalSizePath = totalSizePath;
- totalSizeFieldType = Construct.getNumFieldType(totalSizePath
+ totalSizeFieldType = ReflectionUtil.getNumFieldType(totalSizePath
.get(totalSizePath.size() - 1));
}
@@ -45,7 +45,7 @@
}
private int getSizeFieldValue(Message m) {
- Object obj = Construct.followFieldPath(totalSizePath, m);
+ Object obj = ReflectionUtil.followFieldPath(totalSizePath, m);
return ((Number) obj).intValue();
}
@@ -110,11 +110,11 @@
*/
@Override
public void patchSizeFields(Message m, int frameSize) {
- Object obj = Construct.followFieldPath(totalSizePath, m,
+ Object obj = ReflectionUtil.followFieldPath(totalSizePath, m,
totalSizePath.size() - 1);
Field f = totalSizePath.get(totalSizePath.size() - 1);
- Construct.setNumField(f, totalSizeFieldType, obj, frameSize);
+ ReflectionUtil.setNumField(obj, totalSizeFieldType, f, frameSize);
}
}
Modified: gnunet-java/src/org/gnunet/construct/parsers/IntegerParser.java
===================================================================
--- gnunet-java/src/org/gnunet/construct/parsers/IntegerParser.java
2011-12-14 13:57:53 UTC (rev 18593)
+++ gnunet-java/src/org/gnunet/construct/parsers/IntegerParser.java
2011-12-15 07:31:39 UTC (rev 18594)
@@ -1,29 +1,30 @@
package org.gnunet.construct.parsers;
import java.lang.reflect.Field;
-import java.math.BigInteger;
+import java.util.Arrays;
import org.gnunet.construct.Construct;
+import org.gnunet.construct.Construct.ReflectionUtil;
import org.gnunet.messages.Message;
public class IntegerParser extends FieldParser {
- private final int byteSize;
-
+
public static final boolean UNSIGNED = false;
public static final boolean SIGNED = true;
-
+
+ private final int byteSize;
+
private final boolean isSigned;
+ private ReflectionUtil.NumFieldType ft;
- private Construct.NumFieldType ft;
-
public IntegerParser(final int byteSize, final boolean isSigned,
final Field f) {
super(f);
this.byteSize = byteSize;
this.isSigned = isSigned;
-
- ft = Construct.getNumFieldType(f);
+
+ ft = ReflectionUtil.getNumFieldType(f);
}
@Override
@@ -32,31 +33,21 @@
}
@Override
- public int parse(final byte[] srcData, int offset, int frameOffset, final
Message dstObj) {
- byte[] numData;
-
- if (isSigned) {
- numData = new byte[byteSize];
- System.arraycopy(srcData, offset, numData, 0, byteSize);
- } else {
- numData = new byte[byteSize + 1];
- System.arraycopy(srcData, offset, numData, 1, byteSize);
- }
-
- offset += byteSize;
-
- final BigInteger num = new BigInteger(numData);
-
+ public int parse(final byte[] srcData, int offset, int frameOffset,
+ final Message dstObj) {
try {
switch (ft) {
+ case BYTE_PRIM:
+ field.setShort(dstObj, readShort(srcData, offset));
+ break;
case SHORT_PRIM:
- field.setShort(dstObj, num.shortValue());
+ field.setShort(dstObj, readShort(srcData, offset));
break;
case INT_PRIM:
- field.setInt(dstObj, num.intValue());
+ field.setInt(dstObj, readInt(srcData, offset));
break;
case LONG_PRIM:
- field.setLong(dstObj, num.longValue());
+ field.setLong(dstObj, readLong(srcData, offset));
break;
default:
throw new RuntimeException("invalid member type");
@@ -74,18 +65,20 @@
@Override
public int write(final byte[] dstData, final int offset,
final Message srcObj) {
- BigInteger num;
try {
switch (ft) {
+ case BYTE_PRIM:
+ writeInt(field.getInt(srcObj), dstData, offset);
+ break;
case INT_PRIM:
- num = BigInteger.valueOf(field.getInt(srcObj));
+ writeInt(field.getInt(srcObj), dstData, offset);
break;
case SHORT_PRIM:
- num = BigInteger.valueOf(field.getShort(srcObj));
+ writeShort(field.getShort(srcObj), dstData, offset);
break;
case LONG_PRIM:
- num = BigInteger.valueOf(field.getLong(srcObj));
+ writeLong(field.getLong(srcObj), dstData, offset);
break;
default:
throw new RuntimeException("invalid member type: ");
@@ -96,11 +89,6 @@
throw new RuntimeException();
}
- final byte[] numData = num.toByteArray();
-
- System.arraycopy(numData, 0, dstData, offset
- + (byteSize - numData.length), numData.length);
-
return byteSize;
}
@@ -108,9 +96,139 @@
public void patchSizeFields(Message m, int frameSize) {
return;
}
-
-
-
-
-
+
+ public void writeByte(byte val, byte[] data, int offset) {
+ // fill with zeroes if byteSize is too large for a byte
+ Arrays.fill(data, offset, offset + (byteSize - 1), (byte) 0);
+ data[offset + (byteSize - 1)] = val;
+ if (isSigned) {
+ byte sign = (byte) (val & 0x80);
+ // remove the sign bit sign
+ data[offset + (byteSize - 1)] &= ~sign;
+ // ... and put it in the right place (lowest byte)
+ data[offset] |= sign;
+ }
+ }
+
+ public void writeShort(short val, byte[] data, int offset) {
+ Arrays.fill(data, offset, offset + (byteSize - 1), (byte) 0);
+ data[offset + (byteSize - 1)] = (byte) (val & 0xFF);
+ if (byteSize >= 2) {
+ val >>= 8;
+ data[offset + (byteSize - 2)] = (byte) (val & 0xFF);
+ }
+ if (isSigned) {
+ byte sign = (byte) (val & 0x80);
+ // remove the sign bit sign
+ data[offset + (byteSize - 1)] &= ~sign;
+ // ... and put it in the right place (lowest byte)
+ data[offset] |= sign;
+ }
+ }
+
+ public void writeInt(int val, byte[] data, int offset) {
+ Arrays.fill(data, offset, offset + (byteSize - 1), (byte) 0);
+ data[offset + (byteSize - 1)] = (byte) (val & 0xFF);
+ for (int i = 2; i <= 4; ++i) {
+ if (byteSize - i < 0) {
+ break;
+ }
+ val >>= 8;
+ data[offset + (byteSize - i)] = (byte) (val & 0xFF);
+ }
+ if (isSigned) {
+ byte sign = (byte) (val & 0x80);
+ // remove the sign bit sign
+ data[offset + (byteSize - 1)] &= ~sign;
+ // ... and put it in the right place (lowest byte)
+ data[offset] |= sign;
+ }
+ }
+
+ public void writeLong(long val, byte[] data, int offset) {
+ Arrays.fill(data, offset, offset + (byteSize - 1), (byte) 0);
+ data[offset + (byteSize - 1)] = (byte) (val & 0xFF);
+ for (int i = 2; i <= 8; ++i) {
+ if (byteSize - i < 0) {
+ break;
+ }
+ val >>= 8;
+ data[offset + (byteSize - i)] = (byte) (val & 0xFF);
+ }
+ if (isSigned) {
+ byte sign = (byte) (val & 0x80);
+ // remove the sign bit sign
+ data[offset + (byteSize - 1)] &= ~sign;
+ // ... and put it in the right place (lowest byte)
+ data[offset] |= sign;
+ }
+ }
+
+ public byte readByte(byte[] data, int offset) {
+ byte val;
+ val = data[offset + (byteSize - 1)];
+ if (isSigned) {
+ // explicitly OR sign bit to the right place if the source buffer
is
+ // too large
+ byte sign = (byte) (data[offset] & 0x80);
+ val |= sign;
+ }
+ return val;
+ }
+
+ public short readShort(byte[] data, int offset) {
+ short val = 0;
+ // MSB is at lower address in NBO
+ if (byteSize - 2 >= 0) {
+ val = data[offset + (byteSize - 2)];
+ val <<= 8;
+ }
+ val |= data[offset + (byteSize - 1)];
+ if (isSigned) {
+ // explicitly OR sign bit to the right place if the source buffer
is
+ // too large
+ byte sign = (byte) (data[offset] & 0x80);
+ val |= sign;
+ }
+ return val;
+ }
+
+ public int readInt(byte[] data, int offset) {
+ int val = 0;
+ // MSB is at lower address in NBO
+ for (int i = 4; i >= 2; --i) {
+ if (byteSize - i >= 0) {
+ val = data[offset + (byteSize - i)];
+ val <<= 8;
+ }
+ }
+ val |= data[offset + (byteSize - 1)];
+ if (isSigned) {
+ // explicitly OR sign bit to the right place if the source buffer
is
+ // too large
+ byte sign = (byte) (data[offset] & 0x80);
+ val |= sign;
+ }
+ return val;
+ }
+
+ public long readLong(byte[] data, int offset) {
+ long val = 0;
+ // MSB is at lower address in NBO
+ for (int i = 8; i >= 2; --i) {
+ if (byteSize - i >= 0) {
+ val = data[offset + (byteSize - i)];
+ val <<= 8;
+ }
+ }
+ val |= data[offset + (byteSize - 1)];
+ if (isSigned) {
+ // explicitly OR sign bit to the right place if the source buffer
is
+ // too large
+ byte sign = (byte) (data[offset] & 0x80);
+ val |= sign;
+ }
+ return val;
+ }
+
}
Modified: gnunet-java/src/org/gnunet/construct/parsers/NestedParser.java
===================================================================
--- gnunet-java/src/org/gnunet/construct/parsers/NestedParser.java
2011-12-14 13:57:53 UTC (rev 18593)
+++ gnunet-java/src/org/gnunet/construct/parsers/NestedParser.java
2011-12-15 07:31:39 UTC (rev 18594)
@@ -4,6 +4,7 @@
import org.gnunet.messages.Message;
+
public class NestedParser extends FieldParser {
private final Parser nestedParser;
Modified:
gnunet-java/src/org/gnunet/construct/parsers/VariableSizeArrayParser.java
===================================================================
--- gnunet-java/src/org/gnunet/construct/parsers/VariableSizeArrayParser.java
2011-12-14 13:57:53 UTC (rev 18593)
+++ gnunet-java/src/org/gnunet/construct/parsers/VariableSizeArrayParser.java
2011-12-15 07:31:39 UTC (rev 18594)
@@ -4,20 +4,20 @@
import java.lang.reflect.Field;
import org.gnunet.construct.Construct;
-import org.gnunet.construct.Construct.NumFieldType;
+import org.gnunet.construct.Construct.ReflectionUtil;
import org.gnunet.messages.Message;
public class VariableSizeArrayParser extends FieldParser {
private final FieldParser elemParser;
private Field sizeField;
- private NumFieldType ft;
+ private ReflectionUtil.NumFieldType ft;
public VariableSizeArrayParser(final FieldParser elemParser, Field
sizeField, Field arrayField) {
super(arrayField);
this.elemParser = elemParser;
this.sizeField = sizeField;
- this.ft = Construct.getNumFieldType(sizeField);
+ this.ft = ReflectionUtil.getNumFieldType(sizeField);
}
@Override
@@ -86,7 +86,7 @@
@Override
public void patchSizeFields(Message m, int frameSize) {
int size = Array.getLength(getFieldValue(m));
- Construct.setNumField(sizeField, ft, m, size);
+ ReflectionUtil.setNumField(m, ft, sizeField, size);
}
}
Added: gnunet-java/src/org/gnunet/exceptions/InterfaceViolationException.java
===================================================================
--- gnunet-java/src/org/gnunet/exceptions/InterfaceViolationException.java
(rev 0)
+++ gnunet-java/src/org/gnunet/exceptions/InterfaceViolationException.java
2011-12-15 07:31:39 UTC (rev 18594)
@@ -0,0 +1,16 @@
+package org.gnunet.exceptions;
+
+
+/**
+ * Thrown if an annotation or method is used incorrectly.
+ *
+ * @author Florian Dold
+ *
+ */
address@hidden("serial")
+public class InterfaceViolationException extends RuntimeException {
+
+ public InterfaceViolationException(String string) {
+ super(string);
+ }
+}
Added: gnunet-java/src/org/gnunet/exceptions/InternalLogicError.java
===================================================================
--- gnunet-java/src/org/gnunet/exceptions/InternalLogicError.java
(rev 0)
+++ gnunet-java/src/org/gnunet/exceptions/InternalLogicError.java
2011-12-15 07:31:39 UTC (rev 18594)
@@ -0,0 +1,13 @@
+package org.gnunet.exceptions;
+
+/**
+ * Thrown if something happens that is clearly the fault of the gnunet-java
+ * authors.
+ *
+ * @author Florian Dold
+ *
+ */
address@hidden("serial")
+public class InternalLogicError extends Error {
+
+}
Added: gnunet-java/src/org/gnunet/exceptions/MessageFormatException.java
===================================================================
--- gnunet-java/src/org/gnunet/exceptions/MessageFormatException.java
(rev 0)
+++ gnunet-java/src/org/gnunet/exceptions/MessageFormatException.java
2011-12-15 07:31:39 UTC (rev 18594)
@@ -0,0 +1,12 @@
+package org.gnunet.exceptions;
+
+
+/**
+ * Thrown when a received message is invalid
+ *
+ * @author Florian Dold
+ *
+ */
+public class MessageFormatException extends RuntimeException {
+
+}
Modified: gnunet-java/src/org/gnunet/messages/QueryMessage.java
===================================================================
--- gnunet-java/src/org/gnunet/messages/QueryMessage.java 2011-12-14
13:57:53 UTC (rev 18593)
+++ gnunet-java/src/org/gnunet/messages/QueryMessage.java 2011-12-15
07:31:39 UTC (rev 18594)
@@ -1,9 +1,12 @@
package org.gnunet.messages;
import org.gnunet.construct.ByteFill;
+import org.gnunet.construct.MessageId;
import org.gnunet.construct.Nested;
import org.gnunet.construct.UInt8;
+
address@hidden
public class QueryMessage implements Message {
@Nested
Copied: gnunet-java/src/org/gnunet/messages/RelativeTimeMessage.java (from rev
18411, gnunet-java/src/org/gnunet/messages/RelativeTimeNBO.java)
===================================================================
--- gnunet-java/src/org/gnunet/messages/RelativeTimeMessage.java
(rev 0)
+++ gnunet-java/src/org/gnunet/messages/RelativeTimeMessage.java
2011-12-15 07:31:39 UTC (rev 18594)
@@ -0,0 +1,27 @@
+package org.gnunet.messages;
+
+import org.gnunet.construct.UInt8;
+import org.gnunet.util.RelativeTime;
+
+public class RelativeTimeMessage implements Message {
+
+ /**
+ * Value__ still in Java-byte order, needs to be converted to Network byte
+ * order by the Construct class.
+ */
+ @UInt8
+ public long value__;
+
+ public RelativeTimeMessage(final long value) {
+ this.value__ = value;
+ }
+
+ public RelativeTimeMessage(final RelativeTime t) {
+ if (t.equals(RelativeTime.FOREVER)) {
+ this.value__ = -1;
+ } else {
+ this.value__ = t.getMilliseconds();
+ }
+ }
+
+}
Deleted: gnunet-java/src/org/gnunet/messages/RelativeTimeNBO.java
===================================================================
--- gnunet-java/src/org/gnunet/messages/RelativeTimeNBO.java 2011-12-14
13:57:53 UTC (rev 18593)
+++ gnunet-java/src/org/gnunet/messages/RelativeTimeNBO.java 2011-12-15
07:31:39 UTC (rev 18594)
@@ -1,27 +0,0 @@
-package org.gnunet.messages;
-
-import org.gnunet.construct.UInt8;
-import org.gnunet.util.RelativeTime;
-
-public class RelativeTimeNBO implements Message {
-
- /**
- * Value__ still in Java-byte order, needs to be converted to Network byte
- * order by the Construct class.
- */
- @UInt8
- public long value__;
-
- public RelativeTimeNBO(final long value) {
- this.value__ = value;
- }
-
- public RelativeTimeNBO(final RelativeTime t) {
- if (t.equals(RelativeTime.FOREVER)) {
- this.value__ = -1;
- } else {
- this.value__ = t.getMilliseconds();
- }
- }
-
-}
Modified: gnunet-java/src/org/gnunet/service/StatisticsService.java
===================================================================
--- gnunet-java/src/org/gnunet/service/StatisticsService.java 2011-12-14
13:57:53 UTC (rev 18593)
+++ gnunet-java/src/org/gnunet/service/StatisticsService.java 2011-12-15
07:31:39 UTC (rev 18594)
@@ -106,8 +106,8 @@
// XXX: What about reconnect stuff?
currentAction = actions.removeFirst();
// XXX: what about destruction?
- this.th = client.notifyTransmitReady(currentAction.msize, /* XXX:dummy
*/
- null, true, /* XXX:dummy */null);
+ //this.th = client.notifyTransmitReady(currentAction.msize, /*
XXX:dummy */
+ // null, true, /* XXX:dummy */null);
// XXX totally incomplete
}
Modified: gnunet-java/src/org/gnunet/util/Client.java
===================================================================
--- gnunet-java/src/org/gnunet/util/Client.java 2011-12-14 13:57:53 UTC (rev
18593)
+++ gnunet-java/src/org/gnunet/util/Client.java 2011-12-15 07:31:39 UTC (rev
18594)
@@ -17,12 +17,12 @@
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
-
package org.gnunet.util;
import java.nio.channels.SocketChannel;
+import java.nio.channels.spi.SelectorProvider;
-import org.gnunet.messages.MessageHeader;
+import org.gnunet.messages.Message;
import org.gnunet.util.Scheduler.Task;
/**
@@ -38,6 +38,12 @@
}
}
+ public class Transmitter {
+ public void transmit(Message m) {
+
+ }
+ }
+
/**
* Wait until the service is running.
*
@@ -67,20 +73,20 @@
* configuration to use
*/
public Client(final String serviceName, final Configuration cfg) {
-
+
// XXX: catch exn
- long port = cfg.getValueNumer(serviceName, "PORT");
+ long port = cfg.getValueNumer(serviceName, "PORT");
if (port > 65535 || port <= 0) {
// XXX: throw something else
throw new RuntimeException("invalid port");
}
String hostname = cfg.getValueString(serviceName, "HOSTNAME");
+ SocketChannel chan = SelectorProvider.provider().openSocketChannel();
+
// [...]
-
-
}
-
+
/**
* Read from the service.
*
@@ -89,11 +95,16 @@
* @param timeout
* how long to wait until timing out
*/
-
public void receive(final MessageHandler handler, final RelativeTime
timeout) {
throw new UnsupportedOperationException();
}
-
- // public TransmitHandle transmit(Message m, RelativeTime timeout, boolean
autoRetry, )
-
+
+ public TransmitHandle notifyTransmitReady(int size, RelativeTime timeout,
+ boolean autoRetry, TransmitHandler cb) {
+ Scheduler.addWrite(timeout, chan, t);
+ }
+
+ // public TransmitHandle transmit(Message m, RelativeTime timeout, boolean
+ // autoRetry, )
+
}
Modified: gnunet-java/src/org/gnunet/util/MessageHandler.java
===================================================================
--- gnunet-java/src/org/gnunet/util/MessageHandler.java 2011-12-14 13:57:53 UTC
(rev 18593)
+++ gnunet-java/src/org/gnunet/util/MessageHandler.java 2011-12-15 07:31:39 UTC
(rev 18594)
@@ -22,6 +22,7 @@
import org.gnunet.messages.Message;
import org.grothoff.Runabout;
+import org.grothoff.Runabout;
/**
*
@@ -29,17 +30,14 @@
*
*/
public abstract class MessageHandler extends Runabout {
-
- public void process (Message m)
- {
- this.visitAppropriate(m);
- }
-
+
/**
* Method to call when we receive a message from the service.
*
* @param msg
* message received, NULL on timeout or fatal error
*/
- // public void visit(MyMessage msg);
+ public void process(Message m) {
+ this.visitAppropriate(m);
+ }
}
Modified: gnunet-java/src/org/gnunet/util/RelativeTime.java
===================================================================
--- gnunet-java/src/org/gnunet/util/RelativeTime.java 2011-12-14 13:57:53 UTC
(rev 18593)
+++ gnunet-java/src/org/gnunet/util/RelativeTime.java 2011-12-15 07:31:39 UTC
(rev 18594)
@@ -20,7 +20,7 @@
package org.gnunet.util;
-import org.gnunet.messages.RelativeTimeNBO;
+import org.gnunet.messages.RelativeTimeMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -162,12 +162,12 @@
return AbsoluteTime.now().add(this);
}
- public RelativeTimeNBO toNBO() {
+ public RelativeTimeMessage toNBO() {
long rval = this.rel_value;
assert rval >= 0;
if (rval == FOREVER.rel_value) {
rval = -1L; /* 0xFFFFFFFFFFFFFFFF for network format! */
}
- return new RelativeTimeNBO(rval);
+ return new RelativeTimeMessage(rval);
}
}
Modified: gnunet-java/src/org/gnunet/util/Scheduler.java
===================================================================
--- gnunet-java/src/org/gnunet/util/Scheduler.java 2011-12-14 13:57:53 UTC
(rev 18593)
+++ gnunet-java/src/org/gnunet/util/Scheduler.java 2011-12-15 07:31:39 UTC
(rev 18594)
@@ -28,6 +28,7 @@
import java.nio.channels.Selector;
import java.nio.channels.spi.SelectorProvider;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.EnumSet;
import java.util.LinkedList;
import java.util.List;
@@ -234,6 +235,12 @@
}
+ public static TaskIdentifier addWrite(RelativeTime timeout,
+ SelectableChannel chan, Task t) {
+ return Scheduler.addSelect(Priority.KEEP, null, timeout,
+ Collections.singleton(chan), Collections.EMPTY_SET, t);
+ }
+
public static TaskIdentifier addWithPriority(final Priority prio,
final Task t) {
return addSelect(prio, null, RelativeTime.ZERO, null, null, t);
Added: gnunet-java/src/org/gnunet/util/TransmitHandler.java
===================================================================
--- gnunet-java/src/org/gnunet/util/TransmitHandler.java
(rev 0)
+++ gnunet-java/src/org/gnunet/util/TransmitHandler.java 2011-12-15
07:31:39 UTC (rev 18594)
@@ -0,0 +1,6 @@
+package org.gnunet.util;
+
+public interface TransmitHandler {
+ public void handle(Client.Transmitter t);
+
+}
Modified: gnunet-java/test/org/gnunet/construct/ConstructTest.java
===================================================================
--- gnunet-java/test/org/gnunet/construct/ConstructTest.java 2011-12-14
13:57:53 UTC (rev 18593)
+++ gnunet-java/test/org/gnunet/construct/ConstructTest.java 2011-12-15
07:31:39 UTC (rev 18594)
@@ -13,28 +13,29 @@
import org.junit.Test;
public class ConstructTest {
-
+
+
@Test
public void test_VarTestMessage() {
VarTestMessage vtm = new VarTestMessage();
vtm.length = 5;
vtm.msgs = new SimpleTestMessage2[5];
- for (int i=0; i < 5; ++i) {
+ for (int i = 0; i < 5; ++i) {
vtm.msgs[i] = new SimpleTestMessage2();
vtm.msgs[i].value = i;
}
-
+
byte[] a = Construct.toBinary(vtm);
-
+
VarTestMessage vtm2 = Construct.parseAs(a, 0, VarTestMessage.class);
-
- Assert.assertEquals(vtm.length, vtm2.length);
- for (int i=0; i < 5; ++i) {
- Assert.assertEquals(vtm.msgs[i].value, vtm2.msgs[i].value);
+
+ Assert.assertEquals(vtm2.length, 5);
+ for (int i = 0; i < 5; ++i) {
+ Assert.assertEquals(vtm2.msgs[i].value, i);
}
-
+
}
-
+
@Test
public void test_SimpleTestMessage() {
SimpleTestMessage stm = new SimpleTestMessage();
@@ -47,38 +48,38 @@
stm.mns[i] = new SimpleTestMessage2();
stm.mns[i].value = i;
}
-
+
byte[] a = Construct.toBinary(stm);
- SimpleTestMessage stm2 = Construct.parseAs(a, 0,
SimpleTestMessage.class);
-
+ SimpleTestMessage stm2 = Construct.parseAs(a, 0,
+ SimpleTestMessage.class);
+
Assert.assertEquals(stm.v1, stm2.v1);
Assert.assertEquals(stm.v2, stm2.v2);
Assert.assertEquals(stm.mn.value, stm2.mn.value);
Assert.assertEquals(stm.mns.length, stm2.mns.length);
-
+
for (int i = 0; i < stm.mns.length; i++) {
Assert.assertEquals(stm.mns[i].value, stm2.mns[i].value);
-
+
}
-
-
+
}
-
+
public static String toHex(byte[] bytes) {
BigInteger bi = new BigInteger(1, bytes);
return String.format("%0" + (bytes.length << 1) + "X", bi);
}
-
+
@Test
public void test_StringMessage() {
StringMessage strm = new StringMessage();
strm.num = 58;
strm.str = "ab";
strm.num2 = 80;
-
+
byte[] a = Construct.toBinary(strm);
StringMessage strm2 = Construct.parseAs(a, 0, StringMessage.class);
-
+
for (byte b : a) {
System.out.print((char) b);
}
@@ -86,72 +87,65 @@
Assert.assertEquals(strm.num, strm2.num);
Assert.assertEquals(strm.num2, strm2.num2);
Assert.assertEquals(strm.str, strm2.str);
-
+
}
-
@Test
public void test_QueryMessage() {
QueryMessage qm = new QueryMessage();
qm.header = new MessageHeader();
qm.header.type = 0x42;
qm.query = 0x43;
- qm.varsize = new byte[]{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
-
-
+ qm.varsize = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
+ 14, 15 };
+
Construct.patchSizeFields(qm);
-
-
+
byte[] a = Construct.toBinary(qm);
-
-
+
QueryMessage qm2 = Construct.parseAs(a, 0, QueryMessage.class);
-
-
+
Assert.assertEquals(qm.header.size, qm2.header.size);
Assert.assertEquals(qm.header.type, qm2.header.type);
Assert.assertEquals(qm.query, qm2.query);
-
+
Assert.assertArrayEquals(qm.varsize, qm2.varsize);
}
-
-
+
@Test
public void test_SizeTestMessage() {
SizeTestMessage stm = new SizeTestMessage();
stm.someValue = 42;
- stm.rest = new byte[]{1,2,3,4,5};
-
+ stm.rest = new byte[] { 1, 2, 3, 4, 5 };
+
Construct.patchSizeFields(stm);
-
-
+
byte[] a = Construct.toBinary(stm);
-
+
SizeTestMessage stm2 = Construct.parseAs(a, 0, SizeTestMessage.class);
-
+
Assert.assertEquals(stm.someValue, stm2.someValue);
Assert.assertEquals(stm.totalSize, stm2.totalSize);
-
-
- Assert.assertArrayEquals(stm.rest, stm2.rest);
+
+ Assert.assertArrayEquals(stm.rest, stm2.rest);
}
-
+
@Test
public void test_MessageHeader() {
MessageHeader h1 = new MessageHeader();
-
+
h1.size = 42;
h1.type = 52;
-
+
byte[] a = Construct.toBinary(h1);
-
+
MessageHeader h2 = Construct.parseAs(a, 0, MessageHeader.class);
-
+
byte[] b = Construct.toBinary(h2);
-
+
Assert.assertArrayEquals(a, b);
-
+
Assert.assertEquals(h1.size, h2.size);
Assert.assertEquals(h1.type, h2.type);
}
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [GNUnet-SVN] r18594 - in gnunet-java: . .externalToolBuilders .settings src/org/gnunet src/org/gnunet/construct src/org/gnunet/construct/parsers src/org/gnunet/exceptions src/org/gnunet/messages src/org/gnunet/service src/org/gnunet/util test/org/gnunet/construct,
gnunet <=