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 "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 * This file contains implementation of HtiDispatcher class. |
|
16 */ |
|
17 |
|
18 #include "stdsoap2.h" //should be first because of WinSock2.h errors |
|
19 |
|
20 #include "common.h" |
|
21 #include "HtiDispatcher.h" |
|
22 #include "soapHandler.h" |
|
23 |
|
24 #include "HtiMessage.h" |
|
25 #include "util.h" |
|
26 #include <sstream> |
|
27 |
|
28 #include <crtdbg.h> |
|
29 |
|
30 const static char* HTI_PLUGIN_FOLDER = "ServicePlugins/"; |
|
31 //used to redispatch hti framework error messages (like not authorized) |
|
32 |
|
33 /** |
|
34 * namespace table is needed to correclty send fault messages |
|
35 * each service plugin should explicitly set its one namespace table before processing |
|
36 * request |
|
37 */ |
|
38 SOAP_NMAC struct Namespace namespaces[] = |
|
39 { |
|
40 {"SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/", "http://www.w3.org/*/soap-envelope", NULL}, |
|
41 {"SOAP-ENC", "http://schemas.xmlsoap.org/soap/encoding/", "http://www.w3.org/*/soap-encoding", NULL}, |
|
42 {NULL, NULL, NULL, NULL} |
|
43 }; |
|
44 |
|
45 //********************************************************************************** |
|
46 // Class HtiDispatcher |
|
47 // |
|
48 // This class |
|
49 // -forwards Soap requests and Hti messages to correct SOAPHandlers |
|
50 // -Is used to initiate SOAPHandlers by reading them from dll's and start them |
|
51 //********************************************************************************** |
|
52 HtiDispatcher::HtiDispatcher(SafeQueue<Data*>* qIn, |
|
53 SafeQueue<Data*>* qOut) |
|
54 : m_Running(true), |
|
55 m_QueueIn(qIn), |
|
56 m_QueueOut(qOut), |
|
57 m_IncomingHtiMessage(NULL) |
|
58 { |
|
59 } |
|
60 |
|
61 HtiDispatcher::~HtiDispatcher() |
|
62 { |
|
63 for( htiSoapActionHashMap::const_iterator i = m_SoapHandlers.begin(); |
|
64 i != m_SoapHandlers.end(); |
|
65 ++i) |
|
66 { |
|
67 delete i->second; |
|
68 } |
|
69 } |
|
70 |
|
71 /** |
|
72 * This method is used to read all the available SoapHandler dll's and initialize an instance of them and store a reference to m_SoapHandlers(by soap action) |
|
73 * and m_HanlersUidMap(by service uid) maps. |
|
74 */ |
|
75 void HtiDispatcher::InitHandlers() |
|
76 { |
|
77 Util::Info( "HtiDispatcher::InitHandlers" ); |
|
78 //1. read exe/plugin directory |
|
79 WIN32_FIND_DATA FileData; |
|
80 HANDLE hSearch; |
|
81 bool fFinished = false; |
|
82 |
|
83 // Start searching for .dll files |
|
84 string searchMask = HTI_PLUGIN_FOLDER; |
|
85 searchMask.append( "*.dll" ); |
|
86 hSearch = FindFirstFile( searchMask.c_str(), &FileData ); |
|
87 if (hSearch == INVALID_HANDLE_VALUE) |
|
88 { |
|
89 Util::Info("No DLLs found."); |
|
90 return; |
|
91 } |
|
92 |
|
93 while (!fFinished) |
|
94 { |
|
95 |
|
96 //2. get list of all DLLs |
|
97 //SoapHandler* t = new SoapHandler("ws_hti.dll"); |
|
98 string pluginPath = HTI_PLUGIN_FOLDER; |
|
99 pluginPath.append( FileData.cFileName ); |
|
100 Util::Info("Try to load:"); |
|
101 Util::Info(pluginPath); |
|
102 SoapHandler* t = new SoapHandler( pluginPath ); |
|
103 if( t->LoadPlugin() ) |
|
104 { |
|
105 //3. add them to m_SoapHandlers |
|
106 char* soapAction = t->soapAction(); |
|
107 m_SoapHandlers[ soapAction ] = t; |
|
108 //add to uid map as well |
|
109 int uid = t->serviceUID(); |
|
110 m_HanlersUidMap[ uid ] = t; |
|
111 |
|
112 stringstream s; |
|
113 s<<"Loaded plugin: "; |
|
114 s<<soapAction; |
|
115 s<<", service UID: "; |
|
116 s<<uid; |
|
117 |
|
118 Util::Info( s.str().c_str() ); |
|
119 } |
|
120 else |
|
121 { |
|
122 delete t; |
|
123 // |
|
124 Util::Error("Failed load DLL as a plugin"); |
|
125 } |
|
126 |
|
127 if (!FindNextFile(hSearch, &FileData)) |
|
128 { |
|
129 if (GetLastError() == ERROR_NO_MORE_FILES) |
|
130 { |
|
131 fFinished = TRUE; |
|
132 } |
|
133 else |
|
134 { |
|
135 Util::Error("Couldn't find next plugin."); |
|
136 fFinished = TRUE; |
|
137 //return; |
|
138 } |
|
139 } |
|
140 } |
|
141 |
|
142 // Close the search handle. |
|
143 |
|
144 FindClose(hSearch); |
|
145 |
|
146 stringstream s; |
|
147 s<<"Plugins loaded "; |
|
148 s<<m_SoapHandlers.size(); |
|
149 Util::Info( s.str().c_str() ); |
|
150 } |
|
151 |
|
152 /** |
|
153 * This method is used to start all the SoapHandler instances that are stored in htiSoapActionHashMap |
|
154 */ |
|
155 void HtiDispatcher::StartHandlers() |
|
156 { |
|
157 for( htiSoapActionHashMap::const_iterator i = m_SoapHandlers.begin(); |
|
158 i != m_SoapHandlers.end(); |
|
159 ++i) |
|
160 { |
|
161 (i->second)->SetDispatcher(this); |
|
162 (i->second)->Start(); |
|
163 } |
|
164 } |
|
165 |
|
166 /* |
|
167 * This loop is used to forward incoming Hti messages(arriving from CommChannelPlugin to m_QueueOut side) to correct SoapHandlers |
|
168 */ |
|
169 void HtiDispatcher::Run() |
|
170 { |
|
171 Util::Debug("HtiDispatcher::Run"); |
|
172 //m_testHandler.SetDispatcher(this); |
|
173 //m_testHandler.Start(); |
|
174 InitHandlers(); |
|
175 StartHandlers(); |
|
176 |
|
177 BYTE* shortData = NULL; //keep data if it's too short to have HtiHeader |
|
178 //or if one Data msg have more than one HtiMessage |
|
179 DWORD msgBufferSize = 8096; |
|
180 BYTE* msgBuffer = (BYTE*)malloc( msgBufferSize ); |
|
181 DWORD shortDataLen = 0; |
|
182 |
|
183 // By setting this threads priority below others will give soaphandler threads |
|
184 // more runtime to process the received messages.. This is to avoid the situation |
|
185 // where two messages arrive so close to each other that m_hReceiveHtiEvent is |
|
186 // signalled twice before the first message has even been processed by the soap plugin. |
|
187 if(!SetThreadPriority( ThreadHandle(), THREAD_PRIORITY_BELOW_NORMAL )) |
|
188 Util::Info("Warning: Could not set HtiDispatcher priority!"); |
|
189 |
|
190 while (m_Running) |
|
191 { |
|
192 try |
|
193 { |
|
194 Data* d = m_QueueOut->front(50); |
|
195 BYTE* p = (BYTE *)d->GetData(); |
|
196 DWORD l = d->GetLength(); |
|
197 //printf("\td = %d\n", m_QueueOut->size()); |
|
198 |
|
199 if (Util::GetVerboseLevel() == Util::VerboseLevel::debug) |
|
200 { |
|
201 char tmp[64]; |
|
202 sprintf(tmp, "[HtiDispatcher] HTI MsgSize = %d", l); |
|
203 string s(tmp); |
|
204 Util::Debug(s); |
|
205 //Util::Hex(p, d->GetLength()); |
|
206 } |
|
207 |
|
208 //Util::Debug("leftovers"); |
|
209 //copy leftovers to the beginning of the buffer |
|
210 if ( shortDataLen > 0 ) |
|
211 { |
|
212 memcpy( msgBuffer, shortData, shortDataLen ); |
|
213 } |
|
214 shortData = msgBuffer; //set shortData to the beginning |
|
215 |
|
216 //copy data to buffer |
|
217 if ( shortDataLen + l > msgBufferSize ) |
|
218 { |
|
219 msgBufferSize = shortDataLen + l; |
|
220 msgBuffer = (BYTE*)realloc(msgBuffer, msgBufferSize); |
|
221 shortData = msgBuffer; |
|
222 } |
|
223 //copy data gotten from queue to the end of shortData |
|
224 memcpy(shortData + shortDataLen, p, l ); |
|
225 shortDataLen = l + shortDataLen; |
|
226 |
|
227 while ( shortDataLen != 0 && |
|
228 (shortDataLen >= HtiMessage::MinHeaderSize() || |
|
229 m_IncomingHtiMessage != NULL ) ) |
|
230 { |
|
231 //new message |
|
232 if ( m_IncomingHtiMessage == NULL ) |
|
233 { |
|
234 if ( shortDataLen >= HtiMessage::MinHeaderSize() ) |
|
235 { |
|
236 if ( HtiMessage::CheckValidHtiHeader(shortData) ) |
|
237 { |
|
238 m_IncomingHtiMessage = new HtiMessage( shortData, shortDataLen ); |
|
239 |
|
240 if (Util::GetVerboseLevel() == Util::VerboseLevel::debug) |
|
241 { |
|
242 char tmp[64]; |
|
243 sprintf(tmp,"New hti message %d", m_IncomingHtiMessage->HtiDataSize()); |
|
244 string s(tmp); |
|
245 Util::Debug(s); |
|
246 //Util::Hex(p, d->GetLength()); |
|
247 } |
|
248 |
|
249 //_RPT2(_CRT_WARN, "income msg %x <%d>\n", m_IncomingHtiMessage, sizeof(HtiMessage)); |
|
250 //check message |
|
251 if ( m_IncomingHtiMessage->IsMessageComplete() ) |
|
252 { |
|
253 Util::Debug("HTI message complete"); |
|
254 DWORD msgSize = m_IncomingHtiMessage->HtiDataSize(); |
|
255 if ( msgSize < shortDataLen ) |
|
256 { |
|
257 //remove used part |
|
258 //BYTE* temp = new BYTE[shortDataLen-msgSize]; |
|
259 //_RPT2(_CRT_WARN, "temp %x <%d>\n", shortData , shortDataLen-msgSize); |
|
260 //memcpy(temp, shortData + msgSize, shortDataLen-msgSize); |
|
261 //_RPT1(_CRT_WARN, "del shortData %x\n", shortData); |
|
262 //delete[] shortData; |
|
263 //shortData = temp; |
|
264 shortData += msgSize; //just move pointer |
|
265 shortDataLen -= msgSize; |
|
266 |
|
267 } |
|
268 else |
|
269 { |
|
270 //_RPT1(_CRT_WARN, "del shortData %x\n", shortData); |
|
271 //delete[] shortData; |
|
272 //shortData = NULL; |
|
273 shortDataLen = 0; |
|
274 } |
|
275 //Dispatch incoming message |
|
276 DispatchToSoapHandlers(); |
|
277 } |
|
278 else |
|
279 { |
|
280 //_RPT1(_CRT_WARN, "del shortData %x\n", shortData); |
|
281 //delete[] shortData; |
|
282 //shortData = NULL; |
|
283 shortDataLen = 0; |
|
284 } |
|
285 } |
|
286 else |
|
287 { |
|
288 //invalid header |
|
289 Util::Error("Invalid HTI header, dismiss Data message"); |
|
290 Util::Hex(shortData, HtiMessage::MinHeaderSize() ); |
|
291 //_RPT1(_CRT_WARN, "del shortData %x\n", shortData); |
|
292 //delete[] shortData; |
|
293 //shortData = NULL; |
|
294 shortDataLen = 0; |
|
295 } |
|
296 } |
|
297 } |
|
298 else //body parts |
|
299 { |
|
300 Util::Debug("add"); |
|
301 DWORD added = m_IncomingHtiMessage->AddToBody( shortData, |
|
302 shortDataLen ); |
|
303 //printf("reminder %d\n", m_IncomingHtiMessage->Reminder()); |
|
304 if ( added < shortDataLen ) |
|
305 { |
|
306 //only part of message was added |
|
307 //remove added part |
|
308 //BYTE* temp = new BYTE[shortDataLen-added]; |
|
309 //_RPT2(_CRT_WARN, "temp %x <%d>\n", shortData , shortDataLen-added); |
|
310 //memcpy(temp, shortData + added, shortDataLen-added); |
|
311 //_RPT1(_CRT_WARN, "del shortData %x\n", shortData ); |
|
312 //delete[] shortData; |
|
313 //shortData = temp; |
|
314 shortData += added; |
|
315 shortDataLen -= added; |
|
316 } |
|
317 else //all data were added |
|
318 { |
|
319 //_RPT1(_CRT_WARN, "del shortData %x\n", shortData ); |
|
320 //delete[] shortData; |
|
321 //shortData = NULL; |
|
322 shortDataLen = 0; |
|
323 } |
|
324 |
|
325 if ( m_IncomingHtiMessage->IsMessageComplete() ) |
|
326 { |
|
327 Util::Debug("HTI message complete"); |
|
328 //Dispatch incoming message |
|
329 DispatchToSoapHandlers(); |
|
330 } |
|
331 } |
|
332 } |
|
333 |
|
334 m_QueueOut->pop(); |
|
335 //_RPT1(_CRT_WARN, "del data %x\n", d); |
|
336 delete d; |
|
337 d = NULL; |
|
338 } catch (TimeoutException te) |
|
339 { |
|
340 //Util::Debug("[DataGatewaySocketWriterThread]timeout exception"); |
|
341 } |
|
342 } |
|
343 free( msgBuffer ); |
|
344 } |
|
345 |
|
346 /** |
|
347 * This method is used to forward the incoming HTI message to correct SoapHandler |
|
348 * The correct SoapHandler is found by service uid |
|
349 */ |
|
350 void HtiDispatcher::DispatchToSoapHandlers() |
|
351 { |
|
352 htiUIDHashMap::const_iterator i; |
|
353 int targetServiceUid; |
|
354 if ( m_IncomingHtiMessage->IsErrorMessage() ) |
|
355 { |
|
356 targetServiceUid = m_IncomingHtiMessage->ErrorServiceUid(); |
|
357 |
|
358 stringstream s; |
|
359 s<<"Received HTI error message\nhtiErrorCode: "; |
|
360 s<<m_IncomingHtiMessage->HtiErrorCode(); |
|
361 s<<"\nserviceUid: "; |
|
362 s<<m_IncomingHtiMessage->ErrorServiceUid(); |
|
363 s<<"\nserviceErrorCode: "; |
|
364 s<<m_IncomingHtiMessage->ServiceErrorCode(); |
|
365 s<<"\nErrorDescription: "; |
|
366 s<<m_IncomingHtiMessage->ErrorDescription(); |
|
367 s<<"\ntargetServiceUid: "; |
|
368 s<<targetServiceUid; |
|
369 |
|
370 Util::Error(s.str().c_str()); |
|
371 } |
|
372 else |
|
373 { |
|
374 targetServiceUid = m_IncomingHtiMessage->GetServiceUID(); |
|
375 } |
|
376 |
|
377 if ( targetServiceUid == HTI_SYSTEM_SERVICE_UID && |
|
378 m_IncomingHtiMessage->IsErrorMessage() ) |
|
379 { |
|
380 Util::Debug("dispatch error"); |
|
381 //if system plugin doesn't wait for a message then find plugin that |
|
382 //is waiting for a message and delivery the incoming msg to it |
|
383 i = m_HanlersUidMap.find( targetServiceUid ); |
|
384 if ( i != m_HanlersUidMap.end() ) |
|
385 { |
|
386 if ( (i->second)->IsWaitsForHtiMessage() ) |
|
387 { |
|
388 Util::Debug("dispatch error to system cause it waits"); |
|
389 if ( !(i->second)->ReceiveHtiMessage(m_IncomingHtiMessage) ) |
|
390 { |
|
391 Util::Error("Failed to dispatch hti message"); |
|
392 Util::Hex(m_IncomingHtiMessage->HtiData(), HtiMessage::MinHeaderSize()); |
|
393 //dismiss message |
|
394 delete m_IncomingHtiMessage; |
|
395 } |
|
396 } |
|
397 else |
|
398 { |
|
399 Util::Debug("find handler that waits"); |
|
400 //find the handler that waits for hti message |
|
401 for( htiUIDHashMap::const_iterator i = m_HanlersUidMap.begin(); |
|
402 i != m_HanlersUidMap.end(); |
|
403 ++i) |
|
404 { |
|
405 Util::Debug((i->second)->soapAction()); |
|
406 if ( (i->second)->IsWaitsForHtiMessage() ) |
|
407 { |
|
408 Util::Debug("found"); |
|
409 if ( !(i->second)->ReceiveHtiMessage(m_IncomingHtiMessage) ) |
|
410 { |
|
411 Util::Error("Failed to dispatch hti message"); |
|
412 Util::Hex(m_IncomingHtiMessage->HtiData(), HtiMessage::MinHeaderSize()); |
|
413 //dismiss message |
|
414 delete m_IncomingHtiMessage; |
|
415 } |
|
416 break; |
|
417 } |
|
418 } |
|
419 } |
|
420 } |
|
421 } |
|
422 else |
|
423 { |
|
424 i = m_HanlersUidMap.find( targetServiceUid ); |
|
425 if ( i != m_HanlersUidMap.end() ) |
|
426 { |
|
427 if ( !(i->second)->ReceiveHtiMessage(m_IncomingHtiMessage) ) |
|
428 { |
|
429 Util::Error("Failed to dispatch hti message"); |
|
430 Util::Hex(m_IncomingHtiMessage->HtiData(), HtiMessage::MinHeaderSize()); |
|
431 //dismiss message |
|
432 delete m_IncomingHtiMessage; |
|
433 } |
|
434 } |
|
435 else |
|
436 { |
|
437 Util::Error("Failed to dispatch hti message, no plug-in with appropriate uid",m_IncomingHtiMessage->GetServiceUID() ); |
|
438 Util::Hex(m_IncomingHtiMessage->HtiData(), HtiMessage::MinHeaderSize()); |
|
439 //dismiss message |
|
440 delete m_IncomingHtiMessage; |
|
441 } |
|
442 } |
|
443 m_IncomingHtiMessage = NULL; |
|
444 |
|
445 // This will give other threads (soaphandlers) some runtime to process the |
|
446 // received message. |
|
447 Sleep(0); |
|
448 } |
|
449 |
|
450 /* |
|
451 * This method is used to forward soap request to correct SOAPHandler |
|
452 * Correct SOAPHandler is found by soap action |
|
453 */ |
|
454 bool HtiDispatcher::DispatchSoapServe(struct soap* soapEnv) |
|
455 { |
|
456 Util::Debug("HtiDispatcher::DispatchSoapServe()"); |
|
457 /* |
|
458 _CrtMemState localMem; |
|
459 _CrtMemCheckpoint( &localMem ); |
|
460 */ |
|
461 soap_begin( soapEnv ); |
|
462 if (soap_begin_recv(soapEnv)) |
|
463 { |
|
464 soap_set_namespaces( soapEnv, namespaces); |
|
465 soap_send_fault(soapEnv); |
|
466 return false; |
|
467 } |
|
468 |
|
469 if ( !(soapEnv->action) ) |
|
470 { |
|
471 //Util::Error("soapAction is missing"); |
|
472 soap_set_namespaces( soapEnv, namespaces); |
|
473 soapEnv->error = soap_sender_fault(soapEnv, "soapAction is missing", NULL); |
|
474 soap_send_fault(soapEnv); |
|
475 return false; |
|
476 } |
|
477 /* |
|
478 //_RPT0(_CRT_WARN, "!!!!!!!!!!!!!!!! Local Objects !!!!!!!!!!!!!!!!\n"); |
|
479 |
|
480 _CrtMemDumpAllObjectsSince( &localMem ); |
|
481 //_RPT1(_CRT_WARN, "action address %x\n", soapEnv->action); |
|
482 */ |
|
483 htiSoapActionHashMap::const_iterator it; |
|
484 it = m_SoapHandlers.find( soapEnv->action ); |
|
485 if ( it != m_SoapHandlers.end() ) |
|
486 { |
|
487 return (it->second)->ServeSoap( soapEnv ); |
|
488 } |
|
489 else |
|
490 { |
|
491 //Util::Error("soapAction is unknown:"); |
|
492 //Util::Error(soapEnv->action); |
|
493 //soapEnv->error = SOAP_NO_METHOD; |
|
494 soap_set_namespaces( soapEnv, namespaces); |
|
495 soapEnv->error = soap_sender_fault(soapEnv, "No plugin found", "no plugin found for requested service in actionSOAP header field"); |
|
496 soap_send_fault(soapEnv); |
|
497 return false; |
|
498 } |
|
499 |
|
500 Util::Debug("HtiDispatcher::DispatchSoapServe() OK"); |
|
501 } |
|
502 |
|
503 /* |
|
504 * SoapHandler calls this method |
|
505 * The method creates a Data object from the HtiMessage given as parameter |
|
506 * and puts the Data object into incoming queue(going eventually to CommChannelPlugin) |
|
507 */ |
|
508 void HtiDispatcher::SendHtiMessage(HtiMessage* msg) |
|
509 { |
|
510 Util::Debug("HtiDispatcher::SendHtiMessage()"); |
|
511 if (msg) |
|
512 { |
|
513 Data* d = new Data(msg->HtiData(), msg->HtiDataSize(), Data::EData); |
|
514 //_RPT2(_CRT_WARN, "d %x <%d>\n", d , sizeof(Data)); |
|
515 |
|
516 if (Util::GetVerboseLevel() == Util::VerboseLevel::debug) |
|
517 { |
|
518 char tmp[64]; |
|
519 sprintf(tmp, "[HtiDispatcher] HTI MsgSize = %d", msg->HtiDataSize()); |
|
520 string s(tmp); |
|
521 Util::Debug(s); |
|
522 Util::Hex( (char*)(msg->HtiData()), 16); |
|
523 } |
|
524 m_QueueIn->push(d); |
|
525 //delete msg; |
|
526 } |
|
527 Util::Debug("HtiDispatcher::SendHtiMessage() OK"); |
|
528 } |
|
529 |
|
530 void HtiDispatcher::Stop() |
|
531 { |
|
532 m_Running = false; |
|
533 HANDLE* handles = new HANDLE[ m_SoapHandlers.size() ]; |
|
534 int h = 0; |
|
535 for( htiSoapActionHashMap::const_iterator i = m_SoapHandlers.begin(); |
|
536 i != m_SoapHandlers.end(); |
|
537 ++i) |
|
538 { |
|
539 (i->second)->Stop(); |
|
540 handles[ h++ ] = (i->second)->ThreadHandle(); |
|
541 } |
|
542 |
|
543 WaitForMultipleObjects(m_SoapHandlers.size(), |
|
544 handles, |
|
545 TRUE, |
|
546 5000/*g_MaximumShutdownWaitTime*/); |
|
547 |
|
548 delete[] handles; |
|
549 } |
|
550 |
|
551 bool HtiDispatcher::IsRunning() |
|
552 { |
|
553 return m_Running; |
|
554 } |
|