WIP: pth, cleanup, transistion mostly done
[rxpd] / src / rxpd_connection.c
index 2c99877dbefbcf2722d447922feb74d4493ba7eb..770ef8bbe26f4f4be3d54e63a41c27620620bdbe 100644 (file)
 #include "rxpd.h"
 
 struct rxpd_connection*
-rxpd_connection_new (struct rxpd_socket* socket, int fd)
+rxpd_connection_new (struct rxpd_socket* socket)
 {
   struct rxpd_connection* self;
   self = rxpd_malloc (sizeof (struct rxpd_connection));
 
-  self->fd = accept (fd, NULL, 0);
+  self->fd = pth_accept (socket->fd, NULL, 0);
   if (self->fd == -1)
     abort ();
 
@@ -39,7 +39,7 @@ rxpd_connection_new (struct rxpd_socket* socket, int fd)
   rxpd_buffer_init (&self->in, self);
   rxpd_buffer_init (&self->out, self);
   
-  event_set (&self->ev, self->fd, EV_READ, rxpd_connection_parse_cmd, self);
+  self->connecter = NULL;
 
   // TODO more info
   rxpd_log (socket->base, LOG_INFO, "incoming connection\n");
@@ -51,7 +51,6 @@ rxpd_connection_delete (struct rxpd_connection* self)
 {
   if (self)
     {
-      event_del (&self->ev);
       close (self->fd);
       free (self->tmp_str);
       LLIST_WHILE_HEAD (&self->tmp_list, n)
@@ -64,11 +63,21 @@ rxpd_connection_delete (struct rxpd_connection* self)
 }
 
 struct rxpd_connection*
-rxpd_connection_schedule (struct rxpd_connection* self)
+rxpd_connection_spawn (struct rxpd_connection* self)
 {
   if (self)
     {
-      event_add (&self->ev, NULL);
+      if (self->connecter)
+        rxpd_die ("connection thread already spawned\n");
+
+      pth_attr_t attr = pth_attr_new ();
+
+      pth_attr_set (attr, PTH_ATTR_JOINABLE, FALSE);
+
+      self->connecter = pth_spawn (attr, rxpd_connection_parse_cmd, self);
+
+      if (!self->connecter)
+        rxpd_die ("failed spawning thread\n");
     }
   return self;
 }
@@ -112,23 +121,22 @@ rxpd_connection_check_policy (struct rxpd_connection* self, char* line)
     }
   return 1;
 }
-void
-rxpd_connection_parse_cmd (int fd, short event, void* ptr)
-{
-  (void) event;
 
+void*
+rxpd_connection_parse_cmd (void* ptr)
+{
   struct rxpd_connection* self = (struct rxpd_connection*) ptr;
   struct rxpd_base* base = self->socket->base;
 
   char* line;
-  line = rxpd_buffer_readline (&self->in, 0);
+  line = rxpd_buffer_readline (&self->in);
 
   if (!line)
     {
       rxpd_log (base, LOG_ERR, "no data\n");
       rxpd_buffer_printf (&self->out, "#ERROR: no data\n");
-      close (fd);
-      return;
+      close (self->fd);
+      return NULL;
     }
 
   rxpd_log (base, LOG_DEBUG, "parse command '%s'\n", line);
@@ -150,19 +158,20 @@ rxpd_connection_parse_cmd (int fd, short event, void* ptr)
   for (i = cmds; i->cmd; ++i)
     if (strncmp (line, i->cmd, i->sz) == 0)
       break;
+
   if (!i->cmd)
     {
       rxpd_log (base, LOG_ERR, "no command\n");
       rxpd_buffer_printf (&self->out, "#ERROR: no command\n");
       rxpd_connection_delete (self);
-      return;
+      return NULL;
     }
 
   if (!rxpd_connection_check_policy (self, line))
     {
       rxpd_buffer_printf (&self->out, "#ERROR: access denied\n");
       rxpd_connection_delete (self);
-      return;
+      return NULL;
     }
 
   if (line[i->sz])
@@ -178,21 +187,26 @@ rxpd_connection_parse_cmd (int fd, short event, void* ptr)
               rxpd_log (base, LOG_ERR, "illeagal filename\n");
               rxpd_buffer_printf (&self->out, "#ERROR: illegal filename\n");
               rxpd_connection_delete (self);
-              return;
+              return NULL;
             }
         }
     }
 
+
+  //TODO memleak have to call connection_destroy instead just returning
+
   // dispatch
   switch (i->nr)
     {
-#define RXPD_CMD(cmd, _)                                                        \
-case RXPD_CMD_##cmd:                                                            \
-  event_set (&self->ev, self->fd, EV_READ, rxpd_connection_cmd_##cmd, self);    \
-  rxpd_connection_cmd_##cmd (fd, 0, ptr);                                       \
+#define RXPD_CMD(cmd, _)                        \
+case RXPD_CMD_##cmd:                            \
+   rxpd_connection_cmd_##cmd (self);            \
+   rxpd_connection_delete (self);               \
   break;
+
       RXPD_COMMANDS
 #undef RXPD_CMD
     }
+  return NULL;
 }