winix/core/plugin.cpp

420 lines
7.1 KiB
C++
Executable File

/*
* This file is a part of Winix
* and is not publicly distributed
*
* Copyright (c) 2008-2010, Tomasz Sowa
* All rights reserved.
*
*/
#include <dlfcn.h>
#include <string.h>
#include "plugin.h"
#include "pluginmsg.h"
#include "misc.h"
void Plugin::UnloadPlugins()
{
size_t i;
slots.clear();
for(i=0 ; i<plugins.size() ; ++i)
dlclose(plugins[i].handle);
plugins.clear();
}
Plugin::Plugin()
{
current_plugin = -1;
db = 0;
config = 0;
request = 0;
system = 0;
functions = 0;
templates = 0;
synchro = 0;
session_manager = 0;
ret_false = 0;
ret_true = 0;
}
Plugin::~Plugin()
{
UnloadPlugins();
}
void Plugin::SetDb(Db * pdb)
{
db = pdb;
}
void Plugin::SetConfig(Config * pconfig)
{
config = pconfig;
}
void Plugin::SetRequest(Request * prequest)
{
request = prequest;
}
void Plugin::SetSystem(System * psystem)
{
system = psystem;
}
void Plugin::SetFunctions(Functions * pfunctions)
{
functions = pfunctions;
}
void Plugin::SetTemplates(Templates * ptemplates)
{
templates = ptemplates;
}
void Plugin::SetSynchro(Synchro * psynchro)
{
synchro = psynchro;
}
void Plugin::SetSessionManager(SessionManager * psession_manager)
{
session_manager = psession_manager;
}
bool Plugin::SetPointers(PluginInfo & info)
{
// for safety we call a plugin function only when all our pointers are not null
bool res = (db && config && request && system && functions && templates && synchro && session_manager);
if( !res )
log << log1 << "Plugin: cannot call a function - some of the winix pointers are null" << logend;
info.db = db;
info.config = config;
info.request = request;
info.system = system;
info.functions = functions;
info.templates = templates;
info.synchro = synchro;
info.session_manager = session_manager;
return res;
}
void Plugin::LoadPlugins(const std::wstring & plugins_dir, const std::vector<std::wstring> & plugins)
{
for(size_t i=0 ; i<plugins.size() ; ++i)
{
if( !plugins[i].empty() && plugins[i][0] == '/' )
{
LoadPlugin(plugins[i]);
}
else
{
temp_path = plugins_dir;
temp_path += '/';
temp_path += plugins[i];
LoadPlugin(temp_path);
}
}
}
void Plugin::LoadPlugin(const std::string & filename)
{
LoadPlugin(filename.c_str());
}
void * Plugin::LoadInitFun(const char * filename, Fun1 & fun_init)
{
void * p = dlopen(filename, RTLD_NOW | RTLD_LOCAL);
if( !p )
{
log << log1 << "Plugin: cannot load a plugin: \"" << filename << "\"" << logend;
log << log1 << "Plugin: dlerror: " << dlerror() << logend;
return 0;
}
fun_init = (Fun1)dlfunc(p, "Init");
if( !fun_init )
{
log << log1 << "Plugin: cannot load a plugin: " << filename
<< " (there is no Init() function)" << logend;
dlclose(p);
return 0;
}
log << log2 << "Plugin: plugin loaded"
<< ", file: " << filename
<< ", index: " << plugins.size() << logend;
return p;
}
void Plugin::LoadPlugin(const char * filename)
{
Fun1 fun_init;
void * plugin_handle;
int old_current_plugin;
if( !SetPointers(info) )
return;
if( !(plugin_handle = LoadInitFun(filename, fun_init)) )
return;
info.Clear();
old_current_plugin = current_plugin;
current_plugin = (int)plugins.size();
info.plugin_id = current_plugin;
fun_init(info);
PluginsItem item;
item.handle = plugin_handle;
item.plugin_name = reinterpret_cast<const wchar_t *>(info.p1);
plugins.push_back(item);
current_plugin = old_current_plugin;
}
void Plugin::LoadPlugin(const wchar_t * filename)
{
AssignString(filename, afilename);
LoadPlugin(afilename.c_str());
}
void Plugin::LoadPlugin(const std::wstring & filename)
{
LoadPlugin(filename.c_str());
}
bool Plugin::HasPlugin(const wchar_t * name)
{
if( *name == 0 )
return false;
for(size_t i=0 ; i<plugins.size() ; ++i)
{
if( plugins[i].plugin_name && Equal(plugins[i].plugin_name, name) )
return true;
}
return false;
}
bool Plugin::HasPlugin(const std::wstring & name)
{
return HasPlugin(name.c_str());
}
void Plugin::Call(int message, Slots::iterator & slot)
{
if( !SetPointers(info) )
return;
current_plugin = slot->second.index;
info.plugin_id = current_plugin;
if( request && request->session && current_plugin != -1 )
info.plugin_data_base = request->session->plugin_data.Get(current_plugin);
else
info.plugin_data_base = 0;
if( !slot->second.is_running )
{
if( config->log_plugin_call )
log << log1 << "Plugin: calling plugin id: " << slot->second.index << ", message: " << message << logend;
slot->second.is_running = true;
if( slot->second.fun1 )
slot->second.fun1(info);
if( slot->second.fun2 )
slot->second.fun2();
slot->second.is_running = false;
if( config->log_plugin_call )
log << log1 << "Plugin: returning from plugin id: " << slot->second.index << ", message: " << message << logend;
}
else
{
log << log1 << "Plugin: id: " << slot->second.index
<< ", message: " << message
<< ", recurrences are not allowed" << logend;
}
}
void Plugin::Call(int message, void * p1_, void * p2_, long l1_, long l2_)
{
// how many plugins return 'false' and 'true'
// we are using local variables because Call() method can be called
// from a plugin too (one Call() can execute another Call())
int ret_false_loc = 0;
int ret_true_loc = 0;
int old_current_plugin = current_plugin;
Slots::iterator i = slots.lower_bound(message);
for( ; i!=slots.end() && i->first==message ; ++i )
{
info.Clear();
info.p1 = p1_;
info.p2 = p2_;
info.l1 = l1_;
info.l2 = l2_;
Call(message, i);
if( info.res )
++ret_true_loc;
else
++ret_false_loc;
}
current_plugin = old_current_plugin;
ret_false = ret_false_loc;
ret_true = ret_true_loc;
}
void Plugin::Call(int message)
{
Call(message, 0, 0, 0, 0);
}
void Plugin::Call(int message, void * p1_)
{
Call(message, p1_, 0, 0, 0);
}
void Plugin::Call(int message, void * p1_, void * p2_)
{
Call(message, p1_, p2_, 0, 0);
}
void Plugin::Call(int message, long l1_)
{
Call(message, 0, 0, l1_, 0);
}
void Plugin::Call(int message, long l1_, long l2_)
{
Call(message, 0, 0, l1_, l2_);
}
void Plugin::Call(int message, void * p1_, long l1_)
{
Call(message, p1_, 0, l1_, 0);
}
void Plugin::Call(int message, void * p1_, long l1_, long l2_)
{
Call(message, p1_, 0, l1_, l2_);
}
void Plugin::Call(int message, void * p1_, void * p2_, long l1_)
{
Call(message, p1_, p2_, l1_, 0);
}
size_t Plugin::Size()
{
return plugins.size();
}
int Plugin::True()
{
return ret_true;
}
int Plugin::False()
{
return ret_false;
}
void Plugin::Assign(int message, Fun1 fun1)
{
Slot s;
if( current_plugin == -1 )
return;
s.fun1 = fun1;
s.index = current_plugin;
slots.insert( std::make_pair(message, s) );
log << log3 << "Plugin: added function for message: " << message << ", plugin index: " << s.index << logend;
}
void Plugin::Assign(int message, Fun2 fun2)
{
Slot s;
if( current_plugin == -1 )
return;
s.fun2 = fun2;
s.index = current_plugin;
slots.insert( std::make_pair(message, s) );
log << log3 << "Plugin: added function for message: " << message << ", plugin index: " << s.index << logend;
}