Cleaned up plugin callback hooks.

Started working on command storage by plugin.
This commit is contained in:
Jan Vidar Krey 2011-01-07 16:09:20 +01:00
parent ff5609b018
commit 8a6a10d4ec
11 changed files with 174 additions and 36 deletions

View File

@ -140,6 +140,7 @@ libuhub_SOURCES := \
src/core/route.c \
src/core/user.c \
src/core/usermanager.c \
src/core/plugincallback.c \
src/core/plugininvoke.c \
src/core/pluginloader.c \
src/network/backend.c \

View File

@ -1,6 +1,6 @@
/*
* uhub - A tiny ADC p2p connection hub
* Copyright (C) 2007-2010, Jan Vidar Krey
* Copyright (C) 2007-2011, Jan Vidar Krey
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by

View File

@ -854,7 +854,7 @@ void hub_plugins_load(struct hub_info* hub)
if (!hub->plugins)
return;
if (plugin_initialize(hub->config, hub->plugins) < 0)
if (plugin_initialize(hub->config, hub) < 0)
{
hub_free(hub->plugins);
hub->plugins = 0;

84
src/core/plugincallback.c Normal file
View File

@ -0,0 +1,84 @@
/*
* uhub - A tiny ADC p2p connection hub
* Copyright (C) 2007-2011, Jan Vidar Krey
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "uhub.h"
struct plugin_callback_data
{
struct linked_list* commands;
};
/*
static struct plugin_callback_data* get_callback_data(struct plugin_handle* plugin)
{
uhub_assert(plugin && plugin->handle && plugin->handle->callback_data);
struct plugin_callback_data* data = (struct plugin_callback_data*) plugin->handle->callback_data;
return data;
}
*/
static struct hub_user* convert_user_type(struct plugin_user* user)
{
struct hub_user* huser = (struct hub_user*) user;
return huser;
}
static int cbfunc_send_message(struct plugin_handle* plugin, struct plugin_user* user, const char* message)
{
// struct plugin_callback_data* data = get_callback_data(plugin);
char* buffer = adc_msg_escape(message);
struct adc_message* command = adc_msg_construct(ADC_CMD_IMSG, strlen(buffer) + 6);
adc_msg_add_argument(command, buffer);
route_to_user(plugin_get_hub(plugin), convert_user_type(user), command);
adc_msg_free(command);
hub_free(buffer);
return 1;
}
static int cbfunc_user_disconnect(struct plugin_handle* plugin, struct plugin_user* user)
{
// struct plugin_callback_data* data = get_callback_data(plugin);
hub_disconnect_user(plugin_get_hub(plugin), convert_user_type(user), quit_kicked);
return 0;
}
static int cbfunc_command_add(struct plugin_handle* plugin, struct plugin_command_handle* cmdh)
{
// struct plugin_callback_data* data = get_callback_data(plugin);
return 0;
}
static int cbfunc_command_del(struct plugin_handle* plugin, struct plugin_command_handle* cmdh)
{
// struct plugin_callback_data* data = get_callback_data(plugin);
return 0;
}
void plugin_register_callback_functions(struct plugin_handle* handle)
{
handle->hub.send_message = cbfunc_send_message;
handle->hub.user_disconnect = cbfunc_user_disconnect;
handle->hub.command_add = cbfunc_command_add;
handle->hub.command_del = cbfunc_command_del;
}
void plugin_unregister_callback_functions(struct plugin_handle* handle)
{
}

28
src/core/plugincallback.h Normal file
View File

@ -0,0 +1,28 @@
/*
* uhub - A tiny ADC p2p connection hub
* Copyright (C) 2007-2011, Jan Vidar Krey
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef HAVE_UHUB_PLUGIN_CALLBACK_H
#define HAVE_UHUB_PLUGIN_CALLBACK_H
struct plugin_handle;
extern void plugin_register_callback_functions(struct plugin_handle* handle);
extern void plugin_unregister_callback_functions(struct plugin_handle* handle);
#endif /* HAVE_UHUB_PLUGIN_CALLBACK_H */

View File

@ -1,6 +1,6 @@
/*
* uhub - A tiny ADC p2p connection hub
* Copyright (C) 2007-2010, Jan Vidar Krey
* Copyright (C) 2007-2011, Jan Vidar Krey
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -78,14 +78,6 @@ static struct plugin_user* convert_user_type(struct hub_user* user)
{
struct plugin_user* puser = (struct plugin_user*) user;
return puser;
#if 0
puser->sid = user->id.sid;
puser->nick = user->id.nick;
puser->cid = user->id.cid;
puser->user_agent = user->user_agent;
puser->addr = user->id.addr;
puser->credentials = user->credentials;
#endif
}
plugin_st plugin_check_ip_early(struct hub_info* hub, struct ip_addr_encap* addr)

View File

@ -1,6 +1,6 @@
/*
* uhub - A tiny ADC p2p connection hub
* Copyright (C) 2007-2010, Jan Vidar Krey
* Copyright (C) 2007-2011, Jan Vidar Krey
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by

View File

@ -1,6 +1,6 @@
/*
* uhub - A tiny ADC p2p connection hub
* Copyright (C) 2007-2010, Jan Vidar Krey
* Copyright (C) 2007-2011, Jan Vidar Krey
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -22,6 +22,19 @@
#ifdef PLUGIN_SUPPORT
#include "plugin_api/handle.h"
struct plugin_hub_internals
{
struct hub_info* hub;
plugin_unregister_f unregister;
};
static struct plugin_hub_internals* get_internals(struct plugin_handle* handle)
{
assert(handle && handle->handle && handle->handle->internals);
struct plugin_hub_internals* internals = (struct plugin_hub_internals*) handle->handle->internals;
return internals;
}
struct uhub_plugin* plugin_open(const char* filename)
{
LOG_TRACE("plugin_open: \"%s\"", filename);
@ -41,6 +54,8 @@ struct uhub_plugin* plugin_open(const char* filename)
return 0;
}
plugin->internals = hub_malloc_zero(sizeof(struct plugin_hub_internals));
return plugin;
#else
return 0;
@ -49,6 +64,7 @@ struct uhub_plugin* plugin_open(const char* filename)
void plugin_close(struct uhub_plugin* plugin)
{
hub_free(plugin->internals);
#ifdef HAVE_DLOPEN
dlclose(plugin->handle);
hub_free(plugin);
@ -65,21 +81,16 @@ void* plugin_lookup_symbol(struct uhub_plugin* plugin, const char* symbol)
#endif
}
static void plugin_register_hub_functions(struct plugin_handle* handle)
{
handle->hub.send_message = NULL;
handle->hub.user_disconnect = NULL;
handle->hub.command_add = NULL;
handle->hub.command_del = NULL;
}
struct plugin_handle* plugin_load(const char* filename, const char* config)
struct plugin_handle* plugin_load(const char* filename, const char* config, struct hub_info* hub)
{
plugin_register_f register_f;
plugin_unregister_f unregister_f;
int ret;
struct plugin_handle* handle = hub_malloc_zero(sizeof(struct plugin_handle));
struct uhub_plugin* plugin = plugin_open(filename);
struct plugin_hub_internals* internals;
if (!plugin)
return NULL;
@ -94,7 +105,9 @@ struct plugin_handle* plugin_load(const char* filename, const char* config)
register_f = plugin_lookup_symbol(plugin, "plugin_register");
unregister_f = plugin_lookup_symbol(plugin, "plugin_unregister");
plugin_register_hub_functions(handle);
plugin_register_callback_functions(handle);
internals = (struct plugin_hub_internals*) plugin->internals;
if (register_f && unregister_f)
{
@ -105,7 +118,11 @@ struct plugin_handle* plugin_load(const char* filename, const char* config)
{
LOG_INFO("Loaded plugin: %s: %s, version %s.", filename, handle->name, handle->version);
LOG_TRACE("Plugin API version: %d (func table size: " PRINTF_SIZE_T ")", handle->plugin_api_version, handle->plugin_funcs_size);
plugin->unregister = unregister_f;
// Set hub internals
internals->unregister = unregister_f;
internals->hub = hub;
return handle;
}
else
@ -126,15 +143,18 @@ struct plugin_handle* plugin_load(const char* filename, const char* config)
void plugin_unload(struct plugin_handle* plugin)
{
plugin->handle->unregister(plugin);
struct plugin_hub_internals* internals = get_internals(plugin);
plugin_unregister_callback_functions(plugin);
internals->unregister(plugin);
plugin_close(plugin->handle);
}
static int plugin_parse_line(char* line, int line_count, void* ptr_data)
{
struct uhub_plugins* handle = (struct uhub_plugins*) ptr_data;
struct plugin_handle* plugin;
struct hub_info* hub = (struct hub_info*) ptr_data;
struct uhub_plugins* handle = hub->plugins;
struct cfg_tokens* tokens = cfg_tokenize(line);
struct plugin_handle* plugin;
char *directive, *soname, *params;
if (cfg_token_count(tokens) == 0)
@ -159,7 +179,7 @@ static int plugin_parse_line(char* line, int line_count, void* ptr_data)
params = "";
LOG_TRACE("Load plugin: \"%s\", params=\"%s\"", soname, params);
plugin = plugin_load(soname, params);
plugin = plugin_load(soname, params, hub);
if (plugin)
{
list_append(handle->loaded, plugin);
@ -172,12 +192,12 @@ static int plugin_parse_line(char* line, int line_count, void* ptr_data)
return -1;
}
int plugin_initialize(struct hub_config* config, struct uhub_plugins* handle)
int plugin_initialize(struct hub_config* config, struct hub_info* hub)
{
int ret;
handle->loaded = list_create();
if (!handle->loaded)
hub->plugins->loaded = list_create();
if (!hub->plugins->loaded)
return -1;
if (config)
@ -185,7 +205,7 @@ int plugin_initialize(struct hub_config* config, struct uhub_plugins* handle)
if (!*config->file_plugins)
return 0;
ret = file_read_lines(config->file_plugins, handle, &plugin_parse_line);
ret = file_read_lines(config->file_plugins, hub, &plugin_parse_line);
if (ret == -1)
return -1;
}
@ -205,4 +225,11 @@ void plugin_shutdown(struct uhub_plugins* handle)
list_destroy(handle->loaded);
}
// Used internally only
struct hub_info* plugin_get_hub(struct plugin_handle* plugin)
{
struct plugin_hub_internals* data = get_internals(plugin);
return data->hub;
}
#endif /* PLUGIN_SUPPORT */

View File

@ -1,6 +1,6 @@
/*
* uhub - A tiny ADC p2p connection hub
* Copyright (C) 2007-2010, Jan Vidar Krey
* Copyright (C) 2007-2011, Jan Vidar Krey
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -23,13 +23,15 @@
#include "plugin_api/handle.h"
struct hub_config;
struct hub_info;
struct linked_list;
struct plugin_handle;
struct uhub_plugin
{
void* handle;
plugin_unregister_f unregister;
void* internals; // Hub internal stuff
void* callback_data; // Hub internal stuff
};
struct uhub_plugins
@ -38,11 +40,11 @@ struct uhub_plugins
};
// High level plugin loader ode
extern struct plugin_handle* plugin_load(const char* filename, const char* config);
extern struct plugin_handle* plugin_load(const char* filename, const char* config, struct hub_info* hub);
extern void plugin_unload(struct plugin_handle* plugin);
// extern void plugin_unload(struct plugin_handle*);
extern int plugin_initialize(struct hub_config* config, struct uhub_plugins* handle);
extern int plugin_initialize(struct hub_config* config, struct hub_info* hub);
extern void plugin_shutdown(struct uhub_plugins* handle);
// Low level plugin loader code (used internally)
@ -50,5 +52,8 @@ extern struct uhub_plugin* plugin_open(const char* filename);
extern void plugin_close(struct uhub_plugin*);
extern void* plugin_lookup_symbol(struct uhub_plugin*, const char* symbol);
// Used internally only
extern struct hub_info* plugin_get_hub(struct plugin_handle*);
#endif /* HAVE_UHUB_PLUGIN_LOADER_H */

View File

@ -95,7 +95,7 @@ struct plugin_funcs
struct plugin_command_handle;
typedef int (*hfunc_send_message)(struct plugin_handle*, struct plugin_user* user, const char* message);
typedef int (*hfunc_user_disconnect)(struct plugin_user* user);
typedef int (*hfunc_user_disconnect)(struct plugin_handle*, struct plugin_user* user);
typedef int (*hfunc_command_add)(struct plugin_handle*, struct plugin_command_handle*);
typedef int (*hfunc_command_del)(struct plugin_handle*, struct plugin_command_handle*);

View File

@ -92,6 +92,7 @@ extern "C" {
#include "core/commands.h"
#include "core/inf.h"
#include "core/hubevent.h"
#include "core/plugincallback.h"
#include "core/plugininvoke.h"
#include "core/pluginloader.h"