- more code
authorMatthias Wachs <wachs@net.in.tum.de>
Wed, 22 Feb 2012 18:05:18 +0000 (18:05 +0000)
committerMatthias Wachs <wachs@net.in.tum.de>
Wed, 22 Feb 2012 18:05:18 +0000 (18:05 +0000)
src/namestore/gnunet-service-namestore.c
src/namestore/namestore.h
src/namestore/namestore_api.c
src/namestore/test_namestore_api.c

index af8234638789fbb1bce6926f7039ad6081e42b1a..59126ba6a78b7172dd26276e4973a5260d5ee679 100644 (file)
@@ -54,9 +54,34 @@ cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   GNUNET_free (db_lib_name);
 }
 
-static void handle_start ()
+/**
+ * Called whenever a client is disconnected.  Frees our
+ * resources associated with that client.
+ *
+ * @param cls closure
+ * @param client identification of the client
+ */
+static void
+client_disconnect_notification (void *cls, struct GNUNET_SERVER_Client *client)
+{
+  if (NULL != client)
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p disconnected \n", client);
+}
+
+static void handle_start (void *cls,
+                          struct GNUNET_SERVER_Client * client,
+                          const struct GNUNET_MessageHeader * message)
+{
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p connected\n");
+  GNUNET_SERVER_receive_done (client, GNUNET_OK);
+}
+
+static void handle_lookup_name (void *cls,
+                          struct GNUNET_SERVER_Client * client,
+                          const struct GNUNET_MessageHeader * message)
 {
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "START");
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_LOOKUP_NAME");
+  GNUNET_SERVER_receive_done (client, GNUNET_OK);
 }
 
 
@@ -78,6 +103,8 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
     {&handle_start, NULL,
      GNUNET_MESSAGE_TYPE_NAMESTORE_START, sizeof (struct StartMessage)},
+    {&handle_lookup_name, NULL,
+     GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME, 0},
     {NULL, NULL, 0, 0}
   };
 
@@ -98,6 +125,9 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
 
   /* Configuring server handles */
   GNUNET_SERVER_add_handlers (server, handlers);
+  GNUNET_SERVER_disconnect_notify (server,
+                                   &client_disconnect_notification,
+                                   NULL);
 
   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
                                 NULL);
index 4266a32c7815bb5218b7ec601c0f1f8bf6d679cd..d469ac836db8270e56046f04087ec0dc6e0c5467 100644 (file)
 #ifndef NAMESTORE_H
 #define NAMESTORE_H
 
+/*
+ * Collect message types here, move to protocols later
+ */
+#define GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME 431
+
 GNUNET_NETWORK_STRUCT_BEGIN
 /**
- * Change in blacklisting (either request or notification,
- * depending on which direction it is going).
+ * Connect to namestore service
  */
 struct StartMessage
 {
@@ -42,5 +46,21 @@ struct StartMessage
 };
 GNUNET_NETWORK_STRUCT_END
 
+GNUNET_NETWORK_STRUCT_BEGIN
+/**
+ * Connect to namestore service
+ */
+struct LookupNameMessage
+{
+
+  /**
+   * Type will be GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME
+   */
+  struct GNUNET_MessageHeader header;
+
+};
+GNUNET_NETWORK_STRUCT_END
+
+
 /* end of namestore.h */
 #endif
index 0aec6bb8ec6906ae724f12992d1155c7ba26e11e..ba034495845e895706fe0878747446fa4e38de32 100644 (file)
@@ -248,6 +248,7 @@ do_transmit (struct GNUNET_NAMESTORE_Handle *nsh)
     return;
   if (NULL == nsh->client)
     return;                     /* currently reconnecting */
+
   nsh->th = GNUNET_CLIENT_notify_transmit_ready (nsh->client, p->size,
                                            GNUNET_TIME_UNIT_FOREVER_REL,
                                            GNUNET_NO, &transmit_message_to_namestore,
@@ -579,7 +580,7 @@ GNUNET_NAMESTORE_record_remove (struct GNUNET_NAMESTORE_Handle *h,
  *         cancel
  */
 struct GNUNET_NAMESTORE_QueueEntry *
-GNUNET_NAMESTORE_lookup_name (struct GNUNET_NAMESTORE_Handle *h, 
+GNUNET_NAMESTORE_lookup_name (struct GNUNET_NAMESTORE_Handle *nsh,
                               const GNUNET_HashCode *zone,
                               const char *name,
                               uint32_t record_type,
@@ -601,6 +602,22 @@ GNUNET_NAMESTORE_lookup_name (struct GNUNET_NAMESTORE_Handle *h,
   proc(proc_cls, zone, name, record_type,
        GNUNET_TIME_absolute_get_forever(), 0, NULL, 0, NULL); /*TERMINATE*/
 #endif
+
+  GNUNET_assert (NULL != nsh);
+
+  struct PendingMessage * p;
+  struct LookupNameMessage * msg;
+  size_t msg_len = sizeof (struct LookupNameMessage);
+
+  p = GNUNET_malloc (sizeof (struct PendingMessage) + msg_len);
+  p->size = msg_len;
+  p->is_init = GNUNET_NO;
+  msg = (struct LookupNameMessage *) &p[1];
+  msg->header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME);
+  msg->header.size = htons (msg_len);
+  GNUNET_CONTAINER_DLL_insert (nsh->pending_head, nsh->pending_tail, p);
+  do_transmit (nsh);
+
   return qe;
 }
 
index 781b48e493dbabda6dad0bc955bc56e7486c248b..65223929a1138d76fead5a8235139d13ec7e3e8d 100644 (file)
@@ -72,8 +72,13 @@ stop_arm ()
 static void
 endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
+  if (nsh != NULL)
+    GNUNET_NAMESTORE_disconnect (nsh, GNUNET_YES);
+  nsh = NULL;
+
   if (NULL != arm)
     stop_arm();
+
   res = 1;
 }
 
@@ -87,6 +92,11 @@ end (void)
     endbadly_task = GNUNET_SCHEDULER_NO_TASK;
   }
 
+  if (nsh != NULL)
+    GNUNET_NAMESTORE_disconnect (nsh, GNUNET_YES);
+  nsh = NULL;
+
+
   if (NULL != arm)
     stop_arm();
 
@@ -105,7 +115,8 @@ run (void *cls, char *const *args, const char *cfgfile,
 
   nsh = GNUNET_NAMESTORE_connect (cfg);
   GNUNET_break (NULL != nsh);
-  GNUNET_NAMESTORE_disconnect (nsh, GNUNET_YES);
+
+  GNUNET_NAMESTORE_lookup_name (nsh, NULL, NULL, 0, NULL, NULL);
 
   //stop_arm ();
   //end ();