fixes
authorChristian Grothoff <christian@grothoff.org>
Thu, 12 May 2011 14:01:29 +0000 (14:01 +0000)
committerChristian Grothoff <christian@grothoff.org>
Thu, 12 May 2011 14:01:29 +0000 (14:01 +0000)
src/mesh/gnunet-service-mesh.c
src/mesh/mesh_api_new.c
src/mesh/test_mesh_api.c

index d545d8dd39f7d4d0a534fbfe6cd313c37bc6d589..f07465ae2a9a0430d354df272dea1c337349b576 100644 (file)
@@ -1296,6 +1296,27 @@ core_disconnect (void *cls,
 /************************      MAIN FUNCTIONS      ****************************/
 /******************************************************************************/
 
+/**
+ * Task run during shutdown.
+ *
+ * @param cls unused
+ * @param tc unused
+ */
+static void
+shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ if (core_handle != NULL)
+    {
+      GNUNET_CORE_disconnect (core_handle);
+      core_handle = NULL;
+    }
+ if (dht_handle != NULL)
+    {
+      GNUNET_DHT_disconnect (dht_handle);
+      dht_handle = NULL;
+    } 
+}
+
 /**
  * Process mesh requests.
  *
@@ -1331,6 +1352,11 @@ GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "******* MESH DEBUG MESSAGE 4 ********\n");
     if (dht_handle == NULL) {
         GNUNET_break(0);
     }
+
+  /* Scheduled the task to clean up when shutdown is called */
+    GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
+                                 &shutdown_task, NULL);
+
 }
 
 /**
index ca68279ed3d4a59114716bf7955920c67c8a5869..989b526aeb5da70e5598e6f9fe2f8e42096bd5ce 100644 (file)
@@ -76,6 +76,9 @@ struct GNUNET_MESH_Handle {
      */
     GNUNET_MESH_TunnelEndHandler                *cleaner;
 
+
+    struct GNUNET_CLIENT_TransmitHandle *th;
+
     /**
      * Closure for all the handlers given by the client
      */
@@ -127,26 +130,28 @@ struct GNUNET_MESH_TransmitHandle {
  * @param buf where the callee should write the message
  * @return number of bytes written to buf
  */
-size_t 
+static size_t 
 send_connect_packet (void *cls, size_t size, void *buf) {
-    struct GNUNET_MESH_Handle           *h;
+    struct GNUNET_MESH_Handle           *h = cls;
     struct GNUNET_MESH_ClientConnect    *msg;
     uint16_t                            *types;
     int                                 ntypes;
     GNUNET_MESH_ApplicationType         *apps;
     int                                 napps;
 
-    if(0 == size || buf == NULL) {
+    h->th = NULL;
+    if (0 == size || buf == NULL) {
         GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Send: buffer size 0 or buffer invalid\n");
+       // FIXME: disconnect, reconnect, retry!
         return 0;
     }
-    if(sizeof(struct GNUNET_MessageHeader) > size) {
+    if (sizeof(struct GNUNET_MessageHeader) > size) {
         GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Send: buffer size too small\n");
+       // FIXME: disconnect, reconnect, retry!
         return 0;
     }
 
     msg = (struct GNUNET_MESH_ClientConnect *) buf;
-    h = (struct GNUNET_MESH_Handle *) cls;
     msg->header.type = GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT;
 
     for(ntypes = 0, types = NULL; h->message_handlers[ntypes].type; ntypes++) {
@@ -219,7 +224,8 @@ GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
                      void *cls,
                      GNUNET_MESH_TunnelEndHandler cleaner,
                      const struct GNUNET_MESH_MessageHandler *handlers,
-                     const GNUNET_MESH_ApplicationType *stypes) {
+                     const GNUNET_MESH_ApplicationType *stypes) 
+{
     struct GNUNET_MESH_Handle           *h;
     size_t                              size;
 
@@ -247,12 +253,12 @@ GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
     size += h->n_handlers * sizeof(uint16_t);
     size += h->n_applications * sizeof(GNUNET_MESH_ApplicationType);
 
-    GNUNET_CLIENT_notify_transmit_ready(h->mesh,
-                                        size,
-                                        GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
-                                        GNUNET_NO,
-                                        &send_connect_packet,
-                                        (void *)h);
+    h->th = GNUNET_CLIENT_notify_transmit_ready(h->mesh,
+                                               size,
+                                               GNUNET_TIME_UNIT_FOREVER_REL,
+                                               GNUNET_YES,
+                                               &send_connect_packet,
+                                               (void *)h);
 
     return h;
 }
@@ -263,10 +269,14 @@ GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
  *
  * @param handle connection to mesh to disconnect
  */
-void GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle) {
-
-    GNUNET_free(handle);
-    return;
+void 
+GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle) 
+{
+  if (NULL != handle->th) 
+    GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
+  if (NULL != handle->mesh)
+    GNUNET_CLIENT_disconnect (handle->mesh, GNUNET_NO);
+  GNUNET_free(handle);
 }
 
 
index 00212eb4976afa7e1888bf6149171bfc86a236fd..5f988f555e23a14d2d438f3bb91134c1ed398b32 100644 (file)
@@ -7,16 +7,31 @@ static struct GNUNET_MESH_MessageHandler handlers[] = {
     {NULL, 0, 0}
 };
 
+static struct GNUNET_OS_Process            *arm_pid;
+
+static struct GNUNET_MESH_Handle           *mesh;
+
+static struct GNUNET_DHT_Handle            *dht;
+
+static void
+  do_shutdown (void *cls,
+              const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  if (NULL != mesh)
+    GNUNET_MESH_disconnect (mesh);
+    if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM))
+        GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
+    GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (arm_pid));
+    GNUNET_OS_process_close (arm_pid);
+}
+
 
 static void
 run (void *cls,
      char *const *args,
      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg) {
-    struct GNUNET_OS_Process            *arm_pid;
-    struct GNUNET_MESH_Handle           *mesh;
-    struct GNUNET_DHT_Handle            *dht;
     GNUNET_MESH_ApplicationType         app;
-    char                                buffer[2048];
+    // char                                buffer[2048];
 
 
     arm_pid = GNUNET_OS_start_process (NULL, NULL,
@@ -41,16 +56,15 @@ run (void *cls,
     }
 
     /* do real test work here */
-    if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM))
-        GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
-    GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (arm_pid));
-    GNUNET_OS_process_close (arm_pid);
-
-    return;
+    GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
+                                 &do_shutdown,
+                                 NULL);
 }
 
 
 
+
+
 int main (int argc, char *argv[]) {
     int ret;
     char *const argv2[] = {"test-mesh-api",