hxing
[oweals/gnunet.git] / src / core / test_core_quota_compliance.c
index 41d156c6bc2d6fa028990ff0e0e3cd404afa87a9..5fce06b31d0f8d7118c31d0bd9587a2e1584d558 100644 (file)
@@ -33,8 +33,8 @@
 #include "gnunet_transport_service.h"
 #include "gnunet_statistics_service.h"
 
-#define VERBOSE GNUNET_NO
-#define DEBUG_TRANSMISSION GNUNET_NO
+#define VERBOSE GNUNET_EXTRA_LOGGING
+#define DEBUG_TRANSMISSION GNUNET_EXTRA_LOGGING
 
 #define SYMMETRIC 0
 #define ASYMMETRIC_SEND_LIMITED 1
@@ -77,10 +77,12 @@ struct PeerContext
 {
   struct GNUNET_CONFIGURATION_Handle *cfg;
   struct GNUNET_CORE_Handle *ch;
+  struct GNUNET_CORE_TransmitHandle *nth;
   struct GNUNET_PeerIdentity id;
   struct GNUNET_TRANSPORT_Handle *th;
   struct GNUNET_MessageHeader *hello;
   struct GNUNET_STATISTICS_Handle *stats;
+  struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
   int connect_status;
 #if START_ARM
   struct GNUNET_OS_Process *arm_proc;
@@ -122,8 +124,14 @@ terminate_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct GNUNET_CORE_Handle *ch;
 
-  GNUNET_TRANSPORT_get_hello_cancel (p2.th, &process_hello, &p2);
-  GNUNET_TRANSPORT_get_hello_cancel (p1.th, &process_hello, &p1);
+  err_task = GNUNET_SCHEDULER_NO_TASK;
+  GNUNET_TRANSPORT_get_hello_cancel (p2.ghh);
+  GNUNET_TRANSPORT_get_hello_cancel (p1.ghh);
+  if (p1.nth != NULL)
+  {
+    GNUNET_CORE_notify_transmit_ready_cancel (p1.nth);
+    p1.nth = NULL;
+  }
   ch = p1.ch;
   p1.ch = NULL;
   GNUNET_CORE_disconnect (ch);
@@ -140,15 +148,21 @@ terminate_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 static void
 terminate_task_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
+  err_task = GNUNET_SCHEDULER_NO_TASK;
+
   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Testcase failed!\n");
   //GNUNET_break (0);
-
+  if (p1.nth != NULL)
+  {
+    GNUNET_CORE_notify_transmit_ready_cancel (p1.nth);
+    p1.nth = NULL;
+  }
   if (measure_task != GNUNET_SCHEDULER_NO_TASK)
     GNUNET_SCHEDULER_cancel (measure_task);
 
-  GNUNET_TRANSPORT_get_hello_cancel (p1.th, &process_hello, &p1);
-  GNUNET_TRANSPORT_get_hello_cancel (p2.th, &process_hello, &p2);
+  GNUNET_TRANSPORT_get_hello_cancel (p1.ghh);
+  GNUNET_TRANSPORT_get_hello_cancel (p2.ghh);
 
   GNUNET_CORE_disconnect (p1.ch);
   p1.ch = NULL;
@@ -285,7 +299,7 @@ measurement_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Max. outbound quota allowed: %llu kB/s\n",max_quota_out/1024);
 */
   GNUNET_SCHEDULER_cancel (err_task);
-  GNUNET_SCHEDULER_add_now (&terminate_task, NULL);
+  err_task = GNUNET_SCHEDULER_add_now (&terminate_task, NULL);
 
 }
 
@@ -296,15 +310,16 @@ transmit_ready (void *cls, size_t size, void *buf)
   struct TestMessage hdr;
   unsigned int ret;
 
+  p1.nth = NULL;
   GNUNET_assert (size <= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE);
   if (buf == NULL)
   {
     if ((p1.ch != NULL) && (p1.connect_status == 1))
       GNUNET_break (NULL !=
-                    GNUNET_CORE_notify_transmit_ready (p1.ch, GNUNET_NO, 0,
-                                                       FAST_TIMEOUT, &p2.id,
-                                                       MESSAGESIZE,
-                                                       &transmit_ready, &p1));
+                    (p1.nth = GNUNET_CORE_notify_transmit_ready (p1.ch, GNUNET_NO, 0,
+                                                               FAST_TIMEOUT, &p2.id,
+                                                               MESSAGESIZE,
+                                                                &transmit_ready, &p1)));
     return 0;
   }
   GNUNET_assert (tr_n < TOTAL_MSGS);
@@ -348,7 +363,7 @@ connect_notify (void *cls, const struct GNUNET_PeerIdentity *peer,
   struct PeerContext *pc = cls;
 
   if (0 == memcmp (&pc->id, peer, sizeof (struct GNUNET_PeerIdentity)))
-    return;
+    return; /* loopback */
   GNUNET_assert (pc->connect_status == 0);
   pc->connect_status = 1;
   if (pc == &p1)
@@ -372,10 +387,10 @@ connect_notify (void *cls, const struct GNUNET_PeerIdentity *peer,
                                       NULL);
 
     GNUNET_break (NULL !=
-                  GNUNET_CORE_notify_transmit_ready (p1.ch, GNUNET_NO, 0,
-                                                     TIMEOUT, &p2.id,
-                                                     MESSAGESIZE,
-                                                     &transmit_ready, &p1));
+                  (p1.nth = GNUNET_CORE_notify_transmit_ready (p1.ch, GNUNET_NO, 0,
+                                                              TIMEOUT, &p2.id,
+                                                              MESSAGESIZE,
+                                                              &transmit_ready, &p1)));
   }
 }
 
@@ -386,8 +401,13 @@ disconnect_notify (void *cls, const struct GNUNET_PeerIdentity *peer)
   struct PeerContext *pc = cls;
 
   if (0 == memcmp (&pc->id, peer, sizeof (struct GNUNET_PeerIdentity)))
-    return;
+    return; /* loopback */
   pc->connect_status = 0;
+  if (pc->nth != NULL)
+  {
+    GNUNET_CORE_notify_transmit_ready_cancel (pc->nth);
+    pc->nth = NULL;
+  }
 #if DEBUG_TRANSMISSION
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Encrypted connection to `%4s' cut\n",
               GNUNET_i2s (peer));
@@ -484,8 +504,7 @@ static struct GNUNET_CORE_MessageHandler handlers[] = {
 
 static void
 init_notify (void *cls, struct GNUNET_CORE_Handle *server,
-             const struct GNUNET_PeerIdentity *my_identity,
-             const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
+             const struct GNUNET_PeerIdentity *my_identity)
 {
   struct PeerContext *p = cls;
 
@@ -494,15 +513,15 @@ init_notify (void *cls, struct GNUNET_CORE_Handle *server,
               GNUNET_i2s (my_identity));
   GNUNET_assert (server != NULL);
   p->id = *my_identity;
-  p->ch = server;
+  GNUNET_assert (p->ch == server);
   if (cls == &p1)
   {
     GNUNET_assert (ok == 2);
     OKPP;
     /* connect p2 */
-    GNUNET_CORE_connect (p2.cfg, 1, &p2, &init_notify, &connect_notify,
-                         &disconnect_notify, NULL, &inbound_notify, GNUNET_YES,
-                         &outbound_notify, GNUNET_YES, handlers);
+    p2.ch = GNUNET_CORE_connect (p2.cfg, 1, &p2, &init_notify, &connect_notify,
+                                &disconnect_notify, NULL, &inbound_notify, GNUNET_YES,
+                                &outbound_notify, GNUNET_YES, handlers);
   }
   else
   {
@@ -514,7 +533,8 @@ init_notify (void *cls, struct GNUNET_CORE_Handle *server,
                 "Asking core (1) to connect to peer `%4s'\n",
                 GNUNET_i2s (&p2.id));
 #endif
-    GNUNET_CORE_peer_request_connect (p1.ch, &p2.id, NULL, NULL);
+    GNUNET_TRANSPORT_try_connect (p1.th, &p2.id);
+    GNUNET_TRANSPORT_try_connect (p2.th, &p1.id);
   }
 }
 
@@ -563,7 +583,7 @@ setup_peer (struct PeerContext *p, const char *cfgname)
   GNUNET_assert (p->stats != NULL);
   p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL, p, NULL, NULL, NULL);
   GNUNET_assert (p->th != NULL);
-  GNUNET_TRANSPORT_get_hello (p->th, &process_hello, p);
+  p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &process_hello, p);
 }
 
 
@@ -609,9 +629,9 @@ run (void *cls, char *const *args, const char *cfgfile,
                                                         "TOTAL_QUOTA_OUT",
                                                         &current_quota_p2_out));
 
-  GNUNET_CORE_connect (p1.cfg, 1, &p1, &init_notify, &connect_notify,
-                       &disconnect_notify, NULL, &inbound_notify, GNUNET_YES,
-                       &outbound_notify, GNUNET_YES, handlers);
+  p1.ch = GNUNET_CORE_connect (p1.cfg, 1, &p1, &init_notify, &connect_notify,
+                              &disconnect_notify, NULL, &inbound_notify, GNUNET_YES,
+                              &outbound_notify, GNUNET_YES, handlers);
 }