- fix
[oweals/gnunet.git] / src / util / server.c
index 4622e0779cc96c39078e072151bec8a3485bc8e8..2c3533d6fbcaea46fccee5303e52744481420bf2 100644 (file)
@@ -110,6 +110,16 @@ struct GNUNET_SERVER_Handle
    */
   struct NotifyList *disconnect_notify_list_tail;
 
+  /**
+   * Head of linked list of functions to call on connects by clients.
+   */
+  struct NotifyList *connect_notify_list_head;
+
+  /**
+   * Tail of linked list of functions to call on connects by clients.
+   */
+  struct NotifyList *connect_notify_list_tail;
+
   /**
    * Function to call for access control.
    */
@@ -296,11 +306,6 @@ struct GNUNET_SERVER_Client
    */
   int receive_pending;
 
-  /**
-   * Finish pending write when disconnecting?
-   */
-  int finish_pending_write;
-
   /**
    * Persist the file handle for this client no matter what happens,
    * force the OS to close once the process actually dies.  Should only
@@ -660,6 +665,8 @@ GNUNET_SERVER_resume (struct GNUNET_SERVER_Handle *server)
   struct GNUNET_NETWORK_FDSet *r;
   unsigned int i;
 
+  if (NULL == server->listen_sockets)
+    return;
   if (NULL == server->listen_sockets[0])
     return; /* nothing to do, no listen sockets! */
   if (NULL == server->listen_sockets[1])
@@ -759,6 +766,14 @@ GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *server)
                                 npos);
     GNUNET_free (npos);
   }
+  while (NULL != (npos = server->connect_notify_list_head))
+  {
+    npos->callback (npos->callback_cls, NULL);
+    GNUNET_CONTAINER_DLL_remove (server->connect_notify_list_head,
+                                server->connect_notify_list_tail,
+                                npos);
+    GNUNET_free (npos);
+  }
   GNUNET_free (server);
 }
 
@@ -830,11 +845,9 @@ warn_no_receive_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
                                     &warn_no_receive_done, client);
   if (0 == (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
     LOG (GNUNET_ERROR_TYPE_WARNING,
-         _
-         ("Processing code for message of type %u did not call GNUNET_SERVER_receive_done after %llums\n"),
+         _("Processing code for message of type %u did not call `GNUNET_SERVER_receive_done' after %s\n"),
          (unsigned int) client->warn_type,
-         (unsigned long long)
-         GNUNET_TIME_absolute_get_duration (client->warn_start).rel_value);
+         GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (client->warn_start), GNUNET_YES));
 }
 
 
@@ -904,6 +917,10 @@ GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server,
                "Expected %u bytes for message of type %u, got %u\n",
                mh->expected_size, mh->type, size);
           GNUNET_break_op (0);
+#else
+          LOG (GNUNET_ERROR_TYPE_DEBUG,
+               "Expected %u bytes for message of type %u, got %u\n",
+               mh->expected_size, mh->type, size);
 #endif
           return GNUNET_SYSERR;
         }
@@ -1166,6 +1183,7 @@ GNUNET_SERVER_connect_socket (struct GNUNET_SERVER_Handle *server,
                               struct GNUNET_CONNECTION_Handle *connection)
 {
   struct GNUNET_SERVER_Client *client;
+  struct NotifyList *n;
 
   client = GNUNET_malloc (sizeof (struct GNUNET_SERVER_Client));
   client->connection = connection;
@@ -1183,6 +1201,9 @@ GNUNET_SERVER_connect_socket (struct GNUNET_SERVER_Handle *server,
     client->mst =
         GNUNET_SERVER_mst_create (&client_message_tokenizer_callback, server);
   GNUNET_assert (NULL != client->mst);
+  for (n = server->connect_notify_list_head; NULL != n; n = n->next)
+    n->callback (n->callback_cls, client);
+
   client->receive_pending = GNUNET_YES;
   GNUNET_CONNECTION_receive (client->connection,
                              GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
@@ -1282,10 +1303,34 @@ GNUNET_SERVER_disconnect_notify (struct GNUNET_SERVER_Handle *server,
 
 
 /**
- * Ask the server to stop notifying us whenever a client disconnects.
+ * Ask the server to notify us whenever a client connects.
+ * This function is called whenever the actual network connection
+ * is opened.
  *
  * @param server the server manageing the clients
- * @param callback function to call on disconnect
+ * @param callback function to call on sconnect
+ * @param callback_cls closure for callback
+ */
+void
+GNUNET_SERVER_connect_notify (struct GNUNET_SERVER_Handle *server,
+    GNUNET_SERVER_ConnectCallback callback, void *callback_cls)
+{
+  struct NotifyList *n;
+
+  n = GNUNET_malloc (sizeof (struct NotifyList));
+  n->callback = callback;
+  n->callback_cls = callback_cls;
+  GNUNET_CONTAINER_DLL_insert (server->connect_notify_list_head,
+                              server->connect_notify_list_tail,
+                              n);
+}
+
+
+/**
+ * Ask the server to stop notifying us whenever a client connects.
+ *
+ * @param server the server manageing the clients
+ * @param callback function to call on connect
  * @param callback_cls closure for callback
  */
 void
@@ -1310,6 +1355,34 @@ GNUNET_SERVER_disconnect_notify_cancel (struct GNUNET_SERVER_Handle *server,
 }
 
 
+/**
+ * Ask the server to stop notifying us whenever a client disconnects.
+ *
+ * @param server the server manageing the clients
+ * @param callback function to call on disconnect
+ * @param callback_cls closure for callback
+ */
+void
+GNUNET_SERVER_connect_notify_cancel (struct GNUNET_SERVER_Handle *server,
+    GNUNET_SERVER_ConnectCallback callback, void *callback_cls)
+{
+  struct NotifyList *pos;
+
+  for (pos = server->connect_notify_list_head; NULL != pos; pos = pos->next)
+    if ((pos->callback == callback) && (pos->callback_cls == callback_cls))
+      break;
+  if (NULL == pos)
+  {
+    GNUNET_break (0);
+    return;
+  }
+  GNUNET_CONTAINER_DLL_remove (server->connect_notify_list_head,
+                              server->connect_notify_list_tail,
+                              pos);
+  GNUNET_free (pos);
+}
+
+
 /**
  * Destroy the connection that is passed in via 'cls'.  Used
  * as calling 'GNUNET_CONNECTION_destroy' from within a function