source: nscp/include/nscapi/nscapi_core_wrapper.cpp @ 2c95d22

0.4.00.4.10.4.2
Last change on this file since 2c95d22 was 2c95d22, checked in by Michael Medin <michael@…>, 22 months ago

2011-08-14 MickeM

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