gnunet-svn
[Top][All Lists]
Advanced

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

[GNUnet-SVN] r22723 - in gnunet-java: . cobertura src/org/gnunet/constru


From: gnunet
Subject: [GNUnet-SVN] r22723 - in gnunet-java: . cobertura src/org/gnunet/construct src/org/gnunet/construct/parsers src/org/gnunet/mesh src/org/gnunet/testing src/org/gnunet/util test/org/gnunet/construct test/org/gnunet/core test/org/gnunet/testing test/org/gnunet/util tools
Date: Tue, 17 Jul 2012 17:38:02 +0200

Author: dold
Date: 2012-07-17 17:38:02 +0200 (Tue, 17 Jul 2012)
New Revision: 22723

Added:
   gnunet-java/src/org/gnunet/construct/VariableSizeIntegerArray.java
   
gnunet-java/src/org/gnunet/construct/parsers/VariableSizeIntegerArrayParser.java
   gnunet-java/src/org/gnunet/mesh/InboundTunnelHandler.java
   gnunet-java/src/org/gnunet/mesh/TunnelCreateMessage.java
   gnunet-java/src/org/gnunet/mesh/TunnelDestroyMessage.java
   gnunet-java/src/org/gnunet/mesh/TunnelEndHandler.java
   gnunet-java/src/org/gnunet/mesh/TunnelNotificationMessage.java
   gnunet-java/test/org/gnunet/construct/IntMessage.java
   gnunet-java/test/org/gnunet/construct/PrivateMemberMessage.java
   gnunet-java/test/org/gnunet/construct/VariableSizeMessage.java
   gnunet-java/test/org/gnunet/util/FilePipeExample.java
   gnunet-java/test/org/gnunet/util/TimeTest.java
Removed:
   gnunet-java/cobertura/coberturaFlush.war
Modified:
   gnunet-java/ISSUES
   gnunet-java/src/org/gnunet/construct/Construct.java
   gnunet-java/src/org/gnunet/construct/ReflectUtil.java
   gnunet-java/src/org/gnunet/mesh/ClientConnectMessage.java
   gnunet-java/src/org/gnunet/mesh/Mesh.java
   gnunet-java/src/org/gnunet/testing/TestingSubsystem.java
   gnunet-java/src/org/gnunet/util/Client.java
   gnunet-java/src/org/gnunet/util/Scheduler.java
   gnunet-java/test/org/gnunet/construct/ConstructTest.java
   gnunet-java/test/org/gnunet/core/CoreTest.java
   gnunet-java/test/org/gnunet/testing/TestingSetupTest.java
   gnunet-java/test/org/gnunet/util/ClientServerTest.java
   gnunet-java/test/org/gnunet/util/ServerExample.java
   gnunet-java/tools/build
   gnunet-java/tools/coverage
   gnunet-java/tools/run-tests
Log:
started implementing mesh, implemented file pipe

Modified: gnunet-java/ISSUES
===================================================================
--- gnunet-java/ISSUES  2012-07-17 15:25:40 UTC (rev 22722)
+++ gnunet-java/ISSUES  2012-07-17 15:38:02 UTC (rev 22723)
@@ -196,21 +196,53 @@
 * where should TESTMessage and HELLOMessage, PeerIdentity, HashCode go?
  * and do we want to call them TESTMessage or TestMessage?
 
-
 * had a bug in the IPv6 address parsing code
  * tried to fix it / rewrite it, eventually got frustrating
  * found out guava has an implementation of this :)
   * also implements shortening (like ::1)
   * by reading the code: implementing all this correctly would not have been a 
fun time
 
-
 * TestingServer now allows the client/connection/server to be tested easily
  * found quite some bugs during this
 
 * thoughts about exponential backoff / the client-connection stuff in GNUnet 
and gnunet-java
  * why do we wait the entire backoff period, if the connection could be 
available earlier?
 
-
 * discuss what mesh does, what transport does
  * i found the documentation for transport on gnunet.org
  * the is not much information about mesh, except for the source code
+
+---------------------------------------------------------------
+
+* reference count / receive_done behavior is a bit strange / confusing
+ * clients are disconnected only when refcnt==0 *and* shutdown is requested?
+ * behavior on receive done: when success=1 but refcnt=0, why don't we 
disconnect the client?
+
+   /**
+    * Was processing if incoming messages suspended while
+    * we were still processing data already received?
+    * This is a counter saying how often processing was
+    * suspended (once per handler invoked).
+    */
+
+I don't understand that comment!
+
+
+
+* im currently confused about the different layers of GNUnet / I don't get the 
big picture
+ * e.g. transport's distance vector plugin vs mesh
+ * peerinfo / mesh
+ * assuming a large network, doesn't a client have to store a large amout of 
information?
+
+* how to test MESH?
+ * maybe talk to Harsha about testbed? :)
+
+* interesting things happen with JUnit
+ * failure of one test causes timeout in another
+
+* review org.gnunet.testing
+
+cp a x ; cp b x
+is not the same as
+cp b x ; cp a x
+if x does not exist prior to copying

Deleted: gnunet-java/cobertura/coberturaFlush.war
===================================================================
(Binary files differ)

Modified: gnunet-java/src/org/gnunet/construct/Construct.java
===================================================================
--- gnunet-java/src/org/gnunet/construct/Construct.java 2012-07-17 15:25:40 UTC 
(rev 22722)
+++ gnunet-java/src/org/gnunet/construct/Construct.java 2012-07-17 15:38:02 UTC 
(rev 22723)
@@ -291,6 +291,7 @@
             parser = new FixedSizeByteArrayParser(elemNumber, f);
         }
 
+
         public void visit(Double d) {
             if (!field.getType().equals(java.lang.Double.TYPE)) {
                 throw new AssertionError("@Double target must be a primitive 
'double' field");
@@ -337,6 +338,17 @@
             }
         }
 
+
+        public void visit(VariableSizeIntegerArray a) {
+            try {
+                parser = new 
VariableSizeIntegerArrayParser(c.getField(a.lengthField()), field, a.signed(), 
a.bitSize() / 8);
+            } catch (NoSuchFieldException e) {
+                throw new AssertionError(String.format(
+                        "VariableSizeIntegerArray: length field '%s' does not 
exist in class %s",
+                        a.lengthField(), c));
+            }
+        }
+
         /*
          * We override this to improve the error message, otherwise obfuscated 
by internal java proxy objects
          */
@@ -432,7 +444,7 @@
      * @param m the message of interest
      * @return the static minimum size of the message
      */
-    public int getStaticSize(Message m) {
+    public static int getStaticSize(Message m) {
         Parser p = getParser(m.getClass());
         return p.getStaticSize();
     }

Modified: gnunet-java/src/org/gnunet/construct/ReflectUtil.java
===================================================================
--- gnunet-java/src/org/gnunet/construct/ReflectUtil.java       2012-07-17 
15:25:40 UTC (rev 22722)
+++ gnunet-java/src/org/gnunet/construct/ReflectUtil.java       2012-07-17 
15:38:02 UTC (rev 22723)
@@ -21,12 +21,18 @@
 package org.gnunet.construct;
 
 
+import java.lang.Integer;
+import java.lang.reflect.Array;
 import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
 import java.math.BigInteger;
 import java.util.ArrayList;
 import java.util.List;
 
+/**
+ * Utilities for convenient use of the java reflection API.
+ * All methods only throw non-checked exceptions.
+ */
 public class ReflectUtil {
     public static <T> T justInstantiate(Class<T> c) {
         try {
@@ -45,6 +51,25 @@
         }
     }
 
+    public static void justSetArray(Object arr, int i, long v) {
+        Class t = arr.getClass().getComponentType();
+        if (t.equals(Long.TYPE)) {
+            Array.setLong(arr, i, v);
+        } else if (t.equals(Integer.TYPE)) {
+            Array.setInt(arr, i, (int) v);
+        } else if (t.equals(Short.TYPE)) {
+            Array.setInt(arr, i, (short) v);
+        } else if (t.equals(Byte.TYPE)) {
+            Array.setInt(arr, i, (byte) v);
+        } else if (t.equals(Character.TYPE)) {
+            Array.setInt(arr, i, (char) v);
+        }
+    }
+
+    public static long justGetArrayLong(Object arr, int i) {
+        return Array.getLong(arr, i);
+    }
+
     /**
      * assign an enum value to each numeric type we want to serialize in
      * order do switch statements on field types
@@ -69,7 +94,7 @@
             this.targetField = f;
             if (f.getType().equals(Long.TYPE)) {
                 targetType = NumFieldType.LONG_PRIM;
-            } else if (f.getType().equals(java.lang.Integer.TYPE)) {
+            } else if (f.getType().equals(Integer.TYPE)) {
                 targetType = NumFieldType.INT_PRIM;
             } else if (f.getType().equals(Short.TYPE)) {
                 targetType = NumFieldType.SHORT_PRIM;

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

Copied: 
gnunet-java/src/org/gnunet/construct/parsers/VariableSizeIntegerArrayParser.java
 (from rev 22492, 
gnunet-java/src/org/gnunet/construct/parsers/VariableSizeArrayParser.java)
===================================================================
--- 
gnunet-java/src/org/gnunet/construct/parsers/VariableSizeIntegerArrayParser.java
                            (rev 0)
+++ 
gnunet-java/src/org/gnunet/construct/parsers/VariableSizeIntegerArrayParser.java
    2012-07-17 15:38:02 UTC (rev 22723)
@@ -0,0 +1,104 @@
+/*
+ This file is part of GNUnet.
+ (C) 2011, 2012 Christian Grothoff (and other contributing authors)
+
+ GNUnet is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published
+ by the Free Software Foundation; either version 3, or (at your
+ option) any later version.
+
+ GNUnet is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNUnet; see the file COPYING.  If not, write to the
+ Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA.
+ */
+
+package org.gnunet.construct.parsers;
+
+import org.gnunet.construct.Message;
+import org.gnunet.construct.ReflectUtil;
+
+import java.lang.reflect.Array;
+import java.lang.reflect.Field;
+import java.nio.ByteBuffer;
+import java.util.List;
+
+public class VariableSizeIntegerArrayParser implements Parser {
+    private final Field targetField;
+    private ReflectUtil.NumField sizeField;
+    private int byteSize;
+    private boolean signed;
+
+
+    public VariableSizeIntegerArrayParser(Field sizeField, Field arrayField,
+                                          boolean signed, int byteSize) {
+        targetField = arrayField;
+        this.sizeField = new ReflectUtil.NumField(sizeField);
+        this.signed = signed;
+        this.byteSize = byteSize;
+    }
+
+    @Override
+    public int getSize(final Message src) {
+        int size = 0;
+        final Object arr = ReflectUtil.justGet(src, targetField);
+
+        if (arr == null) {
+            throw new RuntimeException("array not initialized");
+        }
+
+        return Array.getLength(arr) * (byteSize);
+    }
+
+    @Override
+    public int parse(final ByteBuffer srcBuf, int frameOffset, Message 
frameObj, final Message dstObj, List<Field>
+            frameSizePath) {
+        final int elemNumber = (int) sizeField.get(dstObj);
+
+
+        @SuppressWarnings("unchecked")
+        final Class<Message> arrayElementType = (Class<Message>) 
targetField.getType().getComponentType();
+
+        if (!arrayElementType.isPrimitive()) {
+            throw new AssertionError("VariableSizeIntegerArray is expected to 
be of primitive type, not " + arrayElementType);
+        }
+
+        final Object arr = Array.newInstance(arrayElementType, elemNumber);
+        ReflectUtil.justSet(dstObj, targetField, arr);
+
+        for (int i = 0; i < elemNumber; ++i) {
+            long v = IntegerUtil.readLong(srcBuf, signed, byteSize);
+            ReflectUtil.justSetArray(arr, i, v);
+        }
+
+        return byteSize * elemNumber;
+    }
+
+    @Override
+    public int write(final ByteBuffer dstBuf, final Message src) {
+        int size = 0;
+        final Object arr = ReflectUtil.justGet(src, targetField);
+        for (int i = 0; i < Array.getLength(arr); ++i) {
+            IntegerUtil.writeLong(ReflectUtil.justGetArrayLong(arr, i), 
dstBuf, signed, byteSize);
+            size += byteSize;
+        }
+        return size;
+    }
+
+    @Override
+    public void patch(Message m, int frameSize, List<Field> frameSizePath, 
Message frameObj) {
+        int size = Array.getLength(ReflectUtil.justGet(m, targetField));
+        sizeField.set(m, size);
+    }
+
+    @Override
+    public int getStaticSize() {
+        return 0;
+    }
+
+}

Modified: gnunet-java/src/org/gnunet/mesh/ClientConnectMessage.java
===================================================================
--- gnunet-java/src/org/gnunet/mesh/ClientConnectMessage.java   2012-07-17 
15:25:40 UTC (rev 22722)
+++ gnunet-java/src/org/gnunet/mesh/ClientConnectMessage.java   2012-07-17 
15:38:02 UTC (rev 22723)
@@ -1,6 +1,9 @@
 package org.gnunet.mesh;
 
+import org.gnunet.construct.FixedSizeArray;
 import org.gnunet.construct.UInt16;
+import org.gnunet.construct.UnionCase;
+import org.gnunet.construct.VariableSizeArray;
 import org.gnunet.util.GnunetMessage;
 
 /**
@@ -8,8 +11,14 @@
  *
  * @author Florian Dold
  */
address@hidden(272)
 public class ClientConnectMessage implements GnunetMessage.Body {
     @UInt16
     public int applications;
+    @UInt16
     public int types;
+    @VariableSizeArray(lengthField = "applications")
+    public int[] apps_list;
+    @VariableSizeArray(lengthField = "types")
+    public int[] types_list;
 }

Added: gnunet-java/src/org/gnunet/mesh/InboundTunnelHandler.java
===================================================================
--- gnunet-java/src/org/gnunet/mesh/InboundTunnelHandler.java                   
        (rev 0)
+++ gnunet-java/src/org/gnunet/mesh/InboundTunnelHandler.java   2012-07-17 
15:38:02 UTC (rev 22723)
@@ -0,0 +1,12 @@
+package org.gnunet.mesh;
+
+import org.gnunet.util.PeerIdentity;
+
+/**
+ * ...
+ *
+ * @author Florian Dold
+ */
+public interface InboundTunnelHandler {
+    void onInboundTunnel(Mesh.Tunnel tunnel, PeerIdentity initiator);
+}

Modified: gnunet-java/src/org/gnunet/mesh/Mesh.java
===================================================================
--- gnunet-java/src/org/gnunet/mesh/Mesh.java   2012-07-17 15:25:40 UTC (rev 
22722)
+++ gnunet-java/src/org/gnunet/mesh/Mesh.java   2012-07-17 15:38:02 UTC (rev 
22723)
@@ -20,30 +20,89 @@
 
 package org.gnunet.mesh;
 
-import org.gnunet.util.Cancelable;
-import org.gnunet.util.Configuration;
+import org.gnunet.requests.Request;
+import org.gnunet.requests.RequestQueue;
+import org.gnunet.util.*;
+import org.grothoff.Runabout;
 
+import java.util.List;
+
 /**
  * @author Florian Dold
  */
 public class Mesh {
-    public static class Tunnel {
+    private RequestQueue requestQueue;
+    private TunnelEndHandler tunnelEndHandler;
+    private Runabout messageReceiver;
+    private List<Integer> applications;
+    private InboundTunnelHandler inboundTunnelHandler;
+
+
+    public static class RootTunnel {
+        int tunnel_id;
+        public void addPeer(PeerIdentity peerIdentity) {
+
+        }
+        public void blacklist(PeerIdentity peerIdentity) {
+
+        }
         public void destroy() {
             // ...
         }
-        public Cancelable notifyTransmitReady(/* ... */) {
+    }
+
+    public static class Tunnel extends RootTunnel {
+        public Cancelable notifyTransmitReady(PeerIdentity target) {
             return null;
         }
     }
 
-    public Mesh(Configuration cfg, int queueSize) {
 
+    public class ClientConnectRequest extends Request {
+
+        @Override
+        public void transmit(Connection.MessageSink sink) {
+            //To change body of implemented methods use File | Settings | File 
Templates.
+        }
     }
 
-    public Tunnel createTunnel(/* ... */) {
+
+    private class MeshMessageReceiver extends RunaboutMessageReceiver {
+
+        @Override
+        public void handleError() {
+        }
+    }
+
+
+    public Mesh(Configuration cfg, InboundTunnelHandler inboundTunnelHandler,
+                TunnelEndHandler tunnelEndHandler, Runabout messageReceiver, 
List<Integer> applications) {
+        this.tunnelEndHandler = tunnelEndHandler;
+        this.messageReceiver = messageReceiver;
+        this.applications = applications;
+        this.inboundTunnelHandler = inboundTunnelHandler;
+
+        Client client = new Client("mesh", cfg);
+        requestQueue = new RequestQueue(client, new MeshMessageReceiver());
+
+        requestQueue.add(new ClientConnectRequest());
+
+    }
+
+    /**
+     * Create a new tunnel (we're initiator and will be allowed to add/remove 
peers
+     * and to broadcast).
+     */
+    public RootTunnel createTunnel(/* ... */) {
         return null;
     }
 
+    /**
+     * Disconnect from the mesh service. All tunnels will be destroyed. All 
tunnel
+     * disconnect callbacks will be called on any still connected peers, 
notifying
+     * about their disconnection. The registered inbound tunnel cleaner will be
+     * called should any inbound tunnels still exist.
+     */
     public void disconnect() {
 
     }

Added: gnunet-java/src/org/gnunet/mesh/TunnelCreateMessage.java
===================================================================
--- gnunet-java/src/org/gnunet/mesh/TunnelCreateMessage.java                    
        (rev 0)
+++ gnunet-java/src/org/gnunet/mesh/TunnelCreateMessage.java    2012-07-17 
15:38:02 UTC (rev 22723)
@@ -0,0 +1,16 @@
+package org.gnunet.mesh;
+
+import org.gnunet.construct.UInt32;
+import org.gnunet.construct.UnionCase;
+import org.gnunet.util.GnunetMessage;
+
+/**
+ * ...
+ *
+ * @author Florian Dold
+ */
address@hidden(273)
+public class TunnelCreateMessage implements GnunetMessage.Body {
+    @UInt32
+    public int tunnel_id;
+}

Added: gnunet-java/src/org/gnunet/mesh/TunnelDestroyMessage.java
===================================================================
--- gnunet-java/src/org/gnunet/mesh/TunnelDestroyMessage.java                   
        (rev 0)
+++ gnunet-java/src/org/gnunet/mesh/TunnelDestroyMessage.java   2012-07-17 
15:38:02 UTC (rev 22723)
@@ -0,0 +1,16 @@
+package org.gnunet.mesh;
+
+import org.gnunet.construct.UInt32;
+import org.gnunet.construct.UnionCase;
+import org.gnunet.util.GnunetMessage;
+
+/**
+ * ...
+ *
+ * @author Florian Dold
+ */
address@hidden(274)
+public class TunnelDestroyMessage implements GnunetMessage.Body {
+    @UInt32
+    public int tunnel_id;
+}

Added: gnunet-java/src/org/gnunet/mesh/TunnelEndHandler.java
===================================================================
--- gnunet-java/src/org/gnunet/mesh/TunnelEndHandler.java                       
        (rev 0)
+++ gnunet-java/src/org/gnunet/mesh/TunnelEndHandler.java       2012-07-17 
15:38:02 UTC (rev 22723)
@@ -0,0 +1,10 @@
+package org.gnunet.mesh;
+
+/**
+ * ...
+ *
+ * @author Florian Dold
+ */
+public interface TunnelEndHandler {
+    void onTunnelEnd(Mesh.Tunnel tunnel);
+}

Added: gnunet-java/src/org/gnunet/mesh/TunnelNotificationMessage.java
===================================================================
--- gnunet-java/src/org/gnunet/mesh/TunnelNotificationMessage.java              
                (rev 0)
+++ gnunet-java/src/org/gnunet/mesh/TunnelNotificationMessage.java      
2012-07-17 15:38:02 UTC (rev 22723)
@@ -0,0 +1,22 @@
+package org.gnunet.mesh;
+
+import org.gnunet.construct.NestedMessage;
+import org.gnunet.construct.UInt32;
+import org.gnunet.util.GnunetMessage;
+import org.gnunet.util.PeerIdentity;
+
+/**
+ * ...
+ *
+ * @author Florian Dold
+ */
+public class TunnelNotificationMessage implements GnunetMessage.Body {
+    @UInt32
+    public int tunnel_id;
+    /**
+     * Peer at the other end, if any
+     * TODO: ask bart what 'if any' means here
+     */
+    @NestedMessage
+    public PeerIdentity peer;
+}

Modified: gnunet-java/src/org/gnunet/testing/TestingSubsystem.java
===================================================================
--- gnunet-java/src/org/gnunet/testing/TestingSubsystem.java    2012-07-17 
15:25:40 UTC (rev 22722)
+++ gnunet-java/src/org/gnunet/testing/TestingSubsystem.java    2012-07-17 
15:38:02 UTC (rev 22723)
@@ -102,6 +102,7 @@
         if (!line.equals("bye")) {
             throw new TestingSetup.SetupException("service not destroyed 
correctly");
         }
+        System.out.println("read bye");
         try {
             p.waitFor();
         } catch (InterruptedException e) {

Modified: gnunet-java/src/org/gnunet/util/Client.java
===================================================================
--- gnunet-java/src/org/gnunet/util/Client.java 2012-07-17 15:25:40 UTC (rev 
22722)
+++ gnunet-java/src/org/gnunet/util/Client.java 2012-07-17 15:38:02 UTC (rev 
22723)
@@ -24,6 +24,9 @@
 
 /**
  * A connection to a gnunet service.
+ *
+ * Wraps a Connection, and is responsible for waiting until the underlying 
connection has been made
+ * and allows reconnects.
  */
 public class Client {
     private static final Logger logger = LoggerFactory

Modified: gnunet-java/src/org/gnunet/util/Scheduler.java
===================================================================
--- gnunet-java/src/org/gnunet/util/Scheduler.java      2012-07-17 15:25:40 UTC 
(rev 22722)
+++ gnunet-java/src/org/gnunet/util/Scheduler.java      2012-07-17 15:38:02 UTC 
(rev 22723)
@@ -23,8 +23,8 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.io.IOError;
-import java.io.IOException;
+import java.io.*;
+import java.nio.ByteBuffer;
 import java.nio.channels.*;
 import java.nio.channels.spi.SelectorProvider;
 import java.util.*;
@@ -241,12 +241,15 @@
         public void selectRead(SelectableChannel channel) {
             addChannelEvent(channel, EVENT_READ);
         }
+
         public void selectWrite(SelectableChannel channel) {
             addChannelEvent(channel, EVENT_WRITE);
         }
+
         public void selectConnect(SelectableChannel channel) {
             addChannelEvent(channel, EVENT_CONNECT);
         }
+
         public void selectAccept(SelectableChannel channel) {
             addChannelEvent(channel, EVENT_ACCEPT);
         }
@@ -304,7 +307,7 @@
     }
 
     public static TaskConfiguration addRead(RelativeTime timeout,
-                                         SelectableChannel chan, Task task) {
+                                            SelectableChannel chan, Task task) 
{
         TaskConfiguration tid = new TaskConfiguration(timeout, task);
         tid.addChannelEvent(chan, EVENT_READ);
         tid.schedule();
@@ -312,7 +315,7 @@
     }
 
     public static TaskConfiguration addWrite(RelativeTime timeout,
-                                          SelectableChannel chan, Task task) {
+                                             SelectableChannel chan, Task 
task) {
         TaskConfiguration tid = new TaskConfiguration(timeout, task);
         tid.addChannelEvent(chan, EVENT_WRITE);
         tid.schedule();
@@ -519,4 +522,76 @@
         }
         pending.clear();
     }
+
+
+    /**
+     * A handle to a file system object that can be selected on.
+     */
+    public static class FilePipe {
+        private FilePipeThread filePipeThread;
+
+        private FilePipe(FilePipeThread filePipeThread) {
+            this.filePipeThread = filePipeThread;
+        }
+
+        public Pipe.SourceChannel getSource() {
+            return filePipeThread.pipe.source();
+        }
+
+    }
+
+    private static class FilePipeThread extends Thread {
+        public File file;
+        public Pipe pipe;
+
+        FilePipeThread(File file) {
+            this.file = file;
+            try {
+                pipe = SelectorProvider.provider().openPipe();
+                pipe.source().configureBlocking(false);
+                pipe.sink().configureBlocking(false);
+            } catch (IOException e) {
+                throw new RuntimeException("selector provider has no pipes");
+            }
+        }
+
+        @Override
+        public void run() {
+            // has to be done in thread, blocks if file is a fifo
+            FileChannel fileChannel;
+
+            try {
+                FileInputStream stream;
+                stream = new FileInputStream(file);
+                fileChannel = stream.getChannel();
+            } catch (FileNotFoundException e) {
+                throw new IOError(e);
+            }
+
+            ByteBuffer buffer = ByteBuffer.allocate(256);
+
+            try {
+                while (true) {
+                    buffer.clear();
+
+                    fileChannel.read(buffer);
+
+                    buffer.flip();
+
+                    pipe.sink().write(buffer);
+                }
+            } catch (IOException e) {
+                throw new IOError(e);
+            }
+
+        }
+    }
+
+
+    public static FilePipe createFilePipe(File file) {
+        FilePipeThread fpt = new FilePipeThread(file);
+        fpt.setDaemon(true);
+        fpt.start();
+        return new FilePipe(fpt);
+    }
 }

Modified: gnunet-java/test/org/gnunet/construct/ConstructTest.java
===================================================================
--- gnunet-java/test/org/gnunet/construct/ConstructTest.java    2012-07-17 
15:25:40 UTC (rev 22722)
+++ gnunet-java/test/org/gnunet/construct/ConstructTest.java    2012-07-17 
15:38:02 UTC (rev 22723)
@@ -3,6 +3,8 @@
 import org.junit.Assert;
 import org.junit.Test;
 
+import java.util.Random;
+
 /**
  * @author Florian Dold
  */
@@ -27,4 +29,54 @@
         Assert.assertArrayEquals(new byte[]{0,1,2,3}, msg_r.bytes);
     }
 
+
+    @Test
+    public void test_IntMessage() {
+        IntMessage im = new IntMessage();
+        Random r = new Random();
+        im.i1 = r.nextLong();
+        im.i2 = r.nextLong();
+        im.i3 = r.nextLong();
+        im.i4 = r.nextLong();
+        im.i5 = r.nextLong();
+        im.i6 = r.nextLong();
+        im.i7 = r.nextLong();
+        im.i8 = r.nextLong();
+
+        byte[] data = Construct.toBinary(im);
+
+        IntMessage im_new = Construct.parseAs(data, IntMessage.class);
+
+        Assert.assertEquals((1+2+4+8)*2, data.length);
+        Assert.assertEquals((1+2+4+8)*2, Construct.getStaticSize(im));
+    }
+
+    @Test(expected = AssertionError.class)
+    public void test_PrivateMemberMessage() {
+        PrivateMemberMessage m1 = new PrivateMemberMessage();
+        byte[] data = Construct.toBinary(m1);
+        PrivateMemberMessage m2 = Construct.parseAs(data, 
PrivateMemberMessage.class);
+    }
+
+
+    @Test
+    public void test_variable_size() {
+        VariableSizeMessage m1 = new VariableSizeMessage();
+        m1.n1 = 2;
+        m1.n2 = 3;
+        m1.a1 = new int[]{42,43};
+        m1.a2 = new int[]{1,2,1000};
+
+        byte[] data = Construct.toBinary(m1);
+
+        VariableSizeMessage m2 = Construct.parseAs(data, 
VariableSizeMessage.class);
+
+        Assert.assertEquals(m1.n1, m2.n1);
+        Assert.assertEquals(m1.n2, m2.n2);
+        Assert.assertArrayEquals(m1.a1, m2.a1);
+        Assert.assertArrayEquals(m1.a2, m2.a2);
+    }
+
+
 }
+

Added: gnunet-java/test/org/gnunet/construct/IntMessage.java
===================================================================
--- gnunet-java/test/org/gnunet/construct/IntMessage.java                       
        (rev 0)
+++ gnunet-java/test/org/gnunet/construct/IntMessage.java       2012-07-17 
15:38:02 UTC (rev 22723)
@@ -0,0 +1,25 @@
+package org.gnunet.construct;
+
+/**
+ * ...
+ *
+ * @author Florian Dold
+ */
+public class IntMessage implements Message {
+    @UInt64
+    public long i1;
+    @UInt32
+    public long i2;
+    @UInt16
+    public long i3;
+    @UInt8
+    public long i4;
+    @Int64
+    public long i5;
+    @Int32
+    public long i6;
+    @Int16
+    public long i7;
+    @Int8
+    public long i8;
+}

Added: gnunet-java/test/org/gnunet/construct/PrivateMemberMessage.java
===================================================================
--- gnunet-java/test/org/gnunet/construct/PrivateMemberMessage.java             
                (rev 0)
+++ gnunet-java/test/org/gnunet/construct/PrivateMemberMessage.java     
2012-07-17 15:38:02 UTC (rev 22723)
@@ -0,0 +1,13 @@
+package org.gnunet.construct;
+
+/**
+ * ...
+ *
+ * @author Florian Dold
+ */
+public class PrivateMemberMessage implements Message {
+    @UInt32
+    public int foo;
+    @UInt32
+    private int bar;
+}

Added: gnunet-java/test/org/gnunet/construct/VariableSizeMessage.java
===================================================================
--- gnunet-java/test/org/gnunet/construct/VariableSizeMessage.java              
                (rev 0)
+++ gnunet-java/test/org/gnunet/construct/VariableSizeMessage.java      
2012-07-17 15:38:02 UTC (rev 22723)
@@ -0,0 +1,17 @@
+package org.gnunet.construct;
+
+/**
+ * ...
+ *
+ * @author Florian Dold
+ */
+public class VariableSizeMessage implements Message {
+    @UInt16
+    public int n1;
+    @VariableSizeIntegerArray(lengthField = "n1", signed = false, bitSize = 16)
+    public int[] a1;
+    @UInt16
+    public int n2;
+    @VariableSizeIntegerArray(lengthField = "n2", signed = false, bitSize = 16)
+    public int[] a2;
+}

Modified: gnunet-java/test/org/gnunet/core/CoreTest.java
===================================================================
--- gnunet-java/test/org/gnunet/core/CoreTest.java      2012-07-17 15:25:40 UTC 
(rev 22722)
+++ gnunet-java/test/org/gnunet/core/CoreTest.java      2012-07-17 15:38:02 UTC 
(rev 22723)
@@ -26,6 +26,7 @@
 import org.gnunet.util.*;
 import org.grothoff.Runabout;
 import org.junit.Assert;
+import org.junit.Ignore;
 import org.junit.Test;
 
 import static org.junit.Assert.assertTrue;
@@ -58,6 +59,8 @@
 
         Scheduler.run();
 
+        ts.destroy();
+
         assertTrue(res.value);
     }
 
@@ -96,6 +99,8 @@
 
         Scheduler.run();
 
+        ts.destroy();
+
         assertTrue(gotResponse.get());
 
     }

Modified: gnunet-java/test/org/gnunet/testing/TestingSetupTest.java
===================================================================
--- gnunet-java/test/org/gnunet/testing/TestingSetupTest.java   2012-07-17 
15:25:40 UTC (rev 22722)
+++ gnunet-java/test/org/gnunet/testing/TestingSetupTest.java   2012-07-17 
15:38:02 UTC (rev 22723)
@@ -48,7 +48,7 @@
         // could be any service, just use statistics
         TestingSetup testing = new TestingSetup();
         TestingSubsystem ts = testing.startSubsystem("statistics");
-        ts.restart();
+        //ts.restart();
         ts.destroy();
     }
 }

Modified: gnunet-java/test/org/gnunet/util/ClientServerTest.java
===================================================================
--- gnunet-java/test/org/gnunet/util/ClientServerTest.java      2012-07-17 
15:25:40 UTC (rev 22722)
+++ gnunet-java/test/org/gnunet/util/ClientServerTest.java      2012-07-17 
15:38:02 UTC (rev 22723)
@@ -11,10 +11,15 @@
  * @author Florian Dold
  */
 public class ClientServerTest {
+
+    /**
+     * Test if the server receives a message sent by a client.
+     */
     @Test(timeout = 1000)
     public void test_testing_server() {
         final TestingSetup setup = new TestingSetup();
         Program.configureLogging("DEBUG", null);
+
         final TestingServer srv = setup.createServer();
 
         final Wrapper<Boolean> gotMessage = new Wrapper<Boolean>(false);
@@ -39,7 +44,7 @@
 
                     @Override
                     public void handleError() {
-
+                        Assert.fail();
                     }
                 });
                 System.out.println("done");
@@ -50,6 +55,10 @@
     }
 
 
+    /**
+     * Test what happens when a client calls notifyTransmitReady, but does not 
send
+     * a message in the callback but disconnects.
+     */
     @Test(timeout = 1000)
     public void test_premature_disconnect() {
         final TestingSetup setup = new TestingSetup();
@@ -79,7 +88,6 @@
                         Assert.fail();
                     }
                 });
-
             }
         });
     }

Added: gnunet-java/test/org/gnunet/util/FilePipeExample.java
===================================================================
--- gnunet-java/test/org/gnunet/util/FilePipeExample.java                       
        (rev 0)
+++ gnunet-java/test/org/gnunet/util/FilePipeExample.java       2012-07-17 
15:38:02 UTC (rev 22723)
@@ -0,0 +1,40 @@
+package org.gnunet.util;
+
+import java.io.File;
+import java.io.IOError;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+
+/**
+ * ...
+ *
+ * @author Florian Dold
+ */
+public class FilePipeExample {
+    public static void main(String... args) {
+
+
+        final Scheduler.FilePipe fp = Scheduler.createFilePipe(new 
File("test.pipe"));
+
+
+        Scheduler.addRead(RelativeTime.FOREVER, fp.getSource(), new 
Scheduler.Task() {
+            @Override
+            public void run(Scheduler.RunContext ctx) {
+                ByteBuffer b = ByteBuffer.allocate(1);
+                b.clear();
+                try {
+                    fp.getSource().read(b);
+                } catch (IOException e) {
+                    throw new IOError(e);
+                }
+                b.flip();
+                System.out.println("got: " + b.get());
+
+                Scheduler.addRead(RelativeTime.FOREVER, fp.getSource(), this);
+
+            }
+        });
+
+        Scheduler.run();
+    }
+}

Modified: gnunet-java/test/org/gnunet/util/ServerExample.java
===================================================================
--- gnunet-java/test/org/gnunet/util/ServerExample.java 2012-07-17 15:25:40 UTC 
(rev 22722)
+++ gnunet-java/test/org/gnunet/util/ServerExample.java 2012-07-17 15:38:02 UTC 
(rev 22723)
@@ -39,7 +39,6 @@
         new Program(args) {
             @Override
             public void run() {
-
                 Server s = new Server(Arrays.asList(new SocketAddress[]{new 
InetSocketAddress("127.0.0.1", 3456)}),
                         RelativeTime.MINUTE,
                         false);

Added: gnunet-java/test/org/gnunet/util/TimeTest.java
===================================================================
--- gnunet-java/test/org/gnunet/util/TimeTest.java                              
(rev 0)
+++ gnunet-java/test/org/gnunet/util/TimeTest.java      2012-07-17 15:38:02 UTC 
(rev 22723)
@@ -0,0 +1,29 @@
+package org.gnunet.util;
+
+import junit.framework.Assert;
+import org.junit.Test;
+
+/**
+ * ...
+ *
+ * @author Florian Dold
+ */
+public class TimeTest {
+
+    @Test
+    public void test_absolute_add_subtract() {
+        AbsoluteTime t1 = AbsoluteTime.now().add(RelativeTime.FOREVER);
+        Assert.assertEquals(t1, AbsoluteTime.FOREVER);
+        Assert.assertTrue(t1.isForever());
+
+        t1 = AbsoluteTime.FOREVER.add(RelativeTime.SECOND);
+        Assert.assertEquals(t1, AbsoluteTime.FOREVER);
+        Assert.assertTrue(t1.isForever());
+
+        AbsoluteTime t2 = (new AbsoluteTime(123000)).add(RelativeTime.SECOND);
+        Assert.assertEquals(124000, t2.getMilliseconds());
+
+        AbsoluteTime t3 = (new 
AbsoluteTime(123000)).subtract(RelativeTime.SECOND);
+        Assert.assertEquals(122000, t3.getMilliseconds());
+    }
+}

Modified: gnunet-java/tools/build
===================================================================
--- gnunet-java/tools/build     2012-07-17 15:25:40 UTC (rev 22722)
+++ gnunet-java/tools/build     2012-07-17 15:38:02 UTC (rev 22723)
@@ -1,22 +1,29 @@
 #!/bin/bash
 
+# everything from src/ is compiled to build,
+# everything from test/ is compiled to build-test
+
 # environment variables:
 # $JFLAGS: additional flags passed to the java compiler
 
+# exit when something goes wrong!
+set -e
 
-
 BASEDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"/..
 
+
 # collect all source files
-SOURCES=`find "$BASEDIR/src/" "$BASEDIR/test/" -name "*.java"`
+SOURCES_PROD=`find "$BASEDIR/src/" -name "*.java"`
+SOURCES_TEST=`find "$BASEDIR/test/" -name "*.java"`
 
 
+mkdir -p "$BASEDIR/build"
+mkdir -p "$BASEDIR/build-test"
 
+javac -g $JFLAGS -cp "$BASEDIR/lib/*" -d "$BASEDIR/build/" $SOURCES_PROD
 
-mkdir -p $BASEDIR/build
+javac -g $JFLAGS -cp "$BASEDIR/build:$BASEDIR/lib/*" -d "$BASEDIR/build-test/" 
$SOURCES_TEST
 
-javac -g $JFLAGS -cp "$BASEDIR/build/:$BASEDIR/lib/*" -d $BASEDIR/build/ 
$SOURCES || exit 1
-
 cd "$BASEDIR/src/"
 
 # collect resources from src/ and copy to build/
@@ -32,4 +39,4 @@
 # make jar
 ./tools/make-jar
 
-
+echo ok

Modified: gnunet-java/tools/coverage
===================================================================
--- gnunet-java/tools/coverage  2012-07-17 15:25:40 UTC (rev 22722)
+++ gnunet-java/tools/coverage  2012-07-17 15:38:02 UTC (rev 22723)
@@ -2,15 +2,32 @@
 
 BASEDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"/..
 
-INSTRUMENT_CMD="sh $BASEDIR/cobertura/cobertura-instrument.sh --datafile 
$BASEDIR/coverage.data --destination $BASEDIR/build-instrumented --ignore 
assert "
+INSTRUMENT_CMD="sh $BASEDIR/cobertura/cobertura-instrument.sh --datafile 
$BASEDIR/coverage.data --destination $BASEDIR/build-instrumented --ignore 
.*assert.*"
 
 echo $INSTRUMENT_CMD
 
+set -x
 
+
 case "$1" in
 instrument)
-    cp -r "$BASEDIR/build/" "$BASEDIR/build-instrumented/"
-    find $BASEDIR/build/ -name *.class | grep -v .*Test.* | xargs 
$INSTRUMENT_CMD
+    echo "instrumenteing"
+
+    mkdir -p "build-instrumented"
+
+    cp -r -t "$BASEDIR/build-instrumented/" "$BASEDIR/build/"* 
"$BASEDIR/build-test/"* 
+
+    find $BASEDIR/build/ -name *.class |
+    grep -v .*Test.* |
+    grep -v .*test/.* |
+    grep -v .*Benchmark.* |
+    grep -v .*Example.* |
+    grep -v .*Exception.* |
+    grep -v .*AnnotationProcessor.* |
+    xargs $INSTRUMENT_CMD
+
+    echo "instrumenteing end"
+
     ;;
 clean)
     rm -r $BASEDIR/build-instrumented &>/dev/null

Modified: gnunet-java/tools/run-tests
===================================================================
--- gnunet-java/tools/run-tests 2012-07-17 15:25:40 UTC (rev 22722)
+++ gnunet-java/tools/run-tests 2012-07-17 15:38:02 UTC (rev 22723)
@@ -9,7 +9,7 @@
 
 echo Testing classes: $TESTS
 
-java -cp "$BASEDIR/build:$BASEDIR/lib/*" org.junit.runner.JUnitCore $TESTS
+java -cp "$BASEDIR/build:$BASEDIR/build-test:$BASEDIR/lib/*" 
org.junit.runner.JUnitCore $TESTS
 
 cd $OLDPWD
 




reply via email to

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