529 lines
9.9 KiB
C++
529 lines
9.9 KiB
C++
/*
|
|
* This file is a part of Winix
|
|
* and is distributed under the 2-Clause BSD licence.
|
|
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
|
*/
|
|
|
|
/*
|
|
* Copyright (c) 2008-2014, Tomasz Sowa
|
|
* All rights reserved.
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions are met:
|
|
*
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
* this list of conditions and the following disclaimer.
|
|
*
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
* documentation and/or other materials provided with the distribution.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
*
|
|
*/
|
|
|
|
#include <dlfcn.h>
|
|
#include <string.h>
|
|
#include "plugin.h"
|
|
#include "pluginmsg.h"
|
|
#include "misc.h"
|
|
|
|
|
|
namespace Winix
|
|
{
|
|
|
|
|
|
|
|
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;
|
|
cur = 0;
|
|
system = 0;
|
|
functions = 0;
|
|
templates = 0;
|
|
synchro = 0;
|
|
session_manager = 0;
|
|
}
|
|
|
|
|
|
Plugin::~Plugin()
|
|
{
|
|
UnloadPlugins();
|
|
}
|
|
|
|
|
|
void Plugin::SetDb(Db * pdb)
|
|
{
|
|
db = pdb;
|
|
}
|
|
|
|
void Plugin::SetConfig(Config * pconfig)
|
|
{
|
|
config = pconfig;
|
|
}
|
|
|
|
|
|
void Plugin::SetCur(Cur * pcur)
|
|
{
|
|
cur = pcur;
|
|
}
|
|
|
|
|
|
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;
|
|
}
|
|
|
|
|
|
|
|
void Plugin::Lock()
|
|
{
|
|
if( synchro )
|
|
synchro->Lock();
|
|
}
|
|
|
|
|
|
void Plugin::Unlock()
|
|
{
|
|
if( synchro )
|
|
synchro->Unlock();
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Plugin::SetPointers(PluginInfo & info)
|
|
{
|
|
// for safety we call a plugin function only when all our pointers are not null
|
|
bool res = (db && config && cur && system && functions && templates && synchro && session_manager);
|
|
|
|
if( !res )
|
|
{
|
|
Lock();
|
|
log << log1 << "Plugin: cannot call a function - some of the winix pointers are null" << logend;
|
|
Unlock();
|
|
}
|
|
|
|
info.db = db;
|
|
info.config = config;
|
|
info.cur = cur;
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
// we don't have to use Lock() here because plugins are read
|
|
// before threads are started
|
|
void * Plugin::LoadInitFun(const wchar_t * filename, Fun1 & fun_init)
|
|
{
|
|
char file[WINIX_OS_PATH_SIZE];
|
|
|
|
if( !WideToUTF8(filename, file, WINIX_OS_PATH_SIZE) )
|
|
return 0;
|
|
|
|
void * p = dlopen(file, 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)dlsym(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 wchar_t * filename)
|
|
{
|
|
Fun1 fun_init;
|
|
void * plugin_handle;
|
|
int old_current_plugin;
|
|
PluginInfo info;
|
|
|
|
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 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());
|
|
}
|
|
|
|
|
|
bool Plugin::HasMessage(int message)
|
|
{
|
|
return (slots.find(message) != slots.end());
|
|
}
|
|
|
|
|
|
void Plugin::Call(Session * ses, int message, Slots::iterator & slot, PluginInfo & info)
|
|
{
|
|
if( !SetPointers(info) )
|
|
return;
|
|
|
|
current_plugin = slot->second.index;
|
|
info.plugin_id = current_plugin;
|
|
info.session = ses;
|
|
|
|
if( current_plugin != -1 && ses )
|
|
info.plugin_data_base = ses->plugin_data.Get(current_plugin);
|
|
else
|
|
info.plugin_data_base = 0;
|
|
|
|
if( !slot->second.is_running )
|
|
{
|
|
if( config->log_plugin_call )
|
|
{
|
|
Lock();
|
|
log << log1 << "Plugin: calling plugin id: " << slot->second.index << ", message: " << message << logend;
|
|
Unlock();
|
|
}
|
|
|
|
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 )
|
|
{
|
|
Lock();
|
|
log << log1 << "Plugin: returning from plugin id: " << slot->second.index << ", message: " << message
|
|
<< ", result: " << (info.res? "true" : "false") << logend;
|
|
Unlock();
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
Lock();
|
|
log << log1 << "Plugin: id: " << slot->second.index
|
|
<< ", message: " << message
|
|
<< ", recurrences are not allowed" << logend;
|
|
Unlock();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
PluginRes Plugin::Call(Session * ses, int message, void * p1_, void * p2_, long l1_, long l2_)
|
|
{
|
|
PluginRes res;
|
|
int old_current_plugin = current_plugin;
|
|
PluginInfo info;
|
|
|
|
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(ses, message, i, info);
|
|
|
|
if( info.res )
|
|
++res.res_true;
|
|
else
|
|
++res.res_false;
|
|
}
|
|
|
|
current_plugin = old_current_plugin;
|
|
|
|
return res;
|
|
}
|
|
|
|
|
|
PluginRes Plugin::Call(int message)
|
|
{
|
|
return Call(cur->session, message, 0, 0, 0, 0);
|
|
}
|
|
|
|
|
|
PluginRes Plugin::Call(int message, void * p1_)
|
|
{
|
|
return Call(cur->session, message, p1_, 0, 0, 0);
|
|
}
|
|
|
|
|
|
PluginRes Plugin::Call(int message, void * p1_, void * p2_)
|
|
{
|
|
return Call(cur->session, message, p1_, p2_, 0, 0);
|
|
}
|
|
|
|
|
|
PluginRes Plugin::Call(int message, long l1_)
|
|
{
|
|
return Call(cur->session, message, 0, 0, l1_, 0);
|
|
}
|
|
|
|
|
|
PluginRes Plugin::Call(int message, long l1_, long l2_)
|
|
{
|
|
return Call(cur->session, message, 0, 0, l1_, l2_);
|
|
}
|
|
|
|
|
|
PluginRes Plugin::Call(int message, void * p1_, long l1_)
|
|
{
|
|
return Call(cur->session, message, p1_, 0, l1_, 0);
|
|
}
|
|
|
|
|
|
PluginRes Plugin::Call(int message, void * p1_, long l1_, long l2_)
|
|
{
|
|
return Call(cur->session, message, p1_, 0, l1_, l2_);
|
|
}
|
|
|
|
|
|
PluginRes Plugin::Call(int message, void * p1_, void * p2_, long l1_)
|
|
{
|
|
return Call(cur->session, message, p1_, p2_, l1_, 0);
|
|
}
|
|
|
|
|
|
|
|
PluginRes Plugin::Call(Session * ses, int message)
|
|
{
|
|
return Call(ses, message, 0, 0, 0, 0);
|
|
}
|
|
|
|
|
|
PluginRes Plugin::Call(Session * ses, int message, void * p1_)
|
|
{
|
|
return Call(ses, message, p1_, 0, 0, 0);
|
|
}
|
|
|
|
|
|
PluginRes Plugin::Call(Session * ses, int message, void * p1_, void * p2_)
|
|
{
|
|
return Call(ses, message, p1_, p2_, 0, 0);
|
|
}
|
|
|
|
|
|
PluginRes Plugin::Call(Session * ses, int message, long l1_)
|
|
{
|
|
return Call(ses, message, 0, 0, l1_, 0);
|
|
}
|
|
|
|
|
|
PluginRes Plugin::Call(Session * ses, int message, long l1_, long l2_)
|
|
{
|
|
return Call(ses, message, 0, 0, l1_, l2_);
|
|
}
|
|
|
|
|
|
PluginRes Plugin::Call(Session * ses, int message, void * p1_, long l1_)
|
|
{
|
|
return Call(ses, message, p1_, 0, l1_, 0);
|
|
}
|
|
|
|
|
|
PluginRes Plugin::Call(Session * ses, int message, void * p1_, long l1_, long l2_)
|
|
{
|
|
return Call(ses, message, p1_, 0, l1_, l2_);
|
|
}
|
|
|
|
|
|
PluginRes Plugin::Call(Session * ses, int message, void * p1_, void * p2_, long l1_)
|
|
{
|
|
return Call(ses, message, p1_, p2_, l1_, 0);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t Plugin::Size()
|
|
{
|
|
return plugins.size();
|
|
}
|
|
|
|
|
|
/*
|
|
!! IMPROVE ME
|
|
Assign() can work only if other threads are not started
|
|
we can add some barrier/flag so when other threads starts
|
|
then we cannot use Assign() method
|
|
*/
|
|
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;
|
|
}
|
|
|
|
|
|
const Plugin::Plugins * Plugin::GetPlugins()
|
|
{
|
|
return &plugins;
|
|
}
|
|
|
|
|
|
} // namespace Winix
|
|
|