2  *    This program is free software; you can redistribute it and/or modify
 
   3  *    it under the terms of the GNU General Public License as published by
 
   4  *    the Free Software Foundation; either version 2 of the License, or
 
   5  *    (at your option) any later version.
 
   7  *    This program is distributed in the hope that it will be useful,
 
   8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
   9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  10  *    GNU General Public License for more details.
 
  12  *    You should have received a copy of the GNU General Public License
 
  13  *    along with this program; if not, write to the Free Software
 
  14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
  18  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
 
  19  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
 
  22 #include "plugin_runner.h"
 
  24 #include "stg/common.h"
 
  29 //-----------------------------------------------------------------------------
 
  30 PLUGIN_RUNNER::PLUGIN_RUNNER(const std::string & fileName,
 
  31                              const MODULE_SETTINGS & ms,
 
  36                              CORPORATIONS & corporations,
 
  37                              TRAFFCOUNTER & traffcounter,
 
  39                              const SETTINGS & settings)
 
  40     : pluginFileName(fileName),
 
  42       m_plugin(Load(ms, admins, tariffs, users, services, corporations,
 
  43                     traffcounter, store, settings))
 
  46 //-----------------------------------------------------------------------------
 
  47 PLUGIN_RUNNER::~PLUGIN_RUNNER()
 
  49 if (dlclose(libHandle))
 
  51     errorStr = "Failed to unload plugin '" + pluginFileName + "': " + dlerror();
 
  52     printfd(__FILE__, "PLUGIN_RUNNER::Unload() - %s", errorStr.c_str());
 
  55 //-----------------------------------------------------------------------------
 
  56 int PLUGIN_RUNNER::Start()
 
  58 int res = m_plugin.Start();
 
  59 errorStr = m_plugin.GetStrError();
 
  62 //-----------------------------------------------------------------------------
 
  63 int PLUGIN_RUNNER::Stop()
 
  65 int res = m_plugin.Stop();
 
  66 errorStr = m_plugin.GetStrError();
 
  69 //-----------------------------------------------------------------------------
 
  70 int PLUGIN_RUNNER::Reload()
 
  72 int res = m_plugin.Reload();
 
  73 errorStr = m_plugin.GetStrError();
 
  76 //-----------------------------------------------------------------------------
 
  77 PLUGIN & PLUGIN_RUNNER::Load(const MODULE_SETTINGS & ms,
 
  82                              CORPORATIONS & corporations,
 
  83                              TRAFFCOUNTER & traffcounter,
 
  85                              const SETTINGS & settings)
 
  87 if (pluginFileName.empty())
 
  89     const std::string msg = "Empty plugin file name.";
 
  90     printfd(__FILE__, "PLUGIN_RUNNER::Load() - %s\n", msg.c_str());
 
  94 if (access(pluginFileName.c_str(), R_OK))
 
  96     const std::string msg = "Plugin file '" + pluginFileName + "' is missing or inaccessible.";
 
  97     printfd(__FILE__, "PLUGIN_RUNNER::Load() - %s\n", msg.c_str());
 
 101 libHandle = dlopen(pluginFileName.c_str(), RTLD_NOW);
 
 105     std::string msg = "Error loading plugin '" + pluginFileName + "'";
 
 106     const char* error = dlerror();
 
 108         msg = msg + ": '" + error + "'";
 
 109     printfd(__FILE__, "PLUGIN_RUNNER::Load() - %s\n", msg.c_str());
 
 113 PLUGIN * (*GetPlugin)();
 
 114 GetPlugin = (PLUGIN * (*)())dlsym(libHandle, "GetPlugin");
 
 117     const std::string msg = "Plugin '" + pluginFileName + "' does not have GetPlugin() function. ";
 
 118     printfd(__FILE__, "PLUGIN_RUNNER::Load() - %s\n", msg.c_str());
 
 121 PLUGIN * plugin = GetPlugin();
 
 125     const std::string msg = "Failed to create an instance of plugin '" + pluginFileName + "'.";
 
 126     printfd(__FILE__, "PLUGIN_RUNNER::Load() - %s\n", msg.c_str());
 
 130 plugin->SetSettings(ms);
 
 131 plugin->SetTariffs(&tariffs);
 
 132 plugin->SetAdmins(&admins);
 
 133 plugin->SetUsers(&users);
 
 134 plugin->SetServices(&services);
 
 135 plugin->SetCorporations(&corporations);
 
 136 plugin->SetTraffcounter(&traffcounter);
 
 137 plugin->SetStore(&store);
 
 138 plugin->SetStgSettings(&settings);
 
 140 if (plugin->ParseSettings())
 
 142     const std::string msg = "Plugin '" + pluginFileName + "' is unable to parse settings. " + plugin->GetStrError();
 
 143     printfd(__FILE__, "PLUGIN_RUNNER::Load() - %s\n", msg.c_str());