aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2012-04-11 15:15:08 +0000
committerFlorian Dold <florian.dold@gmail.com>2012-04-11 15:15:08 +0000
commitc416ef96040c6f23f07d1ed25e3efc79b29e2d7e (patch)
tree2d1a843f14c8dd05614a18f50fd3c82407444625
parent22cb24c1950c719790e0e42ea434f0465d68f27a (diff)
downloadgnunet-java-c416ef96040c6f23f07d1ed25e3efc79b29e2d7e.tar.gz
gnunet-java-c416ef96040c6f23f07d1ed25e3efc79b29e2d7e.zip
more parts of Core implemented, still major problems
-rw-r--r--.classpath3
-rw-r--r--ISSUES30
-rw-r--r--gnunet-java.eml3
-rw-r--r--lib/commons-io-2.2.jarbin0 -> 173587 bytes
-rw-r--r--src/org/gnunet/construct/Construct.java20
-rw-r--r--src/org/gnunet/construct/IntegerFill.java13
-rw-r--r--src/org/gnunet/construct/MsgMap.txt13
-rw-r--r--src/org/gnunet/construct/parsers/FillIntegerParser.java48
-rw-r--r--src/org/gnunet/construct/parsers/IntegerFillParser.java70
-rw-r--r--src/org/gnunet/core/Core.java296
-rw-r--r--src/org/gnunet/dht/DistributedHashTable.java2
-rw-r--r--src/org/gnunet/nse/NetworkSizeEstimation.java8
-rw-r--r--src/org/gnunet/statistics/Statistics.java2
-rw-r--r--src/org/gnunet/util/Client.java8
-rw-r--r--src/org/gnunet/util/MessageReceiver.java2
-rw-r--r--src/org/gnunet/util/PeerIdentity.java11
-rw-r--r--src/org/gnunet/util/Resolver.java6
-rw-r--r--src/org/gnunet/util/RunaboutMessageReceiver.java3
-rw-r--r--src/org/gnunet/util/Server.java14
-rw-r--r--src/org/gnunet/util/Strings.java32
-rw-r--r--test/org/gnunet/construct/ByteFillMessage.java2
-rw-r--r--test/org/gnunet/construct/ConstructTest.java10
-rw-r--r--test/org/gnunet/construct/QueryMessage.java2
23 files changed, 478 insertions, 120 deletions
diff --git a/.classpath b/.classpath
index 48b3153..cc831fb 100644
--- a/.classpath
+++ b/.classpath
@@ -11,6 +11,7 @@
11 <classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/commons-io-2.1"/> 11 <classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/commons-io-2.1"/>
12 <classpathentry kind="lib" path="lib/log4j-1.2.16.jar"/> 12 <classpathentry kind="lib" path="lib/log4j-1.2.16.jar"/>
13 <classpathentry kind="lib" path="lib/slf4j-log4j12-1.6.4.jar"/> 13 <classpathentry kind="lib" path="lib/slf4j-log4j12-1.6.4.jar"/>
14 <classpathentry kind="lib" path="lib/commons-io-2.1.jar"/> 14 <classpathentry kind="lib" path="/home/dold/gnunet-java/lib/commons-io-2.1.jar"/>
15 <classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/commons-io-2.2"/>
15 <classpathentry kind="output" path="build"/> 16 <classpathentry kind="output" path="build"/>
16</classpath> 17</classpath>
diff --git a/ISSUES b/ISSUES
index 4506d97..f68ad51 100644
--- a/ISSUES
+++ b/ISSUES
@@ -347,12 +347,27 @@ public interface GnunetMessageBody extends MessageUnion { }
347 347
348======================================================================================== 348========================================================================================
349 349
350* why is installing gnunet so compilicated? 350* client_api.c
351 * e.g. stuff like "base of XYZ installation" in ./configure is very confusing 351 * pr = GNUNET_CONTAINER_multihashmap_get (handle->peers, &target->hashPubKey);
352 * many dependencies with out-of-date packages on common distros 352 * the above line crashes when target==NULL, but comment indicates that NULL is valid for target
353
354* ...NOTIFY_PRE_CONNECT.. message type not used
355
356* what about allocation of message IDs for extesions / what about clashes?
353 357
358core:
359* we seem to get all incoming messages if the handlers array is empty (even if we don't request anything)
360* being notified about outbound messages does not seem to work at all
354 361
355 362
363* how does the Hash ascii-encoding work?
364* => solved, ported C-code, encountered some problems because of the lack of unsigned in java
365 (had to use long instead of int in java code because of sign)
366* first implementation got some characters wrong, had to use
367 byte b = data[(int) rpos++];
368 int s = b >= 0 ? b : (256 + b);
369 to compensate for the signed-ness of byte
370* is the current implementation correct now? (it has the same output as peerinfo)
356 371
357 372
358@Fill 373@Fill
@@ -386,13 +401,16 @@ int[] foo;
386 * also GNUNET_CORE_OPTION_SEND_STATUS_CHANGE 401 * also GNUNET_CORE_OPTION_SEND_STATUS_CHANGE
387 * inconsistency in structs with trailing ATS information (NotifyTrafficMessage vs ConnectNotifyMessage) 402 * inconsistency in structs with trailing ATS information (NotifyTrafficMessage vs ConnectNotifyMessage)
388 * why zero-termination *and* size field? 403 * why zero-termination *and* size field?
404 * what does zero-termination really mean? is it only one byte? a whole ats entry?
389 * what happens when we can't connect to core / lose connection? in the c api implementation, this is never handeled? 405 * what happens when we can't connect to core / lose connection? in the c api implementation, this is never handeled?
390 * why is there even the init callback if init can't fail? 406 * why is there even the init callback if init can't fail?
391 407
392 408
393* GNUNET_SERVER_connect_socket: ????? 409* GNUNET_SERVER_connect_socket: what is it for?
394* disabling cork in java? is it possible? 410* how to disable corking in java? is corking the same as TCP_NODELAY?
395 411
396 412
397* what about allocation of message IDs for extesions / what about clashes? 413* why is installing gnunet so compilicated?
414 * e.g. stuff like "base of XYZ installation" in ./configure is very confusing
415 * many dependencies with out-of-date packages on common distros
398 416
diff --git a/gnunet-java.eml b/gnunet-java.eml
index 0fb6fee..8f5fb7b 100644
--- a/gnunet-java.eml
+++ b/gnunet-java.eml
@@ -13,7 +13,4 @@
13 <lib name="slf4j-log4j12-1.6.4.jar" scope="COMPILE"> 13 <lib name="slf4j-log4j12-1.6.4.jar" scope="COMPILE">
14 <relative-module-cls project-related="jar://$PROJECT_DIR$/lib/slf4j-log4j12-1.6.4.jar!/"/> 14 <relative-module-cls project-related="jar://$PROJECT_DIR$/lib/slf4j-log4j12-1.6.4.jar!/"/>
15 </lib> 15 </lib>
16 <lib name="commons-io-2.1.jar" scope="COMPILE">
17 <relative-module-cls project-related="jar://$PROJECT_DIR$/lib/commons-io-2.1.jar!/"/>
18 </lib>
19</component> 16</component>
diff --git a/lib/commons-io-2.2.jar b/lib/commons-io-2.2.jar
new file mode 100644
index 0000000..84ca565
--- /dev/null
+++ b/lib/commons-io-2.2.jar
Binary files differ
diff --git a/src/org/gnunet/construct/Construct.java b/src/org/gnunet/construct/Construct.java
index 3848e49..1b00922 100644
--- a/src/org/gnunet/construct/Construct.java
+++ b/src/org/gnunet/construct/Construct.java
@@ -229,6 +229,10 @@ public class Construct {
229 parser = new StringParser(zts.charset(), zts.optional(), field); 229 parser = new StringParser(zts.charset(), zts.optional(), field);
230 } 230 }
231 231
232 public void visit(IntegerFill i) {
233 parser = new IntegerFillParser(frameSizePath, field, i.signed(), i.bitSize()/8);
234 }
235
232 public void visit(NestedMessage n) { 236 public void visit(NestedMessage n) {
233 237
234 Class<? extends Message> ct; 238 Class<? extends Message> ct;
@@ -287,16 +291,26 @@ public class Construct {
287 parser = new DoubleParser(field); 291 parser = new DoubleParser(field);
288 } 292 }
289 293
294 public void visit(FillWith fw) {
295 Field f = field;
296 Class old_c = c;
297
298 Parser p = getParser((Class<? extends Message>) field.getType()
299 .getComponentType(), this);
300
301 parser = new FillParser(p, frameSizePath ,f);
302 }
303
290 public void visit(VariableSizeArray vsa) { 304 public void visit(VariableSizeArray vsa) {
291 Field f = field; 305 Field f = field;
292 Class old_c = c; 306 Class old_c = c;
293 307
294 //noinspection unchecked 308
295 getParser((Class<? extends Message>) field.getType() 309 Parser p = getParser((Class<? extends Message>) field.getType()
296 .getComponentType(), this); 310 .getComponentType(), this);
297 311
298 try { 312 try {
299 parser = new VariableSizeArrayParser(parser, old_c.getField(vsa 313 parser = new VariableSizeArrayParser(p, old_c.getField(vsa
300 .lengthField()), f); 314 .lengthField()), f);
301 315
302 } catch (SecurityException e) { 316 } catch (SecurityException e) {
diff --git a/src/org/gnunet/construct/IntegerFill.java b/src/org/gnunet/construct/IntegerFill.java
new file mode 100644
index 0000000..6976820
--- /dev/null
+++ b/src/org/gnunet/construct/IntegerFill.java
@@ -0,0 +1,13 @@
1package org.gnunet.construct;
2
3import java.lang.annotation.ElementType;
4import java.lang.annotation.Retention;
5import java.lang.annotation.RetentionPolicy;
6import java.lang.annotation.Target;
7
8@Retention(RetentionPolicy.RUNTIME)
9@Target(ElementType.FIELD)
10public @interface IntegerFill {
11 boolean signed();
12 int bitSize();
13}
diff --git a/src/org/gnunet/construct/MsgMap.txt b/src/org/gnunet/construct/MsgMap.txt
index f4da714..125cc2e 100644
--- a/src/org/gnunet/construct/MsgMap.txt
+++ b/src/org/gnunet/construct/MsgMap.txt
@@ -2,16 +2,25 @@ org.gnunet.construct.UnionTest$TestUnion|0=org.gnunet.construct.UnionTest$TestUn
2org.gnunet.construct.UnionTest$TestUnion|1=org.gnunet.construct.UnionTest$TestUnionCase1 2org.gnunet.construct.UnionTest$TestUnion|1=org.gnunet.construct.UnionTest$TestUnionCase1
3org.gnunet.util.Resolver$Address|0=org.gnunet.util.Resolver$TextualAddress 3org.gnunet.util.Resolver$Address|0=org.gnunet.util.Resolver$TextualAddress
4org.gnunet.util.Resolver$Address|1=org.gnunet.util.Resolver$NumericAddress 4org.gnunet.util.Resolver$Address|1=org.gnunet.util.Resolver$NumericAddress
5org.gnunet.util.GnunetMessage$Body|68=org.gnunet.core.Core$DisconnectNotifyMessage
5org.gnunet.util.GnunetMessage$Body|171=org.gnunet.statistics.Statistics$ResponseEndMessage 6org.gnunet.util.GnunetMessage$Body|171=org.gnunet.statistics.Statistics$ResponseEndMessage
6org.gnunet.util.GnunetMessage$Body|170=org.gnunet.statistics.Statistics$ResponseValueMessage 7org.gnunet.util.GnunetMessage$Body|170=org.gnunet.statistics.Statistics$ResponseValueMessage
8org.gnunet.util.GnunetMessage$Body|70=org.gnunet.core.Core$NotifyInboundTrafficMessage
7org.gnunet.util.GnunetMessage$Body|169=org.gnunet.statistics.Statistics$RequestMessage 9org.gnunet.util.GnunetMessage$Body|169=org.gnunet.statistics.Statistics$RequestMessage
10org.gnunet.util.GnunetMessage$Body|71=org.gnunet.core.Core$NotifyOutboundTrafficMessage
8org.gnunet.util.GnunetMessage$Body|168=org.gnunet.statistics.Statistics$SetMessage 11org.gnunet.util.GnunetMessage$Body|168=org.gnunet.statistics.Statistics$SetMessage
9org.gnunet.util.GnunetMessage$Body|4=org.gnunet.util.Resolver$GetMessage 12org.gnunet.util.GnunetMessage$Body|4=org.gnunet.util.Resolver$GetMessage
10org.gnunet.util.GnunetMessage$Body|323=org.gnunet.nse.NetworkSizeEstimation$UpdateMessage 13org.gnunet.util.GnunetMessage$Body|64=org.gnunet.core.Core$InitMessage
14org.gnunet.util.GnunetMessage$Body|65=org.gnunet.core.Core$InitReplyMessage
11org.gnunet.util.GnunetMessage$Body|5=org.gnunet.util.Resolver$ResolverResponse 15org.gnunet.util.GnunetMessage$Body|5=org.gnunet.util.Resolver$ResolverResponse
12org.gnunet.util.GnunetMessage$Body|143=org.gnunet.dht.DistributedHashTable$DHTClientGetMessage 16org.gnunet.util.GnunetMessage$Body|143=org.gnunet.dht.DistributedHashTable$DHTClientGetMessage
17org.gnunet.util.GnunetMessage$Body|67=org.gnunet.core.Core$ConnectNotifyMessage
13org.gnunet.util.GnunetMessage$Body|142=org.gnunet.dht.DistributedHashTable$DHTClientPutMessage 18org.gnunet.util.GnunetMessage$Body|142=org.gnunet.dht.DistributedHashTable$DHTClientPutMessage
19org.gnunet.util.GnunetMessage$Body|76=org.gnunet.core.Core$SendMessage
20org.gnunet.util.GnunetMessage$Body|74=org.gnunet.core.Core$SendMessageRequest
21org.gnunet.util.GnunetMessage$Body|75=org.gnunet.core.Core$SendMessageReady
22org.gnunet.util.GnunetMessage$Body|323=org.gnunet.nse.NetworkSizeEstimation$UpdateMessage
14org.gnunet.util.GnunetMessage$Body|321=org.gnunet.nse.NetworkSizeEstimation$StartMessage 23org.gnunet.util.GnunetMessage$Body|321=org.gnunet.nse.NetworkSizeEstimation$StartMessage
15org.gnunet.util.GnunetMessage$Body|144=org.gnunet.dht.DistributedHashTable$DHTClientGetStopMessage 24org.gnunet.util.GnunetMessage$Body|144=org.gnunet.dht.DistributedHashTable$DHTClientGetStopMessage
16org.gnunet.util.GnunetMessage$Body|145=org.gnunet.dht.DistributedHashTable$DHTClientResultMessage 25org.gnunet.util.GnunetMessage$Body|145=org.gnunet.dht.DistributedHashTable$DHTClientResultMessage
17# generated 2012/03/28 21:13:11 26# generated 2012/04/11 15:38:56
diff --git a/src/org/gnunet/construct/parsers/FillIntegerParser.java b/src/org/gnunet/construct/parsers/FillIntegerParser.java
deleted file mode 100644
index ea8990b..0000000
--- a/src/org/gnunet/construct/parsers/FillIntegerParser.java
+++ /dev/null
@@ -1,48 +0,0 @@
1package org.gnunet.construct.parsers;
2
3
4import org.gnunet.construct.Message;
5import org.gnunet.construct.ReflectUtil;
6
7import java.lang.reflect.Array;
8import java.lang.reflect.Field;
9import java.nio.ByteBuffer;
10import java.util.List;
11
12public class FillIntegerParser implements Parser {
13 private final Field targetField;
14 private final boolean isSigned;
15 private final int byteSize;
16
17 private final List<Field> totalSizePath;
18 private final ReflectUtil.NumField totalSizeField;
19
20 public FillIntegerParser(Field targetField, List<Field> totalSizePath, boolean isSigned, int byteSize) {
21 this.targetField = targetField;
22 this.isSigned = isSigned;
23 this.byteSize = byteSize;
24
25 this.totalSizePath = totalSizePath;
26 totalSizeField = new ReflectUtil.NumField(totalSizePath.get(totalSizePath.size() - 1));
27 }
28
29 @Override
30 public int getSize(Message srcObj) {
31 return byteSize * Array.getLength(ReflectUtil.justGet(srcObj, targetField));
32 }
33
34 @Override
35 public int parse(ByteBuffer srcBuf, int frameStart, Message frameObj, Message dstObj) {
36 return 0;
37 }
38
39 @Override
40 public int write(ByteBuffer dstBuf, Message srcObj) {
41 return 0;
42 }
43
44 @Override
45 public void patch(Message m, int frameSize, Message frameObj) {
46 totalSizeField.set(ReflectUtil.followFieldPathToParent(totalSizePath, m), frameSize);
47 }
48}
diff --git a/src/org/gnunet/construct/parsers/IntegerFillParser.java b/src/org/gnunet/construct/parsers/IntegerFillParser.java
new file mode 100644
index 0000000..587155c
--- /dev/null
+++ b/src/org/gnunet/construct/parsers/IntegerFillParser.java
@@ -0,0 +1,70 @@
1package org.gnunet.construct.parsers;
2
3import org.gnunet.construct.Message;
4import org.gnunet.construct.ReflectUtil;
5
6import java.lang.reflect.Array;
7import java.lang.reflect.Field;
8import java.nio.ByteBuffer;
9import java.util.List;
10
11public class IntegerFillParser implements Parser {
12
13 private final List<Field> totalSizePath;
14 private final ReflectUtil.NumField totalSizeField;
15 private final Field targetField;
16 private final boolean signed;
17 private final int byteSize;
18
19 public IntegerFillParser(List<Field> totalSizePath, Field targetField,
20 boolean signed, int byteSize ) {
21
22 this.totalSizePath = totalSizePath;
23 this.totalSizeField = new ReflectUtil.NumField(totalSizePath.get(totalSizePath.size() - 1));
24 this.targetField = targetField;
25 this.signed = signed;
26 this.byteSize = byteSize;
27 }
28
29
30 @Override
31 public int getSize(Message srcObj) {
32 final Object arr = ReflectUtil.justGet(srcObj, targetField);
33
34 if (arr == null) {
35 throw new RuntimeException("array not initialized");
36 }
37
38 return byteSize * Array.getLength(arr);
39 }
40
41 @Override
42 public int parse(ByteBuffer srcBuf, int frameStart, Message frameObj, Message dstObj) {
43 final int frameSize = (int) totalSizeField.get(ReflectUtil.followFieldPathToParent(totalSizePath, frameObj));
44 int remaining = frameStart + frameSize - srcBuf.position();
45
46 throw new UnsupportedOperationException("not yet implemented");
47
48 // return getSize(dstObj);
49 }
50
51 @Override
52 public int write(ByteBuffer dstBuf, Message srcObj) {
53 final Object arr = ReflectUtil.justGet(srcObj, targetField);
54
55 if (arr == null) {
56 throw new RuntimeException("array not initialized");
57 }
58
59 for (int i = 0; i < Array.getLength(arr); ++i) {
60 IntegerUtil.writeLong(Array.getLong(arr, i), dstBuf, signed, byteSize);
61 }
62
63 return getSize(srcObj);
64 }
65
66 @Override
67 public void patch(Message m, int frameSize, Message frameObj) {
68 totalSizeField.set(ReflectUtil.followFieldPathToParent(totalSizePath, frameObj), frameSize);
69 }
70}
diff --git a/src/org/gnunet/core/Core.java b/src/org/gnunet/core/Core.java
index 7834f70..425336c 100644
--- a/src/org/gnunet/core/Core.java
+++ b/src/org/gnunet/core/Core.java
@@ -2,9 +2,13 @@ package org.gnunet.core;
2 2
3import org.gnunet.construct.*; 3import org.gnunet.construct.*;
4import org.gnunet.util.*; 4import org.gnunet.util.*;
5import org.grothoff.Runabout; 5import org.slf4j.Logger;
6import org.slf4j.LoggerFactory;
6 7
7public class Core { 8public class Core {
9 private static final Logger logger = LoggerFactory
10 .getLogger(Core.class);
11
8 12
9 enum CoreOption { 13 enum CoreOption {
10 NOTHING(0), 14 NOTHING(0),
@@ -26,8 +30,7 @@ public class Core {
26 @UInt32 30 @UInt32
27 public long options; 31 public long options;
28 32
29 @FillWith 33 @IntegerFill(signed = false, bitSize = 16)
30 @UInt16
31 public int[] interested; 34 public int[] interested;
32 } 35 }
33 36
@@ -42,14 +45,14 @@ public class Core {
42 public PeerIdentity myIdentity; 45 public PeerIdentity myIdentity;
43 } 46 }
44 47
45 48 @UnionCase(70)
46 public static class NotifyInboundTrafficMessage implements GnunetMessage.Body { 49 public static class NotifyInboundTrafficMessage implements GnunetMessage.Body {
47 /** 50 /**
48 * Number of ATS key-value pairs that follow this struct 51 * Number of ATS key-value pairs that follow this struct
49 * (excluding the 0-terminator). 52 * (excluding the 0-terminator).
50 */ 53 */
51 @UInt32 54 @UInt32
52 long ats_count; 55 public long ats_count;
53 56
54 /** 57 /**
55 * Identity of the receiver or sender. 58 * Identity of the receiver or sender.
@@ -66,6 +69,32 @@ public class Core {
66 } 69 }
67 70
68 71
72 @UnionCase(71)
73 public static class NotifyOutboundTrafficMessage implements GnunetMessage.Body {
74 /**
75 * Number of ATS key-value pairs that follow this struct
76 * (excluding the 0-terminator).
77 */
78 @UInt32
79 public long ats_count;
80
81 /**
82 * Identity of the receiver or sender.
83 */
84 @NestedMessage
85 public PeerIdentity peer;
86
87 /**
88 * First of the ATS information blocks (we must have at least
89 * one due to the 0-termination requirement).
90 */
91 //@NestedMessage
92 //public ATSInformation atsInformation;
93 @ByteFill
94 public byte[] ats;
95 }
96
97
69 /** 98 /**
70 * Message sent by the service to clients to notify them 99 * Message sent by the service to clients to notify them
71 * about a peer connecting. 100 * about a peer connecting.
@@ -85,9 +114,13 @@ public class Core {
85 @NestedMessage 114 @NestedMessage
86 public PeerIdentity peer; 115 public PeerIdentity peer;
87 116
88 @FillWith 117
89 @NestedMessage 118 @ByteFill
90 public ATSInformation atsInformation; 119 public byte[] atsInfo;
120
121
122 //@FillWith
123 //public ATSInformation[] atsInformation;
91 124
92 } 125 }
93 126
@@ -231,37 +264,213 @@ public class Core {
231 264
232 } 265 }
233 266
234 public interface Init { 267 public interface InitCallback {
235 void onInit(PeerIdentity myIdentity); 268 void onInit(PeerIdentity myIdentity);
236 } 269 }
237 270
238 private final Client client; 271 public interface ConnectHandler {
272 void onConnect(PeerIdentity peerIdentity);
273 }
274
275 public interface DisconnectHandler {
276 void onDisconnect(PeerIdentity peerIdentity);
277
278 }
239 279
280 private final Client client;
240 /** 281 /**
241 * Receive messages that have a visit method in messageHandler. 282 * set to null once connected for the first time
242 *
243 * @param cfg configuration to use
244 * @param init callback, called after handshake with core. called with identity of the local peer.
245 * @param init Called after handshake with the core service failed or succeeded.
246 * Guaranteed to be called only after the constructur returns.
247 */ 283 */
248 public Core(Configuration cfg, Init init, MessageHandler[] handlers) { 284 private InitCallback init;
249 client = new Client("core", cfg); 285
286 private HeaderNotify notifyOutboundHeaders;
287 private HeaderNotify notifyInboundHeaders;
288
289 private MessageNotify notifyOutboundMessages;
290 private MessageNotify notifyInboundMessages;
291
292 private int initOptions;
293
294 private ConnectHandler connectHandler;
295 private DisconnectHandler disconnectHandler;
296
297 private PeerIdentity myIdentity;
298
299 private final CoreReceiver coreReceiver = new CoreReceiver();
300
301
302 public interface HeaderNotify {
303 void notify(GnunetMessage.Header header);
304 }
305
306 public interface MessageNotify {
307 void notify(GnunetMessage messageBody);
308 }
309
310 public static class Builder {
311 /**
312 * Configuration for a core connection.
313 *
314 * @param cfg Mandatory configuration
315 */
316 final private Configuration cfg;
317 private InitCallback init;
318 private HeaderNotify notifyOutboundHeaders;
319 private HeaderNotify notifyInboundHeaders;
320
321 private MessageNotify notifyOutboundMessages;
322 private MessageNotify notifyInboundMessages;
323 private ConnectHandler connectHandler;
324 private DisconnectHandler disconnectHandler;
325 private PeerIdentity myIdentity;
326
327 public Builder(Configuration cfg) {
328 this.cfg = cfg;
329 }
330
331 public Builder withInit(InitCallback init) {
332 this.init = init;
333 return this;
334 }
250 335
336 public Builder addHandler(MessageHandler handler) {
337 return this;
338 }
339
340 public Builder withNotifyOutboundFull(MessageNotify h) {
341 return this;
342 }
343
344 public Builder withNotifyOutboundHeaders(HeaderNotify h) {
345 notifyOutboundHeaders = h;
346 return this;
347 }
348
349 public Builder withNotifyConnect(ConnectHandler h) {
350 connectHandler = h;
351 return this;
352 }
353
354 public Builder withNotifyDisconnect(DisconnectHandler h) {
355 disconnectHandler = h;
356 return this;
357 }
358
359 public Core build() {
360 return new Core(this);
361 }
362
363 }
364
365 private Core(Builder b) {
366 client = new Client("core", b.cfg);
367 this.notifyOutboundHeaders = b.notifyOutboundHeaders;
368
369 initOptions = 0;
370
371 if (notifyOutboundHeaders != null) {
372 //initOptions |= CoreOption.SEND_HDR_OUTBOUND.val;
373 }
374 this.init = b.init;
375
376
377 // XXX: only temporary for debugging
378 initOptions |= CoreOption.SEND_FULL_INBOUND.val;
379 initOptions |= CoreOption.SEND_FULL_OUTBOUND.val;
380
381 this.connectHandler = b.connectHandler;
382 this.disconnectHandler = b.disconnectHandler;
383
384 reconnect();
385 }
386
387 void reconnect() {
251 InitMessage initMessage = new InitMessage(); 388 InitMessage initMessage = new InitMessage();
252 389
253 // ... 390 initMessage.interested = new int[0];
391 initMessage.options = initOptions;
254 392
255 client.transmitAndGetResponse(initMessage, RelativeTime.FOREVER, ); 393 client.transmitAndGetResponse(initMessage, RelativeTime.FOREVER, true, new CoreReceiver());
256 } 394 }
257 395
396 public class CoreReceiver extends RunaboutMessageReceiver {
397
398 public void visit(ConnectNotifyMessage m) {
399 if (connectHandler != null) {
400 connectHandler.onConnect(m.peer);
401 }
402 client.receive(RelativeTime.FOREVER, this);
403 }
404
405 public void visit(InitReplyMessage m) {
406 myIdentity = m.myIdentity;
407 if (init != null) {
408 init.onInit(m.myIdentity);
409 init = null;
410 }
411 client.receive(RelativeTime.FOREVER, this);
412 }
413
414 public void visit(NotifyInboundTrafficMessage m) {
415 client.receive(RelativeTime.FOREVER, this);
416 }
417
418 public void visit(NotifyOutboundTrafficMessage m) {
419 System.out.println("outbound");
420
421 client.receive(RelativeTime.FOREVER, this);
422 }
423
424 public void visit(SendMessageReady m) {
425
426 // todo: ...
427
428 client.receive(RelativeTime.FOREVER, this);
429 }
430
431 @Override
432 public void visitDefault(Object o) {
433 logger.warn("received unexpected message from core: {}", o.getClass());
434 client.receive(RelativeTime.FOREVER, this);
435 }
436
437 @Override
438 public void handleError() {
439 }
440 }
441
442
258 public Cancelable notifyTransmitReady(boolean cork, long priority, RelativeTime maxdelay, 443 public Cancelable notifyTransmitReady(boolean cork, long priority, RelativeTime maxdelay,
259 PeerIdentity target, int size, MessageTransmitter transmitter) { 444 PeerIdentity target, int size, MessageTransmitter transmitter) {
445 final SendMessageRequest smr = new SendMessageRequest();
446 smr.deadline = maxdelay.toAbsolute().asMessage();
447 if (target == null) {
448 smr.peer = myIdentity;
449 } else {
450 smr.peer = target;
451 }
452 smr.queueSize = 10;
453 smr.smrId = 1;
454 smr.size = size;
455
456 client.notifyTransmitReady(maxdelay, false, new MessageTransmitter() {
457 @Override
458 public void transmit(Client.MessageSink sink) {
459 sink.send(smr);
460 }
461
462 @Override
463 public void handleError(Client.TransmitError error) {
464 throw new RuntimeException("client error");
465 }
466 });
260 return null; 467 return null;
261 } 468 }
262 469
470
263 public Cancelable transmitMessage(GnunetMessage.Body messageBody, RelativeTime timeout, 471 public Cancelable transmitMessage(GnunetMessage.Body messageBody, RelativeTime timeout,
264 PeerIdentity target, Continuation cont) { 472 PeerIdentity target, Continuation cont) {
473 return null;
265 474
266 } 475 }
267 476
@@ -270,8 +479,53 @@ public class Core {
270 * be called *after* all pending notifyTransmitReady 479 * be called *after* all pending notifyTransmitReady
271 * requests have been explicitly cancelled. 480 * requests have been explicitly cancelled.
272 */ 481 */
273
274 public void disconnect() { 482 public void disconnect() {
275 483
276 } 484 }
485
486 public static void main(String[] args) {
487 new Program(args) {
488 Core core;
489
490 public void run() {
491 Core.Builder b = new Core.Builder(cfg);
492
493 b.withInit(new InitCallback() {
494 @Override
495 public void onInit(PeerIdentity myIdentity) {
496 System.out.print("Hello, I'm ");
497 System.out.println(Strings.dataToString(myIdentity.data));
498
499 core.notifyTransmitReady(true, 0, RelativeTime.SECOND, null, 4, new MessageTransmitter() {
500 @Override
501 public void transmit(Client.MessageSink sink) {
502 System.out.println("ready to transmit");
503 }
504
505 @Override
506 public void handleError(Client.TransmitError error) {
507 }
508 });
509 }
510 });
511
512 b.withNotifyOutboundHeaders(new HeaderNotify() {
513 @Override
514 public void notify(GnunetMessage.Header header) {
515 System.out.println("my peer sent message of type " + header.messageType);
516 }
517 });
518
519
520 b.withNotifyConnect(new ConnectHandler() {
521 @Override
522 public void onConnect(PeerIdentity peerIdentity) {
523 System.out.println("connected to " + Strings.dataToString(peerIdentity.data));
524 }
525 });
526
527 core = b.build();
528 }
529 }.start();
530 }
277} 531}
diff --git a/src/org/gnunet/dht/DistributedHashTable.java b/src/org/gnunet/dht/DistributedHashTable.java
index ce69c93..3c8a058 100644
--- a/src/org/gnunet/dht/DistributedHashTable.java
+++ b/src/org/gnunet/dht/DistributedHashTable.java
@@ -317,7 +317,7 @@ public class DistributedHashTable {
317 } 317 }
318 318
319 @Override 319 @Override
320 public void handleError(Client.ReceiveError e) { 320 public void handleError() {
321 } 321 }
322 } 322 }
323 323
diff --git a/src/org/gnunet/nse/NetworkSizeEstimation.java b/src/org/gnunet/nse/NetworkSizeEstimation.java
index c1030ad..1002dbe 100644
--- a/src/org/gnunet/nse/NetworkSizeEstimation.java
+++ b/src/org/gnunet/nse/NetworkSizeEstimation.java
@@ -68,12 +68,8 @@ public class NetworkSizeEstimation {
68 } 68 }
69 69
70 @Override 70 @Override
71 public void handleError(Client.ReceiveError e) { 71 public void handleError() {
72 if (e.equals(Client.ReceiveError.DISCONNECT)) { 72 logger.warn("NSE connection list - trying to reconnect");
73 logger.warn("NSE service disconnected us - trying to reconnect");
74 } else {
75 throw new RuntimeException("unexpected receive error");
76 }
77 client.reconnect(); 73 client.reconnect();
78 requestUpdate(); 74 requestUpdate();
79 } 75 }
diff --git a/src/org/gnunet/statistics/Statistics.java b/src/org/gnunet/statistics/Statistics.java
index d280aaf..5d1e66c 100644
--- a/src/org/gnunet/statistics/Statistics.java
+++ b/src/org/gnunet/statistics/Statistics.java
@@ -104,7 +104,7 @@ public class Statistics {
104 } 104 }
105 105
106 @Override 106 @Override
107 public void handleError(Client.ReceiveError e) { 107 public void handleError() {
108 logger.error("unable to read from statistics service"); 108 logger.error("unable to read from statistics service");
109 } 109 }
110 110
diff --git a/src/org/gnunet/util/Client.java b/src/org/gnunet/util/Client.java
index 4482e62..24b1ade 100644
--- a/src/org/gnunet/util/Client.java
+++ b/src/org/gnunet/util/Client.java
@@ -181,7 +181,7 @@ public class Client {
181 recvTask = null; 181 recvTask = null;
182 if (ctx.reasons.contains(Scheduler.Reason.TIMEOUT)) { 182 if (ctx.reasons.contains(Scheduler.Reason.TIMEOUT)) {
183 currentReceiveHelper = null; 183 currentReceiveHelper = null;
184 receiver.handleError(ReceiveError.TIMEOUT); 184 receiver.handleError();
185 } else if (ctx.reasons.contains(Scheduler.Reason.READ_READY)) { 185 } else if (ctx.reasons.contains(Scheduler.Reason.READ_READY)) {
186 try { 186 try {
187 int n = chan.read(recvBuffer); 187 int n = chan.read(recvBuffer);
@@ -190,7 +190,7 @@ public class Client {
190 recvBuffer.position()); 190 recvBuffer.position());
191 chan.close(); 191 chan.close();
192 chan = null; 192 chan = null;
193 receiver.handleError(ReceiveError.DISCONNECT); 193 receiver.handleError();
194 if (currentTransmitHelper != null 194 if (currentTransmitHelper != null
195 && currentTransmitHelper.autoRetry && !currentTransmitHelper.notifyDone()) { 195 && currentTransmitHelper.autoRetry && !currentTransmitHelper.notifyDone()) {
196 logger.debug("reconnecting due to transmitter autoRetry"); 196 logger.debug("reconnecting due to transmitter autoRetry");
@@ -201,7 +201,7 @@ public class Client {
201 logger.debug(String.format("chan read %s bytes", n)); 201 logger.debug(String.format("chan read %s bytes", n));
202 } catch (IOException e) { 202 } catch (IOException e) {
203 logger.error("read failed with exception:", e); 203 logger.error("read failed with exception:", e);
204 receiver.handleError(ReceiveError.MSG_FORMAT); 204 receiver.handleError();
205 return; 205 return;
206 } 206 }
207 if (recvBuffer.remaining() == 0) { 207 if (recvBuffer.remaining() == 0) {
@@ -621,7 +621,7 @@ public class Client {
621 621
622 @Override 622 @Override
623 public void handleError(TransmitError error) { 623 public void handleError(TransmitError error) {
624 receiver.handleError(null); 624 receiver.handleError();
625 } 625 }
626 }); 626 });
627 627
diff --git a/src/org/gnunet/util/MessageReceiver.java b/src/org/gnunet/util/MessageReceiver.java
index 2dd1d5e..da6f225 100644
--- a/src/org/gnunet/util/MessageReceiver.java
+++ b/src/org/gnunet/util/MessageReceiver.java
@@ -34,5 +34,5 @@ public interface MessageReceiver {
34 */ 34 */
35 public void process(GnunetMessage.Body msg); 35 public void process(GnunetMessage.Body msg);
36 36
37 public void handleError(Client.ReceiveError e); 37 public void handleError();
38} 38}
diff --git a/src/org/gnunet/util/PeerIdentity.java b/src/org/gnunet/util/PeerIdentity.java
index 93f55be..dde35a7 100644
--- a/src/org/gnunet/util/PeerIdentity.java
+++ b/src/org/gnunet/util/PeerIdentity.java
@@ -8,4 +8,15 @@ public class PeerIdentity implements Message {
8 8
9 @FixedSizeByteArray(length = 64) 9 @FixedSizeByteArray(length = 64)
10 public byte[] data; 10 public byte[] data;
11
12 static final String HEXES = "0123456789ABCDEF";
13
14 public String getHex() {
15 final StringBuilder hex = new StringBuilder( 2 * data.length );
16 for (final byte b : data) {
17 hex.append(HEXES.charAt((b & 0xF0) >> 4))
18 .append(HEXES.charAt((b & 0x0F)));
19 }
20 return hex.toString();
21 }
11} 22}
diff --git a/src/org/gnunet/util/Resolver.java b/src/org/gnunet/util/Resolver.java
index 8f113d4..c8b86ab 100644
--- a/src/org/gnunet/util/Resolver.java
+++ b/src/org/gnunet/util/Resolver.java
@@ -354,12 +354,8 @@ public class Resolver {
354 } 354 }
355 355
356 @Override 356 @Override
357 public void handleError(Client.ReceiveError e) { 357 public void handleError() {
358 if (e.equals(Client.ReceiveError.TIMEOUT)) {
359 onTimeout(rh); 358 onTimeout(rh);
360 } else {
361 throw new RuntimeException("unexpected receive error");
362 }
363 } 359 }
364 }); 360 });
365 361
diff --git a/src/org/gnunet/util/RunaboutMessageReceiver.java b/src/org/gnunet/util/RunaboutMessageReceiver.java
index 785f276..411bafd 100644
--- a/src/org/gnunet/util/RunaboutMessageReceiver.java
+++ b/src/org/gnunet/util/RunaboutMessageReceiver.java
@@ -1,10 +1,9 @@
1package org.gnunet.util; 1package org.gnunet.util;
2 2
3import org.gnunet.construct.Message;
4import org.grothoff.Runabout; 3import org.grothoff.Runabout;
5 4
6public abstract class RunaboutMessageReceiver extends Runabout implements MessageReceiver { 5public abstract class RunaboutMessageReceiver extends Runabout implements MessageReceiver {
7 public void process(Message msg) { 6 public void process(GnunetMessage.Body msg) {
8 this.visitAppropriate(msg); 7 this.visitAppropriate(msg);
9 } 8 }
10} 9}
diff --git a/src/org/gnunet/util/Server.java b/src/org/gnunet/util/Server.java
index 5ac6e77..f4d1aef 100644
--- a/src/org/gnunet/util/Server.java
+++ b/src/org/gnunet/util/Server.java
@@ -35,6 +35,10 @@ public class Server {
35 35
36 } 36 }
37 37
38 public void injectMessage(/*msg*/) {
39
40 }
41
38 } 42 }
39 43
40 44
@@ -61,7 +65,10 @@ public class Server {
61 65
62 public Server(SocketAddress[] addresses, RelativeTime idleTimeout, boolean requireFound) { 66 public Server(SocketAddress[] addresses, RelativeTime idleTimeout, boolean requireFound) {
63 try { 67 try {
64 ServerSocketChannel socket = ServerSocketChannel.open(); 68 for (SocketAddress addr : addresses) {
69 ServerSocketChannel socket = ServerSocketChannel.open();
70 }
71
65 } catch (IOException e) { 72 } catch (IOException e) {
66 e.printStackTrace(); 73 e.printStackTrace();
67 } 74 }
@@ -78,14 +85,11 @@ public class Server {
78 85
79 } 86 }
80 87
88
81 /** 89 /**
82 * Free resources held by this server. 90 * Free resources held by this server.
83 */ 91 */
84 public void destroy() { 92 public void destroy() {
85 93
86 } 94 }
87
88 public void injectMessage() {
89
90 }
91} 95}
diff --git a/src/org/gnunet/util/Strings.java b/src/org/gnunet/util/Strings.java
new file mode 100644
index 0000000..41255f7
--- /dev/null
+++ b/src/org/gnunet/util/Strings.java
@@ -0,0 +1,32 @@
1package org.gnunet.util;
2
3public class Strings {
4 public static String dataToString(byte[] data) {
5 StringBuilder sb = new StringBuilder();
6 final String encTable = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
7 long rpos;
8 long bits;
9 long vbit;
10 long size = data.length;
11
12 vbit = 0;
13 rpos = 0;
14 bits = 0;
15 while ((rpos < size) || (vbit > 0)) {
16 if ((rpos < size) && (vbit < 5)) {
17 byte b = data[(int) rpos++];
18 int s = b >= 0 ? b : (256 + b);
19 bits = (bits << 8) | s; /* eat 8 more bits */
20 vbit += 8;
21 }
22 if (vbit < 5) {
23 bits <<= (5 - vbit); /* zero-padding */
24 assert (vbit == ((size * 8) % 5));
25 vbit = 5;
26 }
27 sb.append(encTable.charAt((int) (bits >>> (vbit - 5)) & 31));
28 vbit -= 5;
29 }
30 return sb.toString();
31 }
32}
diff --git a/test/org/gnunet/construct/ByteFillMessage.java b/test/org/gnunet/construct/ByteFillMessage.java
index 77bfde1..e2cfb0a 100644
--- a/test/org/gnunet/construct/ByteFillMessage.java
+++ b/test/org/gnunet/construct/ByteFillMessage.java
@@ -2,8 +2,6 @@ package org.gnunet.construct;
2 2
3 3
4public class ByteFillMessage implements Message { 4public class ByteFillMessage implements Message {
5 @NestedMessage
6 public MessageHeader header;
7 5
8 @UInt32 6 @UInt32
9 public int someValue; 7 public int someValue;
diff --git a/test/org/gnunet/construct/ConstructTest.java b/test/org/gnunet/construct/ConstructTest.java
index e0e7b85..11dc097 100644
--- a/test/org/gnunet/construct/ConstructTest.java
+++ b/test/org/gnunet/construct/ConstructTest.java
@@ -1,13 +1,7 @@
1package org.gnunet.construct; 1package org.gnunet.construct;
2 2
3import org.junit.Assert;
4import org.junit.Test;
5
6import java.math.BigInteger;
7import java.nio.ByteBuffer;
8
9public class ConstructTest { 3public class ConstructTest {
10 4 /*
11 5
12 @Test 6 @Test
13 public void test_ByteFillMessage() { 7 public void test_ByteFillMessage() {
@@ -172,4 +166,6 @@ public class ConstructTest {
172 Assert.assertEquals(h1.messageSize, h2.messageSize); 166 Assert.assertEquals(h1.messageSize, h2.messageSize);
173 Assert.assertEquals(h1.messageType, h2.messageType); 167 Assert.assertEquals(h1.messageType, h2.messageType);
174 } 168 }
169
170 */
175} 171}
diff --git a/test/org/gnunet/construct/QueryMessage.java b/test/org/gnunet/construct/QueryMessage.java
index 245dd70..2bc5b61 100644
--- a/test/org/gnunet/construct/QueryMessage.java
+++ b/test/org/gnunet/construct/QueryMessage.java
@@ -2,8 +2,6 @@ package org.gnunet.construct;
2 2
3public class QueryMessage implements Message { 3public class QueryMessage implements Message {
4 4
5 @NestedMessage
6 public MessageHeader header;
7 5
8 @UInt8 6 @UInt8
9 public int query; 7 public int query;