| 1 | #pragma once |
|---|
| 2 | |
|---|
| 3 | #include <string> |
|---|
| 4 | #include <list> |
|---|
| 5 | #include <NSCAPI.h> |
|---|
| 6 | |
|---|
| 7 | namespace NSCHelper |
|---|
| 8 | { |
|---|
| 9 | int wrapReturnString(char *buffer, unsigned int bufLen, std::string str, int defaultReturnCode = NSCAPI::success); |
|---|
| 10 | std::list<std::string> makelist(const unsigned int argLen, char **argument); |
|---|
| 11 | std::string translateMessageType(NSCAPI::messageTypes msgType); |
|---|
| 12 | std::string translateReturn(NSCAPI::returnCodes returnCode); |
|---|
| 13 | inline std::string returnNSCP(NSCAPI::returnCodes returnCode, std::string str) { |
|---|
| 14 | return translateReturn(returnCode) + "&" + str; |
|---|
| 15 | } |
|---|
| 16 | |
|---|
| 17 | /* |
|---|
| 18 | / * ************************************************************************** |
|---|
| 19 | * max_state(STATE_x, STATE_y) |
|---|
| 20 | * compares STATE_x to STATE_y and returns result based on the following |
|---|
| 21 | * STATE_UNKNOWN < STATE_OK < STATE_WARNING < STATE_CRITICAL |
|---|
| 22 | * |
|---|
| 23 | * Note that numerically the above does not hold |
|---|
| 24 | **************************************************************************** / |
|---|
| 25 | |
|---|
| 26 | int |
|---|
| 27 | max_state (int a, int b) |
|---|
| 28 | { |
|---|
| 29 | if (a == STATE_CRITICAL || b == STATE_CRITICAL) |
|---|
| 30 | return STATE_CRITICAL; |
|---|
| 31 | else if (a == STATE_WARNING || b == STATE_WARNING) |
|---|
| 32 | return STATE_WARNING; |
|---|
| 33 | else if (a == STATE_OK || b == STATE_OK) |
|---|
| 34 | return STATE_OK; |
|---|
| 35 | else if (a == STATE_UNKNOWN || b == STATE_UNKNOWN) |
|---|
| 36 | return STATE_UNKNOWN; |
|---|
| 37 | else if (a == STATE_DEPENDENT || b == STATE_DEPENDENT) |
|---|
| 38 | return STATE_DEPENDENT; |
|---|
| 39 | else |
|---|
| 40 | return max (a, b); |
|---|
| 41 | } |
|---|
| 42 | @bug Use this sceme instead!! |
|---|
| 43 | */ |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | inline void escalteReturnCode(NSCAPI::returnCodes ¤tReturnCode, NSCAPI::returnCodes newReturnCode) { |
|---|
| 47 | if (newReturnCode == NSCAPI::returnCRIT) |
|---|
| 48 | currentReturnCode = NSCAPI::returnCRIT; |
|---|
| 49 | else if ((newReturnCode == NSCAPI::returnWARN) && (currentReturnCode != NSCAPI::returnCRIT) ) |
|---|
| 50 | currentReturnCode = NSCAPI::returnWARN; |
|---|
| 51 | else if ((newReturnCode == NSCAPI::returnUNKNOWN) |
|---|
| 52 | && (currentReturnCode != NSCAPI::returnCRIT) |
|---|
| 53 | && (currentReturnCode != NSCAPI::returnWARN) ) |
|---|
| 54 | currentReturnCode = NSCAPI::returnUNKNOWN; |
|---|
| 55 | } |
|---|
| 56 | inline void escalteReturnCodeToCRIT(NSCAPI::returnCodes ¤tReturnCode) { |
|---|
| 57 | currentReturnCode = NSCAPI::returnCRIT; |
|---|
| 58 | } |
|---|
| 59 | inline void escalteReturnCodeToWARN(NSCAPI::returnCodes ¤tReturnCode) { |
|---|
| 60 | if (currentReturnCode != NSCAPI::returnCRIT) |
|---|
| 61 | currentReturnCode = NSCAPI::returnWARN; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | }; |
|---|
| 65 | |
|---|
| 66 | namespace NSCModuleHelper |
|---|
| 67 | { |
|---|
| 68 | // Types for the Callbacks into the main program |
|---|
| 69 | typedef int (*lpNSAPIGetBasePath)(char*,unsigned int); |
|---|
| 70 | typedef int (*lpNSAPIGetApplicationName)(char*,unsigned int); |
|---|
| 71 | typedef int (*lpNSAPIGetApplicationVersionStr)(char*,unsigned int); |
|---|
| 72 | typedef int (*lpNSAPIGetSettingsString)(const char*,const char*,const char*,char*,unsigned int); |
|---|
| 73 | typedef int (*lpNSAPIGetSettingsInt)(const char*, const char*, int); |
|---|
| 74 | typedef void (*lpNSAPIMessage)(int, const char*, const int, const char*); |
|---|
| 75 | typedef int (*lpNSAPIStopServer)(void); |
|---|
| 76 | typedef int (*lpNSAPIInject)(const char*,char*,unsigned int); |
|---|
| 77 | typedef LPVOID (*lpNSAPILoader)(char*); |
|---|
| 78 | |
|---|
| 79 | // Helper functions for calling into the core |
|---|
| 80 | std::string getApplicationName(void); |
|---|
| 81 | std::string getApplicationVersionString(void); |
|---|
| 82 | std::string getSettingsString(std::string section, std::string key, std::string defaultValue); |
|---|
| 83 | int getSettingsInt(std::string section, std::string key, int defaultValue); |
|---|
| 84 | void Message(int msgType, std::string file, int line, std::string message); |
|---|
| 85 | std::string InjectCommand(std::string command); |
|---|
| 86 | void StopService(void); |
|---|
| 87 | std::string getBasePath(); |
|---|
| 88 | }; |
|---|
| 89 | |
|---|
| 90 | namespace NSCModuleWrapper { |
|---|
| 91 | struct module_version { |
|---|
| 92 | int major; |
|---|
| 93 | int minor; |
|---|
| 94 | int revision; |
|---|
| 95 | }; |
|---|
| 96 | |
|---|
| 97 | BOOL wrapDllMain(HANDLE hModule, DWORD ul_reason_for_call); |
|---|
| 98 | HINSTANCE getModule(); |
|---|
| 99 | |
|---|
| 100 | int wrapModuleHelperInit(NSCModuleHelper::lpNSAPILoader f);; |
|---|
| 101 | int wrapGetModuleName(char* buf, unsigned int buflen, std::string str); |
|---|
| 102 | int wrapLoadModule(bool success); |
|---|
| 103 | int wrapGetModuleVersion(int *major, int *minor, int *revision, module_version version); |
|---|
| 104 | int wrapHasCommandHandler(bool has); |
|---|
| 105 | int wrapHasMessageHandler(bool has); |
|---|
| 106 | int wrapUnloadModule(bool success); |
|---|
| 107 | int wrapHandleCommand(const std::string retStr, char *returnBuffer, unsigned int returnBufferLen); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | ////////////////////////////////////////////////////////////////////////// |
|---|
| 111 | // Module wrappers (definitions) |
|---|
| 112 | #define NSC_WRAPPERS_MAIN() \ |
|---|
| 113 | extern "C" int NSModuleHelperInit(NSCModuleHelper::lpNSAPILoader f); \ |
|---|
| 114 | extern int NSLoadModule(); \ |
|---|
| 115 | extern int NSGetModuleName(char* buf, int buflen); \ |
|---|
| 116 | extern int NSGetModuleVersion(int *major, int *minor, int *revision); \ |
|---|
| 117 | extern int NSHasCommandHandler(); \ |
|---|
| 118 | extern int NSHasMessageHandler(); \ |
|---|
| 119 | extern void NSHandleMessage(int msgType, char* file, int line, char* message); \ |
|---|
| 120 | extern int NSHandleCommand(const char* command, const unsigned int argLen, char **argument, char *returnBuffer, unsigned int returnBufferLen); \ |
|---|
| 121 | extern int NSUnloadModule(); |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | #define NSC_LOG_ERROR_STD(msg) NSC_LOG_ERROR(((std::string)msg).c_str()) |
|---|
| 126 | #define NSC_LOG_ERROR(msg) \ |
|---|
| 127 | NSCModuleHelper::Message(NSCAPI::error, __FILE__, __LINE__, msg) |
|---|
| 128 | |
|---|
| 129 | #define NSC_LOG_CRITICAL_STD(msg) NSC_LOG_CRITICAL(((std::string)msg).c_str()) |
|---|
| 130 | #define NSC_LOG_CRITICAL(msg) \ |
|---|
| 131 | NSCModuleHelper::Message(NSCAPI::critical, __FILE__, __LINE__, msg) |
|---|
| 132 | |
|---|
| 133 | #define NSC_LOG_MESSAGE_STD(msg) NSC_LOG_MESSAGE(((std::string)msg).c_str()) |
|---|
| 134 | #define NSC_LOG_MESSAGE(msg) \ |
|---|
| 135 | NSCModuleHelper::Message(NSCAPI::log, __FILE__, __LINE__, msg) |
|---|
| 136 | |
|---|
| 137 | #define NSC_DEBUG_MSG_STD(msg) NSC_DEBUG_MSG(((std::string)msg).c_str()) |
|---|
| 138 | #define NSC_DEBUG_MSG(msg) \ |
|---|
| 139 | NSCModuleHelper::Message(NSCAPI::debug, __FILE__, __LINE__, msg) |
|---|
| 140 | |
|---|
| 141 | ////////////////////////////////////////////////////////////////////////// |
|---|
| 142 | // Message wrappers below this point |
|---|
| 143 | |
|---|
| 144 | #define NSC_WRAPPERS_MAIN_DEF(toObject) \ |
|---|
| 145 | extern int NSModuleHelperInit(NSCModuleHelper::lpNSAPILoader f) { \ |
|---|
| 146 | return NSCModuleWrapper::wrapModuleHelperInit(f); \ |
|---|
| 147 | } \ |
|---|
| 148 | extern int NSLoadModule() { \ |
|---|
| 149 | return NSCModuleWrapper::wrapLoadModule(toObject.loadModule()); \ |
|---|
| 150 | } \ |
|---|
| 151 | extern int NSGetModuleName(char* buf, int buflen) { \ |
|---|
| 152 | return NSCModuleWrapper::wrapGetModuleName(buf, buflen, toObject.getModuleName()); \ |
|---|
| 153 | } \ |
|---|
| 154 | extern int NSGetModuleVersion(int *major, int *minor, int *revision) { \ |
|---|
| 155 | return NSCModuleWrapper::wrapGetModuleVersion(major, minor, revision, toObject.getModuleVersion()); \ |
|---|
| 156 | } \ |
|---|
| 157 | extern int NSHasCommandHandler() { \ |
|---|
| 158 | return NSCModuleWrapper::wrapHasCommandHandler(toObject.hasCommandHandler()); \ |
|---|
| 159 | } \ |
|---|
| 160 | extern int NSHasMessageHandler() { \ |
|---|
| 161 | return NSCModuleWrapper::wrapHasMessageHandler(toObject.hasMessageHandler()); \ |
|---|
| 162 | } \ |
|---|
| 163 | extern int NSUnloadModule() { \ |
|---|
| 164 | return NSCModuleWrapper::wrapUnloadModule(toObject.unloadModule()); \ |
|---|
| 165 | } |
|---|
| 166 | #define NSC_WRAPPERS_HANDLE_MSG_DEF(toObject) \ |
|---|
| 167 | extern void NSHandleMessage(int msgType, char* file, int line, char* message) { \ |
|---|
| 168 | toObject.handleMessage(msgType, file, line, message); \ |
|---|
| 169 | } |
|---|
| 170 | #define NSC_WRAPPERS_IGNORE_MSG_DEF() \ |
|---|
| 171 | extern void NSHandleMessage(int msgType, char* file, int line, char* message) { \ |
|---|
| 172 | } |
|---|
| 173 | #define NSC_WRAPPERS_HANDLE_CMD_DEF(toObject) \ |
|---|
| 174 | extern int NSHandleCommand(const char* command, const unsigned int argLen, char **argument, char *returnBuffer, unsigned int returnBufferLen) { \ |
|---|
| 175 | return NSCModuleWrapper::wrapHandleCommand(toObject.handleCommand(command, argLen, argument), returnBuffer, returnBufferLen); \ |
|---|
| 176 | } |
|---|
| 177 | #define NSC_WRAPPERS_IGNORE_CMD_DEF() \ |
|---|
| 178 | extern int NSHandleCommand(const char* command, const unsigned int argLen, char **argument, char *returnBuffer, unsigned int returnBufferLen) { \ |
|---|
| 179 | return NSCAPI::failed; \ |
|---|
| 180 | } |
|---|
| 181 | #define NSC_LOG_DEBUG(str) \ |
|---|
| 182 | NSCHelper::DebugMessage(__FILE__, __LINE__, str); |
|---|