#include <dlfcn.h>
#include <unistd.h>
-#include <signal.h>
+#include "stg/common.h"
+#include "stg/traffcounter.h"
#include "plugin_runner.h"
-#include "common.h"
-#include "conffiles.h"
+#include "settings_impl.h"
+#include "admins_impl.h"
+#include "tariffs_impl.h"
+#include "users_impl.h"
+#include "services_impl.h"
+#include "corps_impl.h"
//-----------------------------------------------------------------------------
PLUGIN_RUNNER::PLUGIN_RUNNER(const std::string & pFileName,
const MODULE_SETTINGS & ms,
- ADMINS * a,
- TARIFFS * t,
- USERS * u,
+ ADMINS_IMPL * a,
+ TARIFFS_IMPL * t,
+ USERS_IMPL * u,
+ SERVICES_IMPL * svc,
+ CORPORATIONS_IMPL * crp,
TRAFFCOUNTER * tc,
STORE * st,
- const SETTINGS * s)
+ const SETTINGS_IMPL * s)
: pluginFileName(pFileName),
pluginSettingFileName(),
plugin(NULL),
admins(a),
tariffs(t),
users(u),
+ services(svc),
+ corps(crp),
store(st),
traffCnt(tc),
stgSettings(s),
admins(rvalue.admins),
tariffs(rvalue.tariffs),
users(rvalue.users),
+ services(rvalue.services),
+ corps(rvalue.corps),
store(rvalue.store),
traffCnt(rvalue.traffCnt),
stgSettings(rvalue.stgSettings),
admins = rvalue.admins;
tariffs = rvalue.tariffs;
users = rvalue.users;
+services = rvalue.services;
+corps = rvalue.corps;
store = rvalue.store;
traffCnt = rvalue.traffCnt;
stgSettings = rvalue.stgSettings;
Unload();
}
-isPluginLoaded = 0;
+isPluginLoaded = false;
}
//-----------------------------------------------------------------------------
PLUGIN * PLUGIN_RUNNER::GetPlugin()
{
+if (!isPluginLoaded)
+ {
+ errorStr = "Plugin '" + pluginFileName + "' is not loaded yet!";
+ printfd(__FILE__, "PLUGIN_RUNNER::GetPlugin() - %s\n", errorStr.c_str());
+ return NULL;
+ }
+
return plugin;
}
//-----------------------------------------------------------------------------
if (Load())
return -1;
+if (!plugin)
+ {
+ errorStr = "Plugin '" + pluginFileName + "' was not created!";
+ printfd(__FILE__, "PLUGIN_RUNNER::Start() - %s\n", errorStr.c_str());
+ return -1;
+ }
+
plugin->SetTariffs(tariffs);
plugin->SetAdmins(admins);
plugin->SetUsers(users);
+plugin->SetServices(services);
+plugin->SetCorporations(corps);
plugin->SetTraffcounter(traffCnt);
plugin->SetStore(store);
plugin->SetStgSettings(stgSettings);
errorStr = plugin->GetStrError();
return -1;
}
+
return 0;
}
//-----------------------------------------------------------------------------
int PLUGIN_RUNNER::Stop()
{
-plugin->Stop();
+if (!isPluginLoaded)
+ {
+ errorStr = "Plugin '" + pluginFileName + "' was not loaded yet!";
+ printfd(__FILE__, "PLUGIN_RUNNER::Stop() - %s\n", errorStr.c_str());
+ return -1;
+ }
-//if (Unload())
-// return -1;
-return 0;
+if (!plugin)
+ {
+ errorStr = "Plugin '" + pluginFileName + "' was not created!";
+ printfd(__FILE__, "PLUGIN_RUNNER::Stop() - %s\n", errorStr.c_str());
+ return -1;
+ }
+
+return plugin->Stop();
}
//-----------------------------------------------------------------------------
int PLUGIN_RUNNER::Reload()
{
+if (!isPluginLoaded)
+ {
+ errorStr = "Plugin '" + pluginFileName + "' was not loaded yet!";
+ printfd(__FILE__, "PLUGIN_RUNNER::Reload() - %s\n", errorStr.c_str());
+ return -1;
+ }
+
+if (!plugin)
+ {
+ errorStr = "Plugin '" + pluginFileName + "' was not created!";
+ printfd(__FILE__, "PLUGIN_RUNNER::Reload() - %s\n", errorStr.c_str());
+ return -1;
+ }
+
int res = plugin->Reload();
errorStr = plugin->GetStrError();
return res;
bool PLUGIN_RUNNER::IsRunning()
{
if (!isPluginLoaded)
+ {
+ errorStr = "Plugin '" + pluginFileName + "' was not loaded yet!";
+ printfd(__FILE__, "PLUGIN_RUNNER::IsRunning() - %s\n", errorStr.c_str());
return false;
+ }
+
+if (!plugin)
+ {
+ errorStr = "Plugin '" + pluginFileName + "' was not created!";
+ printfd(__FILE__, "PLUGIN_RUNNER::IsRunning() - %s\n", errorStr.c_str());
+ return false;
+ }
+
return plugin->IsRunning();
}
//-----------------------------------------------------------------------------
int PLUGIN_RUNNER::Load()
{
-if (!pluginFileName.size())
+if (isPluginLoaded)
+ {
+ errorStr = "Plugin '" + pluginFileName + "' was already loaded!";
+ printfd(__FILE__, "PLUGIN_RUNNER::Load() - %s\n", errorStr.c_str());
+ return -1;
+ }
+
+if (pluginFileName.empty())
{
- errorStr = "Plugin loading failed. No plugin";
- printfd(__FILE__, "%s\n", errorStr.c_str());
+ errorStr = "Empty plugin file name!";
+ printfd(__FILE__, "PLUGIN_RUNNER::Load() - %s\n", errorStr.c_str());
return -1;
}
if (!libHandle)
{
- errorStr = std::string("Plugin loading failed. ") + dlerror();
- printfd(__FILE__, "%s\n", errorStr.c_str());
+ errorStr = "Error loading plugin '" + pluginFileName + "': '" + dlerror() + "'";
+ printfd(__FILE__, "PLUGIN_RUNNER::Load() - %s\n", errorStr.c_str());
return -1;
}
+isPluginLoaded = true;
+
PLUGIN * (*GetPlugin)();
GetPlugin = (PLUGIN * (*)())dlsym(libHandle, "GetPlugin");
if (!GetPlugin)
{
- errorStr = std::string("GetPlugin() not found. ") + dlerror();
+ errorStr = "Plugin '" + pluginFileName + "' does not have GetPlugin() function. " + dlerror();
+ printfd(__FILE__, "PLUGIN_RUNNER::Load() - %s\n", errorStr.c_str());
return -1;
}
plugin = GetPlugin();
-isPluginLoaded++;
if (!plugin)
{
- errorStr = "Plugin was not created!";
- printfd(__FILE__, "%s\n", errorStr.c_str());
+ errorStr = "Failed to create an instance of plugin '" + pluginFileName + "'.";
+ printfd(__FILE__, "PLUGIN_RUNNER::Load() - %s\n", errorStr.c_str());
return -1;
}
plugin->SetSettings(modSettings);
-printfd(__FILE__, "Plugin %s parsesettings\n", plugin->GetVersion().c_str());
if (plugin->ParseSettings())
{
- errorStr = "Plugin \'" + plugin->GetVersion() + "\' error: " + plugin->GetStrError();
+ errorStr = "Plugin '" + pluginFileName + "' is unable to parse settings. " + plugin->GetStrError();
+ printfd(__FILE__, "PLUGIN_RUNNER::Load() - %s\n", errorStr.c_str());
return -1;
}
{
if (dlclose(libHandle))
{
- errorStr = dlerror();
- printfd(__FILE__, "Error unloading plugin '%s': '%s'", pluginFileName.c_str(), dlerror());
+ errorStr = "Failed to unload plugin '" + pluginFileName + "': " + dlerror();
+ printfd(__FILE__, "PLUGIN_RUNNER::Unload() - %s", errorStr.c_str());
return -1;
}
- isPluginLoaded--;
+ plugin = NULL;
+ isPluginLoaded = false;
}
return 0;
}
//-----------------------------------------------------------------------------
-const std::string & PLUGIN_RUNNER::GetStrError() const
-{
-return errorStr;
-}
-//-----------------------------------------------------------------------------
-uint16_t PLUGIN_RUNNER::GetStartPosition() const
-{
-return plugin->GetStartPosition();
-}
-//-----------------------------------------------------------------------------
-uint16_t PLUGIN_RUNNER::GetStopPosition() const
-{
-return plugin->GetStopPosition();
-}
-//-----------------------------------------------------------------------------