Author: ArcRiley
Date: 2009-01-05 20:26:19 -0500 (Mon, 05 Jan 2009)
New Revision: 1425

Modified:
   trunk/concordance/src/Core.c
   trunk/concordance/src/Core.h
Log:
creates Session hashtable, reads data from sockets to screen


Modified: trunk/concordance/src/Core.c
===================================================================
--- trunk/concordance/src/Core.c        2009-01-06 01:25:35 UTC (rev 1424)
+++ trunk/concordance/src/Core.c        2009-01-06 01:26:19 UTC (rev 1425)
@@ -38,12 +38,14 @@
 #endif
 
 #include "Core.h"
+#include "utils.h"
 
 
-
 static gpointer   conCore_loop          (gpointer);
-static int        conCore_listenChannel (conCoreObject*, conSocket*);
+static int        conCore_listenChannel (conCoreObject*, conChannel*);
 static gboolean   conCore_listenNew     (GIOChannel*, GIOCondition, gpointer);
+static gboolean   conCore_sessionData   (GIOChannel*, GIOCondition, gpointer);
+
                                                                           /*\ 
 cdef class Core :                                                         \*/
   static char conCore_Doc[] = "Test";
@@ -155,6 +157,18 @@
     if (!conCore_listenChannel(self, &self->s2s))
       return -1;
 
+    /* create session hashtables
+
+       GHashTable* g_hash_table_new_full (GHashFunc hash_func,
+                                          GEqualFunc key_equal_func,
+                                          GDestroyNotify key_destroy_func,
+                                          GDestroyNotify value_destroy_func);
+
+        self->channels uses GIOChannel* as keys.
+    */
+    self->channels = g_hash_table_new_full(g_direct_hash, g_direct_equal,
+                                           NULL, NULL);
+
     /* launch GMainLoop thread before returning
 
        GThread*    g_thread_create     (GThreadFunc func,
@@ -206,15 +220,11 @@
 
 
   static int 
-  conCore_listenChannel(conCoreObject* self, conSocket* sock) {           /*\
+  conCore_listenChannel(conCoreObject* self, conChannel* sock) {          /*\
     cdef :                                                                \*/
       int                ret;
       struct addrinfo*   resinfo = NULL;
-      GSource*           source;
 
-
-
-
     /* Get address family and struct
 
        int getaddrinfo(const char *node, const char *service,
@@ -281,35 +291,17 @@
       if (bind(sock->sock, resinfo->ai_addr, resinfo->ai_addrlen) == 0 &&
           listen(sock->sock, 16) == 0 ) {
 
-        /* create new channel using an OS-dependent function
+        /* create channel and add it to our context watchlist
 
-           GIOChannel*       g_io_channel_win32_new_fd     (gint fd);
-           GIOChannel*       g_io_channel_unix_new         (gint fd);
-        */
-        #ifdef MS_WINDOWS
-          sock->chan = g_io_channel_win32_new_socket(sock->sock);
-        #else
-          sock->chan = g_io_channel_unix_new(sock->sock);
-        #endif
-
-        /* add channel to our context watch
-
-           Note that this block is a replacement for g_io_add_watch() which
-           uses our own context rather than the default context.
-
-           GSource *    g_io_create_watch        (GIOChannel *channel,
-                                                  GIOCondition condition);
-           void         g_source_set_callback    (GSource *source,
-                                                  GSourceFunc func,
+           GIOChannel*  conAddSocket             (gint fd,
+                                                  GIOCondition condition,
+                                                  GSourceFunc callback,
                                                   gpointer data,
-                                                  GDestroyNotify notify);
-           guint        g_source_attach          (GSource *source,
-                                                  GMainContext *context);
+                                                  GMainContext* context);
         */
-        source = g_io_create_watch (sock->chan, G_IO_IN);
-        g_source_set_callback(source, (GSourceFunc)conCore_listenNew,
-                              (gpointer) self, NULL);
-        g_source_attach(source, self->context);
+        sock->chan = conAddSocket(sock->sock, G_IO_IN,
+                                  (GSourceFunc)conCore_listenNew, 
+                                  (gpointer) self, self->context);
         ret = TRUE;
       }
       else {
@@ -334,7 +326,7 @@
 
 
   static gboolean
-  conCore_listenNew(GIOChannel* source, GIOCondition condition,
+  conCore_listenNew(GIOChannel* sourceChannel, GIOCondition condition,
                     gpointer s) {                                         /*\
     cdef :                                                                \*/
       conCoreObject*        self = (conCoreObject*) s;
@@ -342,38 +334,106 @@
       gint                  fd;
       struct sockaddr       addr;
       guint                 addrlen = sizeof(addr);
-      gchar                 host[256];
+      GIOChannel*           newChannel;
+      conSession*           newSession;
 
-    /* we're only interested in G_IO_IN events */
-    if (condition != G_IO_IN)
-      return TRUE;
-
     /* accept new connection, return if we fail to connect
 
        int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
     */
-    if ((fd = accept(g_io_channel_unix_get_fd(source), 
+    if ((fd = accept(g_io_channel_unix_get_fd(sourceChannel), 
                      &addr, &addrlen))<0)
       return TRUE;
 
-    /* lookup hostname
+    if (sourceChannel == self->c2s.chan) {
+      /* create new session
 
-       int getnameinfo(const struct sockaddr *sa, socklen_t salen,
-                       char *host, size_t hostlen,
-                       char *serv, size_t servlen, int flags);
-    */
-    ret = getnameinfo(&addr, addrlen, host, 255, NULL, 0, 0);
-    close(fd);
+         gpointer       g_malloc                 (gsize n_bytes);
+      */
+      newSession = g_malloc(sizeof(conSession));
 
-    if (source == self->c2s.chan)
-      printf("new client: %s\n", host);
+      /* lookup hostname
+
+         int getnameinfo(const struct sockaddr *sa, socklen_t salen,
+                         char *host, size_t hostlen,
+                         char *serv, size_t servlen, int flags);
+      */
+      ret = getnameinfo(&addr, addrlen, newSession->host, 255, NULL, 0, 0);
+
+      /* create channel and add it to our context watchlist
+
+         GIOChannel*    conAddSocket             (gint fd,
+                                                  GIOCondition condition,
+                                                  GSourceFunc callback,
+                                                  gpointer data,
+                                                  GMainContext* context);
+      */
+      newSession->chan = conAddSocket(fd, G_IO_IN | G_IO_HUP,
+                                      (GSourceFunc)conCore_sessionData,
+                                      (gpointer) self, self->context);
+
+      g_io_channel_set_encoding(newSession->chan, NULL, NULL);
+      g_io_channel_set_buffered(newSession->chan, FALSE);
+
+      /* add new channel to the channels hashtable
+
+         void           g_hash_table_insert      (GHashTable *hash_table,
+                                                  gpointer key,
+                                                  gpointer value);
+      */
+      g_hash_table_insert(self->channels, newChannel, newSession);
+      printf("new client: %s\n", newSession->host);
+    }
     else
-      printf("new server: %s\n", host);
+      printf("new server");
 
+    /* return true so GMainLoop will continue watching this channel */
     return TRUE;
   }
 
 
+  static gboolean
+  conCore_sessionData(GIOChannel* channel, GIOCondition condition,
+                      gpointer s) {                                       /*\
+    cdef :                                                                \*/
+      conCoreObject*        self = (conCoreObject*) s;
+      GIOStatus             status;
+      gchar                 buff[1025];
+      guint                 buff_len = 0;
+
+    if (condition == G_IO_IN) {
+
+      /* read channel to buffer
+
+         GIOStatus      g_io_channel_read_chars  (GIOChannel *channel,
+                                                  gchar *buf,
+                                                  gsize count,
+                                                  gsize *bytes_read,
+                                                  GError **error);
+      */
+      status = g_io_channel_read_chars(channel, buff, 1024, &buff_len, NULL);
+      if (status == G_IO_STATUS_NORMAL && buff_len > 0) {
+        buff[buff_len] = '\0';
+        printf("%s\n", buff);
+
+        /* return true so GMainLoop will continue watching this channel */
+        return TRUE;
+      }
+    }
+    printf("end client\n");
+    /* G_IO_EOF, read EOF, or an unknown channel error has occured 
+
+       GIOStatus        g_io_channel_shutdown    (GIOChannel *channel,
+                                                  gboolean flush,
+                                                  GError **err);
+    */
+    g_io_channel_shutdown(channel, FALSE, NULL);
+
+    /* return false so GMainLoop will stop watching this channel */
+    return FALSE;
+  }
+
+
   static PyMethodDef conCore_methods[] = {
     { NULL, NULL },
   };

Modified: trunk/concordance/src/Core.h
===================================================================
--- trunk/concordance/src/Core.h        2009-01-06 01:25:35 UTC (rev 1424)
+++ trunk/concordance/src/Core.h        2009-01-06 01:26:19 UTC (rev 1425)
@@ -23,19 +23,26 @@
 #define CONCORE_H
 
 typedef struct {
+  GIOChannel*  chan;
   const gchar* addr;
   gushort      port;
   int          sock;
-  GIOChannel*  chan;
-} conSocket;
+} conChannel;
 
 typedef struct {
+  PyObject*   self;
+  GIOChannel* chan;
+  gchar       host[256];
+} conSession;
+
+typedef struct {
   PyObject_HEAD
-  conSocket        c2s;
-  conSocket        s2s;
-  GThread*         thread;
+  conChannel       c2s;
+  conChannel       s2s;
+  GHashTable*      channels;
   GMainContext*    context;
   GMainLoop*       mainloop;
+  GThread*         thread;
 } conCoreObject;
 
 PyTypeObject conCore_Type;

_______________________________________________
PySoy-SVN mailing list
PySoy-SVN@pysoy.org
http://www.pysoy.org/mailman/listinfo/pysoy-svn

Reply via email to