- User delete implemented
[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;
f7a7f8e4
VK
48};
49
50
51//
52// Set status text in wait window
53//
54
55inline void SetInfoText(HWND hWnd, char *pszText)
56{
57 SendMessage(hWnd, WM_SET_INFO_TEXT, 0, (LPARAM)pszText);
58}
59
60
61//
62// Login thread
63//
64
65static DWORD WINAPI LoginThread(void *pArg)
66{
67 HWND hWnd = *((HWND *)pArg); // Handle to status window
68 DWORD dwResult;
69
70 dwResult = NXCConnect(g_szServer, g_szLogin, g_szPassword);
71 if (dwResult == RCC_SUCCESS)
72 {
73 // Now we are connected, request data sync
74 SetInfoText(hWnd, "Synchronizing objects...");
75 dwResult = NXCSyncObjects();
76
77 if (dwResult == RCC_SUCCESS)
78 {
79 SetInfoText(hWnd, "Loading user database...");
80 dwResult = NXCLoadUserDB();
81 }
82
83 // Disconnect if some of post-login operations was failed
84 if (dwResult != RCC_SUCCESS)
85 NXCDisconnect();
86 }
87
88 PostMessage(hWnd, WM_REQUEST_COMPLETED, 0, dwResult);
89 return dwResult;
90}
91
92
93//
94// Perform login
95//
96
97DWORD DoLogin(void)
98{
99 HANDLE hThread;
100 HWND hWnd = NULL;
101 DWORD dwThreadId, dwResult;
102
103 hThread = CreateThread(NULL, 0, LoginThread, &hWnd, CREATE_SUSPENDED, &dwThreadId);
104 if (hThread != NULL)
105 {
106 CRequestProcessingDlg wndWaitDlg;
107
108 wndWaitDlg.m_phWnd = &hWnd;
109 wndWaitDlg.m_hThread = hThread;
110 wndWaitDlg.m_strInfoText = "Connecting to server...";
111 dwResult = (DWORD)wndWaitDlg.DoModal();
112 CloseHandle(hThread);
113 }
114 else
115 {
116 dwResult = RCC_SYSTEM_FAILURE;
117 }
118
119 return dwResult;
120}
121
122
123//
124// Login thread
125//
126
127static DWORD WINAPI RequestThread(void *pArg)
128{
129 RqData *pData = (RqData *)pArg;
130 DWORD dwResult;
131
132 switch(pData->dwNumParams)
133 {
134 case 0:
135 dwResult = pData->pFunc();
136 break;
137 case 1:
138 dwResult = pData->pFunc(pData->pArg1);
139 break;
fadbdf37
VK
140 case 2:
141 dwResult = pData->pFunc(pData->pArg1, pData->pArg2);
142 break;
143 case 3:
144 dwResult = pData->pFunc(pData->pArg1, pData->pArg2, pData->pArg3);
145 break;
f7a7f8e4
VK
146 }
147 if (pData->hWnd != NULL)
148 PostMessage(pData->hWnd, WM_REQUEST_COMPLETED, 0, dwResult);
149 return dwResult;
150}
151
152
153//
fadbdf37 154// Perform request (common code)
f7a7f8e4
VK
155//
156
fadbdf37 157static DWORD ExecuteRequest(RqData *pData, char *pszInfoText)
f7a7f8e4
VK
158{
159 HANDLE hThread;
f7a7f8e4
VK
160 DWORD dwThreadId, dwResult;
161
fadbdf37 162 hThread = CreateThread(NULL, 0, RequestThread, pData, 0, &dwThreadId);
f7a7f8e4
VK
163 if (hThread != NULL)
164 {
165 CRequestProcessingDlg wndWaitDlg;
166
fadbdf37
VK
167 // Wait for request completion
168 if (WaitForSingleObject(hThread, UI_THREAD_WAIT_TIME) == WAIT_TIMEOUT)
f7a7f8e4
VK
169 {
170 // Thread still not finished, open status window
171 SuspendThread(hThread);
fadbdf37 172 wndWaitDlg.m_phWnd = &pData->hWnd;
f7a7f8e4
VK
173 wndWaitDlg.m_hThread = hThread;
174 wndWaitDlg.m_strInfoText = pszInfoText;
175 dwResult = (DWORD)wndWaitDlg.DoModal();
176 }
177 else
178 {
179 // Thread is finished, get it's exit code
180 if (!GetExitCodeThread(hThread, &dwResult))
181 dwResult = RCC_SYSTEM_FAILURE;
182 }
183 CloseHandle(hThread);
184 }
185 else
186 {
187 dwResult = RCC_SYSTEM_FAILURE;
188 }
189
190 return dwResult;
191}
192
193
fadbdf37
VK
194//
195// Perform generic request without parameters
196//
197
198DWORD DoRequest(DWORD (* pFunc)(void), char *pszInfoText)
199{
200 RqData rqData;
201
202 rqData.hWnd = NULL;
203 rqData.dwNumParams = 0;
204 rqData.pFunc = (DWORD (*)(...))pFunc;
205 return ExecuteRequest(&rqData, pszInfoText);
206}
207
208
f7a7f8e4
VK
209//
210// Perform request with 1 parameter
211//
212
213DWORD DoRequestArg1(void *pFunc, void *pArg1, char *pszInfoText)
214{
f7a7f8e4 215 RqData rqData;
f7a7f8e4
VK
216
217 rqData.hWnd = NULL;
218 rqData.dwNumParams = 1;
219 rqData.pArg1 = pArg1;
220 rqData.pFunc = (DWORD (*)(...))pFunc;
fadbdf37
VK
221 return ExecuteRequest(&rqData, pszInfoText);
222}
f7a7f8e4 223
f7a7f8e4 224
fadbdf37
VK
225//
226// Perform request with 3 parameter
227//
228
229DWORD DoRequestArg3(void *pFunc, void *pArg1, void *pArg2, void *pArg3, char *pszInfoText)
230{
231 RqData rqData;
232
233 rqData.hWnd = NULL;
234 rqData.dwNumParams = 3;
235 rqData.pArg1 = pArg1;
236 rqData.pArg2 = pArg2;
237 rqData.pArg3 = pArg3;
238 rqData.pFunc = (DWORD (*)(...))pFunc;
239 return ExecuteRequest(&rqData, pszInfoText);
f7a7f8e4 240}