connectivity/com.nokia.tcf/native/TCFNative/TCFServer/RegistryImpl.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 // RegistryImpl.cpp: implementation of the CRegistryImpl class.
       
    18 //
       
    19 //////////////////////////////////////////////////////////////////////
       
    20 
       
    21 #include "stdafx.h"
       
    22 #include "RegistryImpl.h"
       
    23 #include "TCErrorConstants.h"
       
    24 
       
    25 #ifdef _DEBUG
       
    26 extern BOOL gDoLogging;
       
    27 #endif
       
    28 
       
    29 //#define LOG_REGISTRY
       
    30 #if defined(LOG_REGISTRY) && defined(_DEBUG)
       
    31 extern char TCDebugMsg[];
       
    32 extern CServerManager* gManager;
       
    33 #define TCDEBUGOPEN() if (gDoLogging) { gManager->m_DebugLog->WaitForAccess(); }
       
    34 #define TCDEBUGLOGS(s) if (gDoLogging) { sprintf(TCDebugMsg,"%s", s); gManager->m_DebugLog->log(TCDebugMsg); }
       
    35 #define TCDEBUGLOGA1(s, a1) if (gDoLogging) { sprintf(TCDebugMsg, s, a1); gManager->m_DebugLog->log(TCDebugMsg); }
       
    36 #define TCDEBUGLOGA2(s, a1, a2) if (gDoLogging) { sprintf(TCDebugMsg, s, a1, a2); gManager->m_DebugLog->log(TCDebugMsg); }
       
    37 #define TCDEBUGLOGA3(s, a1, a2, a3) if (gDoLogging) { sprintf(TCDebugMsg, s, a1, a2, a3); gManager->m_DebugLog->log(TCDebugMsg); }
       
    38 #define TCDEBUGCLOSE() if (gDoLogging) { gManager->m_DebugLog->ReleaseAccess(); }
       
    39 #else
       
    40 #define TCDEBUGOPEN()
       
    41 #define TCDEBUGLOGS(s)
       
    42 #define TCDEBUGLOGA1(s, a1)
       
    43 #define TCDEBUGLOGA2(s, a1, a2)
       
    44 #define TCDEBUGLOGA3(s, a1, a2, a3)
       
    45 #define TCDEBUGCLOSE()
       
    46 #endif
       
    47 
       
    48 //////////////////////////////////////////////////////////////////////
       
    49 // Construction/Destruction
       
    50 //////////////////////////////////////////////////////////////////////
       
    51 
       
    52 CRegistryImpl::CRegistryImpl()
       
    53 {
       
    54 	TCDEBUGOPEN();
       
    55 	TCDEBUGLOGS("CRegistryImpl::CRegistryImpl\n");
       
    56 	TCDEBUGCLOSE();
       
    57 }
       
    58 
       
    59 CRegistryImpl::CRegistryImpl(DWORD connectionId)
       
    60 {
       
    61 	TCDEBUGOPEN();
       
    62 	TCDEBUGLOGA1("CRegistryImpl::CRegistryImpl connectionId = %d\n", connectionId);
       
    63 
       
    64 	for (int i = 0; i < MAX_MESSAGE_IDS; i++)
       
    65 	{
       
    66 		m_Registry[i].clist = new IdClientList();
       
    67 		m_Registry[i].clist->clear();
       
    68 	}
       
    69 
       
    70 	char mutexName[200];
       
    71 
       
    72 	sprintf(mutexName, "%s%d", REGISTRY_MUTEX_BASENAME, connectionId);
       
    73 	m_Mutex.Open(mutexName, REGISTRY_MUTEX_TIMEOUT);
       
    74 	TCDEBUGCLOSE();
       
    75 }
       
    76 
       
    77 CRegistryImpl::~CRegistryImpl()
       
    78 {
       
    79 	TCDEBUGOPEN();
       
    80 	TCDEBUGLOGS("CRegistryImpl::~CRegistryImpl\n");
       
    81 
       
    82 	for (int i = 0; i < MAX_MESSAGE_IDS; i++)
       
    83 	{
       
    84 		if (m_Registry[i].clist != NULL)
       
    85 		{
       
    86 			m_Registry[i].clist->clear();
       
    87 			delete m_Registry[i].clist;
       
    88 		}
       
    89 	}
       
    90 	m_Mutex.Close();
       
    91 	TCDEBUGCLOSE();
       
    92 }
       
    93 
       
    94 BOOL CRegistryImpl::AddClient(CClient* newClient, long numberIds, BYTE* ids)
       
    95 {
       
    96 	TCDEBUGOPEN();
       
    97 	TCDEBUGLOGS("CRegistryImpl::AddClient\n");
       
    98 
       
    99 	m_Mutex.Wait();
       
   100 
       
   101 	for (int i = 0; i < numberIds; i++)
       
   102 	{
       
   103 		TCDEBUGLOGA1(" id=%x\n", ids[i]);
       
   104 		m_Registry[ids[i]].clist->push_back(newClient);
       
   105 	}
       
   106 
       
   107 	DumpRegistry();
       
   108 	m_Mutex.Release();
       
   109 	TCDEBUGCLOSE();
       
   110 	return TRUE;
       
   111 }
       
   112 BOOL CRegistryImpl::RemoveClient(CClient* client)
       
   113 {
       
   114 	TCDEBUGOPEN();
       
   115 	TCDEBUGLOGS("CRegistryImpl::RemoveClient\n");
       
   116 
       
   117 	m_Mutex.Wait();
       
   118 
       
   119 	BOOL found = FALSE;
       
   120 
       
   121 	for (int i = 0; i < MAX_MESSAGE_IDS; i++)
       
   122 	{
       
   123 		long num = m_Registry[i].clist->size();
       
   124 		if (num != 0)
       
   125 		{
       
   126 			TCDEBUGLOGA3(" CRegistryImpl::RemoveClient client = %x i = %x num = %d\n", client, i, num);
       
   127 			IdClientList::iterator iter;
       
   128 			for (iter = m_Registry[i].clist->begin(); iter != m_Registry[i].clist->end(); iter++)
       
   129 			{
       
   130 				TCDEBUGLOGA2(" CRegistryImpl::RemoveClient iter = %x *iter = %x\n", iter, *iter);
       
   131 				if (client == *iter)
       
   132 				{
       
   133 					m_Registry[i].clist->erase(iter);
       
   134 					found = TRUE;
       
   135 					break;
       
   136 				}
       
   137 			}
       
   138 		}
       
   139 	}
       
   140 
       
   141 	m_Mutex.Release();
       
   142 	TCDEBUGCLOSE();
       
   143 	return found;
       
   144 }
       
   145 
       
   146 void CRegistryImpl::DumpRegistry()
       
   147 {
       
   148 	for (int i = 0; i < MAX_MESSAGE_IDS; i++)
       
   149 	{
       
   150 		long num = m_Registry[i].clist->size();
       
   151 		if (num != 0)
       
   152 		{
       
   153 			TCDEBUGLOGA2(" CRegistryImpl::DumpRegistry i = %x num = %d\n", i, num);
       
   154 			IdClientList::iterator iter;
       
   155 			for (iter = m_Registry[i].clist->begin(); iter != m_Registry[i].clist->end(); iter++)
       
   156 			{
       
   157 				TCDEBUGLOGA2(" CRegistryImpl::DumpRegistry iter = %x *iter = %x\n", iter, *iter);
       
   158 			}
       
   159 		}
       
   160 	}
       
   161 }
       
   162 
       
   163 // fullmessage includes the protocol header
       
   164 // message is unwrapped from the protocol header
       
   165 // this is because some clients want the full message and some clients just want the actual message
       
   166 long CRegistryImpl::RouteMessage(BYTE msgId, BYTE* fullMessage, DWORD fullLen, BYTE* realMessage, DWORD realMessageLen)
       
   167 {
       
   168 
       
   169 	long err = TCAPI_ERR_NONE;
       
   170 
       
   171 //	TCDEBUGOPEN();
       
   172 //	TCDEBUGLOGS("CRegistryImpl::RouteMessage\n");
       
   173 //	TCDEBUGCLOSE();
       
   174 	m_Mutex.Wait();
       
   175 
       
   176 	if (msgId >= 0 && msgId < MAX_MESSAGE_IDS)
       
   177 	{
       
   178 		long numClients = m_Registry[msgId&0xff].clist->size();
       
   179 
       
   180 		TCDEBUGOPEN();
       
   181 		TCDEBUGLOGA2(" CRegistryImpl::RouteMessage msgId = %x numClients = %d\n", msgId, numClients);
       
   182 		TCDEBUGCLOSE();
       
   183 
       
   184 		if (numClients > 0)
       
   185 		{
       
   186 			IdClientList::iterator iter;
       
   187 			for (iter = m_Registry[msgId&0xff].clist->begin(); iter != m_Registry[msgId&0xff].clist->end(); iter++)
       
   188 			{
       
   189 				CClient* client = *iter;
       
   190 				if (client && client->IsStarted())
       
   191 				{
       
   192 					if (client->IsStreamOpen())
       
   193 					{
       
   194 						CInputStream* stream = client->GetInputStream();
       
   195 						// get unwrap format
       
   196 						if (client->m_Options.unWrapFormat == UNWRAP_LEAVE_HEADERS)
       
   197 						{
       
   198 							// use full message
       
   199 							err = stream->AddMessage(fullLen, fullMessage);
       
   200 							// routing errors here can be input stream overflows
       
   201 							//  notify this client right now (no OS error)
       
   202 							if (err != TCAPI_ERR_NONE)
       
   203 							{
       
   204 								client->m_ErrorMonitor->PutError(err, false, 0);
       
   205 								err = TCAPI_ERR_NONE;
       
   206 							}
       
   207 						}
       
   208 						else
       
   209 						{
       
   210 							// use raw message
       
   211 							err = stream->AddMessage(realMessageLen, realMessage);
       
   212 							// routing errors here can be input stream overflows
       
   213 							//  notify this client right now (no OS error)
       
   214 							if (err != TCAPI_ERR_NONE)
       
   215 							{
       
   216 								client->m_ErrorMonitor->PutError(err, false, 0);
       
   217 								err = TCAPI_ERR_NONE;
       
   218 							}
       
   219 						}
       
   220 					}
       
   221 					else if (client->IsMessageFileOpen())
       
   222 					{
       
   223 						CMessageFile* file = client->GetMessageFile();
       
   224 						// get unwrap format
       
   225 						if (client->m_Options.unWrapFormat == UNWRAP_LEAVE_HEADERS)
       
   226 						{
       
   227 							// use full message
       
   228 							err = file->AddMessage(fullLen, fullMessage);
       
   229 							// routing errors here can be input stream overflows
       
   230 							//  notify this client right now (no OS error)
       
   231 							if (err != TCAPI_ERR_NONE)
       
   232 							{
       
   233 								client->m_ErrorMonitor->PutError(err, false, 0);
       
   234 								err = TCAPI_ERR_NONE;
       
   235 							}
       
   236 						}
       
   237 						else
       
   238 						{
       
   239 							// use raw message
       
   240 							err = file->AddMessage(realMessageLen, realMessage);
       
   241 							// routing errors here can be input stream overflows
       
   242 							//  notify this client right now (no OS error)
       
   243 							if (err != TCAPI_ERR_NONE)
       
   244 							{
       
   245 								client->m_ErrorMonitor->PutError(err, false, 0);
       
   246 								err = TCAPI_ERR_NONE;
       
   247 							}
       
   248 						}
       
   249 					}
       
   250 				}
       
   251 			}
       
   252 		}
       
   253 	}
       
   254 
       
   255 	m_Mutex.Release();
       
   256 	return err;
       
   257 }
       
   258