set thread names in server
[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 97 virtual ~Controller();
0cd0d4af
VK
98
99 virtual bool handleMessage(NXCPMessage *msg);
14149881
VK
100};
101
102/**
103 * Alarm comment
104 */
105class LIBNXCLIENT_EXPORTABLE AlarmComment
106{
107private:
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
115public:
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; }
bf6fb6c3
VK
125};
126
127/**
128 * Alarm controller
129 */
130class LIBNXCLIENT_EXPORTABLE AlarmController : public Controller
131{
14149881
VK
132private:
133 NXC_ALARM *createAlarmFromMessage(NXCPMessage *msg);
134
bf6fb6c3
VK
135public:
136 AlarmController(NXCSession *session) : Controller(session) { }
137
14149881
VK
138 UINT32 getAll(ObjectArray<NXC_ALARM> **alarms);
139
140 UINT32 acknowledge(UINT32 alarmId, bool sticky = false, UINT32 timeout = 0);
bf6fb6c3
VK
141 UINT32 resolve(UINT32 alarmId);
142 UINT32 terminate(UINT32 alarmId);
14149881
VK
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);
bf6fb6c3
VK
150};
151
152/**
153 * Data collection controller
154 */
155class LIBNXCLIENT_EXPORTABLE DataCollectionController : public Controller
156{
157public:
158 DataCollectionController(NXCSession *session) : Controller(session) { }
159
a63732ff 160 UINT32 pushData(ObjectArray<NXCPushData> *data, time_t timestamp = 0, UINT32 *failedIndex = NULL);
bf6fb6c3
VK
161};
162
163/**
14149881
VK
164 * Event template
165 */
166class LIBNXCLIENT_EXPORTABLE EventTemplate
167{
168private:
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
176public:
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/**
bf6fb6c3
VK
189 * Event controller
190 */
191class LIBNXCLIENT_EXPORTABLE EventController : public Controller
192{
14149881
VK
193private:
194 MUTEX m_eventTemplateLock;
195 ObjectArray<EventTemplate> *m_eventTemplates;
196
bf6fb6c3 197public:
14149881
VK
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);
bf6fb6c3
VK
206};
207
14149881
VK
208struct ObjectCacheEntry;
209
bf6fb6c3
VK
210/**
211 * Object controller
212 */
213class LIBNXCLIENT_EXPORTABLE ObjectController : public Controller
214{
14149881
VK
215private:
216 ObjectCacheEntry *m_cache;
217 MUTEX m_cacheLock;
218
0cd0d4af
VK
219 void addObject(AbstractObject *object);
220
bf6fb6c3 221public:
14149881
VK
222 ObjectController(NXCSession *session);
223 virtual ~ObjectController();
224
0cd0d4af
VK
225 virtual bool handleMessage(NXCPMessage *msg);
226
14149881
VK
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);
bf6fb6c3
VK
232
233 UINT32 manage(UINT32 objectId);
234 UINT32 unmanage(UINT32 objectId);
235};
236
237/**
14149881
VK
238 * Server controller
239 */
240class LIBNXCLIENT_EXPORTABLE ServerController : public Controller
241{
242public:
243 ServerController(NXCSession *session) : Controller(session) { }
244
245 UINT32 sendSMS(const TCHAR *recipient, const TCHAR *text);
246};
247
248/**
bf6fb6c3
VK
249 * Session
250 */
251class LIBNXCLIENT_EXPORTABLE NXCSession
252{
253private:
254 THREAD m_receiverThread;
255
256 static THREAD_RESULT THREAD_CALL receiverThreadStarter(void *arg);
257 void receiverThread();
258
259protected:
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];
e5390fb5 272 IntegerArray<UINT32> *m_protocolVersions;
bf6fb6c3
VK
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);
0cd0d4af 284 bool handleMessage(NXCPMessage *msg);
bf6fb6c3
VK
285
286public:
287 NXCSession();
288 virtual ~NXCSession();
289
e5390fb5
VK
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);
bf6fb6c3
VK
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; }
e5390fb5 305 UINT32 getProtocolVersion(int index) { return m_protocolVersions->get(index); }
bf6fb6c3
VK
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 */
316UINT32 LIBNXCLIENT_EXPORTABLE NXCGetVersion();
317const TCHAR LIBNXCLIENT_EXPORTABLE *NXCGetErrorText(UINT32 rcc);
318
319bool LIBNXCLIENT_EXPORTABLE NXCInitialize();
320void LIBNXCLIENT_EXPORTABLE NXCShutdown();
321void LIBNXCLIENT_EXPORTABLE NXCSetDebugCallback(NXC_DEBUG_CALLBACK cb);
322
323#endif /* _nxclient_h_ */