change log updated
[public/netxms.git] / include / nxclient.h
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
46 #include "nxclobj.h"
47
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")
66 #define CONTROLLER_SERVER _T("SERVER")
67
68 /**
69 * DCI push data
70 */
71 struct 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 */
83 typedef void (* NXC_DEBUG_CALLBACK)(const TCHAR *msg);
84
85 class NXCSession;
86
87 /**
88 * Abstract controller class
89 */
90 class LIBNXCLIENT_EXPORTABLE Controller
91 {
92 protected:
93 NXCSession *m_session;
94
95 public:
96 Controller(NXCSession *session) { m_session = session; }
97 virtual ~Controller();
98
99 virtual bool handleMessage(NXCPMessage *msg);
100 };
101
102 /**
103 * Alarm comment
104 */
105 class LIBNXCLIENT_EXPORTABLE AlarmComment
106 {
107 private:
108 UINT32 m_id;
109 UINT32 m_alarmId;
110 UINT32 m_userId;
111 TCHAR *m_userName;
112 time_t m_timestamp;
113 TCHAR *m_text;
114
115 public:
116 AlarmComment(NXCPMessage *msg, UINT32 baseId);
117 ~AlarmComment();
118
119 UINT32 getId() { return m_id; }
120 UINT32 getAlarmId() { return m_alarmId; }
121 UINT32 getUserId() { return m_userId; }
122 const TCHAR *getUserName() { return m_userName; }
123 time_t getTimestamp() { return m_timestamp; }
124 const TCHAR *getText() { return m_text; }
125 };
126
127 /**
128 * Alarm controller
129 */
130 class LIBNXCLIENT_EXPORTABLE AlarmController : public Controller
131 {
132 private:
133 NXC_ALARM *createAlarmFromMessage(NXCPMessage *msg);
134
135 public:
136 AlarmController(NXCSession *session) : Controller(session) { }
137
138 UINT32 getAll(ObjectArray<NXC_ALARM> **alarms);
139
140 UINT32 acknowledge(UINT32 alarmId, bool sticky = false, UINT32 timeout = 0);
141 UINT32 resolve(UINT32 alarmId);
142 UINT32 terminate(UINT32 alarmId);
143 UINT32 openHelpdeskIssue(UINT32 alarmId, TCHAR *helpdeskRef);
144
145 UINT32 getComments(UINT32 alarmId, ObjectArray<AlarmComment> **comments);
146 UINT32 addComment(UINT32 alarmId, const TCHAR *text);
147 UINT32 updateComment(UINT32 alarmId, UINT32 commentId, const TCHAR *text);
148
149 TCHAR *formatAlarmText(NXC_ALARM *alarm, const TCHAR *format);
150 };
151
152 /**
153 * Data collection controller
154 */
155 class LIBNXCLIENT_EXPORTABLE DataCollectionController : public Controller
156 {
157 public:
158 DataCollectionController(NXCSession *session) : Controller(session) { }
159
160 UINT32 pushData(ObjectArray<NXCPushData> *data, time_t timestamp = 0, UINT32 *failedIndex = NULL);
161 };
162
163 /**
164 * Event template
165 */
166 class LIBNXCLIENT_EXPORTABLE EventTemplate
167 {
168 private:
169 UINT32 m_code;
170 TCHAR m_name[MAX_EVENT_NAME];
171 int m_severity;
172 UINT32 m_flags;
173 TCHAR *m_messageTemplate;
174 TCHAR *m_description;
175
176 public:
177 EventTemplate(NXCPMessage *msg);
178 ~EventTemplate();
179
180 UINT32 getCode() { return m_code; }
181 const TCHAR *getName() { return m_name; }
182 int getSeverity() { return m_severity; }
183 UINT32 getFlags() { return m_flags; }
184 const TCHAR *getMessageTemplate() { return CHECK_NULL_EX(m_messageTemplate); }
185 const TCHAR *getDescription() { return CHECK_NULL_EX(m_description); }
186 };
187
188 /**
189 * Event controller
190 */
191 class LIBNXCLIENT_EXPORTABLE EventController : public Controller
192 {
193 private:
194 MUTEX m_eventTemplateLock;
195 ObjectArray<EventTemplate> *m_eventTemplates;
196
197 public:
198 EventController(NXCSession *session);
199 virtual ~EventController();
200
201 UINT32 syncEventTemplates();
202 UINT32 getEventTemplates(ObjectArray<EventTemplate> *templates);
203 TCHAR *getEventName(UINT32 code, TCHAR *buffer, size_t bufferSize);
204
205 UINT32 sendEvent(UINT32 code, const TCHAR *name, UINT32 objectId, int argc, TCHAR **argv, const TCHAR *userTag);
206 };
207
208 struct ObjectCacheEntry;
209
210 /**
211 * Object controller
212 */
213 class LIBNXCLIENT_EXPORTABLE ObjectController : public Controller
214 {
215 private:
216 ObjectCacheEntry *m_cache;
217 MUTEX m_cacheLock;
218
219 void addObject(AbstractObject *object);
220
221 public:
222 ObjectController(NXCSession *session);
223 virtual ~ObjectController();
224
225 virtual bool handleMessage(NXCPMessage *msg);
226
227 UINT32 sync();
228 UINT32 syncObjectSet(UINT32 *idList, size_t length, bool syncComments, UINT16 flags);
229 UINT32 syncSingleObject(UINT32 id);
230
231 AbstractObject *findObjectById(UINT32 id);
232
233 UINT32 manage(UINT32 objectId);
234 UINT32 unmanage(UINT32 objectId);
235 };
236
237 /**
238 * Server controller
239 */
240 class LIBNXCLIENT_EXPORTABLE ServerController : public Controller
241 {
242 public:
243 ServerController(NXCSession *session) : Controller(session) { }
244
245 UINT32 sendSMS(const TCHAR *recipient, const TCHAR *text);
246 };
247
248 /**
249 * Session
250 */
251 class LIBNXCLIENT_EXPORTABLE NXCSession
252 {
253 private:
254 THREAD m_receiverThread;
255
256 static THREAD_RESULT THREAD_CALL receiverThreadStarter(void *arg);
257 void receiverThread();
258
259 protected:
260 // communications
261 bool m_connected;
262 bool m_disconnected;
263 VolatileCounter m_msgId;
264 SOCKET m_hSocket;
265 MsgWaitQueue *m_msgWaitQueue;
266 NXCPEncryptionContext *m_encryptionContext;
267 UINT32 m_commandTimeout;
268
269 // server information
270 BYTE m_serverId[8];
271 TCHAR m_serverVersion[64];
272 IntegerArray<UINT32> *m_protocolVersions;
273 TCHAR m_serverTimeZone[MAX_TZ_LEN];
274 UINT32 m_userId;
275 UINT64 m_systemRights;
276 bool m_passwordChangeNeeded;
277
278 // data
279 MUTEX m_dataLock;
280 MUTEX m_msgSendLock;
281 StringObjectMap<Controller> *m_controllers;
282
283 void onNotify(NXCPMessage *msg);
284 bool handleMessage(NXCPMessage *msg);
285
286 public:
287 NXCSession();
288 virtual ~NXCSession();
289
290 UINT32 connect(const TCHAR *host, const TCHAR *login, const TCHAR *password,
291 UINT32 flags = NXCF_DEFAULT, const TCHAR *clientInfo = NULL,
292 const UINT32 *cpvIndexList = NULL, size_t cpvIndexListSize = 0);
293 void disconnect();
294
295 UINT32 createMessageId() { return InterlockedIncrement(&m_msgId); }
296 bool sendMessage(NXCPMessage *msg);
297 NXCPMessage *waitForMessage(UINT16 code, UINT32 id, UINT32 timeout = 0);
298 UINT32 waitForRCC(UINT32 id, UINT32 timeout = 0);
299
300 void setCommandTimeout(UINT32 timeout) { m_commandTimeout = timeout; }
301 UINT32 getCommandTimeout() { return m_commandTimeout; }
302
303 const TCHAR *getServerVersion() { return m_serverVersion; }
304 const TCHAR *getServerTimeZone() { return m_serverTimeZone; }
305 UINT32 getProtocolVersion(int index) { return m_protocolVersions->get(index); }
306 UINT32 getUserId() { return m_userId; }
307 UINT64 getSystemRights() { return m_systemRights; }
308 bool isPasswordChangeNeeded() { return m_passwordChangeNeeded; }
309
310 Controller *getController(const TCHAR *name);
311 };
312
313 /**
314 * Functions
315 */
316 UINT32 LIBNXCLIENT_EXPORTABLE NXCGetVersion();
317 const TCHAR LIBNXCLIENT_EXPORTABLE *NXCGetErrorText(UINT32 rcc);
318
319 bool LIBNXCLIENT_EXPORTABLE NXCInitialize();
320 void LIBNXCLIENT_EXPORTABLE NXCShutdown();
321 void LIBNXCLIENT_EXPORTABLE NXCSetDebugCallback(NXC_DEBUG_CALLBACK cb);
322
323 #endif /* _nxclient_h_ */