5d79fa413c5b7c3bbed22eacce418ce4800baf7d
[public/netxms.git] / src / libnxcl / map.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** Client Library
4 ** Copyright (C) 2006-2009 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 ** File: map.cpp
21 **
22 **/
23
24 #include "libnxcl.h"
25 #include <netxms_maps.h>
26
27
28 //
29 // Get map list
30 //
31
32 DWORD LIBNXCL_EXPORTABLE NXCGetMapList(NXC_SESSION hSession, DWORD *pdwNumMaps,
33 NXC_MAP_INFO **ppMapList)
34 {
35 CSCPMessage msg, *pResponse;
36 DWORD i, dwRqId, dwResult, dwId;
37
38 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
39
40 msg.SetCode(CMD_GET_MAP_LIST);
41 msg.SetId(dwRqId);
42 ((NXCL_Session *)hSession)->SendMsg(&msg);
43
44 pResponse = ((NXCL_Session *)hSession)->WaitForMessage(CMD_REQUEST_COMPLETED, dwRqId);
45 if (pResponse != NULL)
46 {
47 dwResult = pResponse->GetVariableLong(VID_RCC);
48 if (dwResult == RCC_SUCCESS)
49 {
50 *pdwNumMaps = pResponse->GetVariableLong(VID_NUM_MAPS);
51 *ppMapList = (NXC_MAP_INFO *)malloc(sizeof(NXC_MAP_INFO) * (*pdwNumMaps));
52 for(i = 0, dwId = VID_MAP_LIST_BASE; i < *pdwNumMaps; i++)
53 {
54 (*ppMapList)[i].dwMapId = pResponse->GetVariableLong(dwId++);
55 (*ppMapList)[i].dwObjectId = pResponse->GetVariableLong(dwId++);
56 (*ppMapList)[i].dwAccess = pResponse->GetVariableLong(dwId++);
57 pResponse->GetVariableStr(dwId++, (*ppMapList)[i].szName, MAX_DB_STRING);
58 dwId += 6; // Reserved ids for future use
59 }
60 }
61 delete pResponse;
62 }
63 else
64 {
65 dwResult = RCC_TIMEOUT;
66 }
67 return dwResult;
68 }
69
70
71 //
72 // Save map
73 //
74
75 DWORD LIBNXCL_EXPORTABLE NXCSaveMap(NXC_SESSION hSession, void *pMap)
76 {
77 nxSubmap *pSubmap;
78 CSCPMessage msg;
79 DWORD i, dwRqId, dwResult;
80
81 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
82 ((nxMap *)pMap)->Lock();
83
84 msg.SetCode(CMD_SAVE_MAP);
85 msg.SetId(dwRqId);
86 ((nxMap *)pMap)->CreateMessage(&msg);
87 ((NXCL_Session *)hSession)->SendMsg(&msg);
88
89 // Wait for initial confirmation
90 dwResult = ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
91
92 // If OK, send all submaps to server
93 if (dwResult == RCC_SUCCESS)
94 {
95 msg.SetCode(CMD_SUBMAP_DATA);
96 for(i = 0; i < ((nxMap *)pMap)->GetSubmapCount(); i++)
97 {
98 msg.DeleteAllVariables();
99 pSubmap = ((nxMap *)pMap)->GetSubmapByIndex(i);
100 if (pSubmap != NULL)
101 pSubmap->CreateMessage(&msg);
102 if (i == ((nxMap *)pMap)->GetSubmapCount() - 1)
103 msg.SetEndOfSequence();
104 ((NXCL_Session *)hSession)->SendMsg(&msg);
105 }
106
107 // Wait for second confirmation
108 dwResult = ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
109 }
110
111 ((nxMap *)pMap)->Unlock();
112 return dwResult;
113 }
114
115
116 //
117 // Load map
118 //
119
120 DWORD LIBNXCL_EXPORTABLE NXCLoadMap(NXC_SESSION hSession, DWORD dwMapId, void **ppMap)
121 {
122 nxMap *pMap;
123 nxSubmap *pSubmap;
124 CSCPMessage msg, *pResponse;
125 DWORD dwRqId, dwResult, dwId;
126
127 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
128
129 msg.SetCode(CMD_LOAD_MAP);
130 msg.SetId(dwRqId);
131 msg.SetVariable(VID_MAP_ID, dwMapId);
132 ((NXCL_Session *)hSession)->SendMsg(&msg);
133
134 pResponse = ((NXCL_Session *)hSession)->WaitForMessage(CMD_REQUEST_COMPLETED, dwRqId);
135 if (pResponse != NULL)
136 {
137 dwResult = pResponse->GetVariableLong(VID_RCC);
138 if (dwResult == RCC_SUCCESS)
139 {
140 pMap = new nxMap(pResponse);
141 if (pResponse->GetVariableLong(VID_NUM_SUBMAPS) > 0)
142 {
143 do
144 {
145 delete pResponse;
146 pResponse = ((NXCL_Session *)hSession)->WaitForMessage(CMD_SUBMAP_DATA, dwRqId);
147 if (pResponse != NULL)
148 {
149 dwResult = pResponse->GetVariableLong(VID_RCC);
150 if (dwResult == RCC_SUCCESS)
151 {
152 dwId = pResponse->GetVariableLong(VID_OBJECT_ID);
153 pSubmap = pMap->GetSubmap(dwId);
154 pSubmap->ModifyFromMessage(pResponse);
155 }
156 }
157 else
158 {
159 dwResult = RCC_TIMEOUT;
160 break;
161 }
162 } while((dwResult == RCC_SUCCESS) && (!pResponse->IsEndOfSequence()));
163 if (dwResult == RCC_SUCCESS)
164 {
165 *ppMap = pMap;
166 }
167 else
168 {
169 delete pMap;
170 }
171 }
172 else
173 {
174 *ppMap = pMap;
175 }
176 }
177 delete pResponse;
178 }
179 else
180 {
181 dwResult = RCC_TIMEOUT;
182 }
183 return dwResult;
184 }
185
186
187 //
188 // Delete map
189 //
190
191 DWORD LIBNXCL_EXPORTABLE NXCDeleteMap(NXC_SESSION hSession, DWORD dwMapId)
192 {
193 CSCPMessage msg;
194 DWORD dwRqId;
195
196 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
197
198 msg.SetCode(CMD_DELETE_MAP);
199 msg.SetId(dwRqId);
200 msg.SetVariable(VID_MAP_ID, dwMapId);
201 ((NXCL_Session *)hSession)->SendMsg(&msg);
202
203 return ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
204 }
205
206
207 //
208 // Rename map
209 //
210
211 DWORD LIBNXCL_EXPORTABLE NXCRenameMap(NXC_SESSION hSession, DWORD dwMapId, const TCHAR *pszName)
212 {
213 CSCPMessage msg;
214 DWORD dwRqId;
215
216 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
217
218 msg.SetCode(CMD_RENAME_MAP);
219 msg.SetId(dwRqId);
220 msg.SetVariable(VID_MAP_ID, dwMapId);
221 msg.SetVariable(VID_NAME, pszName);
222 ((NXCL_Session *)hSession)->SendMsg(&msg);
223
224 return ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
225 }
226
227
228 //
229 // Resolve map name to id
230 //
231
232 DWORD LIBNXCL_EXPORTABLE NXCResolveMapName(NXC_SESSION hSession, TCHAR *pszMapName,
233 DWORD *pdwMapId)
234 {
235 CSCPMessage msg, *pResponse;
236 DWORD dwRqId, dwResult;
237
238 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
239
240 msg.SetCode(CMD_RESOLVE_MAP_NAME);
241 msg.SetId(dwRqId);
242 msg.SetVariable(VID_NAME, pszMapName);
243 ((NXCL_Session *)hSession)->SendMsg(&msg);
244
245 pResponse = ((NXCL_Session *)hSession)->WaitForMessage(CMD_REQUEST_COMPLETED, dwRqId);
246 if (pResponse != NULL)
247 {
248 dwResult = pResponse->GetVariableLong(VID_RCC);
249 if (dwResult == RCC_SUCCESS)
250 {
251 *pdwMapId = pResponse->GetVariableLong(VID_MAP_ID);
252 }
253 delete pResponse;
254 }
255 else
256 {
257 dwResult = RCC_TIMEOUT;
258 }
259 return dwResult;
260 }
261
262
263 //
264 // Upload background image to server
265 //
266
267 DWORD LIBNXCL_EXPORTABLE NXCUploadSubmapBkImage(NXC_SESSION hSession, DWORD dwMapId,
268 DWORD dwSubmapId, TCHAR *pszFile)
269 {
270 CSCPMessage msg;
271 DWORD dwRqId, dwResult;
272
273 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
274
275 msg.SetCode(CMD_UPLOAD_SUBMAP_BK_IMAGE);
276 msg.SetId(dwRqId);
277 msg.SetVariable(VID_MAP_ID, dwMapId);
278 msg.SetVariable(VID_OBJECT_ID, dwSubmapId);
279 ((NXCL_Session *)hSession)->SendMsg(&msg);
280
281 dwResult = ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
282 if (dwResult == RCC_SUCCESS)
283 {
284 dwResult = ((NXCL_Session *)hSession)->SendFile(dwRqId, pszFile);
285 if (dwResult == RCC_SUCCESS)
286 {
287 // Wait for final confirmation
288 dwResult = ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
289 }
290 }
291 return dwResult;
292 }
293
294
295 //
296 // Retrieve background image from server
297 //
298
299 DWORD LIBNXCL_EXPORTABLE NXCDownloadSubmapBkImage(NXC_SESSION hSession, DWORD dwMapId,
300 DWORD dwSubmapId, TCHAR *pszFile)
301 {
302 CSCPMessage msg;
303 DWORD dwRqId, dwResult;
304
305 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
306 dwResult = ((NXCL_Session *)hSession)->PrepareFileTransfer(pszFile, dwRqId);
307 if (dwResult == RCC_SUCCESS)
308 {
309 msg.SetCode(CMD_GET_SUBMAP_BK_IMAGE);
310 msg.SetId(dwRqId);
311 msg.SetVariable(VID_MAP_ID, dwMapId);
312 msg.SetVariable(VID_OBJECT_ID, dwSubmapId);
313 ((NXCL_Session *)hSession)->SendMsg(&msg);
314
315 // Loading file may take time, so timeout is 300 sec. instead of default
316 dwResult = ((NXCL_Session *)hSession)->WaitForFileTransfer(300000);
317 }
318 return dwResult;
319 }
320
321
322 //
323 // Create new map
324 //
325
326 DWORD LIBNXCL_EXPORTABLE NXCCreateMap(NXC_SESSION hSession, DWORD dwRootObj,
327 TCHAR *pszName, DWORD *pdwMapId)
328 {
329 CSCPMessage msg, *pResponse;
330 DWORD dwRqId, dwResult;
331
332 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
333
334 msg.SetCode(CMD_CREATE_MAP);
335 msg.SetId(dwRqId);
336 msg.SetVariable(VID_OBJECT_ID, dwRootObj);
337 msg.SetVariable(VID_NAME, pszName);
338 ((NXCL_Session *)hSession)->SendMsg(&msg);
339
340 pResponse = ((NXCL_Session *)hSession)->WaitForMessage(CMD_REQUEST_COMPLETED, dwRqId);
341 if (pResponse != NULL)
342 {
343 dwResult = pResponse->GetVariableLong(VID_RCC);
344 if (dwResult == RCC_SUCCESS)
345 {
346 *pdwMapId = pResponse->GetVariableLong(VID_MAP_ID);
347 }
348 delete pResponse;
349 }
350 else
351 {
352 dwResult = RCC_TIMEOUT;
353 }
354 return dwResult;
355 }