make copy of transport_api_core.c
[oweals/gnunet.git] / src / transport / transport-testing.c
index 899ef7851465e5dfbe5196285cbfa8ea166d56d8..e3d4b7a9bdc9d34a07f3be7a94ac5157f8c74c4b 100644 (file)
@@ -2,20 +2,18 @@
      This file is part of GNUnet.
      Copyright (C) 2006, 2009, 2015, 2016 GNUnet e.V.
 
-     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 free software: you can redistribute it and/or modify it
+     under the terms of the GNU Affero General Public License as published
+     by the Free Software Foundation, either version 3 of the License,
+     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., 51 Franklin Street, Fifth Floor,
-     Boston, MA 02110-1301, USA.
+     Affero General Public License for more details.
+    
+     You should have received a copy of the GNU Affero General Public License
+     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 /**
  * @file transport-testing.c
@@ -49,7 +47,7 @@ find_peer_context (struct GNUNET_TRANSPORT_TESTING_Handle *tth,
  *
  * @param p1 first peer
  * @param p2 second peer
- * @param cb function to call 
+ * @param cb function to call
  * @param cb_cls closure for @a cb
  */
 void
@@ -66,7 +64,7 @@ GNUNET_TRANSPORT_TESTING_find_connecting_context (struct GNUNET_TRANSPORT_TESTIN
   {
     ccn = cc->next;
     if ( (cc->p1 == p1) &&
-        (cc->p2 == p2) ) 
+        (cc->p2 == p2) )
       cb (cb_cls,
          cc);
   }
@@ -74,7 +72,7 @@ GNUNET_TRANSPORT_TESTING_find_connecting_context (struct GNUNET_TRANSPORT_TESTIN
 
 
 static void
-set_p1c (void *cls,   
+set_p1c (void *cls,
         struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx)
 {
   int *found = cls;
@@ -86,7 +84,17 @@ set_p1c (void *cls,
 
 
 static void
-set_p2c (void *cls,   
+set_mq (void *cls,
+       struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx)
+{
+  struct GNUNET_MQ_Handle *mq = cls;
+
+  cx->mq = mq;
+}
+
+
+static void
+set_p2c (void *cls,
         struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx)
 {
   int *found = cls;
@@ -98,7 +106,7 @@ set_p2c (void *cls,
 
 
 static void
-clear_p1c (void *cls,   
+clear_p1c (void *cls,
           struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx)
 {
   int *found = cls;
@@ -110,7 +118,7 @@ clear_p1c (void *cls,
 
 
 static void
-clear_p2c (void *cls,   
+clear_p2c (void *cls,
         struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx)
 {
   int *found = cls;
@@ -121,9 +129,10 @@ clear_p2c (void *cls,
 }
 
 
-static void
+static void *
 notify_connect (void *cls,
-                const struct GNUNET_PeerIdentity *peer)
+                const struct GNUNET_PeerIdentity *peer,
+               struct GNUNET_MQ_Handle *mq)
 {
   struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
   struct GNUNET_TRANSPORT_TESTING_Handle *tth = p->tth;
@@ -132,14 +141,18 @@ notify_connect (void *cls,
   struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
   struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ccn;
   int found;
+  void *ret;
 
   p2 = find_peer_context (p->tth,
                           peer);
   if (NULL != p->nc)
-    p->nc (p->cb_cls,
-           peer);
+    ret = p->nc (p->cb_cls,
+                peer,
+                mq);
+  else
+    ret = NULL;
 
-  if (p2 != NULL)
+  if (NULL != p2)
     GNUNET_asprintf (&p2_s,
                      "%u (`%s')",
                      p2->no,
@@ -185,6 +198,10 @@ notify_connect (void *cls,
                                 tth->cc_tail,
                                 cc);
   }
+  GNUNET_TRANSPORT_TESTING_find_connecting_context (p,
+                                                   p2,
+                                                   &set_mq,
+                                                   mq);
   /* update set connected flag for all requests */
   for (cc = tth->cc_head; NULL != cc; cc = cc->next)
   {
@@ -223,6 +240,7 @@ notify_connect (void *cls,
       cc->cb = NULL; /* only notify once! */
     }
   }
+  return ret;
 }
 
 
@@ -237,7 +255,8 @@ offer_hello (void *cls);
 
 static void
 notify_disconnect (void *cls,
-                   const struct GNUNET_PeerIdentity *peer)
+                   const struct GNUNET_PeerIdentity *peer,
+                  void *handler_cls)
 {
   struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
   struct GNUNET_TRANSPORT_TESTING_Handle *tth = p->tth;
@@ -245,10 +264,8 @@ notify_disconnect (void *cls,
   /* Find PeerContext */
   int no = 0;
   struct GNUNET_TRANSPORT_TESTING_PeerContext *p2 = NULL;
-  struct TRANSPORT_TESTING_SendJob *sj;
-  struct TRANSPORT_TESTING_SendJob *sjn;
   struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
-  
+
   p2 = find_peer_context (p->tth,
                          peer);
   no = p->no;
@@ -270,25 +287,15 @@ notify_disconnect (void *cls,
   /* notify about disconnect */
   if (NULL != p->nd)
     p->nd (p->cb_cls,
-           peer);
+           peer,
+          handler_cls);
   if (NULL == p2)
     return;
-  /* abort all transmissions this disconnected pair is involved in */
-  for (sj = tth->sj_head; NULL != sj; sj = sjn)
-  {
-    sjn = sj->next;
-    if ( ( (sj->sender == p2) &&
-          (sj->receiver == p) ) ||
-        ( (sj->receiver == p2) &&
-          (sj->sender == p) ) )
-    {
-      GNUNET_CONTAINER_DLL_remove (tth->sj_head,
-                                  tth->sj_tail,
-                                  sj);
-      GNUNET_TRANSPORT_notify_transmit_ready_cancel (sj->th);
-      GNUNET_free (sj);
-    }
-  }
+  /* clear MQ, it is now invalid */
+  GNUNET_TRANSPORT_TESTING_find_connecting_context (p,
+                                                   p2,
+                                                   &set_mq,
+                                                   NULL);
   /* update set connected flags for all requests */
   GNUNET_TRANSPORT_TESTING_find_connecting_context (p,
                                                    p2,
@@ -321,22 +328,6 @@ notify_disconnect (void *cls,
 }
 
 
-static void
-notify_receive (void *cls,
-                const struct GNUNET_PeerIdentity *peer,
-                const struct GNUNET_MessageHeader *message)
-{
-  struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
-
-  if (NULL == p)
-    return;
-  if (NULL != p->rec)
-    p->rec (p->cb_cls,
-            peer,
-            message);
-}
-
-
 static void
 get_hello (void *cb_cls,
            const struct GNUNET_MessageHeader *message)
@@ -359,8 +350,7 @@ get_hello (void *cb_cls,
          "Peer %u (`%s') successfully started\n",
          p->no,
          GNUNET_i2s (&p->id));
-    p->start_cb (p,
-                 p->start_cb_cls);
+    p->start_cb (p->start_cb_cls);
     p->start_cb = NULL;
   }
 }
@@ -371,7 +361,7 @@ get_hello (void *cb_cls,
  * @param tth the testing handle
  * @param cfgname configuration file
  * @param peer_id a unique number to identify the peer
- * @param rec receive callback
+ * @param handlers functions for receiving messages
  * @param nc connect callback
  * @param nd disconnect callback
  * @param cb_cls closure for callback
@@ -383,16 +373,17 @@ struct GNUNET_TRANSPORT_TESTING_PeerContext *
 GNUNET_TRANSPORT_TESTING_start_peer (struct GNUNET_TRANSPORT_TESTING_Handle *tth,
                                      const char *cfgname,
                                      int peer_id,
-                                     GNUNET_TRANSPORT_ReceiveCallback rec,
-                                     GNUNET_TRANSPORT_NotifyConnect nc,
-                                     GNUNET_TRANSPORT_NotifyDisconnect nd,
+                                     const struct GNUNET_MQ_MessageHandler *handlers,
+                                     GNUNET_TRANSPORT_NotifyConnecT nc,
+                                     GNUNET_TRANSPORT_NotifyDisconnecT nd,
                                     void *cb_cls,
-                                     GNUNET_TRANSPORT_TESTING_StartCallback start_cb,
+                                     GNUNET_SCHEDULER_TaskCallback start_cb,
                                      void *start_cb_cls)
 {
   char *emsg = NULL;
   struct GNUNET_TRANSPORT_TESTING_PeerContext *p;
-  struct GNUNET_PeerIdentity *dummy;
+  struct GNUNET_PeerIdentity dummy;
+  unsigned int i;
 
   if (GNUNET_NO == GNUNET_DISK_file_test (cfgname))
   {
@@ -406,13 +397,24 @@ GNUNET_TRANSPORT_TESTING_start_peer (struct GNUNET_TRANSPORT_TESTING_Handle *tth
   p->tth = tth;
   p->nc = nc;
   p->nd = nd;
-  p->rec = rec;
+  if (NULL != handlers)
+  {
+    for (i=0;NULL != handlers[i].cb;i++) ;
+    p->handlers = GNUNET_new_array (i + 1,
+                                   struct GNUNET_MQ_MessageHandler);
+    GNUNET_memcpy (p->handlers,
+                  handlers,
+                  i * sizeof (struct GNUNET_MQ_MessageHandler));
+  }
   if (NULL != cb_cls)
     p->cb_cls = cb_cls;
   else
     p->cb_cls = p;
   p->start_cb = start_cb;
-  p->start_cb_cls = start_cb_cls;
+  if (NULL != start_cb_cls)
+    p->start_cb_cls = start_cb_cls;
+  else
+    p->start_cb_cls = p;
   GNUNET_CONTAINER_DLL_insert (tth->p_head,
                                tth->p_tail,
                                p);
@@ -479,14 +481,16 @@ GNUNET_TRANSPORT_TESTING_start_peer (struct GNUNET_TRANSPORT_TESTING_Handle *tth
        "Peer %u configured with identity `%s'\n",
        p->no,
        GNUNET_i2s_full (&p->id));
-
-  p->th = GNUNET_TRANSPORT_connect (p->cfg,
-                                    NULL,
-                                    p,
-                                    &notify_receive,
-                                    &notify_connect,
-                                    &notify_disconnect);
-  if (NULL == p->th)
+  p->tmh = GNUNET_TRANSPORT_manipulation_connect (p->cfg);
+  p->th = GNUNET_TRANSPORT_core_connect (p->cfg,
+                                        NULL,
+                                        handlers,
+                                        p,
+                                        &notify_connect,
+                                        &notify_disconnect,
+                                        NULL);
+  if ( (NULL == p->th) ||
+       (NULL == p->tmh) )
   {
     LOG (GNUNET_ERROR_TYPE_ERROR,
          "Failed to connect to transport service for peer `%s': `%s'\n",
@@ -505,10 +509,11 @@ GNUNET_TRANSPORT_TESTING_start_peer (struct GNUNET_TRANSPORT_TESTING_Handle *tth
     GNUNET_TRANSPORT_TESTING_stop_peer (p);
     return NULL;
   }
-  p->ghh = GNUNET_TRANSPORT_get_hello (p->cfg,
-                                       &get_hello,
+  p->ghh = GNUNET_TRANSPORT_hello_get (p->cfg,
+                                      GNUNET_TRANSPORT_AC_ANY,
+                                      &get_hello,
                                        p);
-  GNUNET_assert (p->ghh != NULL);
+  GNUNET_assert (NULL != p->ghh);
   return p;
 }
 
@@ -523,12 +528,12 @@ GNUNET_TRANSPORT_TESTING_start_peer (struct GNUNET_TRANSPORT_TESTING_Handle *tth
  */
 int
 GNUNET_TRANSPORT_TESTING_restart_peer (struct GNUNET_TRANSPORT_TESTING_PeerContext *p,
-                                       GNUNET_TRANSPORT_TESTING_StartCallback restart_cb,
+                                       GNUNET_SCHEDULER_TaskCallback restart_cb,
                                        void *restart_cb_cls)
 {
   struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
   struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ccn;
+
   /* shutdown */
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Stopping peer %u (`%s')\n",
@@ -536,14 +541,19 @@ GNUNET_TRANSPORT_TESTING_restart_peer (struct GNUNET_TRANSPORT_TESTING_PeerConte
        GNUNET_i2s (&p->id));
   if (NULL != p->ghh)
   {
-    GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
+    GNUNET_TRANSPORT_hello_get_cancel (p->ghh);
     p->ghh = NULL;
   }
   if (NULL != p->th)
   {
-    GNUNET_TRANSPORT_disconnect (p->th);
+    GNUNET_TRANSPORT_core_disconnect (p->th);
     p->th = NULL;
   }
+  if (NULL != p->tmh)
+  {
+    GNUNET_TRANSPORT_manipulation_disconnect (p->tmh);
+    p->tmh = NULL;
+  }
   for (cc = p->tth->cc_head; NULL != cc; cc = ccn)
   {
     ccn = cc->next;
@@ -586,15 +596,17 @@ GNUNET_TRANSPORT_TESTING_restart_peer (struct GNUNET_TRANSPORT_TESTING_PeerConte
   p->start_cb = restart_cb;
   p->start_cb_cls = restart_cb_cls;
 
-  p->th = GNUNET_TRANSPORT_connect (p->cfg,
-                                    NULL,
-                                    p,
-                                    &notify_receive,
-                                    &notify_connect,
-                                    &notify_disconnect);
+  p->th = GNUNET_TRANSPORT_core_connect (p->cfg,
+                                        NULL,
+                                        p->handlers,
+                                        p,
+                                        &notify_connect,
+                                        &notify_disconnect,
+                                        NULL);
   GNUNET_assert (NULL != p->th);
   p->ats = GNUNET_ATS_connectivity_init (p->cfg);
-  p->ghh = GNUNET_TRANSPORT_get_hello (p->cfg,
+  p->ghh = GNUNET_TRANSPORT_hello_get (p->cfg,
+                                      GNUNET_TRANSPORT_AC_ANY,
                                        &get_hello,
                                        p);
   GNUNET_assert (NULL != p->ghh);
@@ -623,12 +635,17 @@ GNUNET_TRANSPORT_TESTING_stop_peer (struct GNUNET_TRANSPORT_TESTING_PeerContext
   }
   if (NULL != p->ghh)
   {
-    GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
+    GNUNET_TRANSPORT_hello_get_cancel (p->ghh);
     p->ghh = NULL;
   }
+  if (NULL != p->tmh)
+  {
+    GNUNET_TRANSPORT_manipulation_disconnect (p->tmh);
+    p->tmh = NULL;
+  }
   if (NULL != p->th)
   {
-    GNUNET_TRANSPORT_disconnect (p->th);
+    GNUNET_TRANSPORT_core_disconnect (p->th);
     p->th = NULL;
   }
   if (NULL != p->peer)
@@ -659,6 +676,11 @@ GNUNET_TRANSPORT_TESTING_stop_peer (struct GNUNET_TRANSPORT_TESTING_PeerContext
     GNUNET_CONFIGURATION_destroy (p->cfg);
     p->cfg = NULL;
   }
+  if (NULL != p->handlers)
+  {
+    GNUNET_free (p->handlers);
+    p->handlers = NULL;
+  }
   GNUNET_CONTAINER_DLL_remove (tth->p_head,
                                tth->p_tail,
                                p);
@@ -754,7 +776,7 @@ GNUNET_TRANSPORT_TESTING_connect_peers (struct GNUNET_TRANSPORT_TESTING_PeerCont
       break;
     }
   }
-         
+
   cc = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_ConnectRequest);
   cc->p1 = p1;
   cc->p2 = p2;