src/plugin.c: Change the write callbacks to receive a user_data_t pointer.
authorFlorian Forster <octo@leeloo.lan.home.verplant.org>
Sat, 21 Feb 2009 17:35:45 +0000 (18:35 +0100)
committerFlorian Forster <octo@leeloo.lan.home.verplant.org>
Sat, 21 Feb 2009 17:35:45 +0000 (18:35 +0100)
There are by far not as many write callbacks, so I've just changed the
callback in general rather than introducing a `complex write' callback.

src/csv.c
src/network.c
src/perl.c
src/plugin.c
src/plugin.h
src/rrdcached.c
src/rrdtool.c

index 352557a..d01f47d 100644 (file)
--- a/src/csv.c
+++ b/src/csv.c
@@ -249,7 +249,8 @@ static int csv_config (const char *key, const char *value)
        return (0);
 } /* int csv_config */
 
-static int csv_write (const data_set_t *ds, const value_list_t *vl)
+static int csv_write (const data_set_t *ds, const value_list_t *vl,
+               user_data_t __attribute__((unused)) *user_data)
 {
        struct stat  statbuf;
        char         filename[512];
@@ -356,5 +357,5 @@ void module_register (void)
 {
        plugin_register_config ("csv", csv_config,
                        config_keys, config_keys_num);
-       plugin_register_write ("csv", csv_write);
+       plugin_register_write ("csv", csv_write, /* user_data = */ NULL);
 } /* void module_register */
index 66f0438..3b2c25b 100644 (file)
@@ -1507,7 +1507,8 @@ static void flush_buffer (void)
        memset (&send_buffer_vl, 0, sizeof (send_buffer_vl));
 }
 
-static int network_write (const data_set_t *ds, const value_list_t *vl)
+static int network_write (const data_set_t *ds, const value_list_t *vl,
+               user_data_t __attribute__((unused)) *user_data)
 {
        int status;
 
@@ -1767,7 +1768,8 @@ static int network_init (void)
        /* setup socket(s) and so on */
        if (sending_sockets != NULL)
        {
-               plugin_register_write ("network", network_write);
+               plugin_register_write ("network", network_write,
+                               /* user_data = */ NULL);
                plugin_register_notification ("network", network_notification);
        }
 
index 142e9c0..9ef9cfc 100644 (file)
@@ -1907,7 +1907,8 @@ static int perl_read (void)
        return pplugin_call_all (aTHX_ PLUGIN_READ);
 } /* static int perl_read (void) */
 
-static int perl_write (const data_set_t *ds, const value_list_t *vl)
+static int perl_write (const data_set_t *ds, const value_list_t *vl,
+               user_data_t __attribute__((unused)) *user_data)
 {
        dTHX;
 
@@ -2221,7 +2222,7 @@ static int init_pi (int argc, char **argv)
 
        plugin_register_read ("perl", perl_read);
 
-       plugin_register_write ("perl", perl_write);
+       plugin_register_write ("perl", perl_write, /* user_data = */ NULL);
        plugin_register_flush ("perl", perl_flush);
        plugin_register_shutdown ("perl", perl_shutdown);
        return 0;
index ee39bd3..9bcea3c 100644 (file)
@@ -58,6 +58,13 @@ struct read_func_s
 };
 typedef struct read_func_s read_func_t;
 
+struct write_func_s
+{
+       plugin_write_cb callback;
+       user_data_t udata;
+};
+typedef struct write_func_s write_func_t;
+
 /*
  * Private variables
  */
@@ -636,7 +643,24 @@ int plugin_unregister_read (const char *name)
 
 int plugin_unregister_write (const char *name)
 {
-       return (plugin_unregister (list_write, name));
+       llentry_t *e;
+       write_func_t *wf;
+
+       e = llist_search (list_write, name);
+
+       if (e == NULL)
+               return (-1);
+
+       llist_remove (list_write, e);
+
+       wf = (write_func_t *) e->value;
+       plugin_user_data_destroy (&wf->udata);
+       free (wf);
+       free (e->key);
+
+       llentry_destroy (e);
+
+       return (0);
 }
 
 int plugin_unregister_flush (const char *name)
@@ -815,7 +839,6 @@ int plugin_read_all_once (void)
 int plugin_write (const char *plugin, /* {{{ */
                const data_set_t *ds, const value_list_t *vl)
 {
-  int (*callback) (const data_set_t *ds, const value_list_t *vl);
   llentry_t *le;
   int status;
 
@@ -843,8 +866,10 @@ int plugin_write (const char *plugin, /* {{{ */
     le = llist_head (list_write);
     while (le != NULL)
     {
-      callback = le->value;
-      status = (*callback) (ds, vl);
+      write_func_t *wf = le->value;
+
+      DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
+      status = wf->callback (ds, vl, &wf->udata);
       if (status != 0)
         failure++;
       else
@@ -860,6 +885,7 @@ int plugin_write (const char *plugin, /* {{{ */
   }
   else /* plugin != NULL */
   {
+    write_func_t *wf;
     le = llist_head (list_write);
     while (le != NULL)
     {
@@ -872,8 +898,10 @@ int plugin_write (const char *plugin, /* {{{ */
     if (le == NULL)
       return (ENOENT);
 
-    callback = le->value;
-    status = (*callback) (ds, vl);
+    wf = le->value;
+
+    DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
+    status = wf->callback (ds, vl, &wf->udata);
   }
 
   return (status);
index 1eb9fec..e58444f 100644 (file)
@@ -147,6 +147,8 @@ typedef struct user_data_s user_data_t;
  * Callback types
  */
 typedef int (*plugin_read_cb) (user_data_t *);
+typedef int (*plugin_write_cb) (const data_set_t *, const value_list_t *,
+               user_data_t *);
 
 /*
  * NAME
@@ -240,7 +242,7 @@ int plugin_register_read (const char *name,
 int plugin_register_complex_read (const char *name,
                plugin_read_cb callback, user_data_t *user_data);
 int plugin_register_write (const char *name,
-               int (*callback) (const data_set_t *ds, const value_list_t *vl));
+               plugin_write_cb callback, user_data_t *user_data);
 int plugin_register_flush (const char *name,
                int (*callback) (const int timeout, const char *identifier));
 int plugin_register_shutdown (char *name,
index 31c6352..326a889 100644 (file)
@@ -314,7 +314,8 @@ static int rc_init (void)
   return (0);
 } /* int rc_init */
 
-static int rc_write (const data_set_t *ds, const value_list_t *vl)
+static int rc_write (const data_set_t *ds, const value_list_t *vl,
+    user_data_t __attribute__((unused)) *user_data)
 {
   char filename[512];
   char values[512];
@@ -405,7 +406,7 @@ void module_register (void)
   plugin_register_config ("rrdcached", rc_config,
       config_keys, config_keys_num);
   plugin_register_init ("rrdcached", rc_init);
-  plugin_register_write ("rrdcached", rc_write);
+  plugin_register_write ("rrdcached", rc_write, /* user_data = */ NULL);
   plugin_register_shutdown ("rrdcached", rc_shutdown);
 } /* void module_register */
 
index debb7bd..698f89f 100644 (file)
@@ -746,7 +746,8 @@ static int rrd_compare_numeric (const void *a_ptr, const void *b_ptr)
                return (0);
 } /* int rrd_compare_numeric */
 
-static int rrd_write (const data_set_t *ds, const value_list_t *vl)
+static int rrd_write (const data_set_t *ds, const value_list_t *vl,
+               user_data_t __attribute__((unused)) *user_data)
 {
        struct stat  statbuf;
        char         filename[512];
@@ -1046,7 +1047,7 @@ void module_register (void)
        plugin_register_config ("rrdtool", rrd_config,
                        config_keys, config_keys_num);
        plugin_register_init ("rrdtool", rrd_init);
-       plugin_register_write ("rrdtool", rrd_write);
+       plugin_register_write ("rrdtool", rrd_write, /* user_data = */ NULL);
        plugin_register_flush ("rrdtool", rrd_flush);
        plugin_register_shutdown ("rrdtool", rrd_shutdown);
 }