arg
[oweals/gnunet.git] / src / fs / gnunet-service-fs_lc.c
index e8d6bda479692c7d3877f49eca7bb2de52afdef5..c712f58d67a41dd1a4f527141e647bb0917bce54 100644 (file)
  */
 
 #include "platform.h"
+#include "gnunet-service-fs.h"
 #include "gnunet-service-fs_lc.h"
+#include "gnunet-service-fs_cp.h"
+#include "gnunet-service-fs_pr.h"
 
 
 /**
@@ -55,8 +58,12 @@ struct ClientRequest
    */
   struct GSF_LocalClient *lc;
 
-};
+  /**
+   * Task scheduled to destroy the request.
+   */
+  GNUNET_SCHEDULER_TaskIdentifier kill_task;
 
+};
 
 
 /**
@@ -87,7 +94,6 @@ struct ClientResponse
 };
 
 
-
 /**
  * A local client.
  */
@@ -178,6 +184,33 @@ GSF_local_client_lookup_ (struct GNUNET_SERVER_Client *client)
 }
 
 
+/**
+ * Free the given client request.
+ *
+ * @param cls the client request to free
+ * @param tc task context
+ */ 
+static void
+client_request_destroy (void *cls,
+                       const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  struct ClientRequest *cr = cls;
+  struct GSF_LocalClient *lc;
+
+  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_STATISTICS_update (GSF_stats,
+                           gettext_noop ("# client searches active"),
+                           - 1,
+                           GNUNET_NO);
+  GNUNET_free (cr);
+}
+
+
 /**
  * Handle a reply to a pending request.  Also called if a request
  * expires (then with data == NULL).  The handler may be called
@@ -187,74 +220,75 @@ GSF_local_client_lookup_ (struct GNUNET_SERVER_Client *client)
  * expiration.
  *
  * @param cls user-specified closure
+ * @param eval evaluation of the result
  * @param pr handle to the original pending request
+ * @param reply_anonymity_level anonymity level for the reply, UINT32_MAX for "unknown"
+ * @param expiration when does 'data' expire? 
+ * @param type type of the block
  * @param data response data, NULL on request expiration
  * @param data_len number of bytes in data
- * @param more GNUNET_YES if the request remains active (may call
- *             this function again), GNUNET_NO if the request is
- *             finished (client must not call GSF_pending_request_cancel_)
  */
 static void
 client_response_handler (void *cls,
+                        enum GNUNET_BLOCK_EvaluationResult eval,
                         struct GSF_PendingRequest *pr,
+                        uint32_t reply_anonymity_level,
+                        struct GNUNET_TIME_Absolute expiration,
+                        enum GNUNET_BLOCK_Type type,
                         const void *data,
-                        size_t data_len,
-                        int more)
+                        size_t data_len)
 {
   struct ClientRequest *cr = cls;
+  struct GSF_LocalClient *lc;
+  struct PutMessage *pm;
+  const struct GSF_PendingRequestData *prd;
+  size_t msize;
 
   if (NULL == data)
     {
       /* ugh, request 'timed out' -- how can this be? */
       GNUNET_break (0);
-      GNUNET_assert (GNUNET_NO == more);
       return;
     }
-  /* FIXME: adapt old code below to new API! */
-
-      GNUNET_STATISTICS_update (stats,
-                               gettext_noop ("# replies received for local clients"),
-                               1,
-                               GNUNET_NO);
-      cl = pr->client_request_list->client_list;
-      msize = sizeof (struct PutMessage) + prq->size;
-      creply = GNUNET_malloc (msize + sizeof (struct ClientResponseMessage));
-      creply->msize = msize;
-      creply->client_list = cl;
-      GNUNET_CONTAINER_DLL_insert_after (cl->res_head,
-                                        cl->res_tail,
-                                        cl->res_tail,
-                                        creply);      
-      pm = (struct PutMessage*) &creply[1];
-      pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
-      pm->header.size = htons (msize);
-      pm->type = htonl (prq->type);
-      pm->expiration = GNUNET_TIME_absolute_hton (prq->expiration);
-      memcpy (&pm[1], prq->data, prq->size);      
-      if (NULL == cl->th)
-       {
-#if DEBUG_FS
-         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                     "Transmitting result for query `%s' to client\n",
-                     GNUNET_h2s (key));
-#endif  
-         cl->th = GNUNET_SERVER_notify_transmit_ready (cl->client,
-                                                       msize,
-                                                       GNUNET_TIME_UNIT_FOREVER_REL,
-                                                       &transmit_to_client,
-                                                       cl);
-       }
-      GNUNET_break (cl->th != NULL);
-      if (pr->do_remove)               
-       {
-         prq->finished = GNUNET_YES;
-         destroy_pending_request (pr);         
-       }
-
+  prd = GSF_pending_request_get_data_ (pr);
+  GNUNET_break (type != GNUNET_BLOCK_TYPE_ANY);
+  if ( (prd->type != type) &&
+       (prd->type != GNUNET_BLOCK_TYPE_ANY) )
+    {
+      GNUNET_break (0);
+      return;
+    }
+  GNUNET_STATISTICS_update (GSF_stats,
+                           gettext_noop ("# replies received for local clients"),
+                           1,
+                           GNUNET_NO);
+  GNUNET_assert (pr == cr->pr);
+  lc = cr->lc;
+  msize = sizeof (struct PutMessage) + data_len;
+  {
+    char buf[msize];
+    
+    pm = (struct PutMessage*) buf;
+    pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
+    pm->header.size = htons (msize);
+    pm->type = htonl (type);
+    pm->expiration = GNUNET_TIME_absolute_hton (expiration);
+    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)
+    return;
+  cr->kill_task = GNUNET_SCHEDULER_add_now (&client_request_destroy,
+                                           cr);
 }
 
 
-
 /**
  * Handle START_SEARCH-message (search request from local client).
  *
@@ -270,7 +304,7 @@ GSF_local_client_start_search_handler_ (struct GNUNET_SERVER_Client *client,
   const struct SearchMessage *sm;
   struct GSF_LocalClient *lc;
   struct ClientRequest *cr;
-  struct GSF_PendingRequest *pr;
+  struct GSF_PendingRequestData *prd;
   uint16_t msize;
   unsigned int sc;
   enum GNUNET_BLOCK_Type type;
@@ -285,14 +319,14 @@ GSF_local_client_start_search_handler_ (struct GNUNET_SERVER_Client *client,
                                  GNUNET_SYSERR);
       return NULL;
     }
-  GNUNET_STATISTICS_update (stats,
+  GNUNET_STATISTICS_update (GSF_stats,
                            gettext_noop ("# client searches received"),
                            1,
                            GNUNET_NO);
   sc = (msize - sizeof (struct SearchMessage)) / sizeof (GNUNET_HashCode);
   sm = (const struct SearchMessage*) message;
   type = ntohl (sm->type);
-#if DEBUG_FS
+#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),
@@ -305,23 +339,31 @@ GSF_local_client_start_search_handler_ (struct GNUNET_SERVER_Client *client,
        (type == GNUNET_BLOCK_TYPE_FS_NBLOCK) ||
        (type == GNUNET_BLOCK_TYPE_ANY) )
     {
+      /* FIXME: this does currently not work to filter duplicate
+        results from *local* datastore since the local store is
+        queried before we continue to process additional
+        messages from the client! -- fix protocol? */
       cr = lc->cr_head;
-      while ( (cl != NULL) &&
-             ( (0 != memcmp (GSF_pending_request_get_query_ (cr->pr),
-                             &sm->query,
-                             sizeof (GNUNET_HashCode))) ||
-               (GSF_pending_request_get_type_ (cr->pr) != type) ) )
-       cr = cr->next;
-      if (crl != NULL)         
+      while (cr != NULL) 
+       {
+         prd = GSF_pending_request_get_data_ (cr->pr);
+         if ( (0 != memcmp (&prd->query,
+                            &sm->query,
+                            sizeof (GNUNET_HashCode))) &&
+              (prd->type == type) )
+           break;
+         cr = cr->next;
+       }
+      if (cr != NULL)  
        { 
-#if DEBUG_FS
+#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,
-                                      &sm[1],
+                                      (const GNUNET_HashCode*) &sm[1],
                                       sc);
-         GNUNET_STATISTICS_update (stats,
+         GNUNET_STATISTICS_update (GSF_stats,
                                    gettext_noop ("# client searches updated (merged content seen list)"),
                                    1,
                                    GNUNET_NO);
@@ -331,7 +373,7 @@ GSF_local_client_start_search_handler_ (struct GNUNET_SERVER_Client *client,
        }
     }
 
-  GNUNET_STATISTICS_update (stats,
+  GNUNET_STATISTICS_update (GSF_stats,
                            gettext_noop ("# client searches active"),
                            1,
                            GNUNET_NO);
@@ -346,18 +388,20 @@ GSF_local_client_start_search_handler_ (struct GNUNET_SERVER_Client *client,
   cr->pr = GSF_pending_request_create_ (options,
                                        type,
                                        &sm->query,
-                                       (type == GNUNET_BLOCK_TYPE_SBLOCK)
+                                       (type == GNUNET_BLOCK_TYPE_FS_SBLOCK)
                                        ? &sm->target /* namespace */
                                        : NULL,
                                        (0 != memcmp (&sm->target,
                                                      &all_zeros,
                                                      sizeof (GNUNET_HashCode)))
-                                       ? &sm->target,
+                                       ? (const struct GNUNET_PeerIdentity*) &sm->target
                                        : NULL,
                                        NULL, 0, 0 /* bf */, 
                                        ntohl (sm->anonymity_level),
                                        0 /* priority */,
-                                       &sm[1], sc,
+                                       0 /* ttl */,
+                                       0 /* sender PID */,
+                                       (const GNUNET_HashCode*) &sm[1], sc,
                                        &client_response_handler,
                                        cr);
   return cr->pr;
@@ -387,7 +431,7 @@ transmit_to_client (void *cls,
   struct ClientResponse *res;
   size_t msize;
   
-  cl->th = NULL;
+  lc->th = NULL;
   if (NULL == buf)
     return 0;
   msize = 0;
@@ -397,8 +441,8 @@ transmit_to_client (void *cls,
       memcpy (&cbuf[msize], &res[1], res->msize);
       msize += res->msize;
       size -= res->msize;
-      GNUNET_CONTAINER_DLL_remove (cl->res_head,
-                                  cl->res_tail,
+      GNUNET_CONTAINER_DLL_remove (lc->res_head,
+                                  lc->res_tail,
                                   res);
       GNUNET_free (res);
     }
@@ -431,14 +475,14 @@ GSF_local_client_transmit_ (struct GSF_LocalClient *lc,
   res = GNUNET_malloc (sizeof (struct ClientResponse) + msize);
   res->lc = lc;
   res->msize = msize;
+  memcpy (&res[1], msg, msize);
   GNUNET_CONTAINER_DLL_insert_tail (lc->res_head,
                                    lc->res_tail,
                                    res);
-  if (NULL == lc->tc)
-    lc->tc = GNUNET_CLIENT_notify_transmit_ready (lc->client,
+  if (NULL == lc->th)
+    lc->th = GNUNET_SERVER_notify_transmit_ready (lc->client,
                                                  msize,
                                                  GNUNET_TIME_UNIT_FOREVER_REL,
-                                                 GNUNET_NO,
                                                  &transmit_to_client,
                                                  lc);
 }
@@ -453,10 +497,9 @@ GSF_local_client_transmit_ (struct GSF_LocalClient *lc,
  */
 void
 GSF_client_disconnect_handler_ (void *cls,
-                               const struct GNUNET_SERVER_Client *client)
+                               struct GNUNET_SERVER_Client *client)
 {
   struct GSF_LocalClient *pos;
-  struct DisconnectCallback *dc;
   struct ClientRequest *cr;
   struct ClientResponse *res;
 
@@ -465,13 +508,19 @@ GSF_client_disconnect_handler_ (void *cls,
          (pos->client != client) )
     pos = pos->next;
   if (pos == NULL)
-    return 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_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);
     }
   while (NULL != (res = pos->res_head))
@@ -487,6 +536,9 @@ GSF_client_disconnect_handler_ (void *cls,
       pos->th = NULL;
     }
   GSF_handle_local_client_disconnect_ (pos);
+  GNUNET_CONTAINER_DLL_remove (client_head,
+                              client_tail,
+                              pos);
   GNUNET_free (pos);
 }