-remove trailing whitespace
[oweals/gnunet.git] / src / fs / gnunet-service-fs_mesh_server.c
index 182408ef02cdf569d09a3455e16defd98350db1a..e732da990a610e45a9b57845ddf43bca03249d8f 100644 (file)
 */
 
 /**
- * @file fs/gnunet-service-fs_mesh.c
+ * @file fs/gnunet-service-fs_mesh_server.c
  * @brief non-anonymous file-transfer
  * @author Christian Grothoff
  *
  * TODO:
- * - MESH2 API doesn't allow flow control for server yet (needed!)
- * - likely need to register clean up handler with mesh to handle
- *   client disconnect (likely leaky right now)
- * - server is optional, currently client code will NPE if we have
- *   no server, again MESH2 API requirement forcing this for now
- * - message handlers are symmetric for client/server, should be
- *   separated (currently clients can get requests and servers can
- *   handle answers, not good)
- * - code is entirely untested
- * - might have overlooked a few possible simplifications
  * - PORT is set to old application type, unsure if we should keep
  *   it that way (fine for now)
  */
@@ -82,22 +72,22 @@ struct MeshClient
 {
   /**
    * DLL
-   */ 
+   */
   struct MeshClient *next;
 
   /**
    * DLL
-   */ 
+   */
   struct MeshClient *prev;
 
   /**
-   * Socket for communication.
-   */ 
-  struct GNUNET_MESH_Tunnel *socket;
+   * Tunnel for communication.
+   */
+  struct GNUNET_MESH_Tunnel *tunnel;
 
   /**
    * Handle for active write operation, or NULL.
-   */ 
+   */
   struct GNUNET_MESH_TransmitHandle *wh;
 
   /**
@@ -109,7 +99,7 @@ struct MeshClient
    * Tail of write queue.
    */
   struct WriteQueueItem *wqi_tail;
-  
+
   /**
    * Current active request to the datastore, if we have one pending.
    */
@@ -127,25 +117,25 @@ struct MeshClient
 
   /**
    * Size of the last write that was initiated.
-   */ 
+   */
   size_t reply_size;
 
 };
 
 
 /**
- * Listen socket for incoming requests.
+ * Listen tunnel for incoming requests.
  */
-static struct GNUNET_MESH_Handle *listen_socket;
+static struct GNUNET_MESH_Handle *listen_tunnel;
 
 /**
  * Head of DLL of mesh clients.
- */ 
+ */
 static struct MeshClient *sc_head;
 
 /**
  * Tail of DLL of mesh clients.
- */ 
+ */
 static struct MeshClient *sc_tail;
 
 /**
@@ -160,54 +150,12 @@ static unsigned long long sc_count_max;
 
 
 
-/* ********************* server-side code ************************* */
-
-
-/**
- * We're done with a particular client, clean up.
- *
- * @param sc client to clean up
- */
-static void
-terminate_mesh (struct MeshClient *sc)
-{
-  struct WriteQueueItem *wqi;
-
-  fprintf (stderr,
-          "terminate mesh called for %p\n",
-          sc);
-  GNUNET_STATISTICS_update (GSF_stats,
-                           gettext_noop ("# mesh connections active"), -1,
-                           GNUNET_NO);
-  if (GNUNET_SCHEDULER_NO_TASK != sc->terminate_task)
-    GNUNET_SCHEDULER_cancel (sc->terminate_task); 
-  if (GNUNET_SCHEDULER_NO_TASK != sc->timeout_task)
-    GNUNET_SCHEDULER_cancel (sc->timeout_task); 
-  if (NULL != sc->wh)
-    GNUNET_MESH_notify_transmit_ready_cancel (sc->wh);
-  if (NULL != sc->qe)
-    GNUNET_DATASTORE_cancel (sc->qe);
-  while (NULL != (wqi = sc->wqi_head))
-  {
-    GNUNET_CONTAINER_DLL_remove (sc->wqi_head,
-                                sc->wqi_tail,
-                                wqi);
-    GNUNET_free (wqi);
-  }
-  GNUNET_CONTAINER_DLL_remove (sc_head,
-                              sc_tail,
-                              sc);
-  sc_count--;
-  GNUNET_free (sc);
-}
-
-
 /**
  * Task run to asynchronously terminate the mesh due to timeout.
  *
  * @param cls the 'struct MeshClient'
  * @param tc scheduler context
- */ 
+ */
 static void
 timeout_mesh_task (void *cls,
                     const struct GNUNET_SCHEDULER_TaskContext *tc)
@@ -216,8 +164,11 @@ timeout_mesh_task (void *cls,
   struct GNUNET_MESH_Tunnel *tun;
 
   sc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
-  tun = sc->socket;
-  sc->socket = NULL;
+  tun = sc->tunnel;
+  sc->tunnel = NULL;
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Timeout for inactive mesh client %p\n",
+             sc);
   GNUNET_MESH_tunnel_destroy (tun);
 }
 
@@ -231,7 +182,7 @@ static void
 refresh_timeout_task (struct MeshClient *sc)
 {
   if (GNUNET_SCHEDULER_NO_TASK != sc->timeout_task)
-    GNUNET_SCHEDULER_cancel (sc->timeout_task); 
+    GNUNET_SCHEDULER_cancel (sc->timeout_task);
   sc->timeout_task = GNUNET_SCHEDULER_add_delayed (IDLE_TIMEOUT,
                                                   &timeout_mesh_task,
                                                   sc);
@@ -247,7 +198,10 @@ static void
 continue_reading (struct MeshClient *sc)
 {
   refresh_timeout_task (sc);
-  GNUNET_MESH_receive_done (sc->socket);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Finished processing mesh request from client %p, ready to receive the next one\n",
+             sc);
+  GNUNET_MESH_receive_done (sc->tunnel);
 }
 
 
@@ -274,6 +228,7 @@ write_continuation (void *cls,
                    void *buf)
 {
   struct MeshClient *sc = cls;
+  struct GNUNET_MESH_Tunnel *tun;
   struct WriteQueueItem *wqi;
   size_t ret;
 
@@ -288,15 +243,18 @@ write_continuation (void *cls,
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "Transmission of reply failed, terminating mesh\n");
-    terminate_mesh (sc);    
+    tun = sc->tunnel;
+    sc->tunnel = NULL;
+    GNUNET_MESH_tunnel_destroy (tun);
     return 0;
   }
   GNUNET_CONTAINER_DLL_remove (sc->wqi_head,
                               sc->wqi_tail,
                               wqi);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-             "Transmitted %u byte reply via mesh\n",
-             (unsigned int) size);
+             "Transmitted %u byte reply via mesh to %p\n",
+             (unsigned int) size,
+             sc);
   GNUNET_STATISTICS_update (GSF_stats,
                            gettext_noop ("# Blocks transferred via mesh"), 1,
                            GNUNET_NO);
@@ -316,6 +274,7 @@ static void
 continue_writing (struct MeshClient *sc)
 {
   struct WriteQueueItem *wqi;
+  struct GNUNET_MESH_Tunnel *tun;
 
   if (NULL != sc->wh)
   {
@@ -330,16 +289,18 @@ continue_writing (struct MeshClient *sc)
     continue_reading (sc);
     return;
   }
-  sc->wh = GNUNET_MESH_notify_transmit_ready (sc->socket, GNUNET_NO,
+  sc->wh = GNUNET_MESH_notify_transmit_ready (sc->tunnel, GNUNET_NO,
                                              GNUNET_TIME_UNIT_FOREVER_REL,
-                                             wqi->msize,                                     
+                                             wqi->msize,                               
                                              &write_continuation,
                                              sc);
   if (NULL == sc->wh)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "Write failed; terminating mesh\n");
-    terminate_mesh (sc);
+    tun = sc->tunnel;
+    sc->tunnel = NULL;
+    GNUNET_MESH_tunnel_destroy (tun);
     return;
   }
 }
@@ -359,9 +320,9 @@ continue_writing (struct MeshClient *sc)
  * @param uid unique identifier for the datum;
  *        maybe 0 if no unique identifier is available
  */
-static void 
+static void
 handle_datastore_reply (void *cls,
-                       const struct GNUNET_HashCode * key,
+                       const struct GNUNET_HashCode *key,
                        size_t size, const void *data,
                        enum GNUNET_BLOCK_Type type,
                        uint32_t priority,
@@ -378,7 +339,8 @@ handle_datastore_reply (void *cls,
   if (GNUNET_BLOCK_TYPE_FS_ONDEMAND == type)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-               "Performing on-demand encoding\n");
+               "Performing on-demand encoding for query %s\n",
+               GNUNET_h2s (key));
     if (GNUNET_OK !=
        GNUNET_FS_handle_on_demand_block (key,
                                          size, data, type,
@@ -400,9 +362,10 @@ handle_datastore_reply (void *cls,
     return;
   }
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-             "Starting transmission of %u byte reply for query `%s' via mesh\n",
+             "Starting transmission of %u byte reply for query `%s' via mesh to %p\n",
              (unsigned int) size,
-             GNUNET_h2s (key));
+             GNUNET_h2s (key),
+             sc);
   wqi = GNUNET_malloc (sizeof (struct WriteQueueItem) + msize);
   wqi->msize = msize;
   srm = (struct MeshReplyMessage *) &wqi[1];
@@ -440,13 +403,11 @@ request_cb (void *cls,
   struct MeshClient *sc = *tunnel_ctx;
   const struct MeshQueryMessage *sqm;
 
-  fprintf (stderr,
-          "Request gets %p\n", 
-          sc);
   sqm = (const struct MeshQueryMessage *) message;
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-             "Received query for `%s' via mesh\n",
-             GNUNET_h2s (&sqm->query));
+             "Received query for `%s' via mesh from client %p\n",
+             GNUNET_h2s (&sqm->query),
+             sc);
   GNUNET_STATISTICS_update (GSF_stats,
                            gettext_noop ("# queries received via mesh"), 1,
                            GNUNET_NO);
@@ -455,7 +416,7 @@ request_cb (void *cls,
                                     0,
                                     &sqm->query,
                                     ntohl (sqm->type),
-                                    0 /* priority */, 
+                                    0 /* priority */,
                                     GSF_datastore_queue_size,
                                     GNUNET_TIME_UNIT_FOREVER_REL,
                                     &handle_datastore_reply, sc);
@@ -473,7 +434,7 @@ request_cb (void *cls,
  * Functions of this type are called upon new mesh connection from other peers.
  *
  * @param cls the closure from GNUNET_MESH_connect
- * @param socket the socket representing the mesh
+ * @param tunnel the tunnel representing the mesh
  * @param initiator the identity of the peer who wants to establish a mesh
  *            with us; NULL on binding error
  * @param port mesh port used for the incoming connection
@@ -481,37 +442,35 @@ request_cb (void *cls,
  */
 static void *
 accept_cb (void *cls,
-          struct GNUNET_MESH_Tunnel *socket,
+          struct GNUNET_MESH_Tunnel *tunnel,
           const struct GNUNET_PeerIdentity *initiator,
           uint32_t port)
 {
   struct MeshClient *sc;
 
-  GNUNET_assert (NULL != socket);
+  GNUNET_assert (NULL != tunnel);
   if (sc_count >= sc_count_max)
   {
     GNUNET_STATISTICS_update (GSF_stats,
                              gettext_noop ("# mesh client connections rejected"), 1,
                              GNUNET_NO);
-    GNUNET_MESH_tunnel_destroy (socket);
+    GNUNET_MESH_tunnel_destroy (tunnel);
     return NULL;
   }
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-             "Accepting inbound mesh connection from `%s'\n",
-             GNUNET_i2s (initiator));
   GNUNET_STATISTICS_update (GSF_stats,
                            gettext_noop ("# mesh connections active"), 1,
                            GNUNET_NO);
   sc = GNUNET_new (struct MeshClient);
-  sc->socket = socket;
+  sc->tunnel = tunnel;
   GNUNET_CONTAINER_DLL_insert (sc_head,
                               sc_tail,
                               sc);
   sc_count++;
   refresh_timeout_task (sc);
-  fprintf (stderr,
-          "Accept returns %p\n", 
-          sc);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Accepting inbound mesh connection from `%s' as client %p\n",
+             GNUNET_i2s (initiator),
+             sc);
   return sc;
 }
 
@@ -522,7 +481,7 @@ accept_cb (void *cls,
  *
  * @param cls NULL
  * @param tunnel tunnel of the disconnecting client
- * @param tunnel_ctx our 'struct MeshClient' 
+ * @param tunnel_ctx our 'struct MeshClient'
  */
 static void
 cleaner_cb (void *cls,
@@ -530,12 +489,37 @@ cleaner_cb (void *cls,
            void *tunnel_ctx)
 {
   struct MeshClient *sc = tunnel_ctx;
+  struct WriteQueueItem *wqi;
 
-  fprintf (stderr,
-          "Cleaner called with %p\n", 
-          sc);
-  if (NULL != sc)
-    terminate_mesh (sc);
+  if (NULL == sc)
+    return;
+  sc->tunnel = NULL;
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Terminating mesh connection with client %p\n",
+             sc);
+  GNUNET_STATISTICS_update (GSF_stats,
+                           gettext_noop ("# mesh connections active"), -1,
+                           GNUNET_NO);
+  if (GNUNET_SCHEDULER_NO_TASK != sc->terminate_task)
+    GNUNET_SCHEDULER_cancel (sc->terminate_task);
+  if (GNUNET_SCHEDULER_NO_TASK != sc->timeout_task)
+    GNUNET_SCHEDULER_cancel (sc->timeout_task);
+  if (NULL != sc->wh)
+    GNUNET_MESH_notify_transmit_ready_cancel (sc->wh);
+  if (NULL != sc->qe)
+    GNUNET_DATASTORE_cancel (sc->qe);
+  while (NULL != (wqi = sc->wqi_head))
+  {
+    GNUNET_CONTAINER_DLL_remove (sc->wqi_head,
+                                sc->wqi_tail,
+                                wqi);
+    GNUNET_free (wqi);
+  }
+  GNUNET_CONTAINER_DLL_remove (sc_head,
+                              sc_tail,
+                              sc);
+  sc_count--;
+  GNUNET_free (sc);
 }
 
 
@@ -560,7 +544,10 @@ GSF_mesh_start_server ()
                                             "MAX_MESH_CLIENTS",
                                             &sc_count_max))
     return;
-  listen_socket = GNUNET_MESH_connect (GSF_cfg,
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Initializing mesh FS server with a limit of %llu connections\n",
+             sc_count_max);
+  listen_tunnel = GNUNET_MESH_connect (GSF_cfg,
                                       NULL,
                                       &accept_cb,
                                       &cleaner_cb,
@@ -575,15 +562,13 @@ GSF_mesh_start_server ()
 void
 GSF_mesh_stop_server ()
 {
-  struct MeshClient *sc;
-
-  while (NULL != (sc = sc_head))
-    terminate_mesh (sc);
-  if (NULL != listen_socket)
+  if (NULL != listen_tunnel)
   {
-    GNUNET_MESH_disconnect (listen_socket);
-    listen_socket = NULL;
+    GNUNET_MESH_disconnect (listen_tunnel);
+    listen_tunnel = NULL;
   }
+  GNUNET_assert (NULL == sc_head);
+  GNUNET_assert (0 == sc_count);
 }
 
 /* end of gnunet-service-fs_mesh.c */