|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 // ServerManager.cpp: implementation of the CServerManager class. |
|
18 // |
|
19 ////////////////////////////////////////////////////////////////////// |
|
20 |
|
21 #include "stdafx.h" |
|
22 #include "ServerManager.h" |
|
23 #include "ConnectionImpl.h" |
|
24 #include "resource.h" |
|
25 |
|
26 |
|
27 #ifdef _DEBUG |
|
28 extern BOOL gDoLogging; |
|
29 #endif |
|
30 |
|
31 #define LOG_SERVERMANAGER |
|
32 #if defined(LOG_SERVERMANAGER) && defined(_DEBUG) |
|
33 extern char TCDebugMsg[]; |
|
34 #define TCDEBUGOPEN() if (gDoLogging) { m_DebugLog->WaitForAccess(); } |
|
35 #define TCDEBUGLOGS(s) if (gDoLogging) { sprintf(TCDebugMsg,"%s", s); m_DebugLog->log(TCDebugMsg); } |
|
36 #define TCDEBUGLOGA1(s, a1) if (gDoLogging) { sprintf(TCDebugMsg, s, a1); m_DebugLog->log(TCDebugMsg); } |
|
37 #define TCDEBUGLOGA2(s, a1, a2) if (gDoLogging) { sprintf(TCDebugMsg, s, a1, a2); m_DebugLog->log(TCDebugMsg); } |
|
38 #define TCDEBUGLOGA3(s, a1, a2, a3) if (gDoLogging) { sprintf(TCDebugMsg, s, a1, a2, a3); m_DebugLog->log(TCDebugMsg); } |
|
39 #define TCDEBUGCLOSE() if (gDoLogging) { m_DebugLog->ReleaseAccess(); } |
|
40 #else |
|
41 #define TCDEBUGOPEN() |
|
42 #define TCDEBUGLOGS(s) |
|
43 #define TCDEBUGLOGA1(s, a1) |
|
44 #define TCDEBUGLOGA2(s, a1, a2) |
|
45 #define TCDEBUGLOGA3(s, a1, a2, a3) |
|
46 #define TCDEBUGCLOSE() |
|
47 #endif |
|
48 |
|
49 extern char* gServerLocation; |
|
50 ////////////////////////////////////////////////////////////////////// |
|
51 // Construction/Destruction |
|
52 ////////////////////////////////////////////////////////////////////// |
|
53 CServerManager::CServerManager() |
|
54 { |
|
55 m_DebugLog = NULL; |
|
56 m_Server = NULL; |
|
57 m_ConnectionList = NULL; |
|
58 m_ClientList = NULL; |
|
59 m_NextClientId = 0; |
|
60 m_NextConnectionId = 0; |
|
61 m_Version[0] = NULL; |
|
62 m_ExeLocation = NULL; |
|
63 m_ProtocolList = NULL; |
|
64 m_CommList = NULL; |
|
65 |
|
66 } |
|
67 CServerManager::CServerManager(const char* exeLocation) |
|
68 { |
|
69 #ifdef _DEBUG |
|
70 if (gDoLogging) |
|
71 m_DebugLog = new TCDebugLog("TCF_ServerLog", ::GetCurrentProcessId()); |
|
72 else |
|
73 m_DebugLog = NULL; |
|
74 #else |
|
75 m_DebugLog = NULL; |
|
76 #endif |
|
77 TCDEBUGOPEN(); |
|
78 TCDEBUGLOGS("CServerManager::CServerManager\n"); |
|
79 |
|
80 m_Server = new CServerCommand(); |
|
81 |
|
82 m_ConnectionList = new ConnectionList(); |
|
83 m_ConnectionList->clear(); |
|
84 |
|
85 m_ClientList = new ClientList(); |
|
86 m_ClientList->clear(); |
|
87 |
|
88 m_NextClientId = ::GetCurrentProcessId() * 100; |
|
89 m_NextConnectionId = (::GetCurrentProcessId() * 100) + 100; |
|
90 |
|
91 int ret = ::LoadString(::GetModuleHandle(NULL), IDS_VERSION, m_Version, MAX_VERSION_STRING); |
|
92 TCDEBUGLOGA1(" version=%s\n", m_Version); |
|
93 |
|
94 TCDEBUGLOGA1(" exeLocation=%s\n", exeLocation); |
|
95 m_ExeLocation = new char[MAX_EXEPATHNAME]; |
|
96 m_ExeLocation[0] = '\0'; |
|
97 strcpy(m_ExeLocation, exeLocation); |
|
98 TCDEBUGLOGA1(" m_ExeLocation=%s\n", m_ExeLocation); |
|
99 |
|
100 m_ProtocolList = new ProtocolRegistry(); |
|
101 m_ProtocolList->clear(); |
|
102 |
|
103 m_CommList = new CommRegistry(); |
|
104 m_CommList->clear(); |
|
105 |
|
106 TCDEBUGCLOSE(); |
|
107 } |
|
108 |
|
109 CServerManager::~CServerManager() |
|
110 { |
|
111 TCDEBUGOPEN(); |
|
112 TCDEBUGLOGS("CServerManager::~CServerManager\n"); |
|
113 |
|
114 if (m_Server) |
|
115 delete m_Server; |
|
116 |
|
117 if (m_ConnectionList) |
|
118 { |
|
119 m_ConnectionList->clear(); |
|
120 delete m_ConnectionList; |
|
121 } |
|
122 |
|
123 if (m_ClientList) |
|
124 { |
|
125 m_ClientList->clear(); |
|
126 delete m_ClientList; |
|
127 } |
|
128 |
|
129 if (m_ExeLocation) |
|
130 delete[] m_ExeLocation; |
|
131 |
|
132 if (m_ProtocolList) |
|
133 { |
|
134 m_ProtocolList->clear(); |
|
135 delete m_ProtocolList; |
|
136 } |
|
137 |
|
138 if (m_CommList) |
|
139 { |
|
140 m_CommList->clear(); |
|
141 delete m_CommList; |
|
142 } |
|
143 |
|
144 TCDEBUGCLOSE(); |
|
145 |
|
146 if (m_DebugLog) |
|
147 delete m_DebugLog; |
|
148 |
|
149 } |
|
150 void CServerManager::CommandThread() |
|
151 { |
|
152 TCDEBUGOPEN(); |
|
153 TCDEBUGLOGS("CServerManager::CommandThread\n"); |
|
154 TCDEBUGCLOSE(); |
|
155 |
|
156 bool done = false; |
|
157 eServerCommand command = eCmdNone; |
|
158 ServerCommandData cmdrsp; pServerCommandData pCmdrsp = &cmdrsp; |
|
159 pServerMessageData pMsg = new ServerMessageData(); |
|
160 |
|
161 TCDEBUGOPEN(); |
|
162 RegisterAllProtocols(); |
|
163 RegisterAllComms(); |
|
164 TCDEBUGCLOSE(); |
|
165 |
|
166 while(!done) |
|
167 { |
|
168 while(!m_Server->GetCommand(pCmdrsp, pMsg)) |
|
169 { |
|
170 Sleep(1); |
|
171 } |
|
172 command = pCmdrsp->command; |
|
173 |
|
174 switch(command) |
|
175 { |
|
176 case eCmdConnect: |
|
177 { |
|
178 TCDEBUGOPEN(); |
|
179 TCDEBUGLOGS(" eCmdConnect\n"); |
|
180 TCDEBUGCLOSE(); |
|
181 |
|
182 long ret = TCAPI_ERR_NONE; |
|
183 BOOL connCreated = FALSE; |
|
184 CConnectionImpl* conn = (CConnectionImpl*)FindConnection(&pCmdrsp->connectSettings); |
|
185 if (conn == NULL) |
|
186 { |
|
187 // create new one |
|
188 DWORD connId = m_NextConnectionId; |
|
189 conn = new CConnectionImpl(pCmdrsp->connectSettings, connId); |
|
190 |
|
191 TCDEBUGOPEN(); |
|
192 const char* commPath = FindCommPath(pCmdrsp->connectSettings.connectType); |
|
193 TCDEBUGCLOSE(); |
|
194 TCDEBUGOPEN(); |
|
195 const char* protPath = FindProtocolPath(pCmdrsp->connectSettings.decodeFormat); |
|
196 TCDEBUGCLOSE(); |
|
197 |
|
198 if (protPath == NULL || commPath == NULL) |
|
199 { |
|
200 ret = TCAPI_ERR_UNKNOWN_MEDIA_TYPE; |
|
201 } |
|
202 else |
|
203 { |
|
204 if (conn->CreateCommProtocols(commPath, protPath)) |
|
205 { |
|
206 connCreated = TRUE; |
|
207 } |
|
208 else |
|
209 { |
|
210 ret = TCAPI_ERR_UNKNOWN_MEDIA_TYPE; |
|
211 } |
|
212 } |
|
213 } |
|
214 if (ret == TCAPI_ERR_NONE && (conn->IsDisconnected() || conn->IsRetryTimedOut())) |
|
215 { |
|
216 ret = conn->DoConnect(); |
|
217 } |
|
218 if (ret == TCAPI_ERR_NONE) |
|
219 { |
|
220 DWORD id = m_NextClientId++; |
|
221 // create client |
|
222 CClient* client = new CClient(conn, pCmdrsp->clientOptions, id); |
|
223 // add client to connection's list |
|
224 conn->AddClient(client); |
|
225 // add client to total list |
|
226 m_ClientList->push_back(client); |
|
227 // add connection to connection list |
|
228 if (connCreated) |
|
229 m_ConnectionList->push_back(conn); |
|
230 |
|
231 m_NextConnectionId++; |
|
232 pCmdrsp->response = eRspOK; |
|
233 pCmdrsp->clientId = id; |
|
234 m_Server->SendResponse(pCmdrsp); |
|
235 } |
|
236 else |
|
237 { |
|
238 if (conn->m_OsError > 0) |
|
239 pCmdrsp->osError = conn->m_OsError; |
|
240 else |
|
241 pCmdrsp->osError = 0; |
|
242 if (connCreated) |
|
243 delete conn; |
|
244 pCmdrsp->response = eRspError; |
|
245 pCmdrsp->error = ret; |
|
246 m_Server->SendResponse(pCmdrsp); |
|
247 } |
|
248 } |
|
249 break; |
|
250 case eCmdDisconnect: |
|
251 { |
|
252 TCDEBUGOPEN(); |
|
253 TCDEBUGLOGS(" eCmdDisconnect\n"); |
|
254 TCDEBUGCLOSE(); |
|
255 DWORD id = pCmdrsp->clientId; |
|
256 // find this client in our list |
|
257 CClient* client = FindClient(id); |
|
258 if (client) |
|
259 { |
|
260 // get the connection for this client |
|
261 CConnectionImpl* conn = (CConnectionImpl*)client->GetConnection(); |
|
262 // pause the processing so we can delete the client |
|
263 conn->PauseProcessing(); |
|
264 // stop processing this client |
|
265 client->Stop(); |
|
266 // remove client from registry |
|
267 conn->RemoveClientFromRegistry(client); |
|
268 // remove from connections client list |
|
269 conn->RemoveClient(client); |
|
270 // remove from total client list |
|
271 RemoveClient(client); |
|
272 // delete client |
|
273 delete client; |
|
274 // no more clients on this connection, disconnect this connection |
|
275 if (conn->GetNumberClients() == 0) |
|
276 { |
|
277 conn->ExitProcessing(); |
|
278 conn->DoDisconnect(); |
|
279 RemoveConnection(conn); |
|
280 delete conn; |
|
281 } |
|
282 else |
|
283 { |
|
284 conn->RestartProcessing(); |
|
285 } |
|
286 } |
|
287 pCmdrsp->response = eRspOK; |
|
288 m_Server->SendResponse(pCmdrsp); |
|
289 } |
|
290 break; |
|
291 case eCmdSetMessageIds: |
|
292 { |
|
293 TCDEBUGOPEN(); |
|
294 TCDEBUGLOGS(" eCmdSetMessageIds\n"); |
|
295 TCDEBUGCLOSE(); |
|
296 |
|
297 long ret = TCAPI_ERR_NONE; |
|
298 DWORD id = pCmdrsp->clientId; |
|
299 bool restart = false; |
|
300 // find client |
|
301 CClient* client = FindClient(id); |
|
302 if (client) |
|
303 { |
|
304 // get connection |
|
305 CConnectionImpl* conn = (CConnectionImpl*)client->GetConnection(); |
|
306 if (conn->IsProcessingContinuing()) |
|
307 { |
|
308 conn->PauseProcessing(); |
|
309 restart = true; |
|
310 } |
|
311 if (conn->IsDisconnected()) |
|
312 { |
|
313 ret = TCAPI_ERR_MEDIA_NOT_OPEN; |
|
314 } |
|
315 else if (conn->IsRetryInProgress()) |
|
316 { |
|
317 ret = TCAPI_ERR_COMM_RETRY_IN_PROGRESS; |
|
318 } |
|
319 else if (conn->IsRetryTimedOut()) |
|
320 { |
|
321 ret = TCAPI_ERR_COMM_TIMEOUT; |
|
322 } |
|
323 |
|
324 if (ret == TCAPI_ERR_NONE) |
|
325 { |
|
326 // add client to connection's registry |
|
327 conn->AddClientToRegistry(client, pCmdrsp->number, pCmdrsp->messageIds); |
|
328 } |
|
329 if (restart) |
|
330 conn->RestartProcessing(); |
|
331 } |
|
332 if (ret == TCAPI_ERR_NONE) |
|
333 { |
|
334 pCmdrsp->response = eRspOK; |
|
335 } |
|
336 else |
|
337 { |
|
338 pCmdrsp->response = eRspError; |
|
339 pCmdrsp->error = ret; |
|
340 } |
|
341 m_Server->SendResponse(pCmdrsp); |
|
342 } |
|
343 break; |
|
344 case eCmdGetNumberConnections: |
|
345 { |
|
346 TCDEBUGOPEN(); |
|
347 TCDEBUGLOGS(" eCmdGetNumberConnections\n"); |
|
348 TCDEBUGCLOSE(); |
|
349 long num = m_ConnectionList->size(); |
|
350 pCmdrsp->response = eRspOK; |
|
351 pCmdrsp->numConnections = num; |
|
352 m_Server->SendResponse(pCmdrsp); |
|
353 } |
|
354 break; |
|
355 case eCmdGetConnectionType: |
|
356 { |
|
357 TCDEBUGOPEN(); |
|
358 TCDEBUGLOGS(" eCmdGetConnectionType\n"); |
|
359 TCDEBUGCLOSE(); |
|
360 long index = pCmdrsp->index; |
|
361 CConnectionImpl* pConn = (CConnectionImpl*)FindConnection(index); |
|
362 if (pConn != NULL) |
|
363 { |
|
364 pCmdrsp->response = eRspOK; |
|
365 strncpy(pCmdrsp->connectSettings.connectType, pConn->m_ConnectSettings->connectType, MAX_CONNECTION_TYPE); |
|
366 // pCmdrsp->connectSettings.connectType = pConn->m_ConnectSettings->connectType; |
|
367 m_Server->SendResponse(pCmdrsp); |
|
368 } |
|
369 else |
|
370 { |
|
371 pCmdrsp->response = eRspError; |
|
372 pCmdrsp->error = TCAPI_ERR_MISSING_CONNECTION_SPEC; |
|
373 m_Server->SendResponse(pCmdrsp); |
|
374 } |
|
375 } |
|
376 break; |
|
377 case eCmdOpenStream: |
|
378 { |
|
379 TCDEBUGOPEN(); |
|
380 TCDEBUGLOGS(" eCmdOpenStream\n"); |
|
381 TCDEBUGCLOSE(); |
|
382 |
|
383 long ret = TCAPI_ERR_NONE; |
|
384 // find client |
|
385 DWORD id = pCmdrsp->clientId; |
|
386 CClient* client = FindClient(id); |
|
387 if (client) |
|
388 { |
|
389 // get connection |
|
390 CConnectionImpl* conn = (CConnectionImpl*)client->GetConnection(); |
|
391 if (conn->IsDisconnected()) |
|
392 { |
|
393 ret = TCAPI_ERR_MEDIA_NOT_OPEN; |
|
394 } |
|
395 else if (conn->IsRetryInProgress()) |
|
396 { |
|
397 ret = TCAPI_ERR_COMM_RETRY_IN_PROGRESS; |
|
398 } |
|
399 else if (conn->IsRetryTimedOut()) |
|
400 { |
|
401 ret = TCAPI_ERR_COMM_TIMEOUT; |
|
402 } |
|
403 |
|
404 if (ret == TCAPI_ERR_NONE) |
|
405 { |
|
406 bool restart = false; |
|
407 if (conn->IsProcessingContinuing()) |
|
408 { |
|
409 restart = true; |
|
410 conn->PauseProcessing(); |
|
411 } |
|
412 client->OpenStream(&pCmdrsp->destinationOptions); |
|
413 if (restart) |
|
414 conn->RestartProcessing(); |
|
415 } |
|
416 } |
|
417 if (ret == TCAPI_ERR_NONE) |
|
418 { |
|
419 pCmdrsp->response = eRspOK; |
|
420 } |
|
421 else |
|
422 { |
|
423 pCmdrsp->response = eRspError; |
|
424 pCmdrsp->error = ret; |
|
425 } |
|
426 m_Server->SendResponse(pCmdrsp); |
|
427 } |
|
428 break; |
|
429 case eCmdCloseStream: |
|
430 { |
|
431 TCDEBUGOPEN(); |
|
432 TCDEBUGLOGS(" eCmdCloseStream\n"); |
|
433 TCDEBUGCLOSE(); |
|
434 |
|
435 long ret = TCAPI_ERR_NONE; |
|
436 DWORD id = pCmdrsp->clientId; |
|
437 bool restart = false; |
|
438 CClient* client = FindClient(id); |
|
439 if (client) |
|
440 { |
|
441 // get connection |
|
442 CConnectionImpl* conn = (CConnectionImpl*)client->GetConnection(); |
|
443 if (conn->IsConnected()) |
|
444 { |
|
445 if (!conn->IsRetryTimedOut() && !conn->IsRetryInProgress()) |
|
446 { |
|
447 if (conn->IsProcessingContinuing()) |
|
448 { |
|
449 conn->PauseProcessing(); |
|
450 restart = true; |
|
451 } |
|
452 } |
|
453 client->CloseStream(); |
|
454 if (!conn->IsRetryTimedOut() && !conn->IsRetryInProgress()) |
|
455 { |
|
456 if (restart) |
|
457 conn->RestartProcessing(); |
|
458 } |
|
459 |
|
460 } |
|
461 } |
|
462 if (ret == TCAPI_ERR_NONE) |
|
463 { |
|
464 pCmdrsp->response = eRspOK; |
|
465 } |
|
466 else |
|
467 { |
|
468 pCmdrsp->response = eRspError; |
|
469 pCmdrsp->error = ret; |
|
470 } |
|
471 m_Server->SendResponse(pCmdrsp); |
|
472 } |
|
473 break; |
|
474 case eCmdOpenMessageFile: |
|
475 { |
|
476 TCDEBUGOPEN(); |
|
477 TCDEBUGLOGS(" eCmdOpenMessageFile\n"); |
|
478 TCDEBUGCLOSE(); |
|
479 |
|
480 long ret = TCAPI_ERR_NONE; |
|
481 // find client |
|
482 DWORD id = pCmdrsp->clientId; |
|
483 CClient* client = FindClient(id); |
|
484 if (client) |
|
485 { |
|
486 // get connection |
|
487 CConnectionImpl* conn = (CConnectionImpl*)client->GetConnection(); |
|
488 if (conn->IsDisconnected()) |
|
489 { |
|
490 ret = TCAPI_ERR_MEDIA_NOT_OPEN; |
|
491 } |
|
492 else if (conn->IsRetryInProgress()) |
|
493 { |
|
494 ret = TCAPI_ERR_COMM_RETRY_IN_PROGRESS; |
|
495 } |
|
496 else if (conn->IsRetryTimedOut()) |
|
497 { |
|
498 ret = TCAPI_ERR_COMM_TIMEOUT; |
|
499 } |
|
500 |
|
501 if (ret == TCAPI_ERR_NONE) |
|
502 { |
|
503 bool restart = false; |
|
504 if (conn->IsProcessingContinuing()) |
|
505 { |
|
506 restart = true; |
|
507 conn->PauseProcessing(); |
|
508 } |
|
509 client->OpenMessageFile(&pCmdrsp->destinationOptions); |
|
510 if (restart) |
|
511 conn->RestartProcessing(); |
|
512 } |
|
513 } |
|
514 if (ret == TCAPI_ERR_NONE) |
|
515 { |
|
516 pCmdrsp->response = eRspOK; |
|
517 } |
|
518 else |
|
519 { |
|
520 pCmdrsp->response = eRspError; |
|
521 pCmdrsp->error = ret; |
|
522 } |
|
523 m_Server->SendResponse(pCmdrsp); |
|
524 } |
|
525 break; |
|
526 case eCmdCloseMessageFile: |
|
527 { |
|
528 TCDEBUGOPEN(); |
|
529 TCDEBUGLOGS(" eCmdCloseMessageFile\n"); |
|
530 TCDEBUGCLOSE(); |
|
531 |
|
532 long ret = TCAPI_ERR_NONE; |
|
533 DWORD id = pCmdrsp->clientId; |
|
534 bool restart = false; |
|
535 CClient* client = FindClient(id); |
|
536 if (client) |
|
537 { |
|
538 // get connection |
|
539 CConnectionImpl* conn = (CConnectionImpl*)client->GetConnection(); |
|
540 if (conn->IsConnected()) |
|
541 { |
|
542 if (!conn->IsRetryTimedOut() && !conn->IsRetryInProgress()) |
|
543 { |
|
544 if (conn->IsProcessingContinuing()) |
|
545 { |
|
546 conn->PauseProcessing(); |
|
547 restart = true; |
|
548 } |
|
549 } |
|
550 client->CloseMessageFile(); |
|
551 if (!conn->IsRetryTimedOut() && !conn->IsRetryInProgress()) |
|
552 { |
|
553 if (restart) |
|
554 conn->RestartProcessing(); |
|
555 } |
|
556 |
|
557 } |
|
558 } |
|
559 if (ret == TCAPI_ERR_NONE) |
|
560 { |
|
561 pCmdrsp->response = eRspOK; |
|
562 } |
|
563 else |
|
564 { |
|
565 pCmdrsp->response = eRspError; |
|
566 pCmdrsp->error = ret; |
|
567 } |
|
568 m_Server->SendResponse(pCmdrsp); |
|
569 } |
|
570 break; |
|
571 case eCmdClearMessageFile: |
|
572 { |
|
573 TCDEBUGOPEN(); |
|
574 TCDEBUGLOGS(" eCmdClearMessageFile\n"); |
|
575 TCDEBUGCLOSE(); |
|
576 |
|
577 long ret = TCAPI_ERR_NONE; |
|
578 DWORD id = pCmdrsp->clientId; |
|
579 bool restart = false; |
|
580 CClient* client = FindClient(id); |
|
581 if (client) |
|
582 { |
|
583 // get connection |
|
584 CConnectionImpl* conn = (CConnectionImpl*)client->GetConnection(); |
|
585 if (conn->IsConnected()) |
|
586 { |
|
587 if (!conn->IsRetryTimedOut() && !conn->IsRetryInProgress()) |
|
588 { |
|
589 if (conn->IsProcessingContinuing()) |
|
590 { |
|
591 conn->PauseProcessing(); |
|
592 restart = true; |
|
593 } |
|
594 } |
|
595 client->ClearMessageFile(); |
|
596 if (restart) |
|
597 conn->RestartProcessing(); |
|
598 } |
|
599 } |
|
600 if (ret == TCAPI_ERR_NONE) |
|
601 { |
|
602 pCmdrsp->response = eRspOK; |
|
603 } |
|
604 else |
|
605 { |
|
606 pCmdrsp->response = eRspError; |
|
607 pCmdrsp->error = ret; |
|
608 } |
|
609 m_Server->SendResponse(pCmdrsp); |
|
610 } |
|
611 break; |
|
612 case eCmdStart: |
|
613 { |
|
614 TCDEBUGOPEN(); |
|
615 TCDEBUGLOGS(" eCmdStart\n"); |
|
616 TCDEBUGCLOSE(); |
|
617 |
|
618 long ret = TCAPI_ERR_NONE; |
|
619 DWORD id = pCmdrsp->clientId; |
|
620 CClient* client = FindClient(id); |
|
621 if (client) |
|
622 { |
|
623 // get connection |
|
624 CConnectionImpl* conn = (CConnectionImpl*)client->GetConnection(); |
|
625 conn->PauseProcessing(); |
|
626 if (conn->IsDisconnected()) |
|
627 { |
|
628 ret = TCAPI_ERR_MEDIA_NOT_OPEN; |
|
629 } |
|
630 else if (conn->IsRetryInProgress()) |
|
631 { |
|
632 ret = TCAPI_ERR_COMM_RETRY_IN_PROGRESS; |
|
633 } |
|
634 else if (conn->IsRetryTimedOut()) |
|
635 { |
|
636 ret = TCAPI_ERR_COMM_TIMEOUT; |
|
637 } |
|
638 |
|
639 if (ret == TCAPI_ERR_NONE) |
|
640 { |
|
641 client->Start(); |
|
642 conn->RestartProcessing(); |
|
643 } |
|
644 } |
|
645 if (ret == TCAPI_ERR_NONE) |
|
646 { |
|
647 pCmdrsp->response = eRspOK; |
|
648 } |
|
649 else |
|
650 { |
|
651 pCmdrsp->response = eRspError; |
|
652 pCmdrsp->error = ret; |
|
653 } |
|
654 m_Server->SendResponse(pCmdrsp); |
|
655 } |
|
656 break; |
|
657 case eCmdStop: |
|
658 { |
|
659 TCDEBUGOPEN(); |
|
660 TCDEBUGLOGS(" eCmdStop\n"); |
|
661 TCDEBUGCLOSE(); |
|
662 |
|
663 long ret = TCAPI_ERR_NONE; |
|
664 DWORD id = pCmdrsp->clientId; |
|
665 bool restart = false; |
|
666 CClient* client = FindClient(id); |
|
667 if (client) |
|
668 { |
|
669 // get connection |
|
670 CConnectionImpl* conn = (CConnectionImpl*)client->GetConnection(); |
|
671 if (conn->IsConnected()) |
|
672 { |
|
673 if (!conn->IsRetryTimedOut() && !conn->IsRetryInProgress()) |
|
674 { |
|
675 if (conn->IsProcessingContinuing()) |
|
676 { |
|
677 restart = true; |
|
678 conn->PauseProcessing(); |
|
679 } |
|
680 } |
|
681 client->Stop(); |
|
682 if (!conn->IsRetryTimedOut() && !conn->IsRetryInProgress()) |
|
683 { |
|
684 if (restart) |
|
685 conn->RestartProcessing(); |
|
686 } |
|
687 |
|
688 } |
|
689 } |
|
690 if (ret == TCAPI_ERR_NONE) |
|
691 { |
|
692 pCmdrsp->response = eRspOK; |
|
693 } |
|
694 else |
|
695 { |
|
696 pCmdrsp->response = eRspError; |
|
697 pCmdrsp->error = ret; |
|
698 } |
|
699 m_Server->SendResponse(pCmdrsp); |
|
700 TCDEBUGOPEN(); |
|
701 TCDEBUGLOGS(" eCmdStop done\n"); |
|
702 TCDEBUGCLOSE(); |
|
703 } |
|
704 break; |
|
705 case eCmdSendMessage: |
|
706 { |
|
707 TCDEBUGOPEN(); |
|
708 TCDEBUGLOGS(" eCmdSendMessage\n"); |
|
709 TCDEBUGCLOSE(); |
|
710 |
|
711 long ret = TCAPI_ERR_NONE; |
|
712 DWORD osErr = 0; |
|
713 DWORD id = pCmdrsp->clientId; |
|
714 CClient* client = FindClient(id); |
|
715 if (client) |
|
716 { |
|
717 CConnectionImpl* conn = (CConnectionImpl*)client->GetConnection(); |
|
718 if (conn->IsDisconnected()) |
|
719 { |
|
720 ret = TCAPI_ERR_MEDIA_NOT_OPEN; |
|
721 } |
|
722 else if (conn->IsRetryInProgress()) |
|
723 { |
|
724 ret = TCAPI_ERR_COMM_RETRY_IN_PROGRESS; |
|
725 } |
|
726 else if (conn->IsRetryTimedOut()) |
|
727 { |
|
728 ret = TCAPI_ERR_COMM_TIMEOUT; |
|
729 } |
|
730 |
|
731 if (ret == TCAPI_ERR_NONE) |
|
732 { |
|
733 TCDEBUGOPEN(); |
|
734 TCDEBUGLOGS(" eCmdSendMessage DoSendMessage\n"); |
|
735 TCDEBUGCLOSE(); |
|
736 ret = conn->DoSendMessage(pCmdrsp->encodeOption, pCmdrsp->protocolVersion, pCmdrsp->useMyId, pCmdrsp->myId, pMsg->length, pMsg->message); |
|
737 // ret = conn->DoSendMessage(pMsg->length, pMsg->message); |
|
738 if (ret != TCAPI_ERR_NONE) |
|
739 osErr = conn->m_OsError; |
|
740 TCDEBUGOPEN(); |
|
741 TCDEBUGLOGS(" eCmdSendMessage DoSendMessage done\n"); |
|
742 TCDEBUGCLOSE(); |
|
743 } |
|
744 } |
|
745 if (ret == TCAPI_ERR_NONE) |
|
746 { |
|
747 // TCDEBUGOPEN(); |
|
748 // TCDEBUGLOGS(" eCmdSendMessage OK\n"); |
|
749 // TCDEBUGCLOSE(); |
|
750 pCmdrsp->response = eRspOK; |
|
751 } |
|
752 else |
|
753 { |
|
754 // TCDEBUGOPEN(); |
|
755 // TCDEBUGLOGS(" eCmdSendMessage ERROR\n"); |
|
756 // TCDEBUGCLOSE(); |
|
757 pCmdrsp->response = eRspError; |
|
758 pCmdrsp->error = ret; |
|
759 if (osErr > 0) |
|
760 pCmdrsp->osError = osErr; |
|
761 else |
|
762 pCmdrsp->osError = 0; |
|
763 } |
|
764 m_Server->SendResponse(pCmdrsp); |
|
765 } |
|
766 break; |
|
767 case eCmdExit: |
|
768 { |
|
769 TCDEBUGOPEN(); |
|
770 TCDEBUGLOGS(" eCmdExit\n"); |
|
771 TCDEBUGCLOSE(); |
|
772 DoShutdown(); |
|
773 done = true; |
|
774 } |
|
775 break; |
|
776 case eCmdGetNumberVersions: |
|
777 { |
|
778 TCDEBUGOPEN(); |
|
779 TCDEBUGLOGS(" eCmdGetNumberVersions\n"); |
|
780 TCDEBUGCLOSE(); |
|
781 |
|
782 long ret = TCAPI_ERR_NONE; |
|
783 long numberVersions = 1; // 1 for server |
|
784 DWORD id = pCmdrsp->clientId; |
|
785 CClient* client = FindClient(id); |
|
786 if (client) |
|
787 { |
|
788 CConnectionImpl* conn = (CConnectionImpl*)client->GetConnection(); |
|
789 if (conn->IsConnected()) |
|
790 { |
|
791 if (conn->HasVersion()) |
|
792 numberVersions++; |
|
793 } |
|
794 } |
|
795 if (ret == TCAPI_ERR_NONE) |
|
796 { |
|
797 pCmdrsp->response = eRspOK; |
|
798 pCmdrsp->number = numberVersions; |
|
799 } |
|
800 else |
|
801 { |
|
802 pCmdrsp->response = eRspError; |
|
803 pCmdrsp->error = ret; |
|
804 } |
|
805 m_Server->SendResponse(pCmdrsp); |
|
806 } |
|
807 break; |
|
808 case eCmdGetVersion: |
|
809 { |
|
810 TCDEBUGOPEN(); |
|
811 TCDEBUGLOGS(" eCmdGetVersion\n"); |
|
812 TCDEBUGCLOSE(); |
|
813 // index = 1 ==> TCFServer version |
|
814 // index = 2 ==> Connection version if it exists |
|
815 long ret = TCAPI_ERR_NONE; |
|
816 long index = pCmdrsp->index; |
|
817 pCmdrsp->response = eRspOK; |
|
818 pCmdrsp->version[0] = NULL; |
|
819 if (index == 1) |
|
820 { |
|
821 pCmdrsp->response = eRspOK; |
|
822 strncpy(pCmdrsp->version, m_Version, MAX_VERSION_STRING); |
|
823 } |
|
824 else if (index == 2) |
|
825 { |
|
826 DWORD id = pCmdrsp->clientId; |
|
827 CClient* client = FindClient(id); |
|
828 if (client) |
|
829 { |
|
830 CConnectionImpl* conn = (CConnectionImpl*)client->GetConnection(); |
|
831 if (conn->IsConnected()) |
|
832 { |
|
833 pCmdrsp->response = eRspOK; |
|
834 if (conn->HasVersion()) |
|
835 { |
|
836 conn->GetVersion(pCmdrsp->version); |
|
837 } |
|
838 } |
|
839 else |
|
840 { |
|
841 pCmdrsp->response = eRspError; |
|
842 pCmdrsp->error = TCAPI_ERR_MEDIA_NOT_OPEN; |
|
843 } |
|
844 } |
|
845 } |
|
846 m_Server->SendResponse(pCmdrsp); |
|
847 } |
|
848 break; |
|
849 case eCmdGetClientStatus: |
|
850 { |
|
851 TCDEBUGOPEN(); |
|
852 TCDEBUGLOGS(" eCmdGetClientStatus\n"); |
|
853 TCDEBUGCLOSE(); |
|
854 long ret = TCAPI_ERR_NONE; |
|
855 DWORD id = pCmdrsp->clientId; |
|
856 CClient* client = FindClient(id); |
|
857 pCmdrsp->response = eRspOK; |
|
858 pCmdrsp->clientStatus = eUnknownClient; |
|
859 if (client) |
|
860 { |
|
861 if (client->IsStarted()) |
|
862 { |
|
863 pCmdrsp->clientStatus = eStarted; |
|
864 } |
|
865 else |
|
866 { |
|
867 pCmdrsp->clientStatus = eStopped; |
|
868 } |
|
869 } |
|
870 m_Server->SendResponse(pCmdrsp); |
|
871 } |
|
872 break; |
|
873 default: |
|
874 { |
|
875 TCDEBUGOPEN(); |
|
876 TCDEBUGLOGA1(" unknown command = %d\n", command); |
|
877 TCDEBUGCLOSE(); |
|
878 pCmdrsp->response = eRspOK;//eRspError; |
|
879 pCmdrsp->error = TCAPI_ERR_FEATURE_NOT_IMPLEMENTED; |
|
880 m_Server->SendResponse(pCmdrsp); |
|
881 } |
|
882 break; |
|
883 } |
|
884 } |
|
885 |
|
886 pCmdrsp->response = eRspExitted; |
|
887 m_Server->SendResponse(&cmdrsp); |
|
888 |
|
889 if (pMsg) |
|
890 delete pMsg; |
|
891 |
|
892 TCDEBUGOPEN(); |
|
893 TCDEBUGLOGS("CServerManager::CommandThread exitting\n"); |
|
894 TCDEBUGCLOSE(); |
|
895 } |
|
896 |
|
897 CConnection* CServerManager::FindConnection(pConnectData pConData) |
|
898 { |
|
899 TCDEBUGLOGS("CServerManager::FindConnection1\n"); |
|
900 CConnection* connection = NULL; |
|
901 |
|
902 if (m_ConnectionList->size() != 0) |
|
903 { |
|
904 ConnectionList::iterator pConn; |
|
905 for (pConn = m_ConnectionList->begin(); pConn != m_ConnectionList->end(); pConn++) |
|
906 { |
|
907 if ((*pConn)->IsEqual(pConData)) |
|
908 { |
|
909 connection = *pConn; |
|
910 break; |
|
911 } |
|
912 } |
|
913 } |
|
914 |
|
915 TCDEBUGLOGA1("CServerManager::FindConnection1 connection=%x\n", connection); |
|
916 return connection; |
|
917 |
|
918 } |
|
919 CConnection* CServerManager::FindConnection(long index) |
|
920 { |
|
921 TCDEBUGLOGS("CServerManager::FindConnection2\n"); |
|
922 CConnection* connection = NULL; |
|
923 |
|
924 if (m_ConnectionList->size() >= index) |
|
925 { |
|
926 connection = m_ConnectionList->at(index); |
|
927 } |
|
928 |
|
929 TCDEBUGLOGA1("CServerManager::FindConnection2 connection=%x\n", connection); |
|
930 return connection; |
|
931 } |
|
932 CClient* CServerManager::FindClient(DWORD id) |
|
933 { |
|
934 CClient* found = NULL; |
|
935 |
|
936 if (m_ClientList->size() != 0) |
|
937 { |
|
938 ClientList::iterator iter; |
|
939 for (iter = m_ClientList->begin(); iter != m_ClientList->end(); iter++) |
|
940 { |
|
941 if ((*iter)->GetClientId() == id) |
|
942 { |
|
943 found = *iter; |
|
944 break; |
|
945 } |
|
946 } |
|
947 } |
|
948 return found; |
|
949 } |
|
950 |
|
951 BOOL CServerManager::RemoveClient(CClient* client) |
|
952 { |
|
953 BOOL found = FALSE; |
|
954 |
|
955 if (m_ClientList->size() != 0) |
|
956 { |
|
957 ClientList::iterator iter; |
|
958 for (iter = m_ClientList->begin(); iter != m_ClientList->end(); iter++) |
|
959 { |
|
960 if ((*iter)->GetClientId() == client->GetClientId()) |
|
961 { |
|
962 m_ClientList->erase(iter); |
|
963 found = TRUE; |
|
964 break; |
|
965 } |
|
966 } |
|
967 } |
|
968 return found; |
|
969 } |
|
970 |
|
971 BOOL CServerManager::RemoveConnection(CConnection* conn) |
|
972 { |
|
973 BOOL found = FALSE; |
|
974 |
|
975 if (m_ConnectionList->size() != 0) |
|
976 { |
|
977 ConnectionList::iterator iter; |
|
978 for (iter = m_ConnectionList->begin(); iter != m_ConnectionList->end(); iter++) |
|
979 { |
|
980 if ((*iter)->GetConnectionId() == conn->GetConnectionId()) |
|
981 { |
|
982 m_ConnectionList->erase(iter); |
|
983 found = TRUE; |
|
984 break; |
|
985 } |
|
986 } |
|
987 } |
|
988 return found; |
|
989 } |
|
990 |
|
991 void CServerManager::DoShutdown() |
|
992 { |
|
993 // for each connection |
|
994 // stop processing on that connection |
|
995 // disconnect that connection |
|
996 if (m_ConnectionList->size() != 0) |
|
997 { |
|
998 ConnectionList::iterator pConn; |
|
999 for (pConn = m_ConnectionList->begin(); pConn != m_ConnectionList->end(); pConn++) |
|
1000 { |
|
1001 (*pConn)->ExitProcessing(); |
|
1002 (*pConn)->DoDisconnect(); |
|
1003 delete (*pConn); |
|
1004 } |
|
1005 } |
|
1006 } |
|
1007 void CServerManager::RegisterAllComms() |
|
1008 { |
|
1009 TCDEBUGLOGS("CServerManager::RegisterAllComms\n"); |
|
1010 |
|
1011 m_CommList->clear(); |
|
1012 if (m_ExeLocation && (m_ExeLocation[0] != '\0')) |
|
1013 { |
|
1014 char* searchPath = new char[MAX_EXEPATHNAME]; |
|
1015 char* loadPath = new char[MAX_EXEPATHNAME]; |
|
1016 strncpy(searchPath, m_ExeLocation, MAX_EXEPATHNAME); |
|
1017 sprintf(searchPath, "%s%c*", searchPath, PATH_DELIMITER); |
|
1018 |
|
1019 TCDEBUGLOGA1("CServerManager::RegisterAllComms searchPath=%s\n", searchPath); |
|
1020 |
|
1021 WIN32_FIND_DATA fileData; |
|
1022 |
|
1023 HANDLE sh = ::FindFirstFile(searchPath, &fileData); |
|
1024 if (sh != INVALID_HANDLE_VALUE) |
|
1025 { |
|
1026 BOOL done = FALSE; |
|
1027 while (!done) |
|
1028 { |
|
1029 TCDEBUGLOGA1("CServerManager::RegisterAllComms file=%s\n", fileData.cFileName); |
|
1030 if (strncmp(fileData.cFileName, COMMDLL_BASENAME, strlen(COMMDLL_BASENAME)) == 0) |
|
1031 { |
|
1032 sprintf(loadPath, "%s%c%s", m_ExeLocation, PATH_DELIMITER, fileData.cFileName); |
|
1033 TCDEBUGLOGA1("CServerManager::RegisterAllComms loadPath=%s\n", loadPath); |
|
1034 |
|
1035 HINSTANCE hLib = ::LoadLibrary(loadPath); |
|
1036 TCDEBUGLOGA1("CServerManager::RegisterAllComms hLib=%x\n", hLib); |
|
1037 |
|
1038 if (hLib) |
|
1039 { |
|
1040 TCDEBUGLOGS("CServerManager::RegisterAllComms - library loaded\n"); |
|
1041 |
|
1042 COMMREGISTER lpFn = (COMMREGISTER)::GetProcAddress(hLib, COMMREGISTER_FNNAME); |
|
1043 TCDEBUGLOGA1("CServerManager::RegisterAllComms lpFn=%x\n", lpFn); |
|
1044 |
|
1045 if (lpFn) |
|
1046 { |
|
1047 TCDEBUGLOGS("CServerManager::RegisterAllComms - function found\n"); |
|
1048 const char* pType = lpFn(); |
|
1049 if (pType) |
|
1050 { |
|
1051 TCDEBUGLOGA1("CServerManager::RegisterAllComms pType=%s\n", pType); |
|
1052 CCommRegistryItem* pComm = new CCommRegistryItem(); |
|
1053 strcpy(pComm->m_CommLibrary, loadPath); |
|
1054 strcpy(pComm->m_CommType, pType); |
|
1055 m_CommList->push_back(pComm); |
|
1056 } |
|
1057 } |
|
1058 ::FreeLibrary(hLib); |
|
1059 } |
|
1060 } |
|
1061 BOOL fNext = ::FindNextFile(sh, &fileData); |
|
1062 if (fNext == FALSE) |
|
1063 done = TRUE; |
|
1064 } |
|
1065 ::FindClose(sh); |
|
1066 } |
|
1067 delete[] searchPath; |
|
1068 delete[] loadPath; |
|
1069 } |
|
1070 TCDEBUGLOGS("CServerManager::RegisterAllComms\n"); |
|
1071 } |
|
1072 |
|
1073 void CServerManager::RegisterAllProtocols() |
|
1074 { |
|
1075 TCDEBUGLOGS("CServerManager::RegisterAllProtocols\n"); |
|
1076 |
|
1077 m_ProtocolList->clear(); |
|
1078 if (m_ExeLocation && (m_ExeLocation[0] != '\0')) |
|
1079 { |
|
1080 char* searchPath = new char[MAX_EXEPATHNAME]; |
|
1081 char* loadPath = new char[MAX_EXEPATHNAME]; |
|
1082 strncpy(searchPath, m_ExeLocation, MAX_EXEPATHNAME); |
|
1083 sprintf(searchPath, "%s%c*", searchPath, PATH_DELIMITER); |
|
1084 |
|
1085 TCDEBUGLOGA1("CServerManager::RegisterAllProtocols searchPath=%s\n", searchPath); |
|
1086 |
|
1087 WIN32_FIND_DATA fileData; |
|
1088 |
|
1089 HANDLE sh = ::FindFirstFile(searchPath, &fileData); |
|
1090 BOOL done = FALSE; |
|
1091 |
|
1092 if (sh != INVALID_HANDLE_VALUE) |
|
1093 { |
|
1094 BOOL done = FALSE; |
|
1095 while (!done) |
|
1096 { |
|
1097 TCDEBUGLOGA1("CServerManager::RegisterAllProtocols file=%s\n", fileData.cFileName); |
|
1098 if (strncmp(fileData.cFileName, PROTOCOLDLL_BASENAME, strlen(PROTOCOLDLL_BASENAME)) == 0) |
|
1099 { |
|
1100 sprintf(loadPath, "%s%c%s", m_ExeLocation, PATH_DELIMITER, fileData.cFileName); |
|
1101 TCDEBUGLOGA1("CServerManager::RegisterAllProtocols loadPath=%s\n", loadPath); |
|
1102 |
|
1103 HINSTANCE hLib = ::LoadLibrary(loadPath); |
|
1104 TCDEBUGLOGA1("CServerManager::RegisterAllProtocols hLib=%x\n", hLib); |
|
1105 |
|
1106 if (hLib) |
|
1107 { |
|
1108 TCDEBUGLOGS("CServerManager::RegisterAllProtocols - library loaded\n"); |
|
1109 |
|
1110 PROTOCOLREGISTER lpFn = (PROTOCOLREGISTER)::GetProcAddress(hLib, PROTOCOLREGISTER_FNNAME); |
|
1111 TCDEBUGLOGA1("CServerManager::RegisterAllProtocols lpFn=%x\n", lpFn); |
|
1112 |
|
1113 if (lpFn) |
|
1114 { |
|
1115 TCDEBUGLOGS("CServerManager::RegisterAllProtocols - function found\n"); |
|
1116 const char* pType = lpFn(); |
|
1117 if (pType) |
|
1118 { |
|
1119 TCDEBUGLOGA1("CServerManager::RegisterAllProtocols pType=%s\n", pType); |
|
1120 CProtocolRegistryItem* pProt = new CProtocolRegistryItem(); |
|
1121 strcpy(pProt->m_ProtocolLibrary, loadPath); |
|
1122 strcpy(pProt->m_ProtocolType, pType); |
|
1123 m_ProtocolList->push_back(pProt); |
|
1124 } |
|
1125 } |
|
1126 ::FreeLibrary(hLib); |
|
1127 } |
|
1128 } |
|
1129 BOOL fNext = ::FindNextFile(sh, &fileData); |
|
1130 if (fNext == FALSE) |
|
1131 done = TRUE; |
|
1132 } |
|
1133 ::FindClose(sh); |
|
1134 } |
|
1135 delete[] searchPath; |
|
1136 delete[] loadPath; |
|
1137 } |
|
1138 TCDEBUGLOGS("CServerManager::RegisterAllProtocols\n"); |
|
1139 } |
|
1140 |
|
1141 void CServerManager::UnRegisterAllComms() |
|
1142 { |
|
1143 } |
|
1144 |
|
1145 void CServerManager::UnRegisterAllProtocols() |
|
1146 { |
|
1147 } |
|
1148 |
|
1149 const char* CServerManager::FindProtocolPath(char* protocolType) |
|
1150 { |
|
1151 char* path = NULL; |
|
1152 |
|
1153 TCDEBUGLOGS("CServerManager::FindProtocolPath\n"); |
|
1154 if (m_ProtocolList->size() != 0) |
|
1155 { |
|
1156 ProtocolRegistry::iterator iter; |
|
1157 for (iter = m_ProtocolList->begin(); iter != m_ProtocolList->end(); iter++) |
|
1158 { |
|
1159 if (strcmp((*iter)->m_ProtocolType, protocolType) == 0) |
|
1160 { |
|
1161 path = (*iter)->m_ProtocolLibrary; |
|
1162 break; |
|
1163 } |
|
1164 } |
|
1165 } |
|
1166 |
|
1167 TCDEBUGLOGA1("CServerManager::FindProtocolPath path=%s\n", path); |
|
1168 return path; |
|
1169 } |
|
1170 |
|
1171 const char* CServerManager::FindCommPath(char* commType) |
|
1172 { |
|
1173 char* path = NULL; |
|
1174 |
|
1175 TCDEBUGLOGS("CServerManager::FindCommPath\n"); |
|
1176 if (m_CommList->size() != 0) |
|
1177 { |
|
1178 CommRegistry::iterator iter; |
|
1179 for (iter = m_CommList->begin(); iter != m_CommList->end(); iter++) |
|
1180 { |
|
1181 if (strcmp((*iter)->m_CommType, commType) == 0) |
|
1182 { |
|
1183 path = (*iter)->m_CommLibrary; |
|
1184 break; |
|
1185 } |
|
1186 } |
|
1187 } |
|
1188 |
|
1189 TCDEBUGLOGA1("CServerManager::FindCommPath path=%s\n", path); |
|
1190 return path; |
|
1191 } |
|
1192 |