start fewer FS probes concurrently, run them all in the same task to avoid undue...
[oweals/gnunet.git] / src / fs / gnunet-service-fs_lc.c
index dcedd495adee6e27a495adac4dfc707a1f9a9c31..ef30ba32a03e78e5258a9a0a616cb5ba7f48a85d 100644 (file)
@@ -140,7 +140,7 @@ struct GSF_LocalClient
   /**
    * Context for sending replies.
    */
-  struct GNUNET_CONNECTION_TransmitHandle *th;
+  struct GNUNET_SERVER_TransmitHandle *th;
 
 };
 
@@ -170,11 +170,11 @@ GSF_local_client_lookup_ (struct GNUNET_SERVER_Client *client)
   struct GSF_LocalClient *pos;
 
   pos = client_head;
-  while ((pos != NULL) && (pos->client != client))
+  while ((NULL != pos) && (pos->client != client))
     pos = pos->next;
-  if (pos != NULL)
+  if (NULL != pos)
     return pos;
-  pos = GNUNET_malloc (sizeof (struct GSF_LocalClient));
+  pos = GNUNET_new (struct GSF_LocalClient);
   pos->client = client;
   GNUNET_CONTAINER_DLL_insert (client_head, client_tail, pos);
   return pos;
@@ -197,7 +197,7 @@ client_request_destroy (void *cls,
   cr->kill_task = GNUNET_SCHEDULER_NO_TASK;
   lc = cr->lc;
   GNUNET_CONTAINER_DLL_remove (lc->cr_head, lc->cr_tail, cr);
-  GSF_pending_request_cancel_ (cr->pr, GNUNET_NO);
+  GSF_pending_request_cancel_ (cr->pr, GNUNET_YES);
   GNUNET_STATISTICS_update (GSF_stats,
                             gettext_noop ("# client searches active"), -1,
                             GNUNET_NO);
@@ -221,7 +221,7 @@ client_request_destroy (void *cls,
  * @param last_transmission when was the last time we've tried to download this block? (FOREVER if unknown)
  * @param type type of the block
  * @param data response data, NULL on request expiration
- * @param data_len number of bytes in data
+ * @param data_len number of bytes in @e data
  */
 static void
 client_response_handler (void *cls, enum GNUNET_BLOCK_EvaluationResult eval,
@@ -240,8 +240,10 @@ client_response_handler (void *cls, enum GNUNET_BLOCK_EvaluationResult eval,
 
   if (NULL == data)
   {
-    /* ugh, request 'timed out' -- how can this be? */
-    GNUNET_break (0);
+    /* local-only request, with no result, clean up. */
+    if (GNUNET_SCHEDULER_NO_TASK == cr->kill_task)
+      cr->kill_task = GNUNET_SCHEDULER_add_now (&client_request_destroy,
+                                                cr);
     return;
   }
   prd = GSF_pending_request_get_data_ (pr);
@@ -259,7 +261,7 @@ client_response_handler (void *cls, enum GNUNET_BLOCK_EvaluationResult eval,
   lc = cr->lc;
   msize = sizeof (struct ClientPutMessage) + data_len;
   {
-    char buf[msize];
+    char buf[msize] GNUNET_ALIGN;
 
     pm = (struct ClientPutMessage *) buf;
     pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
@@ -267,17 +269,22 @@ client_response_handler (void *cls, enum GNUNET_BLOCK_EvaluationResult eval,
     pm->type = htonl (type);
     pm->expiration = GNUNET_TIME_absolute_hton (expiration);
     pm->last_transmission = GNUNET_TIME_absolute_hton (last_transmission);
+    pm->num_transmissions = htonl (prd->num_transmissions);
+    pm->respect_offered = htonl (prd->respect_offered);
     memcpy (&pm[1], data, data_len);
     GSF_local_client_transmit_ (lc, &pm->header);
   }
-#if DEBUG_FS_CLIENT
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Queued reply to query `%s' for local client\n",
               GNUNET_h2s (&prd->query), (unsigned int) prd->type);
-#endif
-  if (eval != GNUNET_BLOCK_EVALUATION_OK_LAST)
+  if (GNUNET_BLOCK_EVALUATION_OK_LAST != eval)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+               "Evaluation %d - keeping query alive\n",
+               (int) eval);
     return;
-  if (GNUNET_SCHEDULER_NO_TASK != cr->kill_task)
+  }
+  if (GNUNET_SCHEDULER_NO_TASK == cr->kill_task)
     cr->kill_task = GNUNET_SCHEDULER_add_now (&client_request_destroy, cr);
 }
 
@@ -291,17 +298,16 @@ client_response_handler (void *cls, enum GNUNET_BLOCK_EvaluationResult eval,
  * @param client identification of the client
  * @param message the actual message
  * @param prptr where to store the pending request handle for the request
- * @return GNUNET_YES to start local processing,
- *         GNUNET_NO to not (yet) start local processing,
- *         GNUNET_SYSERR on error
+ * @return #GNUNET_YES to start local processing,
+ *         #GNUNET_NO to not (yet) start local processing,
+ *         #GNUNET_SYSERR on error
  */
 int
 GSF_local_client_start_search_handler_ (struct GNUNET_SERVER_Client *client,
-                                        const struct GNUNET_MessageHeader
-                                        *message,
+                                        const struct GNUNET_MessageHeader *message,
                                         struct GSF_PendingRequest **prptr)
 {
-  static GNUNET_HashCode all_zeros;
+  static struct GNUNET_PeerIdentity all_zeros;
   const struct SearchMessage *sm;
   struct GSF_LocalClient *lc;
   struct ClientRequest *cr;
@@ -313,7 +319,7 @@ GSF_local_client_start_search_handler_ (struct GNUNET_SERVER_Client *client,
 
   msize = ntohs (message->size);
   if ((msize < sizeof (struct SearchMessage)) ||
-      (0 != (msize - sizeof (struct SearchMessage)) % sizeof (GNUNET_HashCode)))
+      (0 != (msize - sizeof (struct SearchMessage)) % sizeof (struct GNUNET_HashCode)))
   {
     GNUNET_break (0);
     *prptr = NULL;
@@ -322,41 +328,37 @@ GSF_local_client_start_search_handler_ (struct GNUNET_SERVER_Client *client,
   GNUNET_STATISTICS_update (GSF_stats,
                             gettext_noop ("# client searches received"), 1,
                             GNUNET_NO);
-  sc = (msize - sizeof (struct SearchMessage)) / sizeof (GNUNET_HashCode);
+  sc = (msize - sizeof (struct SearchMessage)) / sizeof (struct GNUNET_HashCode);
   sm = (const struct SearchMessage *) message;
   type = ntohl (sm->type);
-#if DEBUG_FS_CLIENT
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Received request for `%s' of type %u from local client\n",
               GNUNET_h2s (&sm->query), (unsigned int) type);
-#endif
   lc = GSF_local_client_lookup_ (client);
   cr = NULL;
-  /* detect duplicate KBLOCK requests */
-  if ((type == GNUNET_BLOCK_TYPE_FS_KBLOCK) ||
-      (type == GNUNET_BLOCK_TYPE_FS_NBLOCK) || (type == GNUNET_BLOCK_TYPE_ANY))
+  /* detect duplicate UBLOCK requests */
+  if ((type == GNUNET_BLOCK_TYPE_FS_UBLOCK) ||
+      (type == GNUNET_BLOCK_TYPE_ANY))
   {
     cr = lc->cr_head;
-    while (cr != NULL)
+    while (NULL != cr)
     {
       prd = GSF_pending_request_get_data_ (cr->pr);
       /* only unify with queries that hae not yet started local processing
         (SEARCH_MESSAGE_OPTION_CONTINUED was always set) and that have a
         matching query and type */
       if ((GNUNET_YES != prd->has_started) &&
-         (0 != memcmp (&prd->query, &sm->query, sizeof (GNUNET_HashCode))) &&
+         (0 != memcmp (&prd->query, &sm->query, sizeof (struct GNUNET_HashCode))) &&
           (prd->type == type))
         break;
       cr = cr->next;
     }
   }
-  if (cr != NULL)
+  if (NULL != cr)
   {
-#if DEBUG_FS_CLIENT
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "Have existing request, merging content-seen lists.\n");
-#endif
-    GSF_pending_request_update_ (cr->pr, (const GNUNET_HashCode *) &sm[1], sc);
+    GSF_pending_request_update_ (cr->pr, (const struct GNUNET_HashCode *) &sm[1], sc);
     GNUNET_STATISTICS_update (GSF_stats,
                               gettext_noop
                               ("# client searches updated (merged content seen list)"),
@@ -367,26 +369,25 @@ GSF_local_client_start_search_handler_ (struct GNUNET_SERVER_Client *client,
     GNUNET_STATISTICS_update (GSF_stats,
                               gettext_noop ("# client searches active"), 1,
                               GNUNET_NO);
-    cr = GNUNET_malloc (sizeof (struct ClientRequest));
+    cr = GNUNET_new (struct ClientRequest);
     cr->lc = lc;
     GNUNET_CONTAINER_DLL_insert (lc->cr_head, lc->cr_tail, cr);
     options = GSF_PRO_LOCAL_REQUEST;
     if (0 != (SEARCH_MESSAGE_OPTION_LOOPBACK_ONLY & ntohl (sm->options)))
       options |= GSF_PRO_LOCAL_ONLY;
-    cr->pr = GSF_pending_request_create_ (options, type, &sm->query, (type == GNUNET_BLOCK_TYPE_FS_SBLOCK) ? &sm->target        /* namespace */
-                                          : NULL,
+    cr->pr = GSF_pending_request_create_ (options, type,
+                                         &sm->query,
                                           (0 !=
                                            memcmp (&sm->target, &all_zeros,
-                                                   sizeof (GNUNET_HashCode)))
-                                          ? (const struct GNUNET_PeerIdentity *)
-                                          &sm->target : NULL, NULL, 0,
+                                                   sizeof (struct GNUNET_PeerIdentity)))
+                                          ? &sm->target : NULL, NULL, 0,
                                           0 /* bf */ ,
                                           ntohl (sm->anonymity_level),
                                           0 /* priority */ ,
                                           0 /* ttl */ ,
                                           0 /* sender PID */ ,
                                           0 /* origin PID */ ,
-                                          (const GNUNET_HashCode *) &sm[1], sc,
+                                          (const struct GNUNET_HashCode *) &sm[1], sc,
                                           &client_response_handler, cr);
   }
   *prptr = cr->pr;
@@ -484,27 +485,22 @@ GSF_client_disconnect_handler_ (void *cls, struct GNUNET_SERVER_Client *client)
   pos = client_head;
   while ((pos != NULL) && (pos->client != client))
     pos = pos->next;
-  if (pos == NULL)
+  if (NULL == pos)
     return;
   while (NULL != (cr = pos->cr_head))
   {
-    GNUNET_CONTAINER_DLL_remove (pos->cr_head, pos->cr_tail, cr);
-    GSF_pending_request_cancel_ (cr->pr, GNUNET_NO);
-    GNUNET_STATISTICS_update (GSF_stats,
-                              gettext_noop ("# client searches active"), -1,
-                              GNUNET_NO);
     if (GNUNET_SCHEDULER_NO_TASK != cr->kill_task)
       GNUNET_SCHEDULER_cancel (cr->kill_task);
-    GNUNET_free (cr);
+    client_request_destroy (cr, NULL);
   }
   while (NULL != (res = pos->res_head))
   {
     GNUNET_CONTAINER_DLL_remove (pos->res_head, pos->res_tail, res);
     GNUNET_free (res);
   }
-  if (pos->th != NULL)
+  if (NULL != pos->th)
   {
-    GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
+    GNUNET_SERVER_notify_transmit_ready_cancel (pos->th);
     pos->th = NULL;
   }
   GSF_handle_local_client_disconnect_ (pos);