hti/PC_Tools/HTIGateway/HtiGateway/src/SOAPHandler.cpp
branchRCL_3
changeset 19 07b41fa8d1dd
parent 18 3406c99bc375
child 20 ca8a1b6995f6
equal deleted inserted replaced
18:3406c99bc375 19:07b41fa8d1dd
     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 SOAPHandler class
       
    16 */
       
    17 
       
    18 #include "stdsoap2.h" //should be first because of WinSock2.h errors
       
    19 
       
    20 #include "SOAPHandler.h"
       
    21 #include "HtiMessage.h"
       
    22 
       
    23 #include "datagateway.h" //for htidispatcher
       
    24 #include "util.h"
       
    25 
       
    26 #include <sstream>
       
    27 
       
    28 SOAP_NMAC struct Namespace namespaces_l[] =
       
    29 {
       
    30 	{"SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/", "http://www.w3.org/*/soap-envelope", NULL},
       
    31 	{"SOAP-ENC", "http://schemas.xmlsoap.org/soap/encoding/", "http://www.w3.org/*/soap-encoding", NULL},
       
    32 	{"xsi", "http://www.w3.org/2001/XMLSchema-instance", "http://www.w3.org/*/XMLSchema-instance", NULL},
       
    33 	{"xsd", "http://www.w3.org/2001/XMLSchema", "http://www.w3.org/*/XMLSchema", NULL},
       
    34 	{"ns1", "urn:hti", NULL, NULL},
       
    35 	{NULL, NULL, NULL, NULL}
       
    36 };
       
    37 
       
    38 //**********************************************************************************
       
    39 // Class SOAPHandler
       
    40 //
       
    41 // This class is used to handle SOAP requests, send HtiMessages 
       
    42 // and handle HTI responses by using loaded HtiPluginDll
       
    43 //**********************************************************************************
       
    44 SoapHandler::SoapHandler(const string& pluginName)
       
    45 	: m_PluginName(pluginName),
       
    46 	  m_Running(false),
       
    47 	  m_HtiDispatcher(NULL),
       
    48 	  m_SoapEnv(NULL),
       
    49 	  m_ReceiveHtiMsg(NULL)
       
    50 {
       
    51 	m_hReceiveHtiEvent = CreateEvent(NULL, //sec att
       
    52 								  FALSE, //not manual-reset
       
    53 								  FALSE, //initial state non-signaled
       
    54 								  NULL //name
       
    55 								  );
       
    56 	m_hReceiveSoapEvent = CreateEvent(NULL, //sec att
       
    57 								  FALSE, //not manual-reset
       
    58 								  FALSE, //initial state non-signaled
       
    59 								  NULL //name
       
    60 								  );
       
    61 
       
    62 	m_hHandlerCanAcceptSoapRequest = CreateEvent(NULL, //sec att
       
    63 								  TRUE, //manual-reset
       
    64 								  TRUE, //initial state signaled
       
    65 								  NULL //name
       
    66 								  );
       
    67 
       
    68 	m_hHandlerCanAcceptHtiMessage = CreateEvent(NULL, //sec att
       
    69 								  TRUE, //manual-reset
       
    70 								  TRUE, //initial state signaled
       
    71 								  NULL //name
       
    72 								  );
       
    73 
       
    74 	m_hHandlerWaitsHtiMessage = CreateEvent(NULL, //sec att
       
    75 								  TRUE, //manual-reset
       
    76 								  FALSE, //initial state non-signaled
       
    77 								  NULL //name
       
    78 								  );
       
    79 
       
    80 
       
    81 	m_HtiPlugin = new HtiPluginDll();
       
    82 }
       
    83 
       
    84 SoapHandler::~SoapHandler()
       
    85 {
       
    86 	Stop();
       
    87 	CloseHandle(m_hHandlerWaitsHtiMessage);
       
    88 	CloseHandle(m_hHandlerCanAcceptSoapRequest);
       
    89 	CloseHandle(m_hHandlerCanAcceptHtiMessage);
       
    90 	CloseHandle(m_hReceiveHtiEvent);
       
    91 	CloseHandle(m_hReceiveSoapEvent);
       
    92 	CleanSoapEnv();
       
    93 	delete m_HtiPlugin;
       
    94 }
       
    95 
       
    96 void SoapHandler::CleanSoapEnv()
       
    97 {
       
    98 	if ( m_SoapEnv )
       
    99 	{
       
   100 		soap_destroy(m_SoapEnv); // dealloc C++ data 
       
   101 		soap_end(m_SoapEnv); // dealloc data and clean up 
       
   102 		soap_done(m_SoapEnv); // detach soap struct 
       
   103 		free(m_SoapEnv);
       
   104 		m_SoapEnv = NULL;
       
   105 	}
       
   106 }
       
   107 
       
   108 /**
       
   109  * This loop waits until either a SOAP request or HTI response event has arrived
       
   110  * When one of them arrives, it is handled and the loop starts again to wait
       
   111  * for a new request or response
       
   112  */
       
   113 void SoapHandler::Run()
       
   114 {
       
   115 	Util::Debug("SoapHandler::Run()");
       
   116 	HANDLE events[2];
       
   117 	events[0] = m_hReceiveSoapEvent;
       
   118 	events[1] = m_hReceiveHtiEvent;
       
   119 	m_Running = true;
       
   120 	while (m_Running)
       
   121 	{
       
   122 		Util::Debug("SoapHandler::Run::WaitForMultipleObjects");
       
   123 		DWORD r = WaitForMultipleObjects(
       
   124 						2,
       
   125 						events,
       
   126                         FALSE, //don't wait all
       
   127 						LOOP_WAIT_INTERVAL);
       
   128 		switch (r)
       
   129 		{
       
   130 		case WAIT_OBJECT_0:
       
   131 			{
       
   132 				//process soap request
       
   133 				ResetEvent(m_hHandlerCanAcceptSoapRequest);
       
   134 				ResetEvent(m_hHandlerCanAcceptHtiMessage);
       
   135 				DoServeSoap();
       
   136 				SetEvent(m_hHandlerCanAcceptSoapRequest);
       
   137 				SetEvent(m_hHandlerCanAcceptHtiMessage);
       
   138 			}
       
   139 			break;
       
   140 		case WAIT_OBJECT_0+1:
       
   141 			{
       
   142 				//process HTI response
       
   143 				ResetEvent(m_hHandlerCanAcceptSoapRequest);
       
   144 				ResetEvent(m_hHandlerCanAcceptHtiMessage);
       
   145 				ProcessHtiResponse();
       
   146 				SetEvent(m_hHandlerCanAcceptSoapRequest);
       
   147 				SetEvent(m_hHandlerCanAcceptHtiMessage);
       
   148 			}
       
   149 			break;
       
   150 		case WAIT_TIMEOUT:
       
   151 			{
       
   152 				//do nothing
       
   153 				//just check for m_Running and waits again
       
   154 			}
       
   155 			break;
       
   156 		default:
       
   157 			{
       
   158 			}
       
   159 		}
       
   160 	}
       
   161 }
       
   162 
       
   163 void SoapHandler::Stop()
       
   164 {
       
   165 	m_Running = false;
       
   166 }
       
   167 
       
   168 /**
       
   169  * wait for hti message
       
   170  * Suspend thread until HtiMessage for the loaded plug-in is received
       
   171  * Return true if hti message is received or false if timeout
       
   172  */
       
   173 bool SoapHandler::WaitForHtiMessage(DWORD timeout)
       
   174 {
       
   175 	SetEvent(m_hHandlerCanAcceptHtiMessage);
       
   176 	SetEvent(m_hHandlerWaitsHtiMessage);
       
   177 	//delete m_ReceiveHtiMsg;
       
   178 	Util::Debug("SoapHandler::WaitForHtiMessage");
       
   179 	DWORD r = WaitForSingleObject(m_hReceiveHtiEvent, timeout);
       
   180 
       
   181 	ResetEvent(m_hHandlerCanAcceptHtiMessage);
       
   182 	ResetEvent(m_hHandlerWaitsHtiMessage);
       
   183 	switch ( r )
       
   184 	{
       
   185 	case WAIT_OBJECT_0:
       
   186 		{
       
   187 			return true;
       
   188 		}
       
   189 		break;
       
   190 	case WAIT_TIMEOUT:
       
   191 		{
       
   192 		}
       
   193 		break;
       
   194 	default:
       
   195 		{
       
   196 		}
       
   197 	}
       
   198 	Util::Debug("WaitForHtiMessage OK");
       
   199 	return false;
       
   200 }
       
   201 
       
   202 /**
       
   203  * This method is used to init HtiPluginDll
       
   204  */
       
   205 bool SoapHandler::LoadPlugin()
       
   206 {
       
   207 	//code to load and init plugin dll m_PluginName
       
   208 	return m_HtiPlugin->Init( m_PluginName.c_str() );
       
   209 }
       
   210 
       
   211 /**
       
   212  * This method tells whether or not this handler is currently busy processing request
       
   213  */
       
   214 bool SoapHandler::IsBusyForSoapRequest()
       
   215 {
       
   216 	DWORD r = WaitForSingleObject(m_hHandlerCanAcceptSoapRequest, LOOP_CHECK_INTERVAL);
       
   217 	switch ( r )
       
   218 	{
       
   219 	case WAIT_OBJECT_0:
       
   220 		{
       
   221 			return false;
       
   222 		}
       
   223 		break;
       
   224 	case WAIT_TIMEOUT:
       
   225 		{
       
   226 		}
       
   227 		break;
       
   228 	default:
       
   229 		{
       
   230 		}
       
   231 	}
       
   232 	return true;
       
   233 }
       
   234 
       
   235 /**
       
   236  * This method tells whether or not this handler is currently busy processing hti message
       
   237  */
       
   238 bool SoapHandler::IsBusyForHtiMessage()
       
   239 {
       
   240 	DWORD r = WaitForSingleObject(m_hHandlerCanAcceptHtiMessage, LOOP_CHECK_INTERVAL);
       
   241 	switch ( r )
       
   242 	{
       
   243 	case WAIT_OBJECT_0:
       
   244 		{
       
   245 			return false;
       
   246 		}
       
   247 		break;
       
   248 	case WAIT_TIMEOUT:
       
   249 		{
       
   250 		}
       
   251 		break;
       
   252 	default:
       
   253 		{
       
   254 		}
       
   255 	}
       
   256 	return true;
       
   257 }
       
   258 
       
   259 /*
       
   260  * This method is used to check if SoapHandler is currently waiting for Hti Message
       
   261  */
       
   262 bool SoapHandler::IsWaitsForHtiMessage()
       
   263 {
       
   264 	DWORD r = WaitForSingleObject(m_hHandlerWaitsHtiMessage, LOOP_CHECK_INTERVAL);
       
   265 	switch ( r )
       
   266 	{
       
   267 	case WAIT_OBJECT_0:
       
   268 		{
       
   269 			return true;
       
   270 		}
       
   271 		break;
       
   272 	case WAIT_TIMEOUT:
       
   273 		{
       
   274 		}
       
   275 		break;
       
   276 	default:
       
   277 		{
       
   278 		}
       
   279 	}
       
   280 	return false;
       
   281 }
       
   282 
       
   283 /**
       
   284  * Just a wrapper around soap_serve() created by gSOAP
       
   285  * called by DataGatewaySOAPServerThread for a new SOAP request
       
   286  * return true if request can be processed
       
   287  * This method sets m_hReceiveSoapEvent to signaled state
       
   288  * Run method waits this event object and then handles the soap request when the event switches to signaled state
       
   289  */
       
   290 bool SoapHandler::ServeSoap(struct soap* soapEnv)
       
   291 {
       
   292 	Util::Debug("SoapHandler::ServeSoap");
       
   293 	//check can this handler process request in soapEnv
       
   294 	if ( !IsBusyForSoapRequest() )
       
   295 	{
       
   296 		CleanSoapEnv();
       
   297 		m_SoapEnv = soap_copy(soapEnv);
       
   298 		//soap_free( soapEnv );
       
   299 		m_SoapEnv->user = dynamic_cast<HtiSoapHandlerInterface*>( this );
       
   300 		SetEvent(m_hReceiveSoapEvent);
       
   301 		Util::Debug("SoapHandler::ServeSoap OK");
       
   302 		return true;
       
   303 	}
       
   304 	else
       
   305 	{
       
   306 		Util::Debug("SoapHandler::ServeSoap NOK");
       
   307 		return false;
       
   308 	}
       
   309 }
       
   310 
       
   311 /**
       
   312  * This method is called when incoming Hti message has been read from CommChannelPlugin
       
   313  * The method sets m_hReceiveHtiEvent to signaled state(Run method waits this event object to become signaled)
       
   314  */
       
   315 bool SoapHandler::ReceiveHtiMessage(HtiMessage* htiMessage)
       
   316 {
       
   317 	Util::Debug("SoapHandler::ReceiveHtiMessage");
       
   318 	//_RPT0(_CRT_WARN, "SoapHandler::ReceiveHtiMessage");
       
   319 	//check can this handler process HTI response
       
   320 	if ( !IsBusyForHtiMessage() )
       
   321 	{
       
   322 		//_RPT1(_CRT_WARN, "delete %x\n", m_ReceiveHtiMsg);
       
   323 		//_RPT1(_CRT_WARN, "received %x\n", htiMessage);
       
   324 		ResetEvent(m_hHandlerCanAcceptHtiMessage);
       
   325 		delete m_ReceiveHtiMsg;
       
   326 		m_ReceiveHtiMsg = htiMessage;
       
   327 		SetEvent(m_hReceiveHtiEvent);
       
   328 		Util::Debug("SoapHandler::ReceiveHtiMessage OK");
       
   329 		return true;
       
   330 	}
       
   331 	Util::Debug("SoapHandler::ReceiveHtiMessage NOK");
       
   332 	return false;
       
   333 }
       
   334 
       
   335 /**
       
   336 * Actual SOAP request processing in the thread
       
   337 * if it was accepted in ServeSoap()
       
   338 * The request is processed using HtiPluginDll
       
   339 */
       
   340 void SoapHandler::DoServeSoap()
       
   341 {
       
   342 	Util::Debug("SoapHandler::DoServeSoap");
       
   343 	soap_set_namespaces( m_SoapEnv, m_HtiPlugin->serviceNamespaces()); 
       
   344 	//soap_set_namespaces( m_SoapEnv, namespaces_l);
       
   345 
       
   346 	if (soap_envelope_begin_in(m_SoapEnv)
       
   347 		|| soap_recv_header(m_SoapEnv)
       
   348 		|| soap_body_begin_in(m_SoapEnv) )
       
   349 	{
       
   350 		//soap_set_namespaces( m_SoapEnv, namespaces_l);
       
   351 		soap_send_fault(m_SoapEnv);
       
   352 		Util::Debug("SoapHandler::DoServeSoap NOK");
       
   353 		return;
       
   354 	}
       
   355 
       
   356 	//call soap_serve(soap_server_request) from the dll plug-in
       
   357 	//m_HtiPlugin->soap_serve( m_SoapEnv );
       
   358 	
       
   359 	if (m_HtiPlugin->soap_serve_request(m_SoapEnv)
       
   360 		|| (m_SoapEnv->fserveloop && m_SoapEnv->fserveloop(m_SoapEnv)))
       
   361 	{
       
   362 		//soap_set_namespaces( m_SoapEnv, namespaces_l);
       
   363 		soap_send_fault(m_SoapEnv);
       
   364 		Util::Debug("SoapHandler::DoServeSoap NOK");
       
   365 		return;
       
   366 	}
       
   367 	
       
   368 	CleanSoapEnv();
       
   369 	Util::Debug("SoapHandler::DoServeSoap OK");
       
   370 }
       
   371 
       
   372 /*
       
   373  * HtiPluginDll's call this method
       
   374  * It creates a HtiMessage of the data given as parameters and sends it using HtiDispatcher
       
   375  */
       
   376 void SoapHandler::SendHtiMessage( DWORD serviceId, void* body, DWORD len )
       
   377 {
       
   378 	Util::Debug("SoapHandler::SendHtiMessage");
       
   379 	HtiMessage* msg = new HtiMessage(serviceId, body, len);
       
   380 	//_RPT2(_CRT_WARN, "send msg %x <%d>\n", msg, sizeof(HtiMessage));
       
   381 	m_HtiDispatcher->SendHtiMessage( msg );
       
   382 	//_RPT2(_CRT_WARN, "del send msg %x <%d>\n", msg, sizeof(HtiMessage));
       
   383 	delete msg;
       
   384 	Util::Debug("SoapHandler::SendHtiMessage OK");
       
   385 }
       
   386 
       
   387 /*
       
   388  * HtiPluginDll's call this method
       
   389  * It creates a HtiMessage of the data given as parameters and sends it using HtiDispatcher
       
   390  */
       
   391 void SoapHandler::SendHtiMessage( DWORD serviceId, void* body, DWORD len, BYTE priority )
       
   392 {
       
   393 	Util::Debug("SoapHandler::SendHtiMessage");
       
   394 	//delegate function to dispatcher
       
   395 	HtiMessage* msg = new HtiMessage(serviceId, body, len, priority);
       
   396 	//_RPT2(_CRT_WARN, "send msg %x <%d>\n", msg, sizeof(HtiMessage));
       
   397 	m_HtiDispatcher->SendHtiMessage( msg );
       
   398 	//_RPT2(_CRT_WARN, "del send msg %x <%d>\n", msg, sizeof(HtiMessage));
       
   399 	delete msg;
       
   400 	Util::Debug("SoapHandler::SendHtiMessage OK");
       
   401 }
       
   402 
       
   403 int SoapHandler::ReceivedHtiMessageBodySize()
       
   404 {
       
   405 	if ( m_ReceiveHtiMsg )
       
   406 	{
       
   407 		return m_ReceiveHtiMsg->GetBodySize();
       
   408 	}
       
   409 	return -1;
       
   410 }
       
   411 
       
   412 void* SoapHandler::ReceivedHtiMessageBody()
       
   413 {
       
   414 	if ( m_ReceiveHtiMsg )
       
   415 	{
       
   416 		return m_ReceiveHtiMsg->GetBody();
       
   417 	}
       
   418 	return NULL;
       
   419 }
       
   420 
       
   421 bool SoapHandler::IsReceivedHtiError()
       
   422 {
       
   423 	if ( m_ReceiveHtiMsg )
       
   424 	{
       
   425 		return m_ReceiveHtiMsg->IsErrorMessage();
       
   426 	}
       
   427 	return false;
       
   428 }
       
   429 
       
   430 int SoapHandler::HtiErrorCode()
       
   431 {
       
   432 	if ( m_ReceiveHtiMsg )
       
   433 	{
       
   434 		return m_ReceiveHtiMsg->HtiErrorCode();
       
   435 	}
       
   436 	return -1;
       
   437 }
       
   438 
       
   439 int SoapHandler::HtiServiceErrorCode()
       
   440 {
       
   441 	if ( m_ReceiveHtiMsg )
       
   442 	{
       
   443 		return m_ReceiveHtiMsg->ServiceErrorCode();
       
   444 	}
       
   445 	return -1;
       
   446 }
       
   447 
       
   448 char* SoapHandler::HtiServiceErrorDerscription()
       
   449 {
       
   450 	if ( m_ReceiveHtiMsg )
       
   451 	{
       
   452 		return m_ReceiveHtiMsg->ErrorDescription();
       
   453 	}
       
   454 	return NULL;
       
   455 }
       
   456 
       
   457 void SoapHandler::SendSoapFaultFromReceivedHtiError()
       
   458 {
       
   459 	if ( m_SoapEnv && IsReceivedHtiError() )
       
   460 	{
       
   461         stringstream s;
       
   462 		s<<"<htiError xmlns=\'urn:hti/fault\'><frameworkErrorCode>";
       
   463         s<<m_ReceiveHtiMsg->HtiErrorCode();
       
   464 		s<<"</frameworkErrorCode><serviceErrorCode>";
       
   465 		s<<m_ReceiveHtiMsg->ServiceErrorCode();
       
   466 		s<<"</serviceErrorCode><serviceErrorDescription>";
       
   467 		s<<m_ReceiveHtiMsg->ErrorDescription();
       
   468 		s<<"</serviceErrorDescription>";
       
   469 		s<<"</htiError>";
       
   470 		
       
   471 		soap_receiver_fault(m_SoapEnv,
       
   472 			"HtiError", s.str().c_str() );
       
   473 	}
       
   474 }
       
   475 
       
   476 /**
       
   477  * This method makes HtiPluginDll process HTI response 
       
   478  */
       
   479 void SoapHandler::ProcessHtiResponse()
       
   480 {
       
   481 	m_HtiPlugin->hti_serve( dynamic_cast<HtiSoapHandlerInterface*>( this ) );
       
   482 }