gnunet-svn
[Top][All Lists]
Advanced

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

[GNUnet-SVN] r24728 - in gnunet-java: src/org/gnunet/util test/org/gnune


From: gnunet
Subject: [GNUnet-SVN] r24728 - in gnunet-java: src/org/gnunet/util test/org/gnunet/statistics
Date: Mon, 5 Nov 2012 14:07:58 +0100

Author: dold
Date: 2012-11-05 14:07:58 +0100 (Mon, 05 Nov 2012)
New Revision: 24728

Modified:
   gnunet-java/src/org/gnunet/util/Connection.java
   gnunet-java/test/org/gnunet/statistics/StatisticsTest.java
Log:
fixed a bug in org.gnunet.util.Connection

Modified: gnunet-java/src/org/gnunet/util/Connection.java
===================================================================
--- gnunet-java/src/org/gnunet/util/Connection.java     2012-11-05 13:05:47 UTC 
(rev 24727)
+++ gnunet-java/src/org/gnunet/util/Connection.java     2012-11-05 13:07:58 UTC 
(rev 24728)
@@ -34,6 +34,7 @@
 import java.nio.channels.SocketChannel;
 import java.nio.channels.spi.SelectorProvider;
 import java.util.LinkedList;
+import java.util.List;
 
 /**
  * Integrates sockets with the gnunet-java message loop / the scheduler.
@@ -47,7 +48,7 @@
      */
     private SocketChannel connectionChannel = null;
 
-    private Iterable<AddressProbe> addressProbes = null;
+    private List<AddressProbe> addressProbes = null;
 
     /**
      * The task that is currently used by the resolve mechanism.
@@ -111,6 +112,7 @@
     private static class AddressProbe {
         Cancelable connectTask;
         SocketChannel channel;
+
         public void cancel() {
             if (connectTask != null) {
                 connectTask.cancel();
@@ -201,8 +203,8 @@
                 try {
                     int n = connectionChannel.read(recvBuffer);
                     if (n == -1) {
-                        logger.warn("lost connection to service",
-                                recvBuffer.position());
+                        currentReceiveHelper = null;
+                        logger.warn("lost connection to service");
                         connectionChannel.close();
                         connectionChannel = null;
                         if (Connection.this.currentTransmitHelper != null) {
@@ -295,8 +297,6 @@
         }
 
         public void cancel() {
-            logger.debug("canceled");
-
             if (transmitTask != null) {
                 transmitTask.cancel();
                 transmitTask = null;
@@ -430,7 +430,7 @@
                             if 
(ctx.reasons.contains(Scheduler.Reason.SHUTDOWN)) {
                                 return;
                             }
-                            Connection.this.finishConnect(channel);
+                            Connection.this.finishConnect(addressProbe);
                         }
                     });
 
@@ -457,46 +457,61 @@
     }
 
 
-    private void finishConnect(SocketChannel channel) {
-        if (isConnected()) {
+    private void finishConnect(AddressProbe probe) {
+        // can happen if the addres probe task was already scheduled
+        if (connectionChannel != null) {
+            try {
+                probe.channel.close();
+            } catch (IOException e) {
+                logger.error("could not close channel", e);
+            }
             return;
         }
 
+        SocketChannel channel = probe.channel;
         boolean connected;
         try {
             connected = channel.finishConnect();
-            connectionChannel = channel;
-
-            for (AddressProbe addressProbe : addressProbes) {
-                if (addressProbe.connectTask != null) {
-                    addressProbe.connectTask.cancel();
-                }
-            }
         } catch (IOException e) {
-            logger.debug("finishConnect() was not successful: {}", (Object) e);
+            logger.error("finishConnect() was not successful: {}", (Object) e);
             return;
         }
 
-        if (connected) {
-            if (currentTransmitHelper != null) {
-                currentTransmitHelper.start();
+        if (!connected) {
+            logger.error("socket reported OP_CONNECT but is not connected");
+            return;
+        }
+
+        for (AddressProbe addressProbe : addressProbes) {
+            if (addressProbe != probe && addressProbe.connectTask != null) {
+                addressProbe.connectTask.cancel();
+                try {
+                    addressProbe.channel.close();
+                } catch (IOException e) {
+                    logger.error("could not close channel", e);
+                }
             }
-            if (currentReceiveHelper != null && !currentReceiveHelper.working) 
{
-                currentReceiveHelper.schedule();
-            }
-            Continuation c = notifyConnectedContinuation;
-            notifyConnectedContinuation = null;
-            if (notifyConnectedTimeout != null) {
-                notifyConnectedTimeout.cancel();
-                notifyConnectedTimeout = null;
-            }
-            if (c != null) {
-                c.cont(true);
-            }
+        }
 
-        } else {
-            logger.error("socket reported OP_CONNECT but is not connected");
+        addressProbes.clear();
+
+        connectionChannel = channel;
+
+        if (currentTransmitHelper != null) {
+            currentTransmitHelper.start();
         }
+        if (currentReceiveHelper != null && !currentReceiveHelper.working) {
+            currentReceiveHelper.schedule();
+        }
+        Continuation c = notifyConnectedContinuation;
+        notifyConnectedContinuation = null;
+        if (notifyConnectedTimeout != null) {
+            notifyConnectedTimeout.cancel();
+            notifyConnectedTimeout = null;
+        }
+        if (c != null) {
+            c.cont(true);
+        }
     }
 
     /**
@@ -605,7 +620,7 @@
      * Call cont after establishing the connection or when the timeout has 
occured.
      *
      * @param timeout timeout
-     * @param cont continuation to call
+     * @param cont    continuation to call
      * @return
      */
     /* package-protected */ Cancelable notifyConnected(RelativeTime timeout, 
final Continuation cont) {

Modified: gnunet-java/test/org/gnunet/statistics/StatisticsTest.java
===================================================================
--- gnunet-java/test/org/gnunet/statistics/StatisticsTest.java  2012-11-05 
13:05:47 UTC (rev 24727)
+++ gnunet-java/test/org/gnunet/statistics/StatisticsTest.java  2012-11-05 
13:07:58 UTC (rev 24728)
@@ -93,7 +93,7 @@
         Assert.assertTrue(contReached.get());
     }
 
-    //@Test(timeout = 1000)
+    @Test(timeout = 1000)
     public void test_statistics_get_set() {
         Program.configureLogging();
         final TestingSubsystem ts = new TestingSubsystem("statistics");
@@ -146,7 +146,7 @@
     }
 
 
-    //@Test
+    @Test
     public void test_watch_restart() {
         Program.configureLogging("DEBUG");
         final TestingSubsystem ts = new TestingSubsystem("statistics");
@@ -160,18 +160,25 @@
             }
         });
 
-        Scheduler.addDelayed(new RelativeTime(300), new Scheduler.Task() {
+        Scheduler.addDelayed(new RelativeTime(100), new Scheduler.Task() {
             @Override
             public void run(Scheduler.RunContext ctx) {
                 ts.restart();
             }
         });
 
+        Scheduler.addDelayed(new RelativeTime(200), new Scheduler.Task() {
+            @Override
+            public void run(Scheduler.RunContext ctx) {
+                stat.destroy();
+            }
+        });
+
         Scheduler.run();
     }
 
 
-    //@Test
+    @Test
     public void test_watch_simple() {
         Program.configureLogging("DEBUG");
         final TestingSubsystem ts = new TestingSubsystem("statistics");
@@ -211,7 +218,7 @@
     }
 
 
-    //@Test(timeout = 10000)
+    @Test()
     public void test_watch() {
         Program.configureLogging("DEBUG");
         final TestingSubsystem ts = new TestingSubsystem("statistics");
@@ -272,6 +279,10 @@
         Scheduler.run();
 
         Assert.assertEquals(4, updates.get().intValue());
+    }
 
+    public static void main(String[] args) {
+        StatisticsTest st = new StatisticsTest();
+        st.test_watch_simple();
     }
 }




reply via email to

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