source: nscp/service/commands.hpp @ b3078b4

0.4.00.4.10.4.2
Last change on this file since b3078b4 was b3078b4, checked in by Michael Medin <michael@…>, 3 years ago

added some build stuff

  • Property mode set to 100644
File size: 4.2 KB
Line 
1#pragma once
2#include <boost/shared_ptr.hpp>
3#include <boost/foreach.hpp>
4
5#include "NSCPlugin.h"
6#include "logger.hpp"
7
8namespace nsclient {
9        class commands : boost::noncopyable {
10        public:
11                class command_exception : public std::exception {
12                public:
13                        command_exception(std::wstring error) : std::exception(to_string(error).c_str()) {}
14                        command_exception(std::string error) : std::exception(error.c_str()) {}
15
16                };
17
18        private:
19                typedef boost::shared_ptr<NSCPlugin> plugin_type;
20                typedef std::map<unsigned long,plugin_type> plugin_list_type;
21                typedef std::map<std::wstring,std::wstring> description_list_type;
22                typedef std::map<std::wstring,plugin_type> command_list_type;
23
24
25                nsclient::logger *logger_;
26                plugin_list_type plugins_;
27                description_list_type descriptions_;
28                command_list_type commands_;
29                boost::shared_mutex mutex_;
30
31        public:
32
33                commands(nsclient::logger *logger) : logger_(logger) {}
34
35                void add_plugin(plugin_type plugin) {
36                        if (!plugin || !plugin->hasCommandHandler())
37                                return;
38                        plugins_[plugin->get_id()] = plugin;
39                       
40                }
41
42                void remove_all() {
43                        boost::unique_lock<boost::shared_mutex> writeLock(mutex_, boost::get_system_time() + boost::posix_time::seconds(30));
44                        if (!writeLock.owns_lock()) {
45                                log_error(__FILEW__, __LINE__, _T("Failed to get mutex: commands::remove_all"));
46                                return;
47                        }
48                        descriptions_.clear();
49                        commands_.clear();
50                        plugins_.clear();
51                }
52
53                void remove_plugin(unsigned long id) {
54                        boost::unique_lock<boost::shared_mutex> writeLock(mutex_, boost::get_system_time() + boost::posix_time::seconds(10));
55                        if (!writeLock.owns_lock()) {
56                                log_error(__FILEW__, __LINE__, _T("Failed to get mutex in remove_plugin for plugin id: ") + to_wstring(id));
57                                return;
58                        }
59                        command_list_type::iterator it = commands_.begin();
60                        while (it != commands_.end()) {
61                                if ((*it).second->get_id() == id) {
62                                        command_list_type::iterator toerase = it;
63                                        ++it;
64                                        commands_.erase(toerase);
65                                        description_list_type::iterator dit = descriptions_.find((*toerase).first);
66                                        if (dit != descriptions_.end())
67                                                descriptions_.erase(dit);
68                                } else
69                                        ++it;
70                        }
71                        plugin_list_type::iterator pit = plugins_.find(id);
72                        if (pit != plugins_.end())
73                                plugins_.erase(pit);
74                }
75
76                void register_command(unsigned long plugin_id, std::wstring cmd, std::wstring desc) {
77                        boost::unique_lock<boost::shared_mutex> writeLock(mutex_, boost::get_system_time() + boost::posix_time::seconds(10));
78                        if (!writeLock.owns_lock()) {
79                                log_error(__FILEW__, __LINE__, _T("Failed to get mutex: ") + cmd);
80                                return;
81                        }
82                        descriptions_[cmd] = desc;
83                        commands_[cmd] = plugins_[plugin_id];
84                }
85
86                std::wstring describe(std::wstring command) {
87                        boost::shared_lock<boost::shared_mutex> readLock(mutex_, boost::get_system_time() + boost::posix_time::seconds(5));
88                        if (!readLock.owns_lock()) {
89                                log_error(__FILEW__, __LINE__, _T("Failed to get mutex: ") + command);
90                                return _T("error: ") + command;
91                        }
92                        description_list_type::const_iterator cit = descriptions_.find(command);
93                        if (cit == descriptions_.end())
94                                return _T("Command not found: ") + command;
95                        return (*cit).second;
96                }
97
98                std::list<std::wstring> list() {
99                        std::list<std::wstring> lst;
100                        boost::shared_lock<boost::shared_mutex> readLock(mutex_, boost::get_system_time() + boost::posix_time::seconds(5));
101                        if (!readLock.owns_lock()) {
102                                log_error(__FILEW__, __LINE__, _T("Failed to get mutex"));
103                                return lst;
104                        }
105                        std::pair<std::wstring,std::wstring> cit;
106                        BOOST_FOREACH(cit, descriptions_) {
107                                lst.push_back(cit.first);
108                        }
109                        return lst;
110                }
111
112                plugin_type get(std::wstring command) {
113                        boost::shared_lock<boost::shared_mutex> readLock(mutex_, boost::get_system_time() + boost::posix_time::seconds(5));
114                        if (!readLock.owns_lock()) {
115                                log_error(__FILEW__, __LINE__, _T("Failed to get mutex: ") + command);
116                                throw command_exception("Failed to get mutext (commands::get)");
117                        }
118                        commands_[command];
119                }
120
121
122        private:
123                void log_error(std::wstring file, int line, std::wstring error) {
124                        if (logger_ != NULL)
125                                logger_->nsclient_log_error(file, line, error);
126                }
127        };
128}
Note: See TracBrowser for help on using the repository browser.