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