]> git.stg.codes - stg.git/blob - projects/stargazer/plugin_mgr.cpp
Merge branch 'stg-2.409-radius'
[stg.git] / projects / stargazer / plugin_mgr.cpp
1 /*
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.
6  *
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.
11  *
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
15  */
16
17 /*
18  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
19  */
20
21 #include "plugin_mgr.h"
22
23 #include "plugin_runner.h"
24
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"
32
33 #include "stg/logger.h"
34
35 using STG::PluginManager;
36
37 namespace
38 {
39
40 bool StartModCmp(const PLUGIN_RUNNER * lhs, const PLUGIN_RUNNER * rhs)
41 {
42     return lhs->GetStartPosition() < rhs->GetStartPosition();
43 }
44
45 bool StopModCmp(const PLUGIN_RUNNER * lhs, const PLUGIN_RUNNER * rhs)
46 {
47     return lhs->GetStopPosition() > rhs->GetStopPosition();
48 }
49
50 } // namespace anonymous
51
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())
57 {
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++)
61     {
62         std::string moduleName = modSettings[i].moduleName;
63         std::string modulePath = basePath + "/mod_" + moduleName + ".so";
64         printfd(__FILE__, "Module: %s\n", modulePath.c_str());
65         try
66         {
67             m_modules.push_back(
68                 new PLUGIN_RUNNER(modulePath, moduleName, modSettings[i], admins, tariffs,
69                                   users, services, corporations, traffcounter,
70                                   store, settings)
71             );
72         }
73         catch (const PLUGIN_RUNNER::Error & ex)
74         {
75             m_log(ex.what());
76             printfd(__FILE__, "%s\n", ex.what());
77             // TODO: React
78         }
79     }
80     std::sort(m_modules.begin(), m_modules.end(), StartModCmp);
81     for (size_t i = 0; i < m_modules.size(); ++i)
82     {
83         PLUGIN & plugin = m_modules[i]->GetPlugin();
84         if (m_modules[i]->Start())
85         {
86             m_log("Failed to start module '%s': '%s'", plugin.GetVersion().c_str(),
87                                                        plugin.GetStrError().c_str());
88             printfd(__FILE__, "Failed to start module '%s': '%s'\n", plugin.GetVersion().c_str(),
89                                                                    plugin.GetStrError().c_str());
90         }
91         else
92         {
93             m_log("Module '%s' started successfully.", plugin.GetVersion().c_str());
94             printfd(__FILE__, "Module '%s' started successfully.\n", plugin.GetVersion().c_str());
95         }
96     }
97 }
98
99 PluginManager::~PluginManager()
100 {
101     stop();
102     for (size_t i = 0; i < m_modules.size(); ++i)
103         delete m_modules[i];
104 }
105
106 void PluginManager::reload(const SETTINGS_IMPL& settings)
107 {
108     const std::vector<MODULE_SETTINGS> & modSettings(settings.GetModulesSettings());
109     for (size_t i = 0; i < m_modules.size(); ++i)
110     {
111         for (size_t j = 0; j < modSettings.size(); j++)
112         {
113             if (modSettings[j].moduleName == m_modules[i]->GetName())
114             {
115                 PLUGIN & plugin = m_modules[i]->GetPlugin();
116                 if (m_modules[i]->Reload(modSettings[j]))
117                 {
118                     m_log("Error reloading module '%s': '%s'", plugin.GetVersion().c_str(),
119                                                                plugin.GetStrError().c_str());
120                     printfd(__FILE__, "Error reloading module '%s': '%s'\n", plugin.GetVersion().c_str(),
121                                                                              plugin.GetStrError().c_str());
122                 }
123                 break;
124             }
125         }
126     }
127 }
128
129 void PluginManager::stop()
130 {
131     std::sort(m_modules.begin(), m_modules.end(), StopModCmp);
132     for (size_t i = 0; i < m_modules.size(); ++i)
133     {
134         if (!m_modules[i]->IsRunning())
135             continue;
136         PLUGIN & plugin = m_modules[i]->GetPlugin();
137         if (m_modules[i]->Stop())
138         {
139             m_log("Failed to stop module '%s': '%s'", plugin.GetVersion().c_str(),
140                                                       plugin.GetStrError().c_str());
141             printfd(__FILE__, "Failed to stop module '%s': '%s'\n", plugin.GetVersion().c_str(),
142                                                                     plugin.GetStrError().c_str());
143         }
144         else
145         {
146             m_log("Module '%s' stopped successfully.", plugin.GetVersion().c_str());
147             printfd(__FILE__, "Module '%s' stopped successfully.\n", plugin.GetVersion().c_str());
148         }
149     }
150 }