all command line client tools converted to new client library; message wait queue...
[public/netxms.git] / include / nxclient.h
CommitLineData
bf6fb6c3
VK
1/*
2** NetXMS - Network Management System
3** Client Library API
4** Copyright (C) 2003-2014 Victor Kirhenshtein
5**
6** This program is free software; you can redistribute it and/or modify
7** it under the terms of the GNU Lesser General Public License as published by
8** the Free Software Foundation; either version 3 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 Lesser General Public License
17** along with this program; if not, write to the Free Software
18** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19**
20** File: nxclient.h
21**
22**/
23
24#ifndef _nxclient_h_
25#define _nxclient_h_
26
27#include <nms_common.h>
28#include <nms_util.h>
29#include <nxevent.h>
30#include <nxcpapi.h>
31#include <nxtools.h>
32#include <nxlog.h>
33#include <uuid.h>
34#include <nxcldefs.h>
35
36#ifdef _WIN32
37#ifdef LIBNXCLIENT_EXPORTS
38#define LIBNXCLIENT_EXPORTABLE __declspec(dllexport)
39#else
40#define LIBNXCLIENT_EXPORTABLE __declspec(dllimport)
41#endif
42#else /* _WIN32 */
43#define LIBNXCLIENT_EXPORTABLE
44#endif
45
14149881
VK
46#include "nxclobj.h"
47
bf6fb6c3
VK
48#define MAX_TZ_LEN 32
49
50/**
51 * NXCSession::connect flags
52 */
53#define NXCF_DEFAULT 0
54#define NXCF_ENCRYPT 0x0001
55#define NXCF_EXACT_VERSION_MATCH 0x0002
56#define NXCF_USE_CERTIFICATE 0x0004
57#define NXCF_IGNORE_PROTOCOL_VERSION 0x0008
58
59/**
60 * Standard controller names
61 */
62#define CONTROLLER_ALARMS _T("ALARMS")
63#define CONTROLLER_DATA_COLLECTION _T("DATACOLL")
64#define CONTROLLER_EVENTS _T("EVENTS")
65#define CONTROLLER_OBJECTS _T("OBJECTS")
14149881 66#define CONTROLLER_SERVER _T("SERVER")
bf6fb6c3
VK
67
68/**
69 * DCI push data
70 */
71struct NXCPushData
72{
73 UINT32 dciId; // DCI ID or 0 if name is used
74 TCHAR *dciName;
75 UINT32 nodeId; // Node ID or 0 if name is used
76 TCHAR *nodeName;
77 TCHAR *value;
78};
79
80/**
81 * Debug callback
82 */
83typedef void (* NXC_DEBUG_CALLBACK)(const TCHAR *msg);
84
85class NXCSession;
86
87/**
88 * Abstract controller class
89 */
90class LIBNXCLIENT_EXPORTABLE Controller
91{
92protected:
93 NXCSession *m_session;
94
95public:
96 Controller(NXCSession *session) { m_session = session; }
14149881
VK
97 virtual ~Controller();
98};
99
100/**
101 * Alarm comment
102 */
103class LIBNXCLIENT_EXPORTABLE AlarmComment
104{
105private:
106 UINT32 m_id;
107 UINT32 m_alarmId;
108 UINT32 m_userId;
109 TCHAR *m_userName;
110 time_t m_timestamp;
111 TCHAR *m_text;
112
113public:
114 AlarmComment(NXCPMessage *msg, UINT32 baseId);
115 ~AlarmComment();
116
117 UINT32 getId() { return m_id; }
118 UINT32 getAlarmId() { return m_alarmId; }
119 UINT32 getUserId() { return m_userId; }
120 const TCHAR *getUserName() { return m_userName; }
121 time_t getTimestamp() { return m_timestamp; }
122 const TCHAR *getText() { return m_text; }
bf6fb6c3
VK
123};
124
125/**
126 * Alarm controller
127 */
128class LIBNXCLIENT_EXPORTABLE AlarmController : public Controller
129{
14149881
VK
130private:
131 NXC_ALARM *createAlarmFromMessage(NXCPMessage *msg);
132
bf6fb6c3
VK
133public:
134 AlarmController(NXCSession *session) : Controller(session) { }
135
14149881
VK
136 UINT32 getAll(ObjectArray<NXC_ALARM> **alarms);
137
138 UINT32 acknowledge(UINT32 alarmId, bool sticky = false, UINT32 timeout = 0);
bf6fb6c3
VK
139 UINT32 resolve(UINT32 alarmId);
140 UINT32 terminate(UINT32 alarmId);
14149881
VK
141 UINT32 openHelpdeskIssue(UINT32 alarmId, TCHAR *helpdeskRef);
142
143 UINT32 getComments(UINT32 alarmId, ObjectArray<AlarmComment> **comments);
144 UINT32 addComment(UINT32 alarmId, const TCHAR *text);
145 UINT32 updateComment(UINT32 alarmId, UINT32 commentId, const TCHAR *text);
146
147 TCHAR *formatAlarmText(NXC_ALARM *alarm, const TCHAR *format);
bf6fb6c3
VK
148};
149
150/**
151 * Data collection controller
152 */
153class LIBNXCLIENT_EXPORTABLE DataCollectionController : public Controller
154{
155public:
156 DataCollectionController(NXCSession *session) : Controller(session) { }
157
158 UINT32 pushData(ObjectArray<NXCPushData> *data, UINT32 *failedIndex = NULL);
159};
160
14149881
VK
161/**
162 * Event template
163 */
164class LIBNXCLIENT_EXPORTABLE EventTemplate
165{
166private:
167 UINT32 m_code;
168 TCHAR m_name[MAX_EVENT_NAME];
169 int m_severity;
170 UINT32 m_flags;
171 TCHAR *m_messageTemplate;
172 TCHAR *m_description;
173
174public:
175 EventTemplate(NXCPMessage *msg);
176 ~EventTemplate();
177
178 UINT32 getCode() { return m_code; }
179 const TCHAR *getName() { return m_name; }
180 int getSeverity() { return m_severity; }
181 UINT32 getFlags() { return m_flags; }
182 const TCHAR *getMessageTemplate() { return CHECK_NULL_EX(m_messageTemplate); }
183 const TCHAR *getDescription() { return CHECK_NULL_EX(m_description); }
184};
185
bf6fb6c3
VK
186/**
187 * Event controller
188 */
189class LIBNXCLIENT_EXPORTABLE EventController : public Controller
190{
14149881
VK
191private:
192 MUTEX m_eventTemplateLock;
193 ObjectArray<EventTemplate> *m_eventTemplates;
194
bf6fb6c3 195public:
14149881
VK
196 EventController(NXCSession *session);
197 virtual ~EventController();
198
199 UINT32 syncEventTemplates();
200 UINT32 getEventTemplates(ObjectArray<EventTemplate> *templates);
201 TCHAR *getEventName(UINT32 code, TCHAR *buffer, size_t bufferSize);
202
203 UINT32 sendEvent(UINT32 code, const TCHAR *name, UINT32 objectId, int argc, TCHAR **argv, const TCHAR *userTag);
bf6fb6c3
VK
204};
205
14149881
VK
206struct ObjectCacheEntry;
207
bf6fb6c3
VK
208/**
209 * Object controller
210 */
211class LIBNXCLIENT_EXPORTABLE ObjectController : public Controller
212{
14149881
VK
213private:
214 ObjectCacheEntry *m_cache;
215 MUTEX m_cacheLock;
216
bf6fb6c3 217public:
14149881
VK
218 ObjectController(NXCSession *session);
219 virtual ~ObjectController();
220
221 UINT32 sync();
222 UINT32 syncObjectSet(UINT32 *idList, size_t length, bool syncComments, UINT16 flags);
223 UINT32 syncSingleObject(UINT32 id);
224
225 AbstractObject *findObjectById(UINT32 id);
bf6fb6c3
VK
226
227 UINT32 manage(UINT32 objectId);
228 UINT32 unmanage(UINT32 objectId);
229};
230
14149881
VK
231/**
232 * Server controller
233 */
234class LIBNXCLIENT_EXPORTABLE ServerController : public Controller
235{
236public:
237 ServerController(NXCSession *session) : Controller(session) { }
238
239 UINT32 sendSMS(const TCHAR *recipient, const TCHAR *text);
240};
241
bf6fb6c3
VK
242/**
243 * Session
244 */
245class LIBNXCLIENT_EXPORTABLE NXCSession
246{
247private:
248 THREAD m_receiverThread;
249
250 static THREAD_RESULT THREAD_CALL receiverThreadStarter(void *arg);
251 void receiverThread();
252
253protected:
254 // communications
255 bool m_connected;
256 bool m_disconnected;
257 VolatileCounter m_msgId;
258 SOCKET m_hSocket;
259 MsgWaitQueue *m_msgWaitQueue;
260 NXCPEncryptionContext *m_encryptionContext;
261 UINT32 m_commandTimeout;
262
263 // server information
264 BYTE m_serverId[8];
265 TCHAR m_serverVersion[64];
266 TCHAR m_serverTimeZone[MAX_TZ_LEN];
267 UINT32 m_userId;
268 UINT64 m_systemRights;
269 bool m_passwordChangeNeeded;
270
271 // data
272 MUTEX m_dataLock;
273 MUTEX m_msgSendLock;
274 StringObjectMap<Controller> *m_controllers;
275
276 void onNotify(NXCPMessage *msg);
277
278public:
279 NXCSession();
280 virtual ~NXCSession();
281
282 UINT32 connect(const TCHAR *host, const TCHAR *login, const TCHAR *password, UINT32 flags = NXCF_DEFAULT, const TCHAR *clientInfo = NULL);
283 void disconnect();
284
285 UINT32 createMessageId() { return InterlockedIncrement(&m_msgId); }
286 bool sendMessage(NXCPMessage *msg);
287 NXCPMessage *waitForMessage(UINT16 code, UINT32 id, UINT32 timeout = 0);
288 UINT32 waitForRCC(UINT32 id, UINT32 timeout = 0);
289
290 void setCommandTimeout(UINT32 timeout) { m_commandTimeout = timeout; }
291 UINT32 getCommandTimeout() { return m_commandTimeout; }
292
293 const TCHAR *getServerVersion() { return m_serverVersion; }
294 const TCHAR *getServerTimeZone() { return m_serverTimeZone; }
295 UINT32 getUserId() { return m_userId; }
296 UINT64 getSystemRights() { return m_systemRights; }
297 bool isPasswordChangeNeeded() { return m_passwordChangeNeeded; }
298
299 Controller *getController(const TCHAR *name);
300};
301
302/**
303 * Functions
304 */
305UINT32 LIBNXCLIENT_EXPORTABLE NXCGetVersion();
306const TCHAR LIBNXCLIENT_EXPORTABLE *NXCGetErrorText(UINT32 rcc);
307
308bool LIBNXCLIENT_EXPORTABLE NXCInitialize();
309void LIBNXCLIENT_EXPORTABLE NXCShutdown();
310void LIBNXCLIENT_EXPORTABLE NXCSetDebugCallback(NXC_DEBUG_CALLBACK cb);
311
312#endif /* _nxclient_h_ */