convert fs publish to MQ
[oweals/gnunet.git] / src / dht / gnunet-service-dht_clients.c
index 2ec3c180c848248f23e9c35bcd26936d88369013..b508df68af32171a6964bbb28b0f6128a34663f2 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2009, 2010, 2011 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
@@ -14,8 +14,8 @@
 
      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., 59 Temple Place - Suite 330,
-     Boston, MA 02111-1307, USA.
+     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+     Boston, MA 02110-1301, USA.
 */
 
 /**
@@ -110,7 +110,7 @@ struct ClientList
 
 
 /**
- * Entry in the DHT routing table for a client's GET request.
+ * Entry in the local forwarding map for a client's GET request.
  */
 struct ClientQueryRecord
 {
@@ -253,7 +253,7 @@ static struct ClientMonitorRecord *monitor_head;
 static struct ClientMonitorRecord *monitor_tail;
 
 /**
- * Hashmap for fast key based lookup, maps keys to 'struct ClientQueryRecord' entries.
+ * Hashmap for fast key based lookup, maps keys to `struct ClientQueryRecord` entries.
  */
 static struct GNUNET_CONTAINER_MultiHashMap *forward_map;
 
@@ -265,7 +265,7 @@ static struct GNUNET_CONTAINER_Heap *retry_heap;
 /**
  * Task that re-transmits requests (using retry_heap).
  */
-static GNUNET_SCHEDULER_TaskIdentifier retry_task;
+static struct GNUNET_SCHEDULER_Task * retry_task;
 
 
 /**
@@ -427,7 +427,9 @@ transmit_request (struct ClientQueryRecord *cqr)
                                          GNUNET_CONSTANTS_BLOOMFILTER_K);
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Initiating GET for %s, replication %u, already have %u replies\n",
-       GNUNET_h2s(&cqr->key), cqr->replication, cqr->seen_replies_count);
+       GNUNET_h2s (&cqr->key),
+       cqr->replication,
+       cqr->seen_replies_count);
   GDS_NEIGHBOURS_handle_get (cqr->type, cqr->msg_options, cqr->replication,
                              0 /* hop count */ ,
                              &cqr->key, cqr->xquery, cqr->xquery_size, reply_bf,
@@ -448,18 +450,14 @@ transmit_request (struct ClientQueryRecord *cqr)
  * itself (unless the heap is empty).
  *
  * @param cls unused
- * @param tc scheduler context
  */
 static void
-transmit_next_request_task (void *cls,
-                            const struct GNUNET_SCHEDULER_TaskContext *tc)
+transmit_next_request_task (void *cls)
 {
   struct ClientQueryRecord *cqr;
   struct GNUNET_TIME_Relative delay;
 
-  retry_task = GNUNET_SCHEDULER_NO_TASK;
-  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
-    return;
+  retry_task = NULL;
   while (NULL != (cqr = GNUNET_CONTAINER_heap_remove_root (retry_heap)))
   {
     cqr->hnode = NULL;
@@ -626,7 +624,7 @@ handle_dht_local_get (void *cls, struct GNUNET_SERVER_Client *client,
                            GDS_NEIGHBOURS_get_id(),
                            &get->key);
   /* start remote requests */
-  if (GNUNET_SCHEDULER_NO_TASK != retry_task)
+  if (NULL != retry_task)
     GNUNET_SCHEDULER_cancel (retry_task);
   retry_task = GNUNET_SCHEDULER_add_now (&transmit_next_request_task, NULL);
   /* perform local lookup */
@@ -784,7 +782,8 @@ remove_by_unique_id (void *cls, const struct GNUNET_HashCode * key, void *value)
  *
  */
 static void
-handle_dht_local_get_stop (void *cls, struct GNUNET_SERVER_Client *client,
+handle_dht_local_get_stop (void *cls,
+                           struct GNUNET_SERVER_Client *client,
                            const struct GNUNET_MessageHeader *message)
 {
   const struct GNUNET_DHT_ClientGetStopMessage *dht_stop_msg =
@@ -797,7 +796,8 @@ handle_dht_local_get_stop (void *cls, struct GNUNET_SERVER_Client *client,
                             GNUNET_NO);
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Received GET STOP request for %s from local client %p\n",
-       client, GNUNET_h2s (&dht_stop_msg->key));
+       GNUNET_h2s (&dht_stop_msg->key),
+       client);
   ctx.client = find_active_client (client);
   ctx.unique_id = dht_stop_msg->unique_id;
   GNUNET_CONTAINER_multihashmap_get_multiple (forward_map, &dht_stop_msg->key,
@@ -840,6 +840,7 @@ handle_dht_local_monitor (void *cls, struct GNUNET_SERVER_Client *client,
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
 }
 
+
 /**
  * Handler for monitor stop messages
  *
@@ -927,13 +928,18 @@ send_reply_to_client (void *cls, size_t size, void *buf)
                                  reply);
     memcpy (&cbuf[off], reply->msg, msize);
     GNUNET_free (reply);
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmitting %u bytes to client %p\n",
-                msize, client->client_handle);
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "Transmitting %u bytes to client %p\n",
+                (unsigned int) msize,
+                client->client_handle);
     off += msize;
   }
   process_pending_messages (client);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmitted %u/%u bytes to client %p\n",
-              (unsigned int) off, (unsigned int) size, client->client_handle);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Transmitted %u/%u bytes to client %p\n",
+              (unsigned int) off,
+              (unsigned int) size,
+              client->client_handle);
   return off;
 }
 
@@ -968,7 +974,7 @@ process_pending_messages (struct ClientList *client)
 
 
 /**
- * Closure for 'forward_reply'
+ * Closure for #forward_reply()
  */
 struct ForwardReplyContext
 {
@@ -994,7 +1000,7 @@ struct ForwardReplyContext
   size_t data_size;
 
   /**
-   * Do we need to copy 'pm' because it was already used?
+   * Do we need to copy @a pm because it was already used?
    */
   int do_copy;
 
@@ -1009,11 +1015,13 @@ struct ForwardReplyContext
  * @param cls the 'struct ForwardReplyContext'
  * @param key current key
  * @param value value in the hash map, a ClientQueryRecord
- * @return GNUNET_YES (we should continue to iterate),
- *         if the result is mal-formed, GNUNET_NO
+ * @return #GNUNET_YES (we should continue to iterate),
+ *         if the result is mal-formed, #GNUNET_NO
  */
 static int
-forward_reply (void *cls, const struct GNUNET_HashCode * key, void *value)
+forward_reply (void *cls,
+               const struct GNUNET_HashCode *key,
+               void *value)
 {
   struct ForwardReplyContext *frc = cls;
   struct ClientQueryRecord *record = value;
@@ -1052,12 +1060,20 @@ forward_reply (void *cls, const struct GNUNET_HashCode * key, void *value)
       return GNUNET_YES;        /* duplicate */
     }
   eval =
-      GNUNET_BLOCK_evaluate (GDS_block_context, record->type, key, NULL, 0,
-                             record->xquery, record->xquery_size, frc->data,
+      GNUNET_BLOCK_evaluate (GDS_block_context,
+                             record->type,
+                             GNUNET_BLOCK_EO_NONE,
+                             key,
+                             NULL,
+                             0,
+                             record->xquery,
+                             record->xquery_size,
+                             frc->data,
                              frc->data_size);
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Evaluation result is %d for key %s for local client's query\n",
-       (int) eval, GNUNET_h2s (key));
+       (int) eval,
+       GNUNET_h2s (key));
   switch (eval)
   {
   case GNUNET_BLOCK_EVALUATION_OK_LAST:
@@ -1153,12 +1169,11 @@ GDS_CLIENTS_handle_reply (struct GNUNET_TIME_Absolute expiration,
   struct GNUNET_PeerIdentity *paths;
   size_t msize;
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "reply for key %s\n",
-       GNUNET_h2s (key));
-
   if (NULL == GNUNET_CONTAINER_multihashmap_get (forward_map, key))
   {
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "No matching client for reply for key %s\n",
+         GNUNET_h2s (key));
     GNUNET_STATISTICS_update (GDS_stats,
                               gettext_noop
                               ("# REPLIES ignored for CLIENTS (no match)"), 1,
@@ -1170,10 +1185,13 @@ GDS_CLIENTS_handle_reply (struct GNUNET_TIME_Absolute expiration,
       (get_path_length + put_path_length) * sizeof (struct GNUNET_PeerIdentity);
   if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                _("Could not pass reply to client, message too big!\n"));
+    GNUNET_break (0);
     return;
   }
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Forwarding reply for key %s to client\n",
+       GNUNET_h2s (key));
+
   pm = GNUNET_malloc (msize + sizeof (struct PendingMessage));
   reply = (struct GNUNET_DHT_ClientResultMessage *) &pm[1];
   pm->msg = &reply->header;
@@ -1196,7 +1214,9 @@ GDS_CLIENTS_handle_reply (struct GNUNET_TIME_Absolute expiration,
   frc.data = data;
   frc.data_size = data_size;
   frc.type = type;
-  GNUNET_CONTAINER_multihashmap_get_multiple (forward_map, key, &forward_reply,
+  GNUNET_CONTAINER_multihashmap_get_multiple (forward_map,
+                                              key,
+                                              &forward_reply,
                                               &frc);
 
   if (GNUNET_NO == frc.do_copy)
@@ -1295,7 +1315,7 @@ GDS_CLIENTS_process_get (uint32_t options,
  * @param exp Expiration time of the data.
  * @param key Key of the data.
  * @param data Pointer to the result data.
- * @param size Number of bytes in data.
+ * @param size Number of bytes in @a data.
  */
 void
 GDS_CLIENTS_process_get_resp (enum GNUNET_BLOCK_Type type,
@@ -1438,7 +1458,8 @@ GDS_CLIENTS_process_put (uint32_t options,
       msg_path = (struct GNUNET_PeerIdentity *) &mmsg[1];
       if (path_length > 0)
       {
-        memcpy (msg_path, path,
+        memcpy (msg_path,
+                path,
                 path_length * sizeof (struct GNUNET_PeerIdentity));
       }
       mmsg->expiration_time = GNUNET_TIME_absolute_hton(exp);
@@ -1458,7 +1479,7 @@ GDS_CLIENTS_process_put (uint32_t options,
  * @param server the initialized server
  */
 void
-GDS_CLIENTS_init (struct GNUNET_SERVER_Handle *server)
+GDS_CLIENTS_init ()
 {
   static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
     {&handle_dht_local_put, NULL,
@@ -1478,10 +1499,15 @@ GDS_CLIENTS_init (struct GNUNET_SERVER_Handle *server)
      GNUNET_MESSAGE_TYPE_DHT_CLIENT_GET_RESULTS_KNOWN, 0},
     {NULL, NULL, 0, 0}
   };
+
   forward_map = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_NO);
   retry_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
-  GNUNET_SERVER_add_handlers (server, plugin_handlers);
-  GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
+  GNUNET_SERVER_resume (GDS_server);
+  GNUNET_SERVER_add_handlers (GDS_server,
+                              plugin_handlers);
+  GNUNET_SERVER_disconnect_notify (GDS_server,
+                                   &handle_client_disconnect,
+                                   NULL);
 }
 
 
@@ -1493,10 +1519,10 @@ GDS_CLIENTS_done ()
 {
   GNUNET_assert (client_head == NULL);
   GNUNET_assert (client_tail == NULL);
-  if (GNUNET_SCHEDULER_NO_TASK != retry_task)
+  if (NULL != retry_task)
   {
     GNUNET_SCHEDULER_cancel (retry_task);
-    retry_task = GNUNET_SCHEDULER_NO_TASK;
+    retry_task = NULL;
   }
   if (NULL != retry_heap)
   {