WIP: pth, cleanup, transistion mostly done
authorChristian Thaeter <ct@pipapo.org>
Sun, 21 Oct 2007 23:42:38 +0000 (01:42 +0200)
committerChristian Thaeter <ct@pipapo.org>
Sun, 21 Oct 2007 23:42:38 +0000 (01:42 +0200)
src/rxpd.h
src/rxpd_connection.c
src/rxpd_connection_cmd.c

index a3c39cc..1ce0366 100644 (file)
@@ -70,6 +70,13 @@ enum rxpd_cmd_e {RXPD_COMMANDS};
  */
 #define RXPD_FILE_ILG_CHARS "@&?<>|/: \t\n\r*?\\"
 
+/*
+ * Call a cooperative pth_yield every this much expensive interations
+ * not implemented yet
+ * A higher number favors throughput, lower number improves latency
+ */
+#define RXPD_YIELD_EVERY 500
+
 struct rxpd_base;
 struct rxpd_file;
 struct rxpd_rule;
@@ -262,7 +269,7 @@ void*
 rxpd_connection_parse_cmd (void* ptr);
 
 /* generate prototypes for each defined command */
-#define RXPD_CMD(cmd, _) struct rxpd_connection* rxpd_connection_cmd_##cmd (struct rxpd_connection* self);
+#define RXPD_CMD(cmd, _) void rxpd_connection_cmd_##cmd (struct rxpd_connection* self);
 RXPD_COMMANDS
 #undef RXPD_CMD
 
index 1f048be..770ef8b 100644 (file)
@@ -51,8 +51,6 @@ rxpd_connection_delete (struct rxpd_connection* self)
 {
   if (self)
     {
-      // TODO kill connecter if not self
-
       close (self->fd);
       free (self->tmp_str);
       LLIST_WHILE_HEAD (&self->tmp_list, n)
@@ -202,9 +200,10 @@ rxpd_connection_parse_cmd (void* ptr)
     {
 #define RXPD_CMD(cmd, _)                        \
 case RXPD_CMD_##cmd:                            \
-  return rxpd_connection_cmd_##cmd (ptr);       \
-
+   rxpd_connection_cmd_##cmd (self);            \
+   rxpd_connection_delete (self);               \
   break;
+
       RXPD_COMMANDS
 #undef RXPD_CMD
     }
index 94da7d1..06e969a 100644 (file)
 
 #include "rxpd.h"
 
-struct rxpd_connection*
+#define RXPD_FILENAME_REQUIRED                                          \
+  do {                                                                  \
+    if (!self->file)                                                    \
+      {                                                                 \
+        rxpd_buffer_printf (&self->out, "#ERROR: missing filename\n");  \
+        return;                                                         \
+      }                                                                 \
+  } while (0)
+
+void
 rxpd_connection_cmd_CHECK (struct rxpd_connection* self)
 {
-  char* line;
+  RXPD_FILENAME_REQUIRED;
 
+  char* line;
   while ((line = rxpd_buffer_readline (&self->in)))
     {
       if (*line == '\0')
@@ -34,6 +44,8 @@ rxpd_connection_cmd_CHECK (struct rxpd_connection* self)
         }
       else
         {
+          // TODO lock the list, printf schedules!
+          // TODO implement RXPD_YIELD_EVERY
           LLIST_FOREACH (&self->file->rules, n)
             {
               struct rxpd_rule* rule = (struct rxpd_rule*)n;
@@ -51,24 +63,9 @@ rxpd_connection_cmd_CHECK (struct rxpd_connection* self)
             }
         }
     }
-#if 0
-  else if (!self->file)
-    {
-      rxpd_buffer_printf (&self->out, "#ERROR: no such file\n");
-      rxpd_connection_delete (self);
-      return;
-    }
-  if (rxpd_buffer_state (&self->in) == RXPD_OK)
-    rxpd_connection_schedule (self);
-  else
-    {
-      if (rxpd_buffer_state (&self->in) == RXPD_ERROR)
-        rxpd_buffer_printf (&self->out, "#ERROR:\n");
-      rxpd_connection_delete (self);
-    }
-#endif
 
-  return self;
+  if (rxpd_buffer_state (&self->in) == RXPD_ERROR)
+    rxpd_buffer_printf (&self->out, "#ERROR:\n");
 }
 
 
@@ -89,55 +86,37 @@ rxpd_connection_APPEND_PREPEND_helper (struct rxpd_connection* self)
 
           llist_insert_tail (&self->tmp_list, &rule->node);
         }
-      //          else goto finish;     /* move along, look elsewhere! This goto is not harmful and saves some code. */
     }
 
-#if 0
-  else if (!event && !self->file)
-    {
-      rxpd_buffer_printf (&self->out, "#ERROR: no such file\n");
-      rxpd_connection_delete (self);
-      return;
-    }
-
-  if (rxpd_buffer_state (&self->in) == RXPD_OK)
-    rxpd_connection_schedule (self);
+  if (rxpd_buffer_state (&self->in) == RXPD_ERROR)
+    rxpd_buffer_printf (&self->out, "#ERROR:\n");
   else
-    {
-      // TODO should also print error when any rule compilation failed, use tmp_str to save case?
-      if (rxpd_buffer_state (&self->in) == RXPD_ERROR)
-        rxpd_buffer_printf (&self->out, "#ERROR:\n");
-      else
-        {
-        finish:
-          rxpd_buffer_printf (&self->out, "#OK:\n");
-        }
-
-      rxpd_connection_delete (self);
-    }
-#endif
+    rxpd_buffer_printf (&self->out, "#OK:\n");
 }
 
-struct rxpd_connection*
+void
 rxpd_connection_cmd_APPEND (struct rxpd_connection* self)
 {
+  RXPD_FILENAME_REQUIRED;
+
   rxpd_connection_APPEND_PREPEND_helper (self);
   llist_insertlist_prev (&self->file->rules, &self->tmp_list);
-  return self;
 }
 
-struct rxpd_connection*
+void
 rxpd_connection_cmd_PREPEND (struct rxpd_connection* self)
 {
+  RXPD_FILENAME_REQUIRED;
+
   rxpd_connection_APPEND_PREPEND_helper (self);
   llist_insertlist_next (&self->file->rules, &self->tmp_list);
-  return self;
 }
 
-
-struct rxpd_connection*
+void
 rxpd_connection_cmd_REMOVE (struct rxpd_connection* self)
 {
+  RXPD_FILENAME_REQUIRED;
+
   char* line;
   while ((line = rxpd_buffer_readline (&self->in)))
     {
@@ -158,32 +137,16 @@ rxpd_connection_cmd_REMOVE (struct rxpd_connection* self)
       ;
     }
 
-#if 0
-  else if (!self->file)
-    {
-      rxpd_buffer_printf (&self->out, "#ERROR: no such file\n");
-      rxpd_connection_delete (self);
-      return;
-    }
-
-  if (rxpd_buffer_state (&self->in) == RXPD_OK)
-    rxpd_connection_schedule (self);
-  else
-    {
-      if (rxpd_buffer_state (&self->in) == RXPD_ERROR)
-        rxpd_buffer_printf (&self->out, "#ERROR:\n");
-      rxpd_connection_delete (self);
-    }
-#endif
-  return self;
+  if (rxpd_buffer_state (&self->in) == RXPD_ERROR)
+    rxpd_buffer_printf (&self->out, "#ERROR:\n");
 }
 
 
+
 static int
 rxpd_connection_do_REPLACE (struct rxpd_connection* self)
 {
   struct rxpd_rule* rule;
-
   LLIST_FOREACH (&self->file->rules, n)
     {
       rule = (struct rxpd_rule*)n;
@@ -191,6 +154,7 @@ rxpd_connection_do_REPLACE (struct rxpd_connection* self)
         goto found;
     }
   return 0;
+
  found:
   llist_insertlist_next (&rule->node, &self->tmp_list);
   rxpd_rule_delete (rule);
@@ -199,9 +163,11 @@ rxpd_connection_do_REPLACE (struct rxpd_connection* self)
   return 1;
 }
 
-struct rxpd_connection*
+void
 rxpd_connection_cmd_REPLACE (struct rxpd_connection* self)
 {
+  RXPD_FILENAME_REQUIRED;
+
   char* line;
   while ((line = rxpd_buffer_readline (&self->in)))
     {
@@ -222,86 +188,53 @@ rxpd_connection_cmd_REPLACE (struct rxpd_connection* self)
         self->tmp_str = rxpd_strdup (line);
     }
 
-#if 0
-  else if (!self->file)
-    {
-      rxpd_buffer_printf (&self->out, "#ERROR: no such file\n");
-      rxpd_connection_delete (self);
-      return;
-    }
-
-  if (rxpd_buffer_state (&self->in) == RXPD_OK)
-    rxpd_connection_schedule (self);
-  else
-    {
-      if (rxpd_buffer_state (&self->in) == RXPD_ERROR)
-        rxpd_buffer_printf (&self->out, "#ERROR:\n");
-      else
-        {
-        }
+  if (rxpd_buffer_state (&self->in) == RXPD_ERROR)
+    rxpd_buffer_printf (&self->out, "#ERROR:\n");
 
-      rxpd_connection_delete (self);
-    }
-#endif
   if (rxpd_connection_do_REPLACE (self))
     rxpd_buffer_printf (&self->out, "#OK:\n");
   else
-    rxpd_buffer_printf (&self->out, "#ERROR: rule matching '%s'\n", self->tmp_str);
-  return self;
+    rxpd_buffer_printf (&self->out, "#ERROR: no rule matching '%s'\n", self->tmp_str);
+
 }
 
-struct rxpd_connection*
+void
 rxpd_connection_cmd_LOAD (struct rxpd_connection* self)
 {
-  if (self->file)
-    {
-      if (rxpd_file_load (self->file))
-        {
-          rxpd_buffer_printf (&self->out, "#OK:\n");
-        }
-      else
-        {
-          rxpd_buffer_printf (&self->out, "#ERROR: loading file '%s'\n", (const char*)self->file->node.key);
-        }
-    }
+  RXPD_FILENAME_REQUIRED;
+
+  if (rxpd_file_load (self->file))
+    rxpd_buffer_printf (&self->out, "#OK:\n");
   else
-    rxpd_buffer_printf (&self->out, "#ERROR: no such file\n");
-  return self;
+    rxpd_buffer_printf (&self->out, "#ERROR: loading file '%s'\n", (const char*)self->file->node.key);
 }
 
-struct rxpd_connection*
+void
 rxpd_connection_cmd_SAVE (struct rxpd_connection* self)
 {
-  if (self->file)
-    {
-      if (rxpd_file_save (self->file))
-        {
-          rxpd_buffer_printf (&self->out, "#OK:\n");
-        }
-      else
-        {
-          rxpd_buffer_printf (&self->out, "#ERROR: saving file '%s'\n", (const char*)self->file->node.key);
-        }
-    }
+  RXPD_FILENAME_REQUIRED;
+
+  if (rxpd_file_save (self->file))
+    rxpd_buffer_printf (&self->out, "#OK:\n");
   else
-    rxpd_buffer_printf (&self->out, "#ERROR: no such file\n");
-  return self;
+    rxpd_buffer_printf (&self->out, "#ERROR: saving file '%s'\n", (const char*)self->file->node.key);
 }
 
-struct rxpd_connection*
+
+void
 rxpd_connection_cmd_DELETE (struct rxpd_connection* self)
 {
-  if (self->file)
-    {
-      rxpd_file_delete (self->file);
-      rxpd_buffer_printf (&self->out, "#OK:\n");
-    }
-  return self;
+  RXPD_FILENAME_REQUIRED;
+
+  rxpd_file_delete (self->file);
+  rxpd_buffer_printf (&self->out, "#OK:\n");
 }
 
-struct rxpd_connection*
+void
 rxpd_connection_cmd_FETCH (struct rxpd_connection* self)
 {
+  RXPD_FILENAME_REQUIRED;
+
   struct rxpd_base* base = self->socket->base;
 
   char* line;
@@ -347,33 +280,12 @@ rxpd_connection_cmd_FETCH (struct rxpd_connection* self)
 
       freeaddrinfo (addrs);
     }
-
-
-#if 0
-  else if (!self->file)
-    {
-      rxpd_buffer_printf (&self->out, "#ERROR: no such file\n");
-      rxpd_connection_delete (self);
-      return;
-    }
-
-  if (!event && rxpd_buffer_state (&self->in) == RXPD_OK)
-    rxpd_connection_schedule (self);
-#endif
-  return self;
 }
 
-struct rxpd_connection*
+void
 rxpd_connection_cmd_DUMP (struct rxpd_connection* self)
 {
-#if 0
-  if (!event && !self->file)
-    {
-      rxpd_buffer_printf (&self->out, "#ERROR: no such file\n");
-      rxpd_connection_delete (self);
-      return;
-    }
-#endif
+  RXPD_FILENAME_REQUIRED;
 
   if (llist_is_empty (&self->file->rules))
     rxpd_buffer_printf (&self->out, "#OK:\n");
@@ -390,7 +302,6 @@ rxpd_connection_cmd_DUMP (struct rxpd_connection* self)
             rxpd_buffer_printf (&self->out, "%s\n", rule->string);
         }
     }
-  return self;
 }
 
 
@@ -408,7 +319,7 @@ walk_LIST (PSplay node, const enum psplay_order_e which, int level, void* data)
 }
 
 
-struct rxpd_connection*
+void
 rxpd_connection_cmd_LIST (struct rxpd_connection* self)
 {
   struct rxpd_base* base = self->socket->base;
@@ -417,12 +328,12 @@ rxpd_connection_cmd_LIST (struct rxpd_connection* self)
     rxpd_buffer_printf (&self->out, "#OK:\n");
   else
     psplay_walk (&base->files, NULL, walk_LIST, 0, self);
-  return self;
 }
 
-struct rxpd_connection*
+void
 rxpd_connection_cmd_SHUTDOWN (struct rxpd_connection* self)
 {
+  (void) self;
   //struct rxpd_base* base = self->socket->base;
   // destroy all sockets
 #if 0
@@ -433,11 +344,10 @@ rxpd_connection_cmd_SHUTDOWN (struct rxpd_connection* self)
     }
   rxpd_buffer_printf (&self->out, "#OK:\n");
 #endif
-  return self;
 }
 
 
-struct rxpd_connection*
+void
 rxpd_connection_cmd_VERSION (struct rxpd_connection* self)
 {
   rxpd_buffer_printf (&self->out, PACKAGE_STRING "\n#\n"
@@ -447,10 +357,9 @@ rxpd_connection_cmd_VERSION (struct rxpd_connection* self)
                       "# the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.\n"
                       "# There is NO WARRANTY, to the extent permitted by law.\n#\n"
                       "# http://www.pipapo.org/pipawiki/RegexPolicyDaemon\n");
-  return self;
 }
 
-struct rxpd_connection*
+void
 rxpd_connection_cmd_HELP (struct rxpd_connection* self)
 {
   //struct rxpd_base* base = self->socket->base;
@@ -459,68 +368,43 @@ rxpd_connection_cmd_HELP (struct rxpd_connection* self)
   RXPD_COMMANDS
 #undef RXPD_CMD
   rxpd_buffer_printf (&self->out, "#\n# general syntax is: 'COMMAND:listname\\n..data..'\n");
-  return self;
 }
 
-struct rxpd_connection*
+void
 rxpd_connection_cmd_EXPIRE (struct rxpd_connection* self)
 {
+  RXPD_FILENAME_REQUIRED;
+
   struct rxpd_base* base = self->socket->base;
 
-  char* line;
-  while ((line = rxpd_buffer_readline (&self->in)))
+  char* line = rxpd_buffer_readline (&self->in);
+  if (*line)
     {
-      if (*line)
-        {
-          time_t since = time (NULL) - atoi (line);
-          rxpd_log (base, LOG_INFO, "expire all entries in '%s' since %ld\n",
-                    (const char*) self->file->node.key, since);
+      // TODO strtol, error handling
+      time_t since = time (NULL) - atoi (line);
+      rxpd_log (base, LOG_INFO, "expire all entries in '%s' since %ld\n",
+                (const char*) self->file->node.key, since);
 
-          LLIST_FOREACH (&self->file->rules, n)
+      LLIST_FOREACH (&self->file->rules, n)
+        {
+          struct rxpd_rule* rule = (struct rxpd_rule*)n;
+          if (rule->atime != -1 && rule->atime < since)
             {
-              struct rxpd_rule* rule = (struct rxpd_rule*)n;
-              if (rule->atime != -1 && rule->atime < since)
-                {
-                  n = llist_prev (n);
-                  rxpd_log (base, LOG_DEBUG, "expiring %ld:%s\n", rule->atime, rule->string);
-                  rxpd_buffer_printf (&self->out, "#OK: expiring '%s'\n", rule->string);
-                  rxpd_rule_delete (rule);
-                }
+              n = llist_prev (n);
+              rxpd_log (base, LOG_DEBUG, "expiring %ld:%s\n", rule->atime, rule->string);
+              rxpd_buffer_printf (&self->out, "#OK: expiring '%s'\n", rule->string);
+              rxpd_rule_delete (rule);
             }
         }
-      else
-        rxpd_buffer_printf (&self->out, "#OK:\n");
-    }
-
-
-#if 0
-    }
-  else if (!self->file)
-    {
-      rxpd_buffer_printf (&self->out, "#ERROR: no such file\n");
-      rxpd_connection_delete (self);
-      return;
     }
-
-  if (rxpd_buffer_state (&self->in) == RXPD_OK)
-    rxpd_connection_schedule (self);
   else
-    {
-      if (rxpd_buffer_state (&self->in) == RXPD_ERROR)
-        rxpd_buffer_printf (&self->out, "#ERROR:\n");
-      rxpd_connection_delete (self);
-    }
-#endif
-  return self;
+    rxpd_buffer_printf (&self->out, "#ERROR: no age given\n");
 }
 
 /* Template
-struct rxpd_connection*
-rxpd_connection_cmd_ (struct rxpd_connection* ptr)
+void
+rxpd_connection_cmd_ (struct rxpd_connection* self)
 {
-  (void) fd;
-  (void) event;
-  struct rxpd_connection* self = (struct rxpd_connection*) ptr;
   struct rxpd_base* base = self->socket->base;
   rxpd_die ("Unimplemented\n");
 }