/[projects]/smsdaemon/PluginManager.cpp
ViewVC logotype

Annotation of /smsdaemon/PluginManager.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 208 - (hide annotations) (download)
Sun Dec 21 18:41:08 2008 UTC (15 years, 5 months ago) by torben
File size: 3546 byte(s)
Enable dynamic reload of plugins/tasks when recieving a HUP signal

1 torben 26 #include "PluginManager.h"
2    
3 torben 157 #include "Logger.h"
4 torben 205 #include "ConfigFile.h"
5     #include "Common.h"
6     #include "Util.h"
7 torben 26
8 torben 132 #include "plugins/EchoPlugin.h"
9     #include "plugins/SpamPlugin.h"
10     #include "plugins/ShellExecPlugin.h"
11     #include "plugins/TogPlugin.h"
12     #include "plugins/StatusPlugin.h"
13     #include "plugins/ListPlugin.h"
14 torben 137 #include "plugins/HostStatusPlugin.h"
15 torben 188 #include "plugins/WeatherPlugin.h"
16 torben 193 #include "plugins/UrlTriggerPlugin.h"
17 torben 26
18     typedef std::map<std::string, Plugin*>::iterator MapIterator;
19    
20     PluginManager::PluginManager()
21     {
22     }
23 torben 196
24 torben 26 PluginManager::~PluginManager()
25     {
26     }
27    
28    
29 torben 28 void PluginManager::AddPlugin(Plugin* plugin)
30     {
31 torben 35 if (plugin != 0)
32     {
33 torben 184 if (plugin->IsHelper())
34     {
35     _helper_plugins.push_back(plugin);
36     }
37     else
38     {
39     std::string command = plugin->GetCommand();
40 torben 35
41 torben 184 if ( _plugins[ command ] == 0)
42     _plugins[ command ] = plugin;
43     else
44     Logger::logMessage( std::string("AddPlugin() -- already have a plugin called ") + command);
45     }
46 torben 35 }
47     else
48     {
49 torben 157 Logger::logMessage("AddPlugin() -- cannot register a null pointer");
50 torben 35 }
51 torben 28 }
52    
53 torben 205 Plugin* PluginManager::CreatePlugin(const std::string& pluginName, const std::map<std::string,std::string>& args)
54     {
55     if (pluginName == "echo")
56     return new EchoPlugin();
57    
58     if (pluginName == "spam")
59     return new SpamPlugin();
60    
61     if (pluginName == "tog")
62     return new TogPlugin();
63    
64     if (pluginName == "status")
65     return new StatusPlugin();
66    
67     if (pluginName == "list")
68     return new ListPlugin();
69    
70     if (pluginName == "hoststatus")
71     return new HostStatusPlugin();
72    
73     if (pluginName == "weather")
74     return new WeatherPlugin();
75    
76     if (pluginName == "shellexec")
77     return new ShellExecPlugin(args);
78    
79     if (pluginName == "urltrigger")
80     return new UrlTriggerPlugin(args);
81    
82     return 0;
83     }
84    
85 torben 208
86     void PluginManager::DestroyPlugins()
87     {
88     for (std::map<std::string,Plugin*>::iterator it=_plugins.begin(); it!=_plugins.end(); ++it)
89     {
90     delete it->second;
91     }
92     _plugins.clear();
93     }
94    
95 torben 26 void PluginManager::LoadPlugins()
96     {
97 torben 207 Logger::logMessage("-------- PluginList --------");
98 torben 205 std::vector<Value> pluginlist = Common::instance()->GetConfigfile()->GetValues("smsdaemon", "plugin");
99 torben 188
100 torben 205 for (unsigned i=0; i<pluginlist.size(); i++)
101     {
102     std::string current = pluginlist[i];
103    
104     std::string name;
105     unsigned pos = current.find(' ');
106     std::map<std::string,std::string> args;
107 torben 193
108 torben 205 std::string argstr;
109    
110     if (pos == std::string::npos)
111     {
112     name = current;
113     }
114     else
115     {
116     name = Util::str_trim(current.substr(0,pos));
117     argstr = Util::str_trim(current.substr(pos+1,1024));
118     args = ConfigHelper::ParseArguments(argstr);
119     }
120    
121     Plugin* pl = 0;
122     try
123     {
124     pl = CreatePlugin(name, args );
125     }
126     catch (std::exception& e)
127     {
128     Logger::logMessage(std::string("Failed to load plugin ") + name + " with args: " + argstr);
129     Logger::logMessage(std::string("Reason: ") + e.what());
130     continue;
131     }
132    
133     if (pl)
134     AddPlugin(pl);
135     else
136     Logger::logMessage( std::string("Unknown plugin: ")+name);
137    
138     }
139    
140    
141 torben 196 for (MapIterator it = _plugins.begin(); it != _plugins.end(); ++it)
142 torben 26 {
143     Plugin* pl = (*it).second;
144 torben 35 if (pl != 0)
145 torben 157 Logger::logMessage( std::string("Loaded plugin \"") + pl->GetCommand() + "\" - " + pl->GetDescription() );
146 torben 26 }
147     }
148    
149 torben 205
150 torben 26 Plugin* PluginManager::GetPlugin(const std::string& pluginname)
151     {
152     return _plugins[ pluginname ];
153     }
154    
155    
156     std::vector<Plugin*> PluginManager::GetPluginList()
157     {
158     typedef std::map<std::string, Plugin*>::iterator MapIterator;
159     std::vector<Plugin*> plugin_list;
160    
161     for (MapIterator it = _plugins.begin(); it != _plugins.end(); ++it)
162     {
163     Plugin* pl = (*it).second;
164     plugin_list.push_back(pl);
165     }
166    
167    
168     return plugin_list;
169     }

  ViewVC Help
Powered by ViewVC 1.1.20