gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r6405 - GNUnet/src/applications/chat


From: gnunet
Subject: [GNUnet-SVN] r6405 - GNUnet/src/applications/chat
Date: Thu, 21 Feb 2008 14:56:42 -0700 (MST)

Author: grothoff
Date: 2008-02-21 14:56:42 -0700 (Thu, 21 Feb 2008)
New Revision: 6405

Modified:
   GNUnet/src/applications/chat/chat.c
   GNUnet/src/applications/chat/clientapi.c
   GNUnet/src/applications/chat/gnunet-chat.c
Log:
stuff

Modified: GNUnet/src/applications/chat/chat.c
===================================================================
--- GNUnet/src/applications/chat/chat.c 2008-02-21 16:20:35 UTC (rev 6404)
+++ GNUnet/src/applications/chat/chat.c 2008-02-21 21:56:42 UTC (rev 6405)
@@ -37,27 +37,12 @@
 
 #define MAX_LAST_MESSAGES 12
 
-/* P2P */
-static unsigned int clientCount;
-static struct GNUNET_HashCode **lastMsgs;
-static int ringIndex;
-
 static struct GNUNET_Mutex *chatMutex;
 
 static struct GNUNET_GE_Context *ectx;
 
 static struct GNUNET_GC_Configuration *cfg;
 
-/* P2P-ish */
-struct GNUNET_Server_Chat_Room
-{
-
-  char *room_name;
-
-  GNUNET_HashCode room_name_hash;
-
-};
-
 struct GNUNET_CS_chat_client
 {
   struct GNUNET_ClientHandle *client;
@@ -67,45 +52,8 @@
 
 };
 
- /* YUCK - USE PTR! */
-static struct GNUNET_CS_chat_client client_list;
+static struct GNUNET_CS_chat_client * client_list_head;
 
-/* P2P */
-static void
-markSeen (GNUNET_HashCode * hc)
-{
-  if (++ringIndex >= MAX_LAST_MESSAGES)
-    ringIndex = 0;
-  memcpy (&lastMsgs[ringIndex], hc, sizeof (GNUNET_HashCode));
-}
-
-typedef struct
-{
-  const GNUNET_MessageHeader *message;
-  unsigned int prio;
-  unsigned int delay;
-} BCC;
-
-/* P2P */
-static void
-bccHelper (const GNUNET_PeerIdentity * peer, BCC * bcc)
-{
-  coreAPI->unicast (peer, bcc->message, bcc->prio, bcc->delay);
-}
-
-/* P2P */
-static void
-broadcastToConnected (const GNUNET_MessageHeader * message,
-                      unsigned int prio, unsigned int delay)
-{
-  BCC bcc;
-  bcc.message = message;
-  bcc.prio = prio;
-  bcc.delay = delay;
-  coreAPI->forAllConnectedNodes ((GNUNET_NodeIteratorCallback) bccHelper,
-                                 &bcc);
-}
-
 static int
 csHandleChatMSG (struct GNUNET_ClientHandle *client,
                  const GNUNET_MessageHeader * message)
@@ -127,15 +75,10 @@
   unsigned long room_name_len;
 
   cmsg = (CS_chat_MESSAGE *) message;
-
   if (ntohs (cmsg->header.size) < sizeof (CS_chat_MESSAGE))
     {
-      /* TOO VERBOSE */
-      GNUNET_GE_LOG (ectx,
-                     GNUNET_GE_WARNING | GNUNET_GE_BULK | GNUNET_GE_USER,
-                     _("Message received from client is invalid\n"));
+      GNUNET_GE_BREAK(NULL, 0);
       return GNUNET_SYSERR;     /* invalid message */
-
     }
 
 
@@ -187,13 +130,6 @@
 
       tempClient = tempClient->next;
     }
-
-
-  /* THIS IS P2P STUFF -- MAYBE DEFINE AN INTERNAL P2P API (for bonii)? */
-  markSeen (&hc);
-  broadcastToConnected (message, 5, 1);
-
-
   GNUNET_mutex_unlock (chatMutex);
 
   GNUNET_free (room_name);
@@ -229,15 +165,8 @@
 
   if (ntohs (cmsg->header.size) < sizeof (CS_chat_JOIN_MESSAGE))
     {
-      /* TOO VERBOSE */
-      GNUNET_GE_LOG (ectx,
-                     GNUNET_GE_WARNING | GNUNET_GE_BULK | GNUNET_GE_USER,
-                     _
-                     ("Room join from client is invalid! Size is: %d, should 
be at least %d\n"),
-                     ntohs (cmsg->header.size),
-                     sizeof (CS_chat_JOIN_MESSAGE));
+      GNUNET_GE_BREAK(NULL, 0);
       return GNUNET_SYSERR;     /* invalid message */
-
     }
 
   header_size = ntohs (cmsg->header.size);
@@ -264,21 +193,34 @@
   GNUNET_hash (cmsg, header_size, &hc);
   GNUNET_hash (room_name, strlen (room_name), &room_name_hash);
   GNUNET_mutex_lock (chatMutex);
-  markSeen (&hc);
 
   /*TODO: create client context on the server, very simple as of now */
-  tempClient = &client_list;
-  while ((tempClient->next != NULL) && (tempClient->client != NULL))
+#if EXTRA_CHECKS
+  tempClient = client_list;
+  while ( (tempClient->client != client) &&
+         (tempClient != NULL) )
     tempClient = tempClient->next;
-
+  if (tempClient != NULL)
+    {
+      GNUNET_GE_BREAK(NULL, 0);
+      GNUNET_free (nick);
+      GNUNET_free (client_key);
+      GNUNET_free (room_name);
+      GNUNET_mutex_unlock (chatMutex);
+      return GNUNET_SYSERR;      
+    }
+#endif
+  tempClient = GNUNET_malloc (sizeof (struct GNUNET_CS_chat_client));
+  memset(tempClient, 0, sizeof (struct GNUNET_CS_chat_client));
+  tempClient->next = client_list_head;
+  if (client_list_head != NULL)
+    client_list_head->prev = tempClient;
+  client_list_head = tempClient;
   tempClient->client = client;
-  tempClient->next = GNUNET_malloc (sizeof (struct GNUNET_CS_chat_client));
-  tempClient->next->prev = tempClient;
   memcpy (&tempClient->room_name_hash, &room_name_hash,
           sizeof (GNUNET_HashCode));
-  tempClient = &client_list;
-
   tempCount = 0;
+#if NATE
   while ((tempClient->next != NULL) && (tempClient->client != NULL))
     {
       tempCount++;
@@ -286,8 +228,8 @@
     }
 
   fprintf (stderr, "Number of clients currently is... %d\n", tempCount);
+#endif
 
-
   /* forward to all other TCP chat clients */
   /* marker to check if this is a new client */
   /*
@@ -315,9 +257,9 @@
      }
      }
    */
+
   /* forward to all other nodes in the network */
-  /*pmsg->header.type = htons (GNUNET_P2P_PROTO_CHAT_MSG);
-     broadcastToConnected (&pmsg->header, 5, 1); */
+
   GNUNET_free (nick);
   GNUNET_free (client_key);
   GNUNET_free (room_name);
@@ -335,8 +277,8 @@
   /*GNUNET_RSA_PublicKey *client_key; *//*May use later for extra verification 
*/
   int tempCount;
 
-  struct GNUNET_CS_chat_client *tempClient;
-  struct GNUNET_CS_chat_client *tempClientToRemove;
+  struct GNUNET_CS_chat_client * pos;
+  struct GNUNET_CS_chat_client * prev;
 
   /*client_key = GNUNET_malloc (sizeof (GNUNET_RSA_PublicKey));
      memcpy (client_key, &cmsg->nick[nick_len], pubkey_len); */
@@ -350,35 +292,27 @@
 
   /*TODO: delete client context on the server */
   /* YUCK */
-  tempClient = &client_list;
-  while ((tempClient->next != NULL) && (tempClient->client != NULL))
+  pos = client_list_head;
+  prev = NULL;
+  while ( (pos != NULL) &&
+         (pos->client != client) )
     {
-      /* YUCK YUCK! */
-      if (memcmp (tempClient->client, client, sizeof (client)) == 0)
-        {
-          fprintf (stderr, "Client handle matches, remove it!\n");
-          if (tempClient->prev == NULL)
-            {
-              tempClient->next->prev = NULL;
-              /*tempClientToRemove = &client_list; */
-              client_list = *tempClient->next;
-            }
-          else
-            {
-              tempClient->next->prev = tempClient->prev;
-              tempClient->prev->next = tempClient->next;
-              tempClientToRemove = tempClient;
-              tempClient = tempClient->next;
-              GNUNET_free (tempClientToRemove);
-            }
-        }
+      prev = pos;
+      pos = pos->next;
+    }
+  if (pos != NULL)
+    {
+      if (prev == NULL)
+       client_list_head = pos->next;
       else
-        tempClient = tempClient->next;
-
+       prev->next = pos->next;
+      if (pos->next != NULL)
+       pos->next->prev = pos->prev;
+      GNUNET_free(pos);
     }
-
   /*Count the number of current clients, will be removed */
-  tempClient = &client_list;
+#if NATE
+  tempClient = client_list;
   tempCount = 0;
   while ((tempClient->next != NULL) && (tempClient->client != NULL))
     {
@@ -386,6 +320,7 @@
       tempClient = tempClient->next;
     }
   fprintf (stderr, "Number of clients currently is... %d\n", tempCount);
+#endif
 
   GNUNET_mutex_unlock (chatMutex);
   fprintf (stderr, "End of handleChatLeave\n");

Modified: GNUnet/src/applications/chat/clientapi.c
===================================================================
--- GNUnet/src/applications/chat/clientapi.c    2008-02-21 16:20:35 UTC (rev 
6404)
+++ GNUnet/src/applications/chat/clientapi.c    2008-02-21 21:56:42 UTC (rev 
6405)
@@ -88,31 +88,39 @@
 
   ret = GNUNET_OK;
   /* CHECK FOR SHUTDOWN! */
-  while (ret == GNUNET_OK)
+  while (room->shutdown_flag != GNUNET_YES)
     {
-      if (GNUNET_client_connection_test_connected (room->sock) == GNUNET_NO)
-        {
-         /* FIXME - why limit retries? */
-          retries = 0;   
-          while ((GNUNET_client_connection_test_connected (room->sock) ==
-                  GNUNET_NO) && (retries < MAX_RETRIES))
-            {
-
+      if (disconnected)
+       {
+         if (GNUNET_client_connection_ensure_connected (room->sock) == 
GNUNET_OK)
+           {
+             disconnected = 0;
+             /* send join! */
+           }
+         else
+           {
+             GNUNET_thread_sleep(5 * GNUNET_CRON_SECONDS);
+             continue;
             }
         }
 
       reply = NULL;
 
       if (GNUNET_OK != GNUNET_client_connection_read (room->sock, &reply))
-        {
-         /* NO BREAK! */
-          break;
-        }
+       {
+         disconnected = GNUNET_YES;
+         continue;
+       }
 
       if ((reply->size <
            ntohs (sizeof (GNUNET_MessageHeader) + sizeof (CS_chat_MESSAGE)))
           || (reply->type != ntohs (GNUNET_CS_PROTO_CHAT_MSG)))
-        break;
+       {
+         GNUNET_GE_BREAK(NULL, 0);
+         GNUNET_client_connection_close_temporarily(room->sock);
+         disconnected = GNUNET_YES;
+         continue;
+       }
 
       size = ntohs (reply->size);
 
@@ -145,8 +153,7 @@
         }
 
     }
-  /* RETURN NULL? (void*) ret is TERRIBLE!!! */
-  return (void *) ret;
+  return NULL;
 }
 
 /**
@@ -190,11 +197,8 @@
   struct GNUNET_CHAT_Room *chat_room;
   struct GNUNET_ClientServerConnection *sock;
 
-  int ret;
   int size_of_join;
 
-  ret = GNUNET_OK;
-
   csHdr.size = htons (sizeof (CS_chat_JOIN_MESSAGE));
   csHdr.type = htons (GNUNET_CS_PROTO_CHAT_JOIN_MSG);
 
@@ -203,7 +207,7 @@
   if (sock == NULL)
     {
       fprintf (stderr, _("Error establishing connection with gnunetd.\n"));
-      ret = GNUNET_SYSERR;
+      return NULL;
     }
 
   // connect
@@ -234,8 +238,8 @@
     {
       /* ALREADY LOGGED */
       fprintf (stderr, _("Error writing to socket.\n"));
-      ret = GNUNET_SYSERR;
-      /* WHY CONTINUE HERE? => CREATES BAD THREAD! */
+      GNUNET_free(join_msg);
+      return NULL;
     }
 
   GNUNET_free (join_msg);
@@ -264,12 +268,6 @@
   chat_room->listen_thread =
     GNUNET_thread_create (&poll_thread, chat_room, 1024 * 2);
 
-  // return room struct
-  if (ret != GNUNET_OK)
-    {
-      GNUNET_free (chat_room);
-      return NULL;
-    }
   return chat_room;
 }
 
@@ -279,26 +277,15 @@
 void
 GNUNET_CHAT_leave_room (struct GNUNET_CHAT_Room *chat_room)
 {
-  GNUNET_MessageHeader csHdr;
-  csHdr.type = htons (GNUNET_CS_PROTO_CHAT_LEAVE_MSG);
-  csHdr.size = htons (sizeof (csHdr));
-
-  /*If this fails we don't care, this means the socket is already gone and the 
server should know how to deal with that! */
-  /*We may not even need this message at all, just let the server handle a 
dead socket */ 
-  /* RIGHT, SO WHY HAVE IT? */
-  GNUNET_GE_ASSERT (NULL,
-                    GNUNET_client_connection_write (chat_room->sock,
-                                                    &csHdr) == GNUNET_OK);
-
-
-  // stop thread
-  // join thread
-  // free room struct
-
+  void * unused;
+  chat_room->shutdown_flag = GNUNET_YES;
+  GNUNET_client_connection_close_forever (chat_room->sock);
+  GNUNET_stop_sleep(room->listen_thread);
+  GNUNET_pthread_join(room->listen_thread, &unused);
   GNUNET_free (chat_room->nickname);
   GNUNET_free (chat_room->memberInfo);
   GNUNET_client_connection_destroy (chat_room->sock);
-
+  GNUNET_free (chat_room);
 }
 
 /**

Modified: GNUnet/src/applications/chat/gnunet-chat.c
===================================================================
--- GNUnet/src/applications/chat/gnunet-chat.c  2008-02-21 16:20:35 UTC (rev 
6404)
+++ GNUnet/src/applications/chat/gnunet-chat.c  2008-02-21 21:56:42 UTC (rev 
6405)
@@ -202,7 +202,6 @@
   if (room != NULL)
     {
       GNUNET_CHAT_leave_room (room);
-      GNUNET_free (room);
     }
   if (message != NULL)
     {





reply via email to

[Prev in Thread] Current Thread [Next in Thread]