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 : Maxim Mamontov <faust@stargazer.dp.ua>
21 #include "plugin_mgr.h"
23 #include "plugin_runner.h"
25 #include "admins_impl.h"
26 #include "tariffs_impl.h"
27 #include "services_impl.h"
28 #include "corps_impl.h"
29 #include "users_impl.h"
30 #include "traffcounter_impl.h"
31 #include "settings_impl.h"
33 #include "stg/logger.h"
35 using STG::PluginManager;
40 bool StartModCmp(const PLUGIN_RUNNER * lhs, const PLUGIN_RUNNER * rhs)
42 return lhs->GetStartPosition() < rhs->GetStartPosition();
45 bool StopModCmp(const PLUGIN_RUNNER * lhs, const PLUGIN_RUNNER * rhs)
47 return lhs->GetStopPosition() > rhs->GetStopPosition();
50 } // namespace anonymous
52 PluginManager::PluginManager(const SETTINGS_IMPL& settings,
53 STORE& store, ADMINS_IMPL& admins, TARIFFS_IMPL& tariffs,
54 SERVICES_IMPL& services, CORPORATIONS_IMPL& corporations,
55 USERS_IMPL& users, TRAFFCOUNTER_IMPL& traffcounter)
56 : m_log(GetStgLogger())
58 std::string basePath = settings.GetModulesPath();
59 const std::vector<MODULE_SETTINGS> & modSettings(settings.GetModulesSettings());
60 for (size_t i = 0; i < modSettings.size(); i++)
62 std::string modulePath = basePath + "/mod_" + modSettings[i].moduleName + ".so";
63 printfd(__FILE__, "Module: %s\n", modulePath.c_str());
67 new PLUGIN_RUNNER(modulePath, modSettings[i], admins, tariffs,
68 users, services, corporations, traffcounter,
72 catch (const PLUGIN_RUNNER::Error & ex)
75 printfd(__FILE__, "%s\n", ex.what());
79 std::sort(m_modules.begin(), m_modules.end(), StartModCmp);
80 for (size_t i = 0; i < m_modules.size(); ++i)
82 PLUGIN & plugin = m_modules[i]->GetPlugin();
83 if (m_modules[i]->Start())
85 m_log("Failed to start module '%s': '%s'", plugin.GetVersion().c_str(),
86 plugin.GetStrError().c_str());
87 printfd(__FILE__, "Failed to start module '%s': '%s'", plugin.GetVersion().c_str(),
88 plugin.GetStrError().c_str());
93 PluginManager::~PluginManager()
95 std::sort(m_modules.begin(), m_modules.end(), StopModCmp);
96 for (size_t i = 0; i < m_modules.size(); ++i)
98 for (size_t i = 0; i < m_modules.size(); ++i)
102 void PluginManager::reload()
104 for (size_t i = 0; i < m_modules.size(); ++i)
106 PLUGIN & plugin = m_modules[i]->GetPlugin();
107 if (m_modules[i]->Reload())
109 m_log("Error reloading module '%s': '%s'", plugin.GetVersion().c_str(),
110 plugin.GetStrError().c_str());
111 printfd(__FILE__, "Error reloading module '%s': '%s'\n", plugin.GetVersion().c_str(),
112 plugin.GetStrError().c_str());