Minor changes
[public/netxms.git] / src / console / win32 / comm.cpp
CommitLineData
f7a7f8e4
VK
1/*
2** NetXMS - Network Management System
3** Windows Console
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: comm.cpp
21** Background communication functions
22**
23**/
24
25#include "stdafx.h"
26#include "nxcon.h"
27
28
fadbdf37
VK
29//
30// Constants
31//
32
33#define UI_THREAD_WAIT_TIME 300
34
35
f7a7f8e4
VK
36//
37// Request parameters structure
38//
39
40struct RqData
41{
42 HWND hWnd;
43 DWORD (* pFunc)(...);
44 DWORD dwNumParams;
45 void *pArg1;
46 void *pArg2;
fadbdf37 47 void *pArg3;
9f20696e
VK
48 void *pArg4;
49 void *pArg5;
50 void *pArg6;
f7a7f8e4
VK
51};
52
53
54//
55// Set status text in wait window
56//
57
58inline void SetInfoText(HWND hWnd, char *pszText)
59{
60 SendMessage(hWnd, WM_SET_INFO_TEXT, 0, (LPARAM)pszText);
61}
62
63
34870fc0
VK
64//
65// Check if MIB file is exist and up-to-date
66//
67
68static DWORD CheckMIBFile(char *pszName, BYTE *pHash)
69{
70 char szFileName[MAX_PATH];
71 BYTE currHash[MD5_DIGEST_SIZE];
72 BOOL bNeedUpdate = TRUE;
73 DWORD dwResult = RCC_SUCCESS;
74
75 // Build full file name
76 strcpy(szFileName, g_szWorkDir);
77 strcat(szFileName, WORKDIR_MIBCACHE);
78 strcat(szFileName, "\\");
79 strcat(szFileName, pszName);
80
81 // Check file hash
82 if (CalculateFileMD5Hash(szFileName, currHash))
83 bNeedUpdate = memcmp(currHash, pHash, MD5_DIGEST_SIZE);
84
85 // Download file from server if needed
86 if (bNeedUpdate)
87 {
88 strcpy(szFileName, g_szWorkDir);
89 strcat(szFileName, WORKDIR_MIBCACHE);
90 dwResult = NXCDownloadMIBFile(pszName, szFileName);
91 }
92 return dwResult;
93}
94
95
f7a7f8e4
VK
96//
97// Login thread
98//
99
100static DWORD WINAPI LoginThread(void *pArg)
101{
102 HWND hWnd = *((HWND *)pArg); // Handle to status window
103 DWORD dwResult;
104
105 dwResult = NXCConnect(g_szServer, g_szLogin, g_szPassword);
106 if (dwResult == RCC_SUCCESS)
107 {
108 // Now we are connected, request data sync
109 SetInfoText(hWnd, "Synchronizing objects...");
110 dwResult = NXCSyncObjects();
34870fc0
VK
111 }
112
113 if (dwResult == RCC_SUCCESS)
114 {
115 SetInfoText(hWnd, "Loading user database...");
116 dwResult = NXCLoadUserDB();
117 }
f7a7f8e4 118
34870fc0
VK
119 if (dwResult == RCC_SUCCESS)
120 {
121 NXC_MIB_LIST *pMibList;
122 DWORD i;
123
124 SetInfoText(hWnd, "Loading and initializing MIB files...");
125 dwResult = NXCGetMIBList(&pMibList);
f7a7f8e4
VK
126 if (dwResult == RCC_SUCCESS)
127 {
34870fc0
VK
128 for(i = 0; i < pMibList->dwNumFiles; i++)
129 if ((dwResult = CheckMIBFile(pMibList->ppszName[i], pMibList->ppHash[i])) != RCC_SUCCESS)
130 break;
131 NXCDestroyMIBList(pMibList);
132 if (dwResult == RCC_SUCCESS)
133 CreateMIBTree();
f7a7f8e4 134 }
f7a7f8e4
VK
135 }
136
3421c063
VK
137 if (dwResult == RCC_SUCCESS)
138 {
139 SetInfoText(hWnd, "Loading event information...");
140 dwResult = NXCLoadEventNames();
141 }
142
f70fc197
VK
143 if (dwResult == RCC_SUCCESS)
144 {
145 char szCacheDir[MAX_PATH];
146
147 SetInfoText(hWnd, "Synchronizing images...");
148 strcpy(szCacheDir, g_szWorkDir);
149 strcat(szCacheDir, WORKDIR_IMAGECACHE);
150 dwResult = NXCSyncImages(&g_pSrvImageList, szCacheDir);
cea97623
VK
151 if (dwResult == RCC_SUCCESS)
152 CreateObjectImageList();
f70fc197
VK
153 }
154
34870fc0
VK
155 // Disconnect if some of post-login operations was failed
156 if (dwResult != RCC_SUCCESS)
157 NXCDisconnect();
158
f7a7f8e4
VK
159 PostMessage(hWnd, WM_REQUEST_COMPLETED, 0, dwResult);
160 return dwResult;
161}
162
163
164//
165// Perform login
166//
167
168DWORD DoLogin(void)
169{
170 HANDLE hThread;
171 HWND hWnd = NULL;
172 DWORD dwThreadId, dwResult;
173
174 hThread = CreateThread(NULL, 0, LoginThread, &hWnd, CREATE_SUSPENDED, &dwThreadId);
175 if (hThread != NULL)
176 {
177 CRequestProcessingDlg wndWaitDlg;
178
179 wndWaitDlg.m_phWnd = &hWnd;
180 wndWaitDlg.m_hThread = hThread;
181 wndWaitDlg.m_strInfoText = "Connecting to server...";
182 dwResult = (DWORD)wndWaitDlg.DoModal();
183 CloseHandle(hThread);
184 }
185 else
186 {
187 dwResult = RCC_SYSTEM_FAILURE;
188 }
189
190 return dwResult;
191}
192
193
194//
195// Login thread
196//
197
198static DWORD WINAPI RequestThread(void *pArg)
199{
200 RqData *pData = (RqData *)pArg;
201 DWORD dwResult;
202
203 switch(pData->dwNumParams)
204 {
205 case 0:
206 dwResult = pData->pFunc();
207 break;
208 case 1:
209 dwResult = pData->pFunc(pData->pArg1);
210 break;
fadbdf37
VK
211 case 2:
212 dwResult = pData->pFunc(pData->pArg1, pData->pArg2);
213 break;
214 case 3:
215 dwResult = pData->pFunc(pData->pArg1, pData->pArg2, pData->pArg3);
216 break;
9f20696e
VK
217 case 6:
218 dwResult = pData->pFunc(pData->pArg1, pData->pArg2, pData->pArg3, pData->pArg4, pData->pArg5, pData->pArg6);
219 break;
f7a7f8e4
VK
220 }
221 if (pData->hWnd != NULL)
222 PostMessage(pData->hWnd, WM_REQUEST_COMPLETED, 0, dwResult);
223 return dwResult;
224}
225
226
227//
fadbdf37 228// Perform request (common code)
f7a7f8e4
VK
229//
230
fadbdf37 231static DWORD ExecuteRequest(RqData *pData, char *pszInfoText)
f7a7f8e4
VK
232{
233 HANDLE hThread;
f7a7f8e4
VK
234 DWORD dwThreadId, dwResult;
235
fadbdf37 236 hThread = CreateThread(NULL, 0, RequestThread, pData, 0, &dwThreadId);
f7a7f8e4
VK
237 if (hThread != NULL)
238 {
239 CRequestProcessingDlg wndWaitDlg;
240
fadbdf37
VK
241 // Wait for request completion
242 if (WaitForSingleObject(hThread, UI_THREAD_WAIT_TIME) == WAIT_TIMEOUT)
f7a7f8e4
VK
243 {
244 // Thread still not finished, open status window
245 SuspendThread(hThread);
fadbdf37 246 wndWaitDlg.m_phWnd = &pData->hWnd;
f7a7f8e4
VK
247 wndWaitDlg.m_hThread = hThread;
248 wndWaitDlg.m_strInfoText = pszInfoText;
249 dwResult = (DWORD)wndWaitDlg.DoModal();
250 }
251 else
252 {
253 // Thread is finished, get it's exit code
254 if (!GetExitCodeThread(hThread, &dwResult))
255 dwResult = RCC_SYSTEM_FAILURE;
256 }
257 CloseHandle(hThread);
258 }
259 else
260 {
261 dwResult = RCC_SYSTEM_FAILURE;
262 }
263
264 return dwResult;
265}
266
267
fadbdf37
VK
268//
269// Perform generic request without parameters
270//
271
272DWORD DoRequest(DWORD (* pFunc)(void), char *pszInfoText)
273{
274 RqData rqData;
275
276 rqData.hWnd = NULL;
277 rqData.dwNumParams = 0;
278 rqData.pFunc = (DWORD (*)(...))pFunc;
279 return ExecuteRequest(&rqData, pszInfoText);
280}
281
282
f7a7f8e4
VK
283//
284// Perform request with 1 parameter
285//
286
287DWORD DoRequestArg1(void *pFunc, void *pArg1, char *pszInfoText)
288{
f7a7f8e4 289 RqData rqData;
f7a7f8e4
VK
290
291 rqData.hWnd = NULL;
292 rqData.dwNumParams = 1;
293 rqData.pArg1 = pArg1;
294 rqData.pFunc = (DWORD (*)(...))pFunc;
fadbdf37
VK
295 return ExecuteRequest(&rqData, pszInfoText);
296}
f7a7f8e4 297
f7a7f8e4 298
4925a756
VK
299//
300// Perform request with 2 parameters
301//
302
303DWORD DoRequestArg2(void *pFunc, void *pArg1, void *pArg2, char *pszInfoText)
304{
305 RqData rqData;
306
307 rqData.hWnd = NULL;
308 rqData.dwNumParams = 2;
309 rqData.pArg1 = pArg1;
310 rqData.pArg2 = pArg2;
311 rqData.pFunc = (DWORD (*)(...))pFunc;
312 return ExecuteRequest(&rqData, pszInfoText);
313}
314
315
fadbdf37
VK
316//
317// Perform request with 3 parameter
318//
319
320DWORD DoRequestArg3(void *pFunc, void *pArg1, void *pArg2, void *pArg3, char *pszInfoText)
321{
322 RqData rqData;
323
324 rqData.hWnd = NULL;
325 rqData.dwNumParams = 3;
326 rqData.pArg1 = pArg1;
327 rqData.pArg2 = pArg2;
328 rqData.pArg3 = pArg3;
329 rqData.pFunc = (DWORD (*)(...))pFunc;
330 return ExecuteRequest(&rqData, pszInfoText);
f7a7f8e4 331}
9f20696e
VK
332
333
334//
335// Perform request with 6 parameter
336//
337
338DWORD DoRequestArg6(void *pFunc, void *pArg1, void *pArg2, void *pArg3, void *pArg4,
339 void *pArg5, void *pArg6, char *pszInfoText)
340{
341 RqData rqData;
342
343 rqData.hWnd = NULL;
344 rqData.dwNumParams = 6;
345 rqData.pArg1 = pArg1;
346 rqData.pArg2 = pArg2;
347 rqData.pArg3 = pArg3;
348 rqData.pArg4 = pArg4;
349 rqData.pArg5 = pArg5;
350 rqData.pArg6 = pArg6;
351 rqData.pFunc = (DWORD (*)(...))pFunc;
352 return ExecuteRequest(&rqData, pszInfoText);
353}