From: Florian Forster Date: Sat, 4 Dec 2010 14:49:34 +0000 (+0100) Subject: dotnet plugin: Add support for init functions. X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=9aca8c88ea178ecd8bc87a9b03378a60d4201b7f;p=collectd.git dotnet plugin: Add support for init functions. --- diff --git a/bindings/csharp/collectd.cs b/bindings/csharp/collectd.cs index 279f430e..47ed63cc 100644 --- a/bindings/csharp/collectd.cs +++ b/bindings/csharp/collectd.cs @@ -31,6 +31,7 @@ using System.Runtime.InteropServices; namespace CollectdAPI { + public delegate int CollectdInitCallback (); public delegate int CollectdReadCallback (); public interface IValue /* {{{ */ @@ -349,12 +350,18 @@ namespace CollectdAPI public class Collectd /* {{{ */ { private static Hashtable _readFunctions = new Hashtable (); + private static Hashtable _initFunctions = new Hashtable (); [DllImport("__Internal", EntryPoint="plugin_log")] private extern static int _log ( [MarshalAs(UnmanagedType.SysInt)] int severity, [MarshalAs(UnmanagedType.LPStr)] string message); + [DllImport("__Internal", EntryPoint="dotnet_register_init")] + private extern static int _registerInit ( + [MarshalAs(UnmanagedType.LPStr)] string name, + CollectdInitCallback func); + [DllImport("__Internal", EntryPoint="dotnet_register_read")] private extern static int _registerRead ( [MarshalAs(UnmanagedType.LPStr)] string name, @@ -393,6 +400,15 @@ namespace CollectdAPI return (_log (7, message)); } + public static int RegisterInit (string name, CollectdInitCallback func) + { + if (_initFunctions.Contains (name)) + return (-1); + _initFunctions.Add (name, func); + + return (_registerInit (name, func)); + } /* int RegisterInit */ + public static int RegisterRead (string name, CollectdReadCallback func) { if (_readFunctions.Contains (name)) diff --git a/src/dotnet.c b/src/dotnet.c index 73240a89..da5a81bd 100644 --- a/src/dotnet.c +++ b/src/dotnet.c @@ -48,9 +48,43 @@ struct dotnet_value_list_s }; typedef struct dotnet_value_list_s dotnet_value_list_t; +#define CB_TYPE_INIT 2 +struct dotnet_callback_info_s; +typedef struct dotnet_callback_info_s dotnet_callback_info_t; +struct dotnet_callback_info_s +{ + char *name; + int type; + void *callback; + dotnet_callback_info_t *next; +}; + typedef int (*dotnet_read_cb) (void); static MonoDomain *_domain = NULL; +static dotnet_callback_info_t *callback_list = NULL; + +static int dotnet_init (void) /* {{{ */ +{ + dotnet_callback_info_t *ci; + + for (ci = callback_list; ci != NULL; ci = ci->next) + { + plugin_init_cb cb; + int status; + + if (ci->type != CB_TYPE_INIT) + continue; + + cb = ci->callback; + status = (*cb) (); + if (status != 0) + ERROR ("dotnet plugin: The init function \"%s\" failed with status %i.", + ci->name, status); + } /* for (callback_list) */ + + return (0); +} /* }}} int dotnet_init */ static int dotnet_read (user_data_t *ud) /* {{{ */ { @@ -62,6 +96,34 @@ static int dotnet_read (user_data_t *ud) /* {{{ */ /* * Functions exposed to .Net */ +int dotnet_register_init (const char *name, plugin_init_cb cb) /* {{{ */ +{ + dotnet_callback_info_t *ci; + + if ((name == NULL) || (cb == NULL)) + return (EINVAL); + + ci = malloc (sizeof (*ci)); + if (ci == NULL) + return (ENOMEM); + memset (ci, 0, sizeof (*ci)); + + ci->name = strdup (name); + if (ci->name == NULL) + { + sfree (ci); + return (ENOMEM); + } + + ci->type = CB_TYPE_INIT; + ci->callback = cb; + + ci->next = callback_list; + callback_list = ci; + + return (0); +} /* }}} int dotnet_register_init */ + int dotnet_register_read (const char *name, dotnet_read_cb cb) /* {{{ */ { user_data_t ud; @@ -269,6 +331,7 @@ void module_register (void) return; } + plugin_register_init ("dotnet", dotnet_init); plugin_register_complex_config ("dotnet", dotnet_config); } /* void module_register */