connectivity/com.nokia.tcf/native/TCFNative/Common/Source/ServerClient.cpp
changeset 60 9d2210c8eed2
equal deleted inserted replaced
59:c892c53c664e 60:9d2210c8eed2
       
     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 
       
    18 #include "stdafx.h"
       
    19 #include "ServerClient.h"
       
    20 #include "TCErrorConstants.h"
       
    21 #include <time.h>
       
    22 
       
    23 #ifdef TCF_CLIENT
       
    24 #include "..\..\TCFClient\ClientManager.h"
       
    25 extern CClientManager* gManager;
       
    26 #endif
       
    27 #ifdef TCF_SERVER
       
    28 #include "..\..\TCFServer\ServerManager.h"
       
    29 extern CServerManager* gManager;
       
    30 #endif
       
    31 
       
    32 #ifdef _DEBUG
       
    33 extern BOOL gDoLogging;
       
    34 #endif
       
    35 
       
    36 //#define LOG_SERVERCLIENT
       
    37 #if defined(LOG_SERVERCLIENT) && defined(_DEBUG)
       
    38 extern BOOL gDoLogging;
       
    39 extern char TCDebugMsg[];
       
    40 #define TCDEBUGOPEN() if (gDoLogging) { gManager->m_DebugLog->WaitForAccess(); }
       
    41 #define TCDEBUGLOGS(s) if (gDoLogging) { sprintf(TCDebugMsg,"%s", s); gManager->m_DebugLog->log(TCDebugMsg); }
       
    42 #define TCDEBUGLOGA1(s, a1) if (gDoLogging) { sprintf(TCDebugMsg, s, a1); gManager->m_DebugLog->log(TCDebugMsg); }
       
    43 #define TCDEBUGLOGA2(s, a1, a2) if (gDoLogging) { sprintf(TCDebugMsg, s, a1, a2); gManager->m_DebugLog->log(TCDebugMsg); }
       
    44 #define TCDEBUGLOGA3(s, a1, a2, a3) if (gDoLogging) { sprintf(TCDebugMsg, s, a1, a2, a3); gManager->m_DebugLog->log(TCDebugMsg); }
       
    45 #define TCDEBUGCLOSE() if (gDoLogging) { gManager->m_DebugLog->ReleaseAccess(); }
       
    46 #else
       
    47 #define TCDEBUGOPEN()
       
    48 #define TCDEBUGLOGS(s)
       
    49 #define TCDEBUGLOGA1(s, a1)
       
    50 #define TCDEBUGLOGA2(s, a1, a2)
       
    51 #define TCDEBUGLOGA3(s, a1, a2, a3)
       
    52 #define TCDEBUGCLOSE()
       
    53 #endif
       
    54 
       
    55 CServerCommand::CServerCommand()
       
    56 {
       
    57 	// Server commands/responses
       
    58 	m_ServerCommandMutex.Open(SERVERCOMMANDDATA_MUTEX_NAME, SERVERCOMMANDDATA_MUTEX_TIMEOUT);
       
    59 	m_ServerCommandData.Open(SERVERCOMMANDDATA_MAP_SIZE, SERVERCOMMANDDATA_MAP_NAME);
       
    60 	m_ServerCommandData.Init();
       
    61 	m_ServerMessageData.Open(SERVERMESSAGEDATA_MAP_SIZE, SERVERMESSAGEDATA_MAP_NAME);
       
    62 	m_ServerMessageData.Init();
       
    63 
       
    64 	m_ServerProcessData.Open(SERVERPROCESSDATA_MAP_SIZE, SERVERPROCESSDATA_MAP_NAME);
       
    65 	m_ServerProcessData.Init();
       
    66 
       
    67 	// General server access
       
    68 	m_ServerPipeMutex.Open(SERVERPIPE_MUTEX_NAME, SERVERPIPE_MUTEX_TIMEOUT);
       
    69 
       
    70 	// command/response events
       
    71 	m_hServerCommandReadyEvent = ::CreateEvent(NULL, FALSE, FALSE, SERVER_COMMAND_READY_EVENTNAME);
       
    72 	m_hServerResponseReadyEvent = ::CreateEvent(NULL, FALSE, FALSE, SERVER_RESPONSE_READY_EVENTNAME);
       
    73 }
       
    74 
       
    75 CServerCommand::~CServerCommand()
       
    76 {
       
    77 	m_ServerCommandMutex.Close(); 
       
    78 	m_ServerCommandData.Close();
       
    79 	m_ServerMessageData.Close();
       
    80 
       
    81 	m_ServerProcessData.Close();
       
    82 
       
    83 	m_ServerPipeMutex.Close();
       
    84 
       
    85 	::CloseHandle(m_hServerCommandReadyEvent);
       
    86 	::CloseHandle(m_hServerResponseReadyEvent);
       
    87 
       
    88 }
       
    89 
       
    90 // Client methods
       
    91 BOOL CServerCommand::SendCommand(pServerCommandData pCmd, DWORD msgLength, BYTE* message)
       
    92 {
       
    93 	BOOL sent = FALSE;
       
    94 
       
    95 //	if (pCmd == NULL) return sent;
       
    96 
       
    97 	TCDEBUGOPEN();
       
    98 	TCDEBUGLOGS("CServerCommand::SendCommand\n");
       
    99 	TCDEBUGCLOSE();
       
   100 
       
   101 	WaitForServerCommandAccess();
       
   102 	
       
   103 	pServerCommandData pData1 = GetDataPtr();
       
   104 
       
   105 	if (pCmd->command != eCmdNone)
       
   106 	{
       
   107 		memcpy(pData1, pCmd, sizeof(ServerCommandData));
       
   108 		if (msgLength > 0 && message != NULL)
       
   109 		{
       
   110 			pServerMessageData pData2 = GetMsgPtr();
       
   111 			pData2->length = msgLength;
       
   112 			if (msgLength > 0)
       
   113 				memcpy(pData2->message, message, msgLength);
       
   114 		}
       
   115 		pData1->response = eRspNone; // setup for response
       
   116 		sent = TRUE;
       
   117 		::SetEvent(m_hServerCommandReadyEvent);
       
   118 	}
       
   119 
       
   120 	ReleaseServerCommandAccess();
       
   121 	TCDEBUGOPEN();
       
   122 	TCDEBUGLOGS("CServerCommand::SendCommand done\n");
       
   123 	TCDEBUGCLOSE();
       
   124 
       
   125 	return sent;
       
   126 }
       
   127 
       
   128 BOOL CServerCommand::GetResponse(pServerCommandData pRsp)
       
   129 {
       
   130 	BOOL found = FALSE;
       
   131 
       
   132 	TCDEBUGOPEN();
       
   133 	TCDEBUGLOGS("CServerCommand::GetResponse\n");
       
   134 	TCDEBUGCLOSE();
       
   135 
       
   136 	if (::WaitForSingleObject(m_hServerResponseReadyEvent, SERVER_CMDRSP_EVENT_TIMEOUT) == WAIT_OBJECT_0)
       
   137 	{
       
   138 		WaitForServerCommandAccess();
       
   139 		pServerCommandData pData = GetDataPtr();
       
   140 		if (pData->response != eRspNone)
       
   141 		{
       
   142 			// response is ready when command is cleared
       
   143 			memcpy(pRsp, pData, sizeof(ServerCommandData));
       
   144 			found = TRUE;
       
   145 		}
       
   146 		ReleaseServerCommandAccess();
       
   147 	}
       
   148 	else
       
   149 	{
       
   150 		TCDEBUGLOGS("CServerCommand::GetResponse timeout\n");
       
   151 		pRsp->response = eRspError;
       
   152 		pRsp->error = TCAPI_ERR_COMM_SERVER_RESPONSE_TIMEOUT;
       
   153 	}
       
   154 #if (0)
       
   155 
       
   156 	BOOL timeoutoccurred = FALSE;
       
   157 	pServerCommandData pData = GetDataPtr();
       
   158 	time_t ctime = time(NULL);
       
   159 	time_t timeout = ctime + 60; // wait 60 seconds only
       
   160 
       
   161 //	if (pRsp == NULL) return found;
       
   162 
       
   163 	TCDEBUGOPEN();
       
   164 	TCDEBUGLOGA2("CServerCommand::GetResponse time = %d timeout = %d\n", ctime, timeout);
       
   165 	TCDEBUGCLOSE();
       
   166 
       
   167 	while(!found && !timeoutoccurred)
       
   168 	{
       
   169 		WaitForServerCommandAccess();
       
   170 		if (pData->response != eRspNone)
       
   171 		{
       
   172 			// response is ready when command is cleared
       
   173 			memcpy(pRsp, pData, sizeof(ServerCommandData));
       
   174 			found = TRUE;
       
   175 		}
       
   176 		else
       
   177 		{
       
   178 			time_t ctime = time(NULL);
       
   179 			if (ctime >= timeout)
       
   180 			{
       
   181 				TCDEBUGLOGS("CServerCommand::GetResponse timeout\n");
       
   182 				pRsp->response = eRspError;
       
   183 				pRsp->error = TCAPI_ERR_COMM_SERVER_RESPONSE_TIMEOUT;
       
   184 				timeoutoccurred = TRUE;
       
   185 			}
       
   186 		}
       
   187 		ReleaseServerCommandAccess();
       
   188 		Sleep(1);
       
   189 	}
       
   190 #endif
       
   191 	TCDEBUGOPEN();
       
   192 	TCDEBUGLOGA1("CServerCommand::GetResponse waiting for response found=%d\n", found);
       
   193 	TCDEBUGCLOSE();
       
   194 
       
   195 	return found;
       
   196 }
       
   197 // Server methods
       
   198 BOOL CServerCommand::GetCommand(pServerCommandData pCmd, pServerMessageData pMsg)
       
   199 {
       
   200 	BOOL found = FALSE;
       
   201 
       
   202 	if (::WaitForSingleObject(m_hServerCommandReadyEvent, SERVER_CMDRSP_EVENT_TIMEOUT) == WAIT_OBJECT_0)
       
   203 	{
       
   204 		pServerCommandData pData1 = GetDataPtr();
       
   205 
       
   206 		WaitForServerCommandAccess();
       
   207 		if (pData1->command != eCmdNone)
       
   208 		{
       
   209 			memcpy(pCmd, pData1, sizeof(ServerCommandData));
       
   210 			if (pMsg)
       
   211 			{
       
   212 				pServerMessageData pData2 = GetMsgPtr();
       
   213 				pMsg->length = pData2->length;
       
   214 				memcpy(pMsg->message, pData2->message, pData2->length);
       
   215 			}
       
   216 			found = TRUE;
       
   217 		}
       
   218 		ReleaseServerCommandAccess();
       
   219 	}
       
   220 
       
   221 	return found;
       
   222 }
       
   223 
       
   224 BOOL CServerCommand::SendResponse(pServerCommandData pRsp)
       
   225 {
       
   226 	BOOL sent = FALSE;
       
   227 	pServerCommandData pData = GetDataPtr();
       
   228 
       
   229 	WaitForServerCommandAccess();
       
   230 	if (pRsp->response != eRspNone)
       
   231 	{
       
   232 		memcpy(pData, pRsp, sizeof(ServerCommandData));
       
   233 		pData->command = eCmdNone; // setup for next command
       
   234 		sent = TRUE;
       
   235 		::SetEvent(m_hServerResponseReadyEvent);
       
   236 	}
       
   237 
       
   238 	ReleaseServerCommandAccess();
       
   239 
       
   240 	return sent;
       
   241 }