Changeset 81e420c in nscp for include/nscapi/macros.hpp


Ignore:
Timestamp:
09/01/11 07:05:41 (21 months ago)
Author:
Michael Medin <michael@…>
Branches:
master, 0.4.0, 0.4.1, 0.4.2
Children:
3b11e65
Parents:
1307e3f5
Message:
  • Added support for loading same plugin twice (in different sessions)
  • Added preliminary support for routing passive checks
File:
1 edited

Legend:

Unmodified
Added
Removed
  • include/nscapi/macros.hpp

    r4b1e6fe r81e420c  
    11#pragma once 
    22#include <unicode_char.hpp> 
     3#include <boost/shared_ptr.hpp> 
     4#include <map> 
    35 
    46////////////////////////////////////////////////////////////////////////// 
     
    79  extern "C" int NSModuleHelperInit(unsigned int id, nscapi::core_api::lpNSAPILoader f); \ 
    810  extern "C" int NSLoadModule(); \ 
    9   extern "C" int NSLoadModuleEx(wchar_t* alias, int mode); \ 
     11  extern "C" int NSLoadModuleEx(unsigned int plugin_id, wchar_t* alias, int mode); \ 
    1012  extern "C" void NSDeleteBuffer(char**buffer); \ 
    1113  extern "C" int NSGetModuleName(wchar_t* buf, int buflen); \ 
    1214  extern "C" int NSGetModuleDescription(wchar_t* buf, int buflen); \ 
    1315  extern "C" int NSGetModuleVersion(int *major, int *minor, int *revision); \ 
    14   extern "C" NSCAPI::boolReturn NSHasCommandHandler(); \ 
    15   extern "C" NSCAPI::boolReturn NSHasMessageHandler(); \ 
    16   extern "C" void NSHandleMessage(const char* data); \ 
    17   extern "C" NSCAPI::nagiosReturn NSHandleCommand(const wchar_t* command, const char* request_buffer, const unsigned int request_buffer_len, char** reply_buffer, unsigned int *reply_buffer_len); \ 
    18   extern "C" int NSUnloadModule(); 
     16  extern "C" NSCAPI::boolReturn NSHasCommandHandler(unsigned int plugin_id); \ 
     17  extern "C" NSCAPI::boolReturn NSHasMessageHandler(unsigned int plugin_id); \ 
     18  extern "C" void NSHandleMessage(unsigned int plugin_id, const char* data, unsigned int len); \ 
     19  extern "C" NSCAPI::nagiosReturn NSHandleCommand(unsigned int plugin_id, const wchar_t* command, const char* request_buffer, const unsigned int request_buffer_len, char** reply_buffer, unsigned int *reply_buffer_len); \ 
     20  extern "C" int NSUnloadModule(unsigned int plugin_id); 
    1921 
    2022 
    2123#define NSC_WRAPPERS_CLI() \ 
    22   extern "C" int NSCommandLineExec(wchar_t *command, char *request_buffer, unsigned int request_len, char **response_buffer, unsigned int *response_len); 
     24  extern "C" int NSCommandLineExec(unsigned int plugin_id, wchar_t *command, char *request_buffer, unsigned int request_len, char **response_buffer, unsigned int *response_len); 
    2325 
    2426#define NSC_WRAPPERS_CHANNELS() \ 
    25   extern "C" int NSHasNotificationHandler(); \ 
    26   extern "C" int NSHandleNotification(const wchar_t*, const wchar_t*, NSCAPI::nagiosReturn, const char*, unsigned int); 
     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); 
     29 
     30#define NSC_WRAPPERS_ROUTING() \ 
     31  extern "C" int NSHasRoutingHandler(unsigned int plugin_id); \ 
     32  extern "C" int NSRouteMessage(unsigned int plugin_id, const wchar_t*, const wchar_t*, NSCAPI::nagiosReturn, const char*, unsigned int); 
    2733 
    2834////////////////////////////////////////////////////////////////////////// 
     
    7076#endif 
    7177 
    72 #define NSC_WRAPPERS_MAIN_DEF(toObject) \ 
     78template<class impl_type> 
     79struct 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    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 
     96#define NSC_WRAPPERS_MAIN_DEF(impl_class) \ 
     97  static plugin_instance_data<impl_class> plugin_instance; \ 
    7398  extern int NSModuleHelperInit(unsigned int id, nscapi::core_api::lpNSAPILoader f) { \ 
    7499    try { \ 
     
    79104    } \ 
    80105  } \ 
    81   extern int NSLoadModuleEx(wchar_t* alias, int mode) { \ 
    82   try { \ 
    83   return GET_PLUGIN()->wrapLoadModule(toObject.loadModuleEx(alias, mode)); \ 
     106  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)); \ 
    84109    } catch (...) { \ 
    85110    NSC_LOG_CRITICAL(_T("Unknown exception in: wrapLoadModule(...)")); \ 
     
    88113  } \ 
    89114  extern int NSLoadModule() { \ 
    90   try { \ 
    91   return GET_PLUGIN()->wrapLoadModule(toObject.loadModule()); \ 
    92     } catch (...) { \ 
    93     NSC_LOG_CRITICAL(_T("Unknown exception in: wrapLoadModule(...)")); \ 
    94115    return NSCAPI::hasFailed; \ 
    95     } \ 
    96116  } \ 
    97117  extern int NSGetModuleName(wchar_t* buf, int buflen) { \ 
    98118    try { \ 
    99       return GET_PLUGIN()->wrapGetModuleName(buf, buflen, toObject.getModuleName()); \ 
     119      return GET_PLUGIN()->wrapGetModuleName(buf, buflen, impl_class::getModuleName()); \ 
    100120    } catch (...) { \ 
    101121      NSC_LOG_CRITICAL(_T("Unknown exception in: wrapGetModuleName(...)")); \ 
     
    105125  extern int NSGetModuleDescription(wchar_t* buf, int buflen) { \ 
    106126    try { \ 
    107       return GET_PLUGIN()->wrapGetModuleName(buf, buflen, toObject.getModuleDescription()); \ 
     127      return GET_PLUGIN()->wrapGetModuleName(buf, buflen, impl_class::getModuleDescription()); \ 
    108128    } catch (...) { \ 
    109129      NSC_LOG_CRITICAL(_T("Unknown exception in: wrapGetModuleName(...)")); \ 
     
    113133  extern int NSGetModuleVersion(int *major, int *minor, int *revision) { \ 
    114134    try { \ 
    115       return GET_PLUGIN()->wrapGetModuleVersion(major, minor, revision, toObject.getModuleVersion()); \ 
     135      return GET_PLUGIN()->wrapGetModuleVersion(major, minor, revision, impl_class::getModuleVersion()); \ 
    116136    } catch (...) { \ 
    117137      NSC_LOG_CRITICAL(_T("Unknown exception in: wrapGetModuleVersion(...)")); \ 
     
    119139    } \ 
    120140  } \ 
    121   extern int NSUnloadModule() { \ 
    122     try { \ 
    123       return GET_PLUGIN()->wrapUnloadModule(toObject.unloadModule()); \ 
     141  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; \ 
    124146    } catch (...) { \ 
    125147      NSC_LOG_CRITICAL(_T("Unknown exception in: wrapGetModuleVersion(...)")); \ 
     
    135157  } 
    136158#define NSC_WRAPPERS_HANDLE_MSG_DEF(toObject) \ 
    137   extern void NSHandleMessage(const char* data) { \ 
    138     try { \ 
    139       toObject.handleMessageRAW(data); \ 
     159  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); \ 
    140163    } catch (...) { \ 
    141164      NSC_LOG_CRITICAL(_T("Unknown exception in: handleMessage(...)")); \ 
    142165    } \ 
    143166  } \ 
    144   extern NSCAPI::boolReturn NSHasMessageHandler() { \ 
    145     try { \ 
    146       return GET_PLUGIN()->wrapHasMessageHandler(toObject.hasMessageHandler()); \ 
     167  extern NSCAPI::boolReturn NSHasMessageHandler(unsigned int id) { \ 
     168    try { \ 
     169      return GET_PLUGIN()->wrapHasMessageHandler(plugin_instance.get(id)->hasMessageHandler()); \ 
    147170    } catch (...) { \ 
    148171      NSC_LOG_CRITICAL(_T("Unknown exception in: wrapHasMessageHandler(...)")); \ 
     
    151174  } 
    152175#define NSC_WRAPPERS_IGNORE_MSG_DEF() \ 
    153   extern void NSHandleMessage(const char* data) {} \ 
    154   extern NSCAPI::boolReturn NSHasMessageHandler() { return NSCAPI::isfalse; } 
    155 #define NSC_WRAPPERS_HANDLE_CMD_DEF(toObject) \ 
    156   extern NSCAPI::nagiosReturn NSHandleCommand(const wchar_t* command, const char* request_buffer, const unsigned int request_buffer_len, char** reply_buffer, unsigned int *reply_buffer_len) \ 
     176  extern void NSHandleMessage(unsigned int id, const char* data, unsigned int len) {} \ 
     177  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) \ 
    157180  { \ 
    158181  try { \ 
    159182  std::string request(request_buffer, request_buffer_len), reply; \ 
    160   NSCAPI::nagiosReturn retCode = (&toObject)->handleRAWCommand(command, request, reply); \ 
     183  NSCAPI::nagiosReturn retCode = plugin_instance.get(id)->handleRAWCommand(command, request, reply); \ 
    161184  return GET_PLUGIN()->wrapHandleCommand(retCode, reply, reply_buffer, reply_buffer_len); \ 
    162185    } catch (...) { \ 
     
    165188    } \ 
    166189  } \ 
    167   extern NSCAPI::boolReturn NSHasCommandHandler() { \ 
    168   try { \ 
    169   return GET_PLUGIN()->wrapHasCommandHandler(toObject.hasCommandHandler()); \ 
     190  extern NSCAPI::boolReturn NSHasCommandHandler(unsigned int id) { \ 
     191  try { \ 
     192  return GET_PLUGIN()->wrapHasCommandHandler(plugin_instance.get(id)->hasCommandHandler()); \ 
    170193    } catch (...) { \ 
    171194    NSC_LOG_CRITICAL(_T("Unknown exception in: wrapHasCommandHandler(...)")); \ 
     
    173196    } \ 
    174197  } 
     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  } 
    175217#define NSC_WRAPPERS_IGNORE_CMD_DEF() \ 
    176   extern NSCAPI::nagiosReturn NSHandleCommand(const wchar_t* IN_cmd, const unsigned int IN_argsLen, wchar_t **IN_args, \ 
     218  extern NSCAPI::nagiosReturn NSHandleCommand(unsigned int id, const wchar_t* IN_cmd, const unsigned int IN_argsLen, wchar_t **IN_args, \ 
    177219  wchar_t *OUT_retBufMessage, unsigned int IN_retBufMessageLen, wchar_t *OUT_retBufPerf, unsigned int IN_retBufPerfLen) { \ 
    178220  return NSCAPI::returnIgnored; \ 
    179221  } \ 
    180   extern NSCAPI::boolReturn NSHasCommandHandler() { return NSCAPI::isfalse; } 
     222  extern NSCAPI::boolReturn NSHasCommandHandler(unsigned int id) { return NSCAPI::isfalse; } 
    181223#define NSC_WRAPPERS_IGNORE_NOTIFICATION_DEF() \ 
    182224  extern void NSHandleNotification(const wchar_t*, const wchar_t*, NSCAPI::nagiosReturn, const char*, unsigned int) {} \ 
    183   extern NSCAPI::boolReturn NSHasNotificationHandler() { return NSCAPI::isfalse; } 
    184 #define NSC_WRAPPERS_HANDLE_NOTIFICATION_DEF(toObject) \ 
    185   extern NSCAPI::nagiosReturn NSHandleNotification(const wchar_t* channel, const wchar_t* command, NSCAPI::nagiosReturn code, const char* result_buffer, unsigned int result_buffer_len) \ 
     225  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) \ 
    186228  { \ 
    187229    try { \ 
    188230      std::string result(result_buffer, result_buffer_len); \ 
    189       return GET_PLUGIN()->wrapHandleNotification((&toObject)->handleRAWNotification(channel, command, code, result)); \ 
     231      return GET_PLUGIN()->wrapHandleNotification(plugin_instance.get(id)->handleRAWNotification(channel, command, code, result)); \ 
    190232    } catch (...) { \ 
    191233      NSC_LOG_CRITICAL(_T("Unknown exception in: wrapHasNotificationHandler(...)")); \ 
     
    193235    } \ 
    194236  } \ 
    195   extern NSCAPI::boolReturn NSHasNotificationHandler() { \ 
    196     try { \ 
    197       return GET_PLUGIN()->wrapHasNotificationHandler(toObject.hasNotificationHandler()); \ 
     237  extern NSCAPI::boolReturn NSHasNotificationHandler(unsigned int id) { \ 
     238    try { \ 
     239      return GET_PLUGIN()->wrapHasNotificationHandler(plugin_instance.get(id)->hasNotificationHandler()); \ 
    198240    } catch (...) { \ 
    199241      NSC_LOG_CRITICAL(_T("Unknown exception in: wrapHasNotificationHandler(...)")); \ 
     
    203245 
    204246 
    205 #define NSC_WRAPPERS_CLI_DEF(toObject) \ 
    206   extern int NSCommandLineExec(wchar_t *command, char *request_buffer, unsigned int request_len, char **response_buffer, unsigned int *response_len) { \ 
     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) { \ 
    207249    try { \ 
    208250    std::string request = std::string(request_buffer, request_len); \ 
    209251    std::string response; \ 
    210     NSCAPI::nagiosReturn retCode = (&toObject)->commandRAWLineExec(command, request, response); \ 
     252    NSCAPI::nagiosReturn retCode = plugin_instance.get(id)->commandRAWLineExec(command, request, response); \ 
    211253    return GET_PLUGIN()->wrapCommandLineExec(retCode, response, response_buffer, response_len); \ 
    212254    } catch (const std::exception &e) { \ 
     
    220262    } \ 
    221263  } \ 
    222  
    223264 
    224265 
Note: See TracChangeset for help on using the changeset viewer.