handling replies continuously from server
[oweals/gnunet.git] / src / util / test_service.c
index 078be7a41cc83cd6eb292c9c79b83c97e0f5a43e..55472497210227a89b57f4657ed0c16a00ed01b1 100644 (file)
@@ -44,11 +44,42 @@ static int ok = 1;
 static struct GNUNET_CLIENT_Connection *client;
 
 
+
+
+static void
+do_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  if (NULL != client)
+  {
+    GNUNET_CLIENT_disconnect (client);
+    client = NULL;
+  }
+  if (NULL != sctx)
+  {
+    GNUNET_SERVICE_stop (sctx);
+    sctx = NULL;
+  }
+  else
+  {
+    GNUNET_SCHEDULER_shutdown ();
+  }
+}
+
+
 static size_t
 build_msg (void *cls, size_t size, void *buf)
 {
   struct GNUNET_MessageHeader *msg = buf;
 
+  if (size < sizeof (struct GNUNET_MessageHeader))
+  {
+    /* timeout */
+    GNUNET_break (0);
+    GNUNET_SCHEDULER_add_now (&do_stop, NULL);
+    ok = 1;
+    return 0;
+  }
+
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client connected, transmitting\n");
   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
   msg->type = htons (MY_TYPE);
@@ -62,7 +93,6 @@ ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
 
-
   GNUNET_assert (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE));
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service confirmed running\n");
   client = GNUNET_CLIENT_connect ("test_service", cfg);
@@ -76,24 +106,15 @@ ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 }
 
 
-static void
-do_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
-  GNUNET_CLIENT_disconnect (client);
-  GNUNET_SERVICE_stop (sctx);
-}
-
-
 static void
 recv_cb (void *cls, struct GNUNET_SERVER_Client *sc,
          const struct GNUNET_MessageHeader *message)
 {
+  if (NULL == message)
+    return;
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receiving client message...\n");
   GNUNET_SERVER_receive_done (sc, GNUNET_OK);
-  if (sctx != NULL)
-    GNUNET_SCHEDULER_add_now (&do_stop, NULL);
-  else
-    GNUNET_SCHEDULER_shutdown ();
+  GNUNET_SCHEDULER_add_now (&do_stop, NULL);
   ok = 0;
 }
 
@@ -257,7 +278,6 @@ main (int argc, char *argv[])
                     NULL);
   ret += check ();
   ret += check ();
-
   // FIXME
 #ifndef MINGW
   s = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
@@ -280,7 +300,6 @@ main (int argc, char *argv[])
     ret += check6 ();
   }
   ret += check_start_stop ();
-
   return ret;
 }