source: nscp/include/nscapi/nscapi_core_wrapper.cpp @ 8988f9e

0.4.00.4.10.4.2
Last change on this file since 8988f9e was 8988f9e, checked in by Michael Medin <michael@…>, 3 years ago

Added in performance e data for NRPE and NSCA as well as added in debug build for the protocol buffer library lookup thingy

  • Property mode set to 100644
File size: 25.6 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 <boost/foreach.hpp>
23#include <boost/tokenizer.hpp>
24
25#include <nscapi/nscapi_core_wrapper.hpp>
26#include <nscapi/nscapi_plugin_wrapper.hpp>
27#include <settings/macros.h>
28#include <arrayBuffer.h>
29#include <strEx.h>
30
31#include "../libs/protobuf/plugin.proto.h"
32
33using namespace nscp::helpers;
34
35#define CORE_LOG_ERROR_STD(msg) CORE_LOG_ERROR(((std::wstring)msg).c_str())
36#define CORE_LOG_ERROR(msg) CORE_ANY_MSG(msg,NSCAPI::error)
37
38#define CORE_LOG_CRITICAL_STD(msg) CORE_LOG_CRITICAL(((std::wstring)msg).c_str())
39#define CORE_LOG_CRITICAL(msg) CORE_ANY_MSG(msg,NSCAPI::critical)
40
41#define CORE_LOG_MESSAGE_STD(msg) CORE_LOG_MESSAGE(((std::wstring)msg).c_str())
42#define CORE_LOG_MESSAGE(msg) CORE_ANY_MSG(msg,NSCAPI::log)
43
44#define CORE_DEBUG_MSG_STD(msg) CORE_DEBUG_MSG((std::wstring)msg)
45#define CORE_DEBUG_MSG(msg) CORE_ANY_MSG(msg,NSCAPI::debug)
46
47#define CORE_ANY_MSG(msg, type) Message(type, __FILEW__, __LINE__, msg)
48
49
50//////////////////////////////////////////////////////////////////////////
51// Callbacks into the core
52//////////////////////////////////////////////////////////////////////////
53
54/**
55 * Callback to send a message through to the core
56 *
57 * @param msgType Message type (debug, warning, etc.)
58 * @param file File where message was generated (__FILE__)
59 * @param line Line where message was generated (__LINE__)
60 * @param message Message in human readable format
61 * @throws nscapi::nscapi_exception When core pointer set is unavailable.
62 */
63void nscapi::core_wrapper::Message(int msgType, std::wstring file, int line, std::wstring message) {
64        if (fNSAPIMessage) {
65                if ((msgType == NSCAPI::debug) && (!logDebug()))
66                        return;
67                /*
68                std::wstring::size_type pos = file.find_last_of("\\");
69                if (pos != std::wstring::npos)
70                        file = file.substr(pos);
71                        */
72                return fNSAPIMessage(msgType, file.c_str(), line, message.c_str());
73        }
74        else
75                std::wcout << _T("*** *** *** NSCore not loaded, dumping log: ") << file << _T(":") << line << _T(": ") << std::endl << message << std::endl;
76}
77/**
78 * Inject a request command in the core (this will then be sent to the plug-in stack for processing)
79 * @param command Command to inject (password should not be included.
80 * @return The result (if any) of the command.
81 * @throws nscapi::nscapi_exception When core pointer set is unavailable or an unknown inject error occurs.
82 */
83
84/**
85 * Inject a request command in the core (this will then be sent to the plug-in stack for processing)
86 * @param command Command to inject
87 * @param argLen The length of the argument buffer
88 * @param **argument The argument buffer
89 * @param *returnMessageBuffer Buffer to hold the returned message
90 * @param returnMessageBufferLen Length of returnMessageBuffer
91 * @param *returnPerfBuffer Buffer to hold the returned performance data
92 * @param returnPerfBufferLen returnPerfBuffer
93 * @return The returned status of the command
94 */
95NSCAPI::nagiosReturn nscapi::core_wrapper::InjectCommandRAW(const wchar_t* command, const char *request, const unsigned int request_len, char **response, unsigned int *response_len)
96{
97        if (!fNSAPIInject)
98                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
99        return fNSAPIInject(command, request, request_len, response, response_len);
100}
101
102void nscapi::core_wrapper::DestroyBuffer(char**buffer) {
103        if (!fNSAPIDestroyBuffer)
104                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
105        return fNSAPIDestroyBuffer(buffer);
106}
107
108NSCAPI::errorReturn nscapi::core_wrapper::NotifyChannel(std::wstring channel, std::wstring command, NSCAPI::nagiosReturn code, std::string result) {
109        if (!fNSAPINotify)
110                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
111        return fNSAPINotify(channel.c_str(), command.c_str(), code, result.c_str(), result.size());
112}
113
114/**
115* Inject a request command in the core (this will then be sent to the plug-in stack for processing)
116* @param command Command to inject (password should not be included.
117* @param argLen The length of the argument buffer
118* @param **argument The argument buffer
119* @param message The return message buffer
120* @param perf The return performance data buffer
121* @return The return of the command
122*/
123NSCAPI::nagiosReturn nscapi::core_wrapper::InjectSimpleCommand(const std::wstring command, const std::list<std::wstring> argument, std::wstring & msg, std::wstring & perf)
124{
125        if (!fNSAPIInject)
126                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
127
128        std::string response;
129        NSCAPI::nagiosReturn ret = InjectCommand(command, argument, response);
130        if (!response.empty()) {
131                PluginCommand::ResponseMessage rsp_msg;
132                rsp_msg.ParseFromString(response);
133                if (rsp_msg.payload_size() != 1) {
134                        CORE_LOG_ERROR_STD(_T("Failed to extract return message not 1 payload: ") + strEx::itos(rsp_msg.payload_size()));
135                        return NSCAPI::returnUNKNOWN;
136                }
137                ::PluginCommand::Response payload = rsp_msg.payload(0);
138                msg = to_wstring(payload.message());
139                CORE_LOG_ERROR_STD(_T("Attempting to parse perf data"));
140                perf = to_wstring(::nscapi::functions::build_performance_data(payload));
141                CORE_LOG_ERROR_STD(_T("Attempting to parse perf data: '") + perf + _T("'"));
142        }
143        return ret;
144}
145/**
146* Inject a request command in the core (this will then be sent to the plug-in stack for processing)
147* @param command Command to inject (password should not be included.
148* @param argLen The length of the argument buffer
149* @param **argument The argument buffer
150* @param message The return message buffer
151* @param perf The return performance data buffer
152* @return The return of the command
153*/
154NSCAPI::nagiosReturn nscapi::core_wrapper::InjectCommand(const std::wstring command, const std::list<std::wstring> argument, std::string & result)
155{
156        if (!fNSAPIInject)
157                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
158
159
160        PluginCommand::RequestMessage message;
161        PluginCommand::Header *hdr = message.mutable_header();
162        hdr->set_type(PluginCommand::Header_Type_REQUEST);
163        hdr->set_version(PluginCommand::Header_Version_VERSION_1);
164
165        PluginCommand::Request *req = message.add_payload();
166        req->set_command(to_string(command));
167        req->set_version(PluginCommand::Request_Version_VERSION_1);
168
169        BOOST_FOREACH(std::wstring s, argument)
170                req->add_arguments(to_string(s));
171
172        std::string request;
173        message.SerializeToString(&request);
174
175        char *buffer = NULL;
176        unsigned int buffer_size = 0;
177
178        NSCAPI::nagiosReturn retC = InjectCommandRAW(command.c_str(), request.c_str(), request.size(), &buffer, &buffer_size);
179
180        if (buffer_size > 0 && buffer != NULL) {
181                PluginCommand::ResponseMessage rsp_msg;
182                result = std::string(buffer, buffer_size);
183        }
184
185        DestroyBuffer(&buffer);
186        switch (retC) {
187                case NSCAPI::returnIgnored:
188                        CORE_LOG_MESSAGE_STD(_T("No handler for command '") + command + _T("'."));
189                        break;
190                case NSCAPI::returnOK:
191                case NSCAPI::returnCRIT:
192                case NSCAPI::returnWARN:
193                case NSCAPI::returnUNKNOWN:
194                        break;
195                default:
196                        throw nscapi::nscapi_exception(_T("Unknown return code when injecting: ") + std::wstring(command));
197        }
198        return retC;
199}
200
201/**
202 * A wrapper around the InjetCommand that is simpler to use.
203 * Parses a string by splitting and makes the array and also manages return buffers and such.
204 * @param command The command to execute
205 * @param buffer The buffer to split
206 * @param spliwchar_t The char to use as splitter
207 * @param message The return message buffer
208 * @param perf The return performance data buffer
209 * @return The result of the command
210 */
211NSCAPI::nagiosReturn nscapi::core_wrapper::InjectSplitAndCommand(const wchar_t* command, wchar_t* buffer, wchar_t splitChar, std::wstring & message, std::wstring & perf)
212{
213        if (!fNSAPIInject)
214                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
215
216        std::wstring args = std::wstring(buffer);
217        boost::tokenizer<boost::escaped_list_separator<wchar_t>, std::wstring::const_iterator, std::wstring > tok(args, boost::escaped_list_separator<wchar_t>(L'\\', splitChar, L'\"'));
218        std::list<std::wstring> arglist;
219        BOOST_FOREACH(std::wstring s, tok)
220                arglist.push_back(s);
221        return InjectSimpleCommand(command, arglist, message, perf);
222}
223/**
224 * A wrapper around the InjetCommand that is simpler to use.
225 * @param command The command to execute
226 * @param buffer The buffer to split
227 * @param spliwchar_t The char to use as splitter
228 * @param message The return message buffer
229 * @param perf The return performance data buffer
230 * @return The result of the command
231 */
232NSCAPI::nagiosReturn nscapi::core_wrapper::InjectSplitAndCommand(const std::wstring command, const std::wstring buffer, wchar_t spliwchar_t, std::wstring & message, std::wstring & perf, bool escape) {
233        if (!fNSAPIInject)
234                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
235        boost::tokenizer<boost::escaped_list_separator<wchar_t>, std::wstring::const_iterator, std::wstring > tok(buffer, boost::escaped_list_separator<wchar_t>(L'\\', spliwchar_t, L'\"'));
236        std::list<std::wstring> arglist;
237        BOOST_FOREACH(std::wstring s, tok)
238                arglist.push_back(s);
239        return InjectSimpleCommand(command.c_str(), arglist, message, perf);
240}
241/**
242 * Ask the core to shutdown (only works when run as a service, o/w does nothing ?
243 * @todo Check if this might cause damage if not run as a service.
244 */
245void nscapi::core_wrapper::StopService(void) {
246        if (fNSAPIStopServer)
247                fNSAPIStopServer();
248}
249/**
250 * Close the program (usefull for tray/testmode) without stopping the service (unless this is the service).
251 * @author mickem
252 */
253void nscapi::core_wrapper::Exit(void) {
254        if (fNSAPIExit)
255                fNSAPIExit();
256}
257/**
258 * Retrieve a string from the settings subsystem (INI-file)
259 * Might possibly be located in the registry in the future.
260 *
261 * @param section Section key (generally module specific, make sure this is "unique")
262 * @param key The key to retrieve
263 * @param defaultValue A default value (if no value is set in the settings file)
264 * @return the current value or defaultValue if no value is set.
265 * @throws nscapi::nscapi_exception When core pointer set is unavailable or an error occurs.
266 */
267std::wstring nscapi::core_wrapper::getSettingsString(std::wstring section, std::wstring key, std::wstring defaultValue) {
268        if (!fNSAPIGetSettingsString)
269                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
270        unsigned int buf_len = getBufferLength();
271        wchar_t *buffer = new wchar_t[buf_len+1];
272        if (fNSAPIGetSettingsString(section.c_str(), key.c_str(), defaultValue.c_str(), buffer, buf_len) != NSCAPI::isSuccess) {
273                delete [] buffer;
274                throw nscapi::nscapi_exception(_T("Settings could not be retrieved."));
275        }
276        std::wstring ret = buffer;
277        delete [] buffer;
278        return ret;
279}
280/**
281 * Get a section of settings strings
282 * @param section The section to retrieve
283 * @return The keys in the section
284 */
285std::list<std::wstring> nscapi::core_wrapper::getSettingsSection(std::wstring section) {
286        if (!fNSAPIGetSettingsSection)
287                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
288        arrayBuffer::arrayBuffer aBuffer = NULL;
289        unsigned int argLen = 0;
290        if (fNSAPIGetSettingsSection(section.c_str(), &aBuffer, &argLen) != NSCAPI::isSuccess) {
291                throw nscapi::nscapi_exception(_T("Settings could not be retrieved."));
292        }
293        std::list<std::wstring> ret = arrayBuffer::arrayBuffer2list(argLen, aBuffer);
294        if (fNSAPIReleaseSettingsSectionBuffer(&aBuffer, &argLen) != NSCAPI::isSuccess) {
295                throw nscapi::nscapi_exception(_T("Settings could not be destroyed."));
296        }
297        if (aBuffer != NULL)
298                throw nscapi::nscapi_exception(_T("buffer is not null?."));
299        return ret;
300}
301/**
302 * Retrieve an int from the settings subsystem (INI-file)
303 * Might possibly be located in the registry in the future.
304 *
305 * @param section Section key (generally module specific, make sure this is "unique")
306 * @param key The key to retrieve
307 * @param defaultValue A default value (if no value is set in the settings file)
308 * @return the current value or defaultValue if no value is set.
309 * @throws nscapi::nscapi_exception When core pointer set is unavailable.
310 */
311int nscapi::core_wrapper::getSettingsInt(std::wstring section, std::wstring key, int defaultValue) {
312        if (!fNSAPIGetSettingsInt)
313                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
314        return fNSAPIGetSettingsInt(section.c_str(), key.c_str(), defaultValue);
315}
316void nscapi::core_wrapper::settings_register_key(std::wstring path, std::wstring key, NSCAPI::settings_type type, std::wstring title, std::wstring description, std::wstring defaultValue, bool advanced) {
317        if (!fNSAPISettingsRegKey)
318                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
319        fNSAPISettingsRegKey(path.c_str(), key.c_str(), type, title.c_str(), description.c_str(), defaultValue.c_str(), advanced);
320}
321void nscapi::core_wrapper::settings_register_path(std::wstring path, std::wstring title, std::wstring description, bool advanced) {
322        if (!fNSAPISettingsRegPath)
323                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
324        fNSAPISettingsRegPath(path.c_str(), title.c_str(), description.c_str(), advanced);
325}
326
327
328void nscapi::core_wrapper::settings_save() {
329        if (!fNSAPISettingsSave)
330                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
331        fNSAPISettingsSave();
332}
333
334/**
335 * Retrieve the application name (in human readable format) from the core.
336 * @return A string representing the application name.
337 * @throws nscapi::nscapi_exception When core pointer set is unavailable or an unexpected error occurs.
338 */
339std::wstring nscapi::core_wrapper::getApplicationName() {
340        if (!fNSAPIGetApplicationName)
341                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
342        unsigned int buf_len = getBufferLength();
343        wchar_t *buffer = new wchar_t[buf_len+1];
344        if (fNSAPIGetApplicationName(buffer, buf_len) != NSCAPI::isSuccess) {
345                delete [] buffer;
346                throw nscapi::nscapi_exception(_T("Application name could not be retrieved"));
347        }
348        std::wstring ret = buffer;
349        delete [] buffer;
350        return ret;
351}
352/**
353 * Retrieve the directory root of the application from the core.
354 * @return A string representing the base path.
355 * @throws nscapi::nscapi_exception When core pointer set is unavailable or an unexpected error occurs.
356 */
357std::wstring nscapi::core_wrapper::getBasePath() {
358        if (!fNSAPIGetBasePath)
359                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
360        unsigned int buf_len = getBufferLength();
361        wchar_t *buffer = new wchar_t[buf_len+1];
362        if (fNSAPIGetBasePath(buffer, buf_len) != NSCAPI::isSuccess) {
363                delete [] buffer;
364                throw nscapi::nscapi_exception(_T("Base path could not be retrieved"));
365        }
366        std::wstring ret = buffer;
367        delete [] buffer;
368        return ret;
369}
370
371unsigned int nscapi::core_wrapper::getBufferLength() {
372        static unsigned int len = 0;
373        if (len == 0) {
374                len = getSettingsInt(setting_keys::settings_def::PAYLOAD_LEN_PATH, setting_keys::settings_def::PAYLOAD_LEN, setting_keys::settings_def::PAYLOAD_LEN_DEFAULT);
375        }
376        return len;
377}
378
379
380bool nscapi::core_wrapper::logDebug() {
381        enum status {unknown, debug, nodebug };
382        static status d = unknown;
383        if (d == unknown) {
384                if (checkLogMessages(debug)== NSCAPI::istrue)
385                        d = debug;
386                else
387                        d = nodebug;
388        }
389        return (d == debug);
390}
391
392std::wstring nscapi::core_wrapper::Encrypt(std::wstring str, unsigned int algorithm) {
393        if (!fNSAPIEncrypt)
394                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
395        unsigned int len = 0;
396        // @todo investigate potential problems with static_cast<unsigned int>
397        fNSAPIEncrypt(algorithm, str.c_str(), static_cast<unsigned int>(str.size()), NULL, &len);
398        len+=2;
399        wchar_t *buf = new wchar_t[len+1];
400        NSCAPI::errorReturn ret = fNSAPIEncrypt(algorithm, str.c_str(), static_cast<unsigned int>(str.size()), buf, &len);
401        if (ret == NSCAPI::isSuccess) {
402                std::wstring ret = buf;
403                delete [] buf;
404                return ret;
405        }
406        return _T("");
407}
408std::wstring nscapi::core_wrapper::Decrypt(std::wstring str, unsigned int algorithm) {
409        if (!fNSAPIDecrypt)
410                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
411        unsigned int len = 0;
412        // @todo investigate potential problems with: static_cast<unsigned int>(str.size())
413        fNSAPIDecrypt(algorithm, str.c_str(), static_cast<unsigned int>(str.size()), NULL, &len);
414        len+=2;
415        wchar_t *buf = new wchar_t[len+1];
416        NSCAPI::errorReturn ret = fNSAPIDecrypt(algorithm, str.c_str(), static_cast<unsigned int>(str.size()), buf, &len);
417        if (ret == NSCAPI::isSuccess) {
418                std::wstring ret = buf;
419                delete [] buf;
420                return ret;
421        }
422        return _T("");
423}
424NSCAPI::errorReturn nscapi::core_wrapper::SetSettingsString(std::wstring section, std::wstring key, std::wstring value) {
425        if (!fNSAPISetSettingsString)
426                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
427        return fNSAPISetSettingsString(section.c_str(), key.c_str(), value.c_str());
428}
429NSCAPI::errorReturn nscapi::core_wrapper::SetSettingsInt(std::wstring section, std::wstring key, int value) {
430        if (!fNSAPISetSettingsInt)
431                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
432        return fNSAPISetSettingsInt(section.c_str(), key.c_str(), value);
433}
434NSCAPI::errorReturn nscapi::core_wrapper::WriteSettings(int type) {
435        if (!fNSAPIWriteSettings)
436                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
437        return fNSAPIWriteSettings(type);
438}
439NSCAPI::errorReturn nscapi::core_wrapper::ReadSettings(int type) {
440        if (!fNSAPIReadSettings)
441                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
442        return fNSAPIReadSettings(type);
443}
444NSCAPI::errorReturn nscapi::core_wrapper::Rehash(int flag) {
445        if (!fNSAPIRehash)
446                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
447        return fNSAPIRehash(flag);
448}
449nscapi::core_wrapper::plugin_info_list nscapi::core_wrapper::getPluginList() {
450        if (!fNSAPIGetPluginList || !fNSAPIReleasePluginList)
451                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
452        plugin_info_list ret;
453       
454       
455        int len = 0;
456        //NSCAPI::plugin_info_list **list2;
457        //NSCAPI::plugin_info_list *list[1];
458        NSCAPI::plugin_info *list[1];
459        //typedef NSCAPI::errorReturn (*lpNSAPIGetPluginList)(int *len, NSAPI_plugin_info** list);
460        //typedef NSCAPI::errorReturn (*lpNSAPIReleasePluginList)(int len, NSAPI_plugin_info** list);
461        NSCAPI::errorReturn err = fNSAPIGetPluginList(&len, list);
462        if (err != NSCAPI::isSuccess)
463                return ret;
464        for (int i=0;i<len;i++) {
465                plugin_info_type info;
466                info.description = (*list)[i].description;
467                info.name = (*list)[i].name;
468                info.dll = (*list)[i].dll;
469                ret.push_back(info);
470        }
471        fNSAPIReleasePluginList(len, list);
472        return ret;
473}
474
475std::list<std::wstring> nscapi::core_wrapper::getAllCommandNames() {
476        if (!fNSAPIGetAllCommandNames || !fNSAPIReleaseAllCommandNamessBuffer )
477                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
478        arrayBuffer::arrayBuffer aBuffer = NULL;
479        unsigned int argLen = 0;
480        if (fNSAPIGetAllCommandNames(&aBuffer, &argLen) != NSCAPI::isSuccess) {
481                throw nscapi::nscapi_exception(_T("Commands could not be retrieved."));
482        }
483        std::list<std::wstring> ret = arrayBuffer::arrayBuffer2list(argLen, aBuffer);
484        if (fNSAPIReleaseAllCommandNamessBuffer(&aBuffer, &argLen) != NSCAPI::isSuccess) {
485                throw nscapi::nscapi_exception(_T("Commands could not be destroyed."));
486        }
487        if (aBuffer != NULL)
488                throw nscapi::nscapi_exception(_T("buffer is not null?."));
489        return ret;
490}
491std::wstring nscapi::core_wrapper::describeCommand(std::wstring command) {
492        if (!fNSAPIDescribeCommand)
493                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
494        unsigned int buf_len = getBufferLength();
495        wchar_t *buffer = new wchar_t[buf_len+1];
496        if (fNSAPIDescribeCommand(command.c_str(), buffer, buf_len) != NSCAPI::isSuccess) {
497                delete [] buffer;
498                throw nscapi::nscapi_exception(_T("Base path could not be retrieved"));
499        }
500        std::wstring ret = buffer;
501        delete [] buffer;
502        return ret;
503}
504void nscapi::core_wrapper::registerCommand(std::wstring command, std::wstring description) {
505        if (!fNSAPIRegisterCommand)
506                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
507        fNSAPIRegisterCommand(id_, command.c_str(), description.c_str());
508}
509
510
511bool nscapi::core_wrapper::checkLogMessages(int type) {
512        if (!fNSAPICheckLogMessages)
513                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
514        return fNSAPICheckLogMessages(type) == NSCAPI::istrue;
515}
516/**
517 * Retrieve the application version as a string (in human readable format) from the core.
518 * @return A string representing the application version.
519 * @throws nscapi::nscapi_exception When core pointer set is unavailable.
520 */
521std::wstring nscapi::core_wrapper::getApplicationVersionString() {
522        if (!fNSAPIGetApplicationVersionStr)
523                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
524        unsigned int buf_len = getBufferLength();
525        wchar_t *buffer = new wchar_t[buf_len+1];
526        if (fNSAPIGetApplicationVersionStr(buffer, buf_len) != NSCAPI::isSuccess) {
527                delete [] buffer;
528                return _T("");
529        }
530        std::wstring ret = buffer;
531        delete [] buffer;
532        return ret;
533}
534
535/**
536 * Wrapper function around the ModuleHelperInit call.
537 * This wrapper retrieves all pointers and stores them for future use.
538 * @param f A function pointer to a function that can be used to load function from the core.
539 * @return NSCAPI::success or NSCAPI::failure
540 */
541bool nscapi::core_wrapper::load_endpoints(unsigned int id, nscapi::core_api::lpNSAPILoader f) {
542        id_ = id;
543        fNSAPIGetApplicationName = (nscapi::core_api::lpNSAPIGetApplicationName)f(_T("NSAPIGetApplicationName"));
544        fNSAPIGetApplicationVersionStr = (nscapi::core_api::lpNSAPIGetApplicationVersionStr)f(_T("NSAPIGetApplicationVersionStr"));
545        fNSAPIGetSettingsInt = (nscapi::core_api::lpNSAPIGetSettingsInt)f(_T("NSAPIGetSettingsInt"));
546        fNSAPIGetSettingsString = (nscapi::core_api::lpNSAPIGetSettingsString)f(_T("NSAPIGetSettingsString"));
547        fNSAPIGetSettingsSection = (nscapi::core_api::lpNSAPIGetSettingsSection)f(_T("NSAPIGetSettingsSection"));
548        fNSAPIReleaseSettingsSectionBuffer = (nscapi::core_api::lpNSAPIReleaseSettingsSectionBuffer)f(_T("NSAPIReleaseSettingsSectionBuffer"));
549        fNSAPIMessage = (nscapi::core_api::lpNSAPIMessage)f(_T("NSAPIMessage"));
550        fNSAPIStopServer = (nscapi::core_api::lpNSAPIStopServer)f(_T("NSAPIStopServer"));
551        //fNSAPIExit = (nscapi::core_api::lpNSAPIExit)f(_T("NSAPIExit"));
552        fNSAPIInject = (nscapi::core_api::lpNSAPIInject)f(_T("NSAPIInject"));
553        fNSAPIDestroyBuffer = (nscapi::core_api::lpNSAPIDestroyBuffer)f(_T("NSAPIDestroyBuffer"));
554        fNSAPINotify = (nscapi::core_api::lpNSAPINotify)f(_T("NSAPINotify"));
555        fNSAPIGetBasePath = (nscapi::core_api::lpNSAPIGetBasePath)f(_T("NSAPIGetBasePath"));
556        fNSAPICheckLogMessages = (nscapi::core_api::lpNSAPICheckLogMessages)f(_T("NSAPICheckLogMessages"));
557        fNSAPIDecrypt = (nscapi::core_api::lpNSAPIDecrypt)f(_T("NSAPIDecrypt"));
558        fNSAPIEncrypt = (nscapi::core_api::lpNSAPIEncrypt)f(_T("NSAPIEncrypt"));
559        fNSAPISetSettingsString = (nscapi::core_api::lpNSAPISetSettingsString)f(_T("NSAPISetSettingsString"));
560        fNSAPISetSettingsInt = (nscapi::core_api::lpNSAPISetSettingsInt)f(_T("NSAPISetSettingsInt"));
561        fNSAPIWriteSettings = (nscapi::core_api::lpNSAPIWriteSettings)f(_T("NSAPIWriteSettings"));
562        fNSAPIReadSettings = (nscapi::core_api::lpNSAPIReadSettings)f(_T("NSAPIReadSettings"));
563        fNSAPIRehash = (nscapi::core_api::lpNSAPIRehash)f(_T("NSAPIRehash"));
564
565        fNSAPIDescribeCommand = (nscapi::core_api::lpNSAPIDescribeCommand)f(_T("NSAPIDescribeCommand"));
566        fNSAPIGetAllCommandNames = (nscapi::core_api::lpNSAPIGetAllCommandNames)f(_T("NSAPIGetAllCommandNames"));
567        fNSAPIReleaseAllCommandNamessBuffer = (nscapi::core_api::lpNSAPIReleaseAllCommandNamessBuffer)f(_T("NSAPIReleaseAllCommandNamessBuffer"));
568        fNSAPIRegisterCommand = (nscapi::core_api::lpNSAPIRegisterCommand)f(_T("NSAPIRegisterCommand"));
569
570        fNSAPISettingsRegKey = (nscapi::core_api::lpNSAPISettingsRegKey)f(_T("NSAPISettingsRegKey"));
571        fNSAPISettingsRegPath = (nscapi::core_api::lpNSAPISettingsRegPath)f(_T("NSAPISettingsRegPath"));
572
573        fNSAPIGetPluginList = (nscapi::core_api::lpNSAPIGetPluginList)f(_T("NSAPIGetPluginList"));
574        fNSAPIReleasePluginList = (nscapi::core_api::lpNSAPIReleasePluginList)f(_T("NSAPIReleasePluginList"));
575
576        fNSAPISettingsSave = (nscapi::core_api::lpNSAPISettingsSave)f(_T("NSAPISettingsSave"));
577       
578        return true;
579}
Note: See TracBrowser for help on using the repository browser.