source: nscp/include/nscapi/nscapi_core_wrapper.cpp @ 393a00f

0.4.2
Last change on this file since 393a00f was 393a00f, checked in by Michael Medin <michael@…>, 12 months ago
  • Improved settings API with new protocolbuffers command (available from python)
  • Improved registration API with new protocolbuffers command (available from python)
  • Created new python documentation module docs.py
  • Improved command line syntax so executable commands now take an optional module prefix
  • Property mode set to 100644
File size: 27.8 KB
Line 
1/**************************************************************************
2*   Copyright (C) 2004-2007 by Michael Medin <michael@medin.name>         *
3*                                                                         *
4*   This code is part of NSClient++ - http://trac.nakednuns.org/nscp      *
5*                                                                         *
6*   This program is free software; you can redistribute it and/or modify  *
7*   it under the terms of the GNU General Public License as published by  *
8*   the Free Software Foundation; either version 2 of the License, or     *
9*   (at your option) any later version.                                   *
10*                                                                         *
11*   This program is distributed in the hope that it will be useful,       *
12*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14*   GNU General Public License for more details.                          *
15*                                                                         *
16*   You should have received a copy of the GNU General Public License     *
17*   along with this program; if not, write to the                         *
18*   Free Software Foundation, Inc.,                                       *
19*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
20***************************************************************************/
21
22#include <iostream>
23
24#include <nscapi/nscapi_core_wrapper.hpp>
25#include <nscapi/nscapi_helper.hpp>
26
27#include <strEx.h>
28#include <arrayBuffer.h>
29
30#define CORE_LOG_ERROR_STD(msg) if (should_log(NSCAPI::log_level::error)) { log(NSCAPI::log_level::error, __FILE__, __LINE__, (std::wstring)msg); }
31#define CORE_LOG_ERROR(msg) if (should_log(NSCAPI::log_level::error)) { log(NSCAPI::log_level::error, __FILE__, __LINE__, msg); }
32
33#define LEGACY_BUFFER_LENGTH 4096
34//////////////////////////////////////////////////////////////////////////
35// Callbacks into the core
36//////////////////////////////////////////////////////////////////////////
37
38bool nscapi::core_wrapper::should_log(NSCAPI::nagiosReturn msgType) {
39        enum log_status {unknown, set };
40        static NSCAPI::log_level::level level = NSCAPI::log_level::info;
41        static log_status status = unknown;
42        if (status == unknown) {
43                level = get_loglevel();
44                status = set;
45        }
46        return nscapi::logging::matches(level, msgType);
47}
48
49
50/**
51 * Callback to send a message through to the core
52 *
53 * @param msgType Message type (debug, warning, etc.)
54 * @param file File where message was generated (__FILE__)
55 * @param line Line where message was generated (__LINE__)
56 * @param message Message in human readable format
57 * @throws nscapi::nscapi_exception When core pointer set is unavailable.
58 */
59void nscapi::core_wrapper::log(NSCAPI::nagiosReturn msgType, std::string file, int line, std::wstring logMessage) {
60        if (!should_log(msgType))
61                return;
62        if (!fNSAPISimpleMessage) {
63                std::wcout << _T("NSCORE NOT LOADED Dumping log: ") << line << _T(": ") << std::endl << logMessage << std::endl;
64                return;
65        }
66        try {
67                return fNSAPISimpleMessage(alias.c_str(), msgType, file.c_str(), line, logMessage.c_str());
68        } catch (const std::exception &e) {
69                std::wcout << _T("Failed to generate message: ") << utf8::to_unicode(e.what());
70        } catch (...) {
71                std::wcout << _T("Failed to generate message: UNKNOWN");
72        }
73}
74void nscapi::core_wrapper::log(NSCAPI::nagiosReturn msgType, std::string file, int line, std::string message) {
75        if (!should_log(msgType))
76                return;
77        log(msgType, file, line, utf8::cvt<std::wstring>(message));
78}
79
80NSCAPI::log_level::level nscapi::core_wrapper::get_loglevel() {
81        if (!fNSAPIGetLoglevel) {
82                return NSCAPI::log_level::debug;
83        }
84        return fNSAPIGetLoglevel();
85}
86
87
88/**
89 * Inject a request command in the core (this will then be sent to the plug-in stack for processing)
90 * @param command Command to inject (password should not be included.
91 * @return The result (if any) of the command.
92 * @throws nscapi::nscapi_exception When core pointer set is unavailable or an unknown inject error occurs.
93 */
94
95/**
96 * Inject a request command in the core (this will then be sent to the plug-in stack for processing)
97 * @param command Command to inject
98 * @param argLen The length of the argument buffer
99 * @param **argument The argument buffer
100 * @param *returnMessageBuffer Buffer to hold the returned message
101 * @param returnMessageBufferLen Length of returnMessageBuffer
102 * @param *returnPerfBuffer Buffer to hold the returned performance data
103 * @param returnPerfBufferLen returnPerfBuffer
104 * @return The returned status of the command
105 */
106NSCAPI::nagiosReturn nscapi::core_wrapper::query(const wchar_t* command, const char *request, const unsigned int request_len, char **response, unsigned int *response_len)
107{
108        if (!fNSAPIInject)
109                throw nscapi::nscapi_exception("NSCore has not been initiated...");
110        return fNSAPIInject(command, request, request_len, response, response_len);
111}
112
113
114void nscapi::core_wrapper::DestroyBuffer(char**buffer) {
115        if (!fNSAPIDestroyBuffer)
116                throw nscapi::nscapi_exception("NSCore has not been initiated...");
117        return fNSAPIDestroyBuffer(buffer);
118}
119
120
121NSCAPI::errorReturn nscapi::core_wrapper::submit_message(std::wstring channel, std::string request, std::string &response) {
122
123        if (!fNSAPINotify)
124                throw nscapi::nscapi_exception("NSCore has not been initiated...");
125        char *buffer = NULL;
126        unsigned int buffer_size = 0;
127        NSCAPI::nagiosReturn ret = submit_message(channel.c_str(), request.c_str(), request.size(), &buffer, &buffer_size);
128
129        if (buffer_size > 0 && buffer != NULL) {
130                response = std::string(buffer, buffer_size);
131        }
132
133        DestroyBuffer(&buffer);
134        return ret;
135}
136
137NSCAPI::errorReturn nscapi::core_wrapper::reload(std::wstring module) {
138
139        if (!fNSAPIReload)
140                throw nscapi::nscapi_exception("NSCore has not been initiated...");
141        return fNSAPIReload(module.c_str());
142}
143NSCAPI::nagiosReturn nscapi::core_wrapper::submit_message(const wchar_t* channel, const char *request, const unsigned int request_len, char **response, unsigned int *response_len)
144{
145        if (!fNSAPINotify)
146                throw nscapi::nscapi_exception("NSCore has not been initiated...");
147        return fNSAPINotify(channel, request, request_len, response, response_len);
148}
149
150NSCAPI::nagiosReturn nscapi::core_wrapper::query(const std::wstring & command, const std::string & request, std::string & result)
151{
152        if (!fNSAPIInject)
153                throw nscapi::nscapi_exception("NSCore has not been initiated...");
154        char *buffer = NULL;
155        unsigned int buffer_size = 0;
156        NSCAPI::nagiosReturn retC = query(command.c_str(), request.c_str(), request.size(), &buffer, &buffer_size);
157
158        if (buffer_size > 0 && buffer != NULL) {
159                //PluginCommand::ResponseMessage rsp_msg;
160                result = std::string(buffer, buffer_size);
161        }
162
163        DestroyBuffer(&buffer);
164        switch (retC) {
165                case NSCAPI::returnIgnored:
166                        CORE_LOG_ERROR_STD(_T("No handler for command '") + command + _T("'."));
167                        break;
168                case NSCAPI::returnOK:
169                case NSCAPI::returnCRIT:
170                case NSCAPI::returnWARN:
171                case NSCAPI::returnUNKNOWN:
172                        break;
173                default:
174                        throw nscapi::nscapi_exception("Unknown return code from query: " + utf8::cvt<std::string>(command));
175        }
176        return retC;
177}
178
179NSCAPI::nagiosReturn nscapi::core_wrapper::exec_command(const std::wstring target, const std::wstring command, std::string request, std::string & result) {
180        char *buffer = NULL;
181        unsigned int buffer_size = 0;
182        NSCAPI::nagiosReturn retC = exec_command(target.c_str(), command.c_str(), request.c_str(), request.size(), &buffer, &buffer_size);
183
184        if (buffer_size > 0 && buffer != NULL) {
185                result = std::string(buffer, buffer_size);
186        }
187
188        DestroyBuffer(&buffer);
189        switch (retC) {
190                case NSCAPI::returnIgnored:
191                        CORE_LOG_ERROR_STD(_T("No handler for command '") + command + _T("'."));
192                        break;
193                case NSCAPI::returnOK:
194                case NSCAPI::returnCRIT:
195                case NSCAPI::returnWARN:
196                case NSCAPI::returnUNKNOWN:
197                        break;
198                default:
199                        throw nscapi::nscapi_exception("Unknown return from exec: " + utf8::cvt<std::string>(command));
200        }
201        return retC;
202}
203NSCAPI::nagiosReturn nscapi::core_wrapper::exec_command(const wchar_t* target, const wchar_t* command, const char *request, const unsigned int request_len, char **response, unsigned int *response_len)
204{
205        if (!fNSAPIExecCommand)
206                throw nscapi::nscapi_exception("NSCore has not been initiated...");
207        return fNSAPIExecCommand(target, command, request, request_len, response, response_len);
208}
209
210/**
211 * Ask the core to shutdown (only works when run as a service, o/w does nothing ?
212 * @todo Check if this might cause damage if not run as a service.
213 */
214void nscapi::core_wrapper::StopService(void) {
215        if (fNSAPIStopServer)
216                fNSAPIStopServer();
217}
218/**
219 * Close the program (usefull for tray/testmode) without stopping the service (unless this is the service).
220 * @author mickem
221 */
222void nscapi::core_wrapper::Exit(void) {
223        if (fNSAPIExit)
224                fNSAPIExit();
225}
226/**
227 * Retrieve a string from the settings subsystem (INI-file)
228 * Might possibly be located in the registry in the future.
229 *
230 * @param section Section key (generally module specific, make sure this is "unique")
231 * @param key The key to retrieve
232 * @param defaultValue A default value (if no value is set in the settings file)
233 * @return the current value or defaultValue if no value is set.
234 * @throws nscapi::nscapi_exception When core pointer set is unavailable or an error occurs.
235 */
236std::wstring nscapi::core_wrapper::getSettingsString(std::wstring section, std::wstring key, std::wstring defaultValue) {
237        if (!fNSAPIGetSettingsString)
238                throw nscapi::nscapi_exception("NSCore has not been initiated...");
239        unsigned int buf_len = LEGACY_BUFFER_LENGTH;
240        wchar_t *buffer = new wchar_t[buf_len+1];
241        if (fNSAPIGetSettingsString(section.c_str(), key.c_str(), defaultValue.c_str(), buffer, buf_len) != NSCAPI::isSuccess) {
242                delete [] buffer;
243                throw nscapi::nscapi_exception("Settings could not be retrieved.");
244        }
245        std::wstring ret = buffer;
246        delete [] buffer;
247        return ret;
248}
249
250std::wstring nscapi::core_wrapper::expand_path(std::wstring value) {
251        if (!fNSAPIExpandPath)
252                throw nscapi::nscapi_exception("NSCore has not been initiated...");
253        unsigned int buf_len = LEGACY_BUFFER_LENGTH;
254        wchar_t *buffer = new wchar_t[buf_len+1];
255        if (fNSAPIExpandPath(value.c_str(), buffer, buf_len) != NSCAPI::isSuccess) {
256                delete [] buffer;
257                throw nscapi::nscapi_exception("Settings could not be retrieved.");
258        }
259        std::wstring ret = buffer;
260        delete [] buffer;
261        return ret;
262}
263/**
264 * Get a section of settings strings
265 * @param section The section to retrieve
266 * @return The keys in the section
267 */
268std::list<std::wstring> nscapi::core_wrapper::getSettingsSection(std::wstring section) {
269        if (!fNSAPIGetSettingsSection)
270                throw nscapi::nscapi_exception("NSCore has not been initiated...");
271        array_buffer::arrayBuffer aBuffer = NULL;
272        unsigned int argLen = 0;
273        if (fNSAPIGetSettingsSection(section.c_str(), &aBuffer, &argLen) != NSCAPI::isSuccess) {
274                throw nscapi::nscapi_exception("Settings could not be retrieved.");
275        }
276        std::list<std::wstring> ret = array_buffer::arrayBuffer2list(argLen, aBuffer);
277        if (fNSAPIReleaseSettingsSectionBuffer(&aBuffer, &argLen) != NSCAPI::isSuccess) {
278                throw nscapi::nscapi_exception("Settings could not be destroyed.");
279        }
280        if (aBuffer != NULL)
281                throw nscapi::nscapi_exception("buffer is not null?.");
282        return ret;
283}
284std::list<std::wstring> nscapi::core_wrapper::getSettingsSections(std::wstring section) {
285        if (!fNSAPIGetSettingsSections)
286                throw nscapi::nscapi_exception("NSCore has not been initiated...");
287        array_buffer::arrayBuffer aBuffer = NULL;
288        unsigned int argLen = 0;
289        if (fNSAPIGetSettingsSections(section.c_str(), &aBuffer, &argLen) != NSCAPI::isSuccess) {
290                throw nscapi::nscapi_exception("Settings could not be retrieved.");
291        }
292        std::list<std::wstring> ret = array_buffer::arrayBuffer2list(argLen, aBuffer);
293        if (fNSAPIReleaseSettingsSectionBuffer(&aBuffer, &argLen) != NSCAPI::isSuccess) {
294                throw nscapi::nscapi_exception("Settings could not be destroyed.");
295        }
296        if (aBuffer != NULL)
297                throw nscapi::nscapi_exception("buffer is not null?.");
298        return ret;
299}
300/**
301 * Retrieve an int from the settings subsystem (INI-file)
302 * Might possibly be located in the registry in the future.
303 *
304 * @param section Section key (generally module specific, make sure this is "unique")
305 * @param key The key to retrieve
306 * @param defaultValue A default value (if no value is set in the settings file)
307 * @return the current value or defaultValue if no value is set.
308 * @throws nscapi::nscapi_exception When core pointer set is unavailable.
309 */
310int nscapi::core_wrapper::getSettingsInt(std::wstring section, std::wstring key, int defaultValue) {
311        if (!fNSAPIGetSettingsInt)
312                throw nscapi::nscapi_exception("NSCore has not been initiated...");
313        return fNSAPIGetSettingsInt(section.c_str(), key.c_str(), defaultValue);
314}
315bool nscapi::core_wrapper::getSettingsBool(std::wstring section, std::wstring key, bool defaultValue) {
316        if (!fNSAPIGetSettingsBool)
317                throw nscapi::nscapi_exception("NSCore has not been initiated...");
318        return fNSAPIGetSettingsBool(section.c_str(), key.c_str(), defaultValue?1:0) == 1;
319}
320void nscapi::core_wrapper::settings_register_key(unsigned int plugin_id, std::wstring path, std::wstring key, NSCAPI::settings_type type, std::wstring title, std::wstring description, std::wstring defaultValue, bool advanced) {
321        if (!fNSAPISettingsRegKey)
322                throw nscapi::nscapi_exception("NSCore has not been initiated...");
323        fNSAPISettingsRegKey(plugin_id, path.c_str(), key.c_str(), type, title.c_str(), description.c_str(), defaultValue.c_str(), advanced);
324}
325void nscapi::core_wrapper::settings_register_path(unsigned int plugin_id, std::wstring path, std::wstring title, std::wstring description, bool advanced) {
326        if (!fNSAPISettingsRegPath)
327                throw nscapi::nscapi_exception("NSCore has not been initiated...");
328        fNSAPISettingsRegPath(plugin_id, path.c_str(), title.c_str(), description.c_str(), advanced);
329}
330NSCAPI::errorReturn nscapi::core_wrapper::settings_query(const char *request, const unsigned int request_len, char **response, unsigned int *response_len) {
331        if (!fNSAPISettingsQuery)
332                throw nscapi::nscapi_exception("NSCore has not been initiated...");
333        fNSAPISettingsQuery(request, request_len, response, response_len);
334}
335NSCAPI::errorReturn nscapi::core_wrapper::settings_query(const std::string request, std::string &response) {
336        char *buffer = NULL;
337        unsigned int buffer_size = 0;
338        NSCAPI::errorReturn retC = settings_query(request.c_str(), request.size(), &buffer, &buffer_size);
339        if (buffer_size > 0 && buffer != NULL) {
340                response = std::string(buffer, buffer_size);
341        }
342        DestroyBuffer(&buffer);
343        return retC;
344}
345
346void nscapi::core_wrapper::settings_save() {
347        if (!fNSAPISettingsSave)
348                throw nscapi::nscapi_exception("NSCore has not been initiated...");
349        fNSAPISettingsSave();
350}
351
352
353NSCAPI::errorReturn nscapi::core_wrapper::registry_query(const char *request, const unsigned int request_len, char **response, unsigned int *response_len) {
354        if (!fNSAPIRegistryQuery)
355                throw nscapi::nscapi_exception("NSCore has not been initiated...");
356        fNSAPIRegistryQuery(request, request_len, response, response_len);
357}
358NSCAPI::errorReturn nscapi::core_wrapper::registry_query(const std::string request, std::string &response) {
359        char *buffer = NULL;
360        unsigned int buffer_size = 0;
361        NSCAPI::errorReturn retC = registry_query(request.c_str(), request.size(), &buffer, &buffer_size);
362        if (buffer_size > 0 && buffer != NULL) {
363                response = std::string(buffer, buffer_size);
364        }
365        DestroyBuffer(&buffer);
366        return retC;
367}
368
369/**
370 * Retrieve the application name (in human readable format) from the core.
371 * @return A string representing the application name.
372 * @throws nscapi::nscapi_exception When core pointer set is unavailable or an unexpected error occurs.
373 */
374std::wstring nscapi::core_wrapper::getApplicationName() {
375        if (!fNSAPIGetApplicationName)
376                throw nscapi::nscapi_exception("NSCore has not been initiated...");
377        unsigned int buf_len = LEGACY_BUFFER_LENGTH;
378        wchar_t *buffer = new wchar_t[buf_len+1];
379        if (fNSAPIGetApplicationName(buffer, buf_len) != NSCAPI::isSuccess) {
380                delete [] buffer;
381                throw nscapi::nscapi_exception("Application name could not be retrieved");
382        }
383        std::wstring ret = buffer;
384        delete [] buffer;
385        return ret;
386}
387/**
388 * Retrieve the directory root of the application from the core.
389 * @return A string representing the base path.
390 * @throws nscapi::nscapi_exception When core pointer set is unavailable or an unexpected error occurs.
391 */
392std::wstring nscapi::core_wrapper::getBasePath() {
393        if (!fNSAPIGetBasePath)
394                throw nscapi::nscapi_exception("NSCore has not been initiated...");
395        unsigned int buf_len = LEGACY_BUFFER_LENGTH;
396        wchar_t *buffer = new wchar_t[buf_len+1];
397        if (fNSAPIGetBasePath(buffer, buf_len) != NSCAPI::isSuccess) {
398                delete [] buffer;
399                throw nscapi::nscapi_exception("Base path could not be retrieved");
400        }
401        std::wstring ret = buffer;
402        delete [] buffer;
403        return ret;
404}
405
406std::wstring nscapi::core_wrapper::Encrypt(std::wstring str, unsigned int algorithm) {
407        if (!fNSAPIEncrypt)
408                throw nscapi::nscapi_exception("NSCore has not been initiated...");
409        unsigned int len = 0;
410        // @todo investigate potential problems with static_cast<unsigned int>
411        fNSAPIEncrypt(algorithm, str.c_str(), static_cast<unsigned int>(str.size()), NULL, &len);
412        len+=2;
413        wchar_t *buf = new wchar_t[len+1];
414        NSCAPI::errorReturn ret = fNSAPIEncrypt(algorithm, str.c_str(), static_cast<unsigned int>(str.size()), buf, &len);
415        if (ret == NSCAPI::isSuccess) {
416                std::wstring ret = buf;
417                delete [] buf;
418                return ret;
419        }
420        return _T("");
421}
422std::wstring nscapi::core_wrapper::Decrypt(std::wstring str, unsigned int algorithm) {
423        if (!fNSAPIDecrypt)
424                throw nscapi::nscapi_exception("NSCore has not been initiated...");
425        unsigned int len = 0;
426        // @todo investigate potential problems with: static_cast<unsigned int>(str.size())
427        fNSAPIDecrypt(algorithm, str.c_str(), static_cast<unsigned int>(str.size()), NULL, &len);
428        len+=2;
429        wchar_t *buf = new wchar_t[len+1];
430        NSCAPI::errorReturn ret = fNSAPIDecrypt(algorithm, str.c_str(), static_cast<unsigned int>(str.size()), buf, &len);
431        if (ret == NSCAPI::isSuccess) {
432                std::wstring ret = buf;
433                delete [] buf;
434                return ret;
435        }
436        return _T("");
437}
438NSCAPI::errorReturn nscapi::core_wrapper::SetSettingsString(std::wstring section, std::wstring key, std::wstring value) {
439        if (!fNSAPISetSettingsString)
440                throw nscapi::nscapi_exception("NSCore has not been initiated...");
441        return fNSAPISetSettingsString(section.c_str(), key.c_str(), value.c_str());
442}
443NSCAPI::errorReturn nscapi::core_wrapper::SetSettingsInt(std::wstring section, std::wstring key, int value) {
444        if (!fNSAPISetSettingsInt)
445                throw nscapi::nscapi_exception("NSCore has not been initiated...");
446        return fNSAPISetSettingsInt(section.c_str(), key.c_str(), value);
447}
448NSCAPI::errorReturn nscapi::core_wrapper::WriteSettings(int type) {
449        if (!fNSAPIWriteSettings)
450                throw nscapi::nscapi_exception("NSCore has not been initiated...");
451        return fNSAPIWriteSettings(type);
452}
453NSCAPI::errorReturn nscapi::core_wrapper::ReadSettings(int type) {
454        if (!fNSAPIReadSettings)
455                throw nscapi::nscapi_exception("NSCore has not been initiated...");
456        return fNSAPIReadSettings(type);
457}
458NSCAPI::errorReturn nscapi::core_wrapper::Rehash(int flag) {
459        if (!fNSAPIRehash)
460                throw nscapi::nscapi_exception("NSCore has not been initiated...");
461        return fNSAPIRehash(flag);
462}
463nscapi::core_wrapper::plugin_info_list nscapi::core_wrapper::getPluginList() {
464        if (!fNSAPIGetPluginList || !fNSAPIReleasePluginList)
465                throw nscapi::nscapi_exception("NSCore has not been initiated...");
466        plugin_info_list ret;
467       
468       
469        int len = 0;
470        NSCAPI::plugin_info *list[1];
471        NSCAPI::errorReturn err = fNSAPIGetPluginList(&len, list);
472        if (err != NSCAPI::isSuccess)
473                return ret;
474        for (int i=0;i<len;i++) {
475                plugin_info_type info;
476                info.description = (*list)[i].description;
477                info.name = (*list)[i].name;
478                info.dll = (*list)[i].dll;
479                ret.push_back(info);
480        }
481        fNSAPIReleasePluginList(len, list);
482        return ret;
483}
484
485std::list<std::wstring> nscapi::core_wrapper::getAllCommandNames() {
486        if (!fNSAPIGetAllCommandNames || !fNSAPIReleaseAllCommandNamessBuffer )
487                throw nscapi::nscapi_exception("NSCore has not been initiated...");
488        array_buffer::arrayBuffer aBuffer = NULL;
489        unsigned int argLen = 0;
490        if (fNSAPIGetAllCommandNames(&aBuffer, &argLen) != NSCAPI::isSuccess) {
491                throw nscapi::nscapi_exception("Commands could not be retrieved.");
492        }
493        std::list<std::wstring> ret = array_buffer::arrayBuffer2list(argLen, aBuffer);
494        if (fNSAPIReleaseAllCommandNamessBuffer(&aBuffer, &argLen) != NSCAPI::isSuccess) {
495                throw nscapi::nscapi_exception("Commands could not be destroyed.");
496        }
497        if (aBuffer != NULL)
498                throw nscapi::nscapi_exception("buffer is not null?.");
499        return ret;
500}
501std::wstring nscapi::core_wrapper::describeCommand(std::wstring command) {
502        if (!fNSAPIDescribeCommand)
503                throw nscapi::nscapi_exception("NSCore has not been initiated...");
504        unsigned int buf_len = LEGACY_BUFFER_LENGTH;
505        wchar_t *buffer = new wchar_t[buf_len+1];
506        if (fNSAPIDescribeCommand(command.c_str(), buffer, buf_len) != NSCAPI::isSuccess) {
507                delete [] buffer;
508                throw nscapi::nscapi_exception("Base path could not be retrieved");
509        }
510        std::wstring ret = buffer;
511        delete [] buffer;
512        return ret;
513}
514void nscapi::core_wrapper::registerCommand(unsigned int id, std::wstring command, std::wstring description) {
515        if (!fNSAPIRegisterCommand)
516                throw nscapi::nscapi_exception("NSCore has not been initiated...");
517        if (fNSAPIRegisterCommand(id, command.c_str(), description.c_str()) != NSCAPI::isSuccess) {
518                CORE_LOG_ERROR_STD(_T("Failed to register command: ") + command + _T(" in plugin: ") + strEx::itos(id));
519        }
520}
521
522void nscapi::core_wrapper::registerSubmissionListener(unsigned int id, std::wstring channel) {
523        if (!fNSAPIRegisterSubmissionListener)
524                throw nscapi::nscapi_exception("NSCore has not been initiated...");
525        if (fNSAPIRegisterSubmissionListener(id, channel.c_str()) != NSCAPI::isSuccess) {
526                CORE_LOG_ERROR_STD(_T("Failed to register channel: ") + channel + _T(" in plugin: ") + strEx::itos(id));
527        }
528}
529void nscapi::core_wrapper::registerRoutingListener(unsigned int id, std::wstring channel) {
530        if (!fNSAPIRegisterRoutingListener)
531                throw nscapi::nscapi_exception("NSCore has not been initiated...");
532        if (fNSAPIRegisterRoutingListener(id, channel.c_str()) != NSCAPI::isSuccess) {
533                CORE_LOG_ERROR_STD(_T("Failed to register channel: ") + channel + _T(" in plugin: ") + strEx::itos(id));
534        }
535}
536
537
538bool nscapi::core_wrapper::checkLogMessages(int type) {
539        if (!fNSAPICheckLogMessages)
540                throw nscapi::nscapi_exception("NSCore has not been initiated...");
541        return fNSAPICheckLogMessages(type) == NSCAPI::istrue;
542}
543/**
544 * Retrieve the application version as a string (in human readable format) from the core.
545 * @return A string representing the application version.
546 * @throws nscapi::nscapi_exception When core pointer set is unavailable.
547 */
548std::wstring nscapi::core_wrapper::getApplicationVersionString() {
549        if (!fNSAPIGetApplicationVersionStr)
550                throw nscapi::nscapi_exception("NSCore has not been initiated...");
551        unsigned int buf_len = LEGACY_BUFFER_LENGTH;
552        wchar_t *buffer = new wchar_t[buf_len+1];
553        if (fNSAPIGetApplicationVersionStr(buffer, buf_len) != NSCAPI::isSuccess) {
554                delete [] buffer;
555                return _T("");
556        }
557        std::wstring ret = buffer;
558        delete [] buffer;
559        return ret;
560}
561
562void nscapi::core_wrapper::set_alias(const std::wstring default_alias_, const std::wstring alias_) {
563        alias = default_alias_;
564}
565
566/**
567 * Wrapper function around the ModuleHelperInit call.
568 * This wrapper retrieves all pointers and stores them for future use.
569 * @param f A function pointer to a function that can be used to load function from the core.
570 * @return NSCAPI::success or NSCAPI::failure
571 */
572bool nscapi::core_wrapper::load_endpoints(nscapi::core_api::lpNSAPILoader f) {
573        fNSAPIGetApplicationName = (nscapi::core_api::lpNSAPIGetApplicationName)f(_T("NSAPIGetApplicationName"));
574        fNSAPIGetApplicationVersionStr = (nscapi::core_api::lpNSAPIGetApplicationVersionStr)f(_T("NSAPIGetApplicationVersionStr"));
575        fNSAPIGetSettingsInt = (nscapi::core_api::lpNSAPIGetSettingsInt)f(_T("NSAPIGetSettingsInt"));
576        fNSAPIGetSettingsBool = (nscapi::core_api::lpNSAPIGetSettingsBool)f(_T("NSAPIGetSettingsBool"));
577        fNSAPIGetSettingsString = (nscapi::core_api::lpNSAPIGetSettingsString)f(_T("NSAPIGetSettingsString"));
578        fNSAPIGetSettingsSection = (nscapi::core_api::lpNSAPIGetSettingsSection)f(_T("NSAPIGetSettingsSection"));
579        fNSAPIGetSettingsSections = (nscapi::core_api::lpNSAPIGetSettingsSections)f(_T("NSAPIGetSettingsSections"));
580        fNSAPIReleaseSettingsSectionBuffer = (nscapi::core_api::lpNSAPIReleaseSettingsSectionBuffer)f(_T("NSAPIReleaseSettingsSectionBuffer"));
581        fNSAPIMessage = (nscapi::core_api::lpNSAPIMessage)f(_T("NSAPIMessage"));
582        fNSAPISimpleMessage = (nscapi::core_api::lpNSAPISimpleMessage)f(_T("NSAPISimpleMessage"));
583        fNSAPIStopServer = (nscapi::core_api::lpNSAPIStopServer)f(_T("NSAPIStopServer"));
584        //fNSAPIExit = (nscapi::core_api::lpNSAPIExit)f(_T("NSAPIExit"));
585        fNSAPIInject = (nscapi::core_api::lpNSAPIInject)f(_T("NSAPIInject"));
586        fNSAPIExecCommand = (nscapi::core_api::lpNSAPIExecCommand)f(_T("NSAPIExecCommand"));
587        fNSAPIDestroyBuffer = (nscapi::core_api::lpNSAPIDestroyBuffer)f(_T("NSAPIDestroyBuffer"));
588        fNSAPINotify = (nscapi::core_api::lpNSAPINotify)f(_T("NSAPINotify"));
589        fNSAPIGetBasePath = (nscapi::core_api::lpNSAPIGetBasePath)f(_T("NSAPIGetBasePath"));
590        fNSAPICheckLogMessages = (nscapi::core_api::lpNSAPICheckLogMessages)f(_T("NSAPICheckLogMessages"));
591        fNSAPIDecrypt = (nscapi::core_api::lpNSAPIDecrypt)f(_T("NSAPIDecrypt"));
592        fNSAPIEncrypt = (nscapi::core_api::lpNSAPIEncrypt)f(_T("NSAPIEncrypt"));
593        fNSAPISetSettingsString = (nscapi::core_api::lpNSAPISetSettingsString)f(_T("NSAPISetSettingsString"));
594        fNSAPISetSettingsInt = (nscapi::core_api::lpNSAPISetSettingsInt)f(_T("NSAPISetSettingsInt"));
595        fNSAPIWriteSettings = (nscapi::core_api::lpNSAPIWriteSettings)f(_T("NSAPIWriteSettings"));
596        fNSAPIReadSettings = (nscapi::core_api::lpNSAPIReadSettings)f(_T("NSAPIReadSettings"));
597        fNSAPIRehash = (nscapi::core_api::lpNSAPIRehash)f(_T("NSAPIRehash"));
598        fNSAPIReload = (nscapi::core_api::lpNSAPIReload)f(_T("NSAPIReload"));
599
600        fNSAPIDescribeCommand = (nscapi::core_api::lpNSAPIDescribeCommand)f(_T("NSAPIDescribeCommand"));
601        fNSAPIGetAllCommandNames = (nscapi::core_api::lpNSAPIGetAllCommandNames)f(_T("NSAPIGetAllCommandNames"));
602        fNSAPIReleaseAllCommandNamessBuffer = (nscapi::core_api::lpNSAPIReleaseAllCommandNamessBuffer)f(_T("NSAPIReleaseAllCommandNamessBuffer"));
603        fNSAPIRegisterCommand = (nscapi::core_api::lpNSAPIRegisterCommand)f(_T("NSAPIRegisterCommand"));
604
605        fNSAPISettingsRegKey = (nscapi::core_api::lpNSAPISettingsRegKey)f(_T("NSAPISettingsRegKey"));
606        fNSAPISettingsRegPath = (nscapi::core_api::lpNSAPISettingsRegPath)f(_T("NSAPISettingsRegPath"));
607
608        fNSAPIGetPluginList = (nscapi::core_api::lpNSAPIGetPluginList)f(_T("NSAPIGetPluginList"));
609        fNSAPIReleasePluginList = (nscapi::core_api::lpNSAPIReleasePluginList)f(_T("NSAPIReleasePluginList"));
610
611        fNSAPISettingsSave = (nscapi::core_api::lpNSAPISettingsSave)f(_T("NSAPISettingsSave"));
612        fNSAPISettingsQuery = (nscapi::core_api::lpNSAPISettingsQuery)f(_T("NSAPISettingsQuery"));
613
614        fNSAPIExpandPath = (nscapi::core_api::lpNSAPIExpandPath)f(_T("NSAPIExpandPath"));
615       
616        fNSAPIRegisterSubmissionListener = (nscapi::core_api::lpNSAPIRegisterSubmissionListener)f(_T("NSAPIRegisterSubmissionListener"));
617        fNSAPIRegisterRoutingListener = (nscapi::core_api::lpNSAPIRegisterRoutingListener)f(_T("NSAPIRegisterRoutingListener"));
618        fNSAPIGetLoglevel = (nscapi::core_api::lpNSAPIGetLoglevel)f(_T("NSAPIGetLoglevel"));
619        fNSAPIRegistryQuery = (nscapi::core_api::lpNSAPIRegistryQuery)f(_T("NSAPIRegistryQuery"));
620
621        return true;
622}
Note: See TracBrowser for help on using the repository browser.