]> git.stg.codes - stg.git/blob - stargazer/plugin_mgr.cpp
Public interfaces: part 1
[stg.git] / 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 using STG::PluginRunner;
37
38 namespace
39 {
40
41 bool StartModCmp(const PluginRunner * lhs, const PluginRunner * rhs)
42 {
43     return lhs->GetStartPosition() < rhs->GetStartPosition();
44 }
45
46 bool StopModCmp(const PluginRunner * lhs, const PluginRunner * rhs)
47 {
48     return lhs->GetStopPosition() > rhs->GetStopPosition();
49 }
50
51 } // namespace anonymous
52
53 PluginManager::PluginManager(const SettingsImpl& settings,
54                              Store& store, AdminsImpl& admins, TariffsImpl& tariffs,
55                              ServicesImpl& services, CorporationsImpl& corporations,
56                              UsersImpl& users, TraffCounterImpl& traffcounter)
57     : m_log(Logger::get())
58 {
59     std::string basePath = settings.GetModulesPath();
60     const std::vector<ModuleSettings> & modSettings(settings.GetModulesSettings());
61     for (size_t i = 0; i < modSettings.size(); i++)
62     {
63         std::string moduleName = modSettings[i].moduleName;
64         std::string modulePath = basePath + "/mod_" + moduleName + ".so";
65         printfd(__FILE__, "Module: %s\n", modulePath.c_str());
66         try
67         {
68             m_modules.push_back(
69                 new PluginRunner(modulePath, moduleName, modSettings[i], admins, tariffs,
70                                   users, services, corporations, traffcounter,
71                                   store, settings)
72             );
73         }
74         catch (const PluginRunner::Error & ex)
75         {
76             m_log(ex.what());
77             printfd(__FILE__, "%s\n", ex.what());
78             // TODO: React
79         }
80     }
81     std::sort(m_modules.begin(), m_modules.end(), StartModCmp);
82     for (size_t i = 0; i < m_modules.size(); ++i)
83     {
84         auto& plugin = m_modules[i]->GetPlugin();
85         if (m_modules[i]->Start())
86         {
87             m_log("Failed to start module '%s': '%s'", plugin.GetVersion().c_str(),
88                                                        plugin.GetStrError().c_str());
89             printfd(__FILE__, "Failed to start module '%s': '%s'\n", plugin.GetVersion().c_str(),
90                                                                    plugin.GetStrError().c_str());
91         }
92         else
93         {
94             m_log("Module '%s' started successfully.", plugin.GetVersion().c_str());
95             printfd(__FILE__, "Module '%s' started successfully.\n", plugin.GetVersion().c_str());
96         }
97     }
98 }
99
100 PluginManager::~PluginManager()
101 {
102     stop();
103     for (size_t i = 0; i < m_modules.size(); ++i)
104         delete m_modules[i];
105 }
106
107 void PluginManager::reload(const SettingsImpl& settings)
108 {
109     const std::vector<ModuleSettings> & modSettings(settings.GetModulesSettings());
110     for (size_t i = 0; i < m_modules.size(); ++i)
111     {
112         for (size_t j = 0; j < modSettings.size(); j++)
113         {
114             if (modSettings[j].moduleName == m_modules[i]->GetName())
115             {
116                 auto& plugin = m_modules[i]->GetPlugin();
117                 if (m_modules[i]->Reload(modSettings[j]))
118                 {
119                     m_log("Error reloading module '%s': '%s'", plugin.GetVersion().c_str(),
120                                                                plugin.GetStrError().c_str());
121                     printfd(__FILE__, "Error reloading module '%s': '%s'\n", plugin.GetVersion().c_str(),
122                                                                              plugin.GetStrError().c_str());
123                 }
124                 break;
125             }
126         }
127     }
128 }
129
130 void PluginManager::stop()
131 {
132     std::sort(m_modules.begin(), m_modules.end(), StopModCmp);
133     for (size_t i = 0; i < m_modules.size(); ++i)
134     {
135         if (!m_modules[i]->IsRunning())
136             continue;
137         auto& plugin = m_modules[i]->GetPlugin();
138         if (m_modules[i]->Stop())
139         {
140             m_log("Failed to stop module '%s': '%s'", plugin.GetVersion().c_str(),
141                                                       plugin.GetStrError().c_str());
142             printfd(__FILE__, "Failed to stop module '%s': '%s'\n", plugin.GetVersion().c_str(),
143                                                                     plugin.GetStrError().c_str());
144         }
145         else
146         {
147             m_log("Module '%s' stopped successfully.", plugin.GetVersion().c_str());
148             printfd(__FILE__, "Module '%s' stopped successfully.\n", plugin.GetVersion().c_str());
149         }
150     }
151 }