Implemented object caching on client side
[public/netxms.git] / src / libnxcl / libnxcl.h
CommitLineData
aaa065f8
VK
1/*
2** NetXMS - Network Management System
3** Client Library
4** Copyright (C) 2004 Victor Kirhenshtein
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 Free Software
18** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19**
20** $module: libnxcl.h
21**
22**/
23
24#ifndef _libnxcl_h_
25#define _libnxcl_h_
26
aaa065f8 27#include <nms_common.h>
138d23e1
VK
28#include <nxclapi.h>
29#include <nms_util.h>
30#include <nms_threads.h>
8d42f81f 31#include <nxcscpapi.h>
840eb902 32#include <stdio.h>
dd9c0eb1 33#include <string.h>
4507063f 34#include <stdarg.h>
138d23e1
VK
35
36
abda5c1a
VK
37//
38// Constants
39//
40
bfa29136
VK
41#define OBJECT_CACHE_MAGIC 0x7080ABCD
42
43#define LIBNXCL_VERSION 2
dd9c0eb1 44
abda5c1a
VK
45#define MAX_SERVER_NAME 64
46#define MAX_LOGIN_NAME 64
47#define MAX_PASSWORD_LEN 64
48
b40371d4 49#define VALIDATE_STRING(s) (((s) == NULL) ? _T("") : (s))
8f7e2482 50
abda5c1a 51
cce198f5
VK
52//
53// Session flags
54//
55
56#define NXC_SF_USERDB_LOADED 0x0001
57#define NXC_SF_SYNC_FINISHED 0x0002
bfa29136 58#define NXC_SF_HAS_OBJECT_CACHE 0x0004
cce198f5
VK
59
60
27134fb6
VK
61//
62// Index structure
63//
64
65typedef struct
66{
67 DWORD dwKey;
68 NXC_OBJECT *pObject;
69} INDEX;
70
71
bfa29136
VK
72//
73// Object cache file header structure
74//
75
76typedef struct
77{
78 DWORD dwMagic;
79 DWORD dwStructSize; // sizeof(NXC_OBJECT)
80 DWORD dwTimeStamp;
81 DWORD dwNumObjects;
82} OBJECT_CACHE_HEADER;
83
84
41213c24
VK
85//
86// Session class
87//
88
89class NXCL_Session
90{
91 friend THREAD_RESULT THREAD_CALL NetReceiver(NXCL_Session *pSession);
92
93 // Attributes
94private:
cce198f5 95 DWORD m_dwFlags;
41213c24
VK
96 DWORD m_dwMsgId;
97 DWORD m_dwState;
bfa29136 98 DWORD m_dwTimeStamp; // Last known server's timestamp
41213c24
VK
99 DWORD m_dwNumObjects;
100 INDEX *m_pIndexById;
101 MUTEX m_mutexIndexAccess;
102 SOCKET m_hSocket;
103 MsgWaitQueue m_msgWaitQueue;
104 DWORD m_dwReceiverBufferSize;
cce198f5 105 NXC_DCI_LIST *m_pItemList;
a1cb335b 106 THREAD m_hRecvThread;
cce198f5
VK
107
108 NXC_EVENT_TEMPLATE **m_ppEventTemplates;
109 DWORD m_dwNumTemplates;
110 MUTEX m_mutexEventAccess;
111
112 DWORD m_dwNumUsers;
113 NXC_USER *m_pUserList;
114
115#ifdef _WIN32
116 HANDLE m_condSyncOp;
117#else
118 pthread_cond_t m_condSyncOp;
119 pthread_mutex_t m_mutexSyncOp;
120#endif
121 DWORD m_dwSyncExitCode;
41213c24
VK
122
123public:
124 DWORD m_dwCommandTimeout;
125 NXC_EVENT_HANDLER m_pEventHandler;
126
127 // Methods
128private:
129 void DestroyAllObjects(void);
cce198f5
VK
130 void ProcessDCI(CSCPMessage *pMsg);
131 void DestroyEventDB(void);
132 void DestroyUserDB(void);
133 void ProcessUserDBRecord(CSCPMessage *pMsg);
134 void ProcessUserDBUpdate(CSCPMessage *pMsg);
135
136 void ProcessObjectUpdate(CSCPMessage *pMsg);
137 void AddObject(NXC_OBJECT *pObject, BOOL bSortIndex);
bfa29136 138 void LoadObjectsFromCache(TCHAR *pszCacheFile);
41213c24
VK
139
140public:
141 NXCL_Session();
142 ~NXCL_Session();
143
144 void Attach(SOCKET hSocket) { m_hSocket = hSocket; }
145 void Disconnect(void);
146
a1cb335b
VK
147 void SetRecvThread(THREAD hThread) { m_hRecvThread = hThread; }
148
41213c24
VK
149 BOOL SendMsg(CSCPMessage *pMsg);
150 CSCPMessage *WaitForMessage(WORD wCode, DWORD dwId, DWORD dwTimeOut = 0);
151 CSCP_MESSAGE *WaitForRawMessage(WORD wCode, DWORD dwId, DWORD dwTimeOut = 0);
152 DWORD WaitForRCC(DWORD dwRqId);
153 DWORD CreateRqId(void) { return m_dwMsgId++; }
257defd7 154 DWORD SendFile(DWORD dwRqId, TCHAR *pszFileName);
41213c24
VK
155
156 void CallEventHandler(DWORD dwEvent, DWORD dwCode, void *pArg);
cce198f5
VK
157
158 DWORD WaitForSync(DWORD dwTimeOut);
159 void PrepareForSync(void);
160 void CompleteSync(DWORD dwRetCode);
161
162 DWORD OpenNodeDCIList(DWORD dwNodeId, NXC_DCI_LIST **ppItemList);
163
164 DWORD LoadEventDB(void);
babb8280 165 void AddEventTemplate(NXC_EVENT_TEMPLATE *pEventTemplate, BOOL bLock);
cce198f5
VK
166 void DeleteEDBRecord(DWORD dwEventCode);
167 BOOL GetEventDB(NXC_EVENT_TEMPLATE ***pppTemplateList, DWORD *pdwNumRecords);
168 const TCHAR *GetEventName(DWORD dwId);
169 BOOL GetEventNameEx(DWORD dwId, TCHAR *pszBuffer, DWORD dwBufSize);
170 int GetEventSeverity(DWORD dwId);
171 BOOL GetEventText(DWORD dwId, TCHAR *pszBuffer, DWORD dwBufSize);
172
173 DWORD LoadUserDB(void);
174 BOOL GetUserDB(NXC_USER **ppUserList, DWORD *pdwNumUsers);
175 NXC_USER *FindUserById(DWORD dwId);
176
bfa29136 177 DWORD SyncObjects(TCHAR *pszCacheFile);
cce198f5
VK
178 void LockObjectIndex(void) { MutexLock(m_mutexIndexAccess, INFINITE); }
179 void UnlockObjectIndex(void) { MutexUnlock(m_mutexIndexAccess); }
9d3816ab 180 NXC_OBJECT *FindObjectById(DWORD dwId, BOOL bLock);
cce198f5
VK
181 NXC_OBJECT *FindObjectByName(TCHAR *pszName);
182 void EnumerateObjects(BOOL (* pHandler)(NXC_OBJECT *));
183 NXC_OBJECT *GetRootObject(DWORD dwId, DWORD dwIndex);
184 void *GetObjectIndex(DWORD *pdwNumObjects);
bfa29136
VK
185
186 void SetTimeStamp(DWORD dwTimeStamp) { m_dwTimeStamp = dwTimeStamp; }
187 DWORD GetTimeStamp(void) { return m_dwTimeStamp; }
41213c24
VK
188};
189
190inline void NXCL_Session::CallEventHandler(DWORD dwEvent, DWORD dwCode, void *pArg)
191{
192 if (m_pEventHandler != NULL)
193 m_pEventHandler(this, dwEvent, dwCode, pArg);
194}
195
196
abda5c1a
VK
197//
198// Functions
199//
200
41213c24 201void DestroyObject(NXC_OBJECT *pObject);
cce198f5 202void UpdateUserFromMessage(CSCPMessage *pMsg, NXC_USER *pUser);
37c4d6aa 203
41213c24 204void ProcessAlarmUpdate(NXCL_Session *pSession, CSCPMessage *pMsg);
41213c24
VK
205void ProcessEvent(NXCL_Session *pSession, CSCPMessage *pMsg, CSCP_MESSAGE *pRawMsg);
206void ProcessActionUpdate(NXCL_Session *pSession, CSCPMessage *pMsg);
cce198f5 207void ProcessEventDBRecord(NXCL_Session *pSession, CSCPMessage *pMsg);
0779b7d3 208void ProcessUserDBUpdate(CSCPMessage *pMsg);
cce198f5 209void ProcessDCI(NXCL_Session *pSession, CSCPMessage *pMsg);
39d7a7ed 210
b40371d4 211void DebugPrintf(TCHAR *szFormat, ...);
abda5c1a
VK
212
213
138d23e1
VK
214//
215// Global variables
216//
217
2e327c63 218extern NXC_DEBUG_CALLBACK g_pDebugCallBack;
138d23e1 219
aaa065f8
VK
220
221#endif /* _libnxcl_h_ */