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