Changeset a44cb15 in nscp for include/nscapi/macros.hpp
- Timestamp:
- 09/02/11 07:38:44 (22 months ago)
- Branches:
- master, 0.4.0, 0.4.1, 0.4.2
- Children:
- a14aa07
- Parents:
- 3b11e65
- File:
-
- 1 edited
-
include/nscapi/macros.hpp (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
include/nscapi/macros.hpp
r3b11e65 ra44cb15 2 2 #include <unicode_char.hpp> 3 3 #include <boost/shared_ptr.hpp> 4 #include < map>4 #include <NSCAPI.h> 5 5 6 6 ////////////////////////////////////////////////////////////////////////// … … 26 26 #define NSC_WRAPPERS_CHANNELS() \ 27 27 extern "C" int NSHasNotificationHandler(unsigned int plugin_id); \ 28 extern "C" int NSHandleNotification(unsigned int plugin_id, const wchar_t*, const wchar_t*, NSCAPI::nagiosReturn,const char*, unsigned int);28 extern "C" int NSHandleNotification(unsigned int plugin_id, const wchar_t*, const wchar_t*, const char*, unsigned int); 29 29 30 30 #define NSC_WRAPPERS_ROUTING() \ … … 76 76 #endif 77 77 78 template<class impl_type>79 struct plugin_instance_data {80 typedef std::map<unsigned int, boost::shared_ptr<impl_type> > plugin_list_type;81 plugin_list_type plugins;82 boost::shared_ptr<impl_type> get(unsigned int id) {83 typename plugin_list_type::iterator it = plugins.find(id);84 if (it != plugins.end())85 return it->second;86 boost::shared_ptr<impl_type> impl = boost::shared_ptr<impl_type>(new impl_type());87 plugins[id] = impl;88 return impl;89 }90 void erase(unsigned int id) {91 plugins.erase(id);92 }93 };94 95 78 96 79 #define NSC_WRAPPERS_MAIN_DEF(impl_class) \ 97 static plugin_instance_data<impl_class> plugin_instance; \ 98 extern int NSModuleHelperInit(unsigned int id, nscapi::core_api::lpNSAPILoader f) { \ 99 try { \ 100 return GET_PLUGIN()->wrapModuleHelperInit(id, f); \ 101 } catch (...) { \ 102 NSC_LOG_CRITICAL(_T("Unknown exception in: wrapModuleHelperInit(...)")); \ 103 return NSCAPI::hasFailed; \ 104 } \ 105 } \ 80 typedef impl_class plugin_impl_class; \ 81 static nscapi::plugin_instance_data<plugin_impl_class> plugin_instance; \ 82 extern int NSModuleHelperInit(unsigned int id, nscapi::core_api::lpNSAPILoader f) { return nscapi::basic_wrapper_static<plugin_impl_class>::NSModuleHelperInit(f); } \ 106 83 extern int NSLoadModuleEx(unsigned int id, wchar_t* alias, int mode) { \ 107 try { \ 108 return GET_PLUGIN()->wrapLoadModule(plugin_instance.get(id)->loadModuleEx(alias, mode)); \ 109 } catch (...) { \ 110 NSC_LOG_CRITICAL(_T("Unknown exception in: wrapLoadModule(...)")); \ 111 return NSCAPI::hasFailed; \ 112 } \ 113 } \ 114 extern int NSLoadModule() { \ 115 return NSCAPI::hasFailed; \ 116 } \ 117 extern int NSGetModuleName(wchar_t* buf, int buflen) { \ 118 try { \ 119 return GET_PLUGIN()->wrapGetModuleName(buf, buflen, impl_class::getModuleName()); \ 120 } catch (...) { \ 121 NSC_LOG_CRITICAL(_T("Unknown exception in: wrapGetModuleName(...)")); \ 122 return NSCAPI::hasFailed; \ 123 } \ 124 } \ 125 extern int NSGetModuleDescription(wchar_t* buf, int buflen) { \ 126 try { \ 127 return GET_PLUGIN()->wrapGetModuleName(buf, buflen, impl_class::getModuleDescription()); \ 128 } catch (...) { \ 129 NSC_LOG_CRITICAL(_T("Unknown exception in: wrapGetModuleName(...)")); \ 130 return NSCAPI::hasFailed; \ 131 } \ 132 } \ 133 extern int NSGetModuleVersion(int *major, int *minor, int *revision) { \ 134 try { \ 135 return GET_PLUGIN()->wrapGetModuleVersion(major, minor, revision, impl_class::getModuleVersion()); \ 136 } catch (...) { \ 137 NSC_LOG_CRITICAL(_T("Unknown exception in: wrapGetModuleVersion(...)")); \ 138 return NSCAPI::hasFailed; \ 139 } \ 140 } \ 84 nscapi::basic_wrapper<plugin_impl_class> wrapper(plugin_instance.get(id)); \ 85 return wrapper.NSLoadModuleEx(id, alias, mode); } \ 86 extern int NSLoadModule() { return nscapi::basic_wrapper_static<plugin_impl_class>::NSLoadModule(); } \ 87 extern int NSGetModuleName(wchar_t* buf, int buflen) { return nscapi::basic_wrapper_static<plugin_impl_class>::NSGetModuleName(buf, buflen); } \ 88 extern int NSGetModuleDescription(wchar_t* buf, int buflen) { return nscapi::basic_wrapper_static<plugin_impl_class>::NSGetModuleDescription(buf, buflen); } \ 89 extern int NSGetModuleVersion(int *major, int *minor, int *revision) { return nscapi::basic_wrapper_static<plugin_impl_class>::NSGetModuleVersion(major, minor, revision); } \ 141 90 extern int NSUnloadModule(unsigned int id) { \ 142 try { \ 143 int ret = GET_PLUGIN()->wrapUnloadModule(plugin_instance.get(id)->unloadModule()); \ 144 plugin_instance.erase(id); \ 145 return ret; \ 146 } catch (...) { \ 147 NSC_LOG_CRITICAL(_T("Unknown exception in: wrapGetModuleVersion(...)")); \ 148 return NSCAPI::hasFailed; \ 149 } \ 150 } \ 151 extern void NSDeleteBuffer(char**buffer) { \ 152 try { \ 153 GET_PLUGIN()->wrapDeleteBuffer(buffer); \ 154 } catch (...) { \ 155 NSC_LOG_CRITICAL(_T("Unknown exception in: wrapModuleHelperInit(...)")); \ 156 } \ 157 } 91 nscapi::basic_wrapper<plugin_impl_class> wrapper(plugin_instance.get(id)); \ 92 return wrapper.NSUnloadModule(); } \ 93 extern void NSDeleteBuffer(char**buffer) { nscapi::basic_wrapper_static<plugin_impl_class>::NSDeleteBuffer(buffer); } 94 158 95 #define NSC_WRAPPERS_HANDLE_MSG_DEF(toObject) \ 159 96 extern void NSHandleMessage(unsigned int id, const char* request_buffer, unsigned int request_buffer_len) { \ 160 try { \ 161 std::string request(request_buffer, request_buffer_len); \ 162 plugin_instance.get(id)->handleMessageRAW(request); \ 163 } catch (...) { \ 164 NSC_LOG_CRITICAL(_T("Unknown exception in: handleMessage(...)")); \ 165 } \ 166 } \ 97 nscapi::message_wrapper<plugin_impl_class> wrapper(plugin_instance.get(id)); \ 98 return wrapper.NSHandleMessage(request_buffer, request_buffer_len); } \ 167 99 extern NSCAPI::boolReturn NSHasMessageHandler(unsigned int id) { \ 168 try { \ 169 return GET_PLUGIN()->wrapHasMessageHandler(plugin_instance.get(id)->hasMessageHandler()); \ 170 } catch (...) { \ 171 NSC_LOG_CRITICAL(_T("Unknown exception in: wrapHasMessageHandler(...)")); \ 172 return NSCAPI::isfalse; \ 173 } \ 174 } 100 nscapi::message_wrapper<plugin_impl_class> wrapper(plugin_instance.get(id)); \ 101 return wrapper.NSHasMessageHandler(); } 102 103 #define NSC_WRAPPERS_HANDLE_CMD_DEF() \ 104 extern NSCAPI::nagiosReturn NSHandleCommand(unsigned int id, const wchar_t* command, const char* request_buffer, const unsigned int request_buffer_len, char** reply_buffer, unsigned int *reply_buffer_len) { \ 105 nscapi::command_wrapper<plugin_impl_class> wrapper(plugin_instance.get(id)); \ 106 return wrapper.NSHandleCommand(command, request_buffer, request_buffer_len, reply_buffer, reply_buffer_len); } \ 107 extern NSCAPI::boolReturn NSHasCommandHandler(unsigned int id) { \ 108 nscapi::command_wrapper<plugin_impl_class> wrapper(plugin_instance.get(id)); \ 109 return wrapper.NSHasCommandHandler(); } 110 111 #define NSC_WRAPPERS_ROUTING_DEF() \ 112 extern NSCAPI::nagiosReturn NSRouteMessage(unsigned int id, const wchar_t* channel, const wchar_t* command, const char* request_buffer, const unsigned int request_buffer_len) { \ 113 nscapi::routing_wrapper<plugin_impl_class> wrapper(plugin_instance.get(id)); \ 114 return wrapper.NSRouteMessage(channel, command, request_buffer, request_buffer_len); } \ 115 extern NSCAPI::boolReturn NSHasRoutingHandler(unsigned int id) { \ 116 nscapi::routing_wrapper<plugin_impl_class> wrapper(plugin_instance.get(id)); \ 117 return wrapper.NSHasRoutingHandler(); } 118 119 #define NSC_WRAPPERS_HANDLE_NOTIFICATION_DEF() \ 120 extern int NSHandleNotification(unsigned int id, const wchar_t* channel, const wchar_t* command, const char* result_buffer, unsigned int result_buffer_len) { \ 121 nscapi::submission_wrapper<plugin_impl_class> wrapper(plugin_instance.get(id)); \ 122 return wrapper.NSHandleNotification(channel, command, result_buffer, result_buffer_len); } \ 123 extern NSCAPI::boolReturn NSHasNotificationHandler(unsigned int id) { \ 124 nscapi::submission_wrapper<plugin_impl_class> wrapper(plugin_instance.get(id)); \ 125 return wrapper.NSHasNotificationHandler(); } 126 127 #define NSC_WRAPPERS_CLI_DEF() \ 128 extern int NSCommandLineExec(unsigned int id, wchar_t *command, char *request_buffer, unsigned int request_len, char **response_buffer, unsigned int *response_len) { \ 129 nscapi::cliexec_wrapper<plugin_impl_class> wrapper(plugin_instance.get(id)); \ 130 return wrapper.NSCommandLineExec(command, request_buffer, request_len, response_buffer, response_len); } 131 175 132 #define NSC_WRAPPERS_IGNORE_MSG_DEF() \ 176 133 extern void NSHandleMessage(unsigned int id, const char* data, unsigned int len) {} \ 177 134 extern NSCAPI::boolReturn NSHasMessageHandler(unsigned int id) { return NSCAPI::isfalse; } 178 #define NSC_WRAPPERS_HANDLE_CMD_DEF() \ 179 extern NSCAPI::nagiosReturn NSHandleCommand(unsigned int id, const wchar_t* command, const char* request_buffer, const unsigned int request_buffer_len, char** reply_buffer, unsigned int *reply_buffer_len) \ 180 { \ 181 try { \ 182 std::string request(request_buffer, request_buffer_len), reply; \ 183 NSCAPI::nagiosReturn retCode = plugin_instance.get(id)->handleRAWCommand(command, request, reply); \ 184 return GET_PLUGIN()->wrapHandleCommand(retCode, reply, reply_buffer, reply_buffer_len); \ 185 } catch (...) { \ 186 NSC_LOG_CRITICAL(_T("Unknown exception in: wrapHandleCommand(...)")); \ 187 return NSCAPI::returnIgnored; \ 188 } \ 189 } \ 190 extern NSCAPI::boolReturn NSHasCommandHandler(unsigned int id) { \ 191 try { \ 192 return GET_PLUGIN()->wrapHasCommandHandler(plugin_instance.get(id)->hasCommandHandler()); \ 193 } catch (...) { \ 194 NSC_LOG_CRITICAL(_T("Unknown exception in: wrapHasCommandHandler(...)")); \ 195 return NSCAPI::isfalse; \ 196 } \ 197 } 198 #define NSC_WRAPPERS_ROUTING_DEF() \ 199 extern NSCAPI::nagiosReturn NSRouteMessage(unsigned int id, const wchar_t* channel, const wchar_t* command, const char* request_buffer, const unsigned int request_buffer_len) \ 200 { \ 201 try { \ 202 std::string request(request_buffer, request_buffer_len), reply; \ 203 return GET_PLUGIN()->wrapRouteMessage(plugin_instance.get(id)->RAWRouteMessage(channel, command, request)); \ 204 } catch (...) { \ 205 NSC_LOG_CRITICAL(_T("Unknown exception in: wrapHandleCommand(...)")); \ 206 return NSCAPI::returnIgnored; \ 207 } \ 208 } \ 209 extern NSCAPI::boolReturn NSHasRoutingHandler(unsigned int id) { \ 210 try { \ 211 return GET_PLUGIN()->wrapHasRoutingHandler(plugin_instance.get(id)->hasRoutingHandler()); \ 212 } catch (...) { \ 213 NSC_LOG_CRITICAL(_T("Unknown exception in: wrapHasCommandHandler(...)")); \ 214 return NSCAPI::isfalse; \ 215 } \ 216 } 135 217 136 #define NSC_WRAPPERS_IGNORE_CMD_DEF() \ 218 extern NSCAPI::nagiosReturn NSHandleCommand(unsigned int id, const wchar_t* IN_cmd, const unsigned int IN_argsLen, wchar_t **IN_args, \ 219 wchar_t *OUT_retBufMessage, unsigned int IN_retBufMessageLen, wchar_t *OUT_retBufPerf, unsigned int IN_retBufPerfLen) { \ 220 return NSCAPI::returnIgnored; \ 221 } \ 137 extern NSCAPI::nagiosReturn NSHandleCommand(unsigned int id, const wchar_t* IN_cmd, const unsigned int IN_argsLen, wchar_t **IN_args, wchar_t *OUT_retBufMessage, unsigned int IN_retBufMessageLen, wchar_t *OUT_retBufPerf, unsigned int IN_retBufPerfLen) { return NSCAPI::returnIgnored; } \ 222 138 extern NSCAPI::boolReturn NSHasCommandHandler(unsigned int id) { return NSCAPI::isfalse; } 139 223 140 #define NSC_WRAPPERS_IGNORE_NOTIFICATION_DEF() \ 224 extern void NSHandleNotification(const wchar_t*, const wchar_t*, NSCAPI::nagiosReturn, const char*, unsigned int) {} \141 extern int NSHandleNotification(unsigned int id, const wchar_t* channel, const wchar_t* command, const char* result_buffer, unsigned int result_buffer_len) {} \ 225 142 extern NSCAPI::boolReturn NSHasNotificationHandler(unsigned int id) { return NSCAPI::isfalse; } 226 #define NSC_WRAPPERS_HANDLE_NOTIFICATION_DEF() \227 extern NSCAPI::nagiosReturn NSHandleNotification(unsigned int id, const wchar_t* channel, const wchar_t* command, NSCAPI::nagiosReturn code, const char* result_buffer, unsigned int result_buffer_len) \228 { \229 try { \230 std::string result(result_buffer, result_buffer_len); \231 return GET_PLUGIN()->wrapHandleNotification(plugin_instance.get(id)->handleRAWNotification(channel, command, code, result)); \232 } catch (...) { \233 NSC_LOG_CRITICAL(_T("Unknown exception in: wrapHasNotificationHandler(...)")); \234 return NSCAPI::returnIgnored; \235 } \236 } \237 extern NSCAPI::boolReturn NSHasNotificationHandler(unsigned int id) { \238 try { \239 return GET_PLUGIN()->wrapHasNotificationHandler(plugin_instance.get(id)->hasNotificationHandler()); \240 } catch (...) { \241 NSC_LOG_CRITICAL(_T("Unknown exception in: wrapHasNotificationHandler(...)")); \242 return NSCAPI::isfalse; \243 } \244 }245 246 247 #define NSC_WRAPPERS_CLI_DEF() \248 extern int NSCommandLineExec(unsigned int id, wchar_t *command, char *request_buffer, unsigned int request_len, char **response_buffer, unsigned int *response_len) { \249 try { \250 std::string request = std::string(request_buffer, request_len); \251 std::string response; \252 NSCAPI::nagiosReturn retCode = plugin_instance.get(id)->commandRAWLineExec(command, request, response); \253 return GET_PLUGIN()->wrapCommandLineExec(retCode, response, response_buffer, response_len); \254 } catch (const std::exception &e) { \255 NSC_LOG_CRITICAL(_T("Exception in: commandLineExec(...)") + utf8::cvt<std::wstring>(e.what())); \256 std::wcerr << _T("Exception in: commandLineExec(...)") << utf8::cvt<std::wstring>(e.what()) << std::endl; \257 return NSCAPI::hasFailed; \258 } catch (...) { \259 NSC_LOG_CRITICAL(_T("Unknown exception in: commandLineExec(...)")); \260 std::wcerr << _T("Unknown exception in: commandLineExec(...)") << std::endl; \261 return NSCAPI::hasFailed; \262 } \263 } \264 143 265 144
Note: See TracChangeset
for help on using the changeset viewer.








