accessoryservices/remotecontrolfw/server/src/connectionhistory.cpp
changeset 0 4e1aa6a622a0
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 /**
       
    17  @file
       
    18  @internalComponent
       
    19 */
       
    20 
       
    21 #include <bluetooth/logger.h>
       
    22 #include "connectionhistory.h"
       
    23 #include "connections.h"
       
    24 #include "utils.h"
       
    25 
       
    26 #ifdef __FLOG_ACTIVE
       
    27 _LIT8(KLogComponent, LOG_COMPONENT_REMCON_SERVER);
       
    28 #endif
       
    29 
       
    30 #ifdef _DEBUG
       
    31 PANICCATEGORY("connhist");
       
    32 #endif
       
    33 
       
    34 #ifdef __FLOG_ACTIVE
       
    35 #define LOGCOLLECTIONPOOL	LogCollectionPool()
       
    36 #define LOGADDRESSPOOL		LogAddressPool()
       
    37 #else
       
    38 #define LOGCOLLECTIONPOOL
       
    39 #define LOGADDRESSPOOL
       
    40 #endif // __FLOG_ACTIVE
       
    41 
       
    42 CConnectionHistory* CConnectionHistory::NewL()
       
    43 	{
       
    44 	LOG_STATIC_FUNC
       
    45 	CConnectionHistory* self = new(ELeave) CConnectionHistory();
       
    46 	CleanupStack::PushL(self);
       
    47 	self->ConstructL();
       
    48 	CLEANUPSTACK_POP1(self);
       
    49 	return self;
       
    50 	}
       
    51 
       
    52 CConnectionHistory::CConnectionHistory()
       
    53  :	iHistory(_FOFF(CConnections, iLink)),
       
    54 	iConnectionsPool(_FOFF(CConnections, iLink)),
       
    55 	iAddressPool(_FOFF(TRemConAddress, iLink))
       
    56 	{
       
    57 	LOG_FUNC
       
    58 	}
       
    59 
       
    60 void CConnectionHistory::ConstructL()
       
    61 	{
       
    62 	LOG_FUNC;
       
    63 
       
    64 	// Start the history off with the start-up state of the system- no 
       
    65 	// connections.
       
    66 	CConnections* const conns = CConnections::NewL();
       
    67 	iHistory.AddLast(*conns);
       
    68 
       
    69 	LogConnectionHistory();
       
    70 	}
       
    71 
       
    72 CConnectionHistory::~CConnectionHistory()
       
    73 	{
       
    74 	LOG_FUNC;
       
    75 	LogConnectionHistory();
       
    76 	LOGCOLLECTIONPOOL;
       
    77 	LOGADDRESSPOOL;
       
    78 
       
    79 	// Clean up all the collections of everything!
       
    80 
       
    81 	LOG(_L("\tdestroying history..."));
       
    82 		{
       
    83 		TSglQueIter<CConnections> iter1(iHistory);
       
    84 		CConnections* conns1;
       
    85 		while ( ( conns1 = iter1++ ) != NULL )
       
    86 			{
       
    87 			iHistory.Remove(*conns1);
       
    88 			delete conns1;
       
    89 			}
       
    90 		}
       
    91 
       
    92 	LOG(_L("\tdestroying connections pool..."));
       
    93 		{
       
    94 		TSglQueIter<CConnections> iter2(iConnectionsPool);
       
    95 		CConnections* conns2;
       
    96 		while ( ( conns2 = iter2++ ) != NULL )
       
    97 			{
       
    98 			iConnectionsPool.Remove(*conns2);
       
    99 			delete conns2;
       
   100 			}
       
   101 		}
       
   102 
       
   103 	LOG(_L("\tdestroying address pool..."));
       
   104 		{
       
   105 		TSglQueIter<TRemConAddress> iter3(iAddressPool);
       
   106 		TRemConAddress* addr;		 
       
   107 		while ( ( addr = iter3++ ) != NULL )
       
   108 			{
       
   109 			iAddressPool.Remove(*addr);
       
   110 			delete addr;
       
   111 			}
       
   112 		}
       
   113 	
       
   114 	// Can't LogConnectionHistory here because it breaks our invariant (we've 
       
   115 	// deleted all the history items, so it's empty and Count == 0).
       
   116 	LOGCOLLECTIONPOOL;
       
   117 	LOGADDRESSPOOL;
       
   118 	}
       
   119 
       
   120 void CConnectionHistory::DestroyFirst()
       
   121 	{
       
   122 	LOG_FUNC;
       
   123 	LogConnectionHistory();
       
   124 
       
   125 	ASSERT_DEBUG(!iHistory.IsEmpty());
       
   126 	CConnections* conn = iHistory.First();
       
   127 	ASSERT_DEBUG(conn);
       
   128 	iHistory.Remove(*conn);
       
   129 	delete conn;
       
   130 	ASSERT_DEBUG(!iHistory.IsEmpty());
       
   131 
       
   132 	LogConnectionHistory();
       
   133 	}
       
   134 
       
   135 TUint CConnectionHistory::Count() const
       
   136 	{
       
   137 	TUint count = 0;
       
   138 
       
   139 	TSglQueIter<CConnections> iter(const_cast<CConnectionHistory*>(this)->iHistory);
       
   140 	while ( iter++ != NULL )
       
   141 		{
       
   142 		++count;
       
   143 		}
       
   144 
       
   145 	ASSERT_DEBUG(count >= 1);
       
   146 	return count;
       
   147 	}
       
   148 
       
   149 const CConnections& CConnectionHistory::operator[](TUint aIndex)
       
   150 	{
       
   151 	TSglQueIter<CConnections> iter(iHistory);
       
   152 	CConnections* conn;
       
   153 	TUint count = 0;
       
   154 	while ( ( conn = iter++ ) != NULL )
       
   155 		{
       
   156 		if ( count == aIndex )
       
   157 			{
       
   158 			break;
       
   159 			}
       
   160 		++count;
       
   161 		}
       
   162 	ASSERT_DEBUG(conn);
       
   163 	return *conn;
       
   164 	}
       
   165 
       
   166 CConnections& CConnectionHistory::Last()
       
   167 	{
       
   168 	// The connection history should never be empty. It should always hold at 
       
   169 	// least the current connection set.
       
   170 	ASSERT_DEBUG(!iHistory.IsEmpty());
       
   171 
       
   172 	CConnections* last = iHistory.Last();
       
   173 	ASSERT_DEBUG(last);
       
   174 	return *last;
       
   175 	}
       
   176 
       
   177 TInt CConnectionHistory::NewConnection(const TRemConAddress& aAddr)
       
   178 	{
       
   179 	LOG_FUNC;
       
   180 	LogConnectionHistory();
       
   181 	LOGCOLLECTIONPOOL;
       
   182 	LOGADDRESSPOOL;
       
   183 
       
   184 	TRAPD(err, NewConnectionL(aAddr));
       
   185 	LOG1(_L("\terr = %d"), err);
       
   186 	
       
   187 	LogConnectionHistory();
       
   188 	LOGCOLLECTIONPOOL;
       
   189 	LOGADDRESSPOOL;
       
   190 
       
   191 	return err;
       
   192 	}
       
   193 
       
   194 void CConnectionHistory::NewConnectionL(const TRemConAddress& aAddr)
       
   195 	{
       
   196 	// Called to handle a new connection by updating the connection history.
       
   197 	// Create a copy of the Last item (CConnections) in the history, add the 
       
   198 	// new address to it, and add that (the new CConnections) to the end of 
       
   199 	// the history.
       
   200 	// Then, in order to guarantee that any subsequent disconnection will 
       
   201 	// work, allocate a new CConnections and add it to the pool of 
       
   202 	// CConnections iConnectionsPool. Also allocate N TRemConAddresses and add 
       
   203 	// them to the pool iAddressPool, where N is the number of remotes in the 
       
   204 	// old head CConnections. 
       
   205 	// When a disconnection occurs, the new CConnections will be got from 
       
   206 	// iConnectionsPool, and the required TRemConAddresses will be got from 
       
   207 	// iAddressPool- without failing.
       
   208 	// If any of this fails, roll us back to how we were at the beginning of 
       
   209 	// the call and leave.
       
   210 	// At the end the history will have a new Last, with one new item in it. 
       
   211 
       
   212 	// Make a new item for the history...
       
   213 	CConnections* const newSetOfConnections = CConnections::CopyL(Last());
       
   214 	CleanupStack::PushL(newSetOfConnections);
       
   215 	TRemConAddress* const addr = new(ELeave) TRemConAddress;
       
   216 	*addr = aAddr;
       
   217 	newSetOfConnections->Append(*addr);
       
   218 	// Leave newSetOfConnections on the cleanup stack so if we leave it gets 
       
   219 	// destroyed automatically.
       
   220 	
       
   221 	// ...and pre-allocate memory.
       
   222 	CConnections* conn = CConnections::NewL();
       
   223 	CleanupStack::PushL(conn); // leave this on the cleanup stack so it gets 
       
   224 	// dealt with if we leave.
       
   225 
       
   226 	// Get the number of TRemConAddresses we need to pre-allocate.
       
   227 	const TUint count = Last().Count();
       
   228 	for ( TUint ii = 0 ; ii < count ; ++ii )
       
   229 		{
       
   230 		TRemConAddress* preAllocAddr = new TRemConAddress;
       
   231 		if ( preAllocAddr )
       
   232 			{
       
   233 			iAddressPool.AddLast(*preAllocAddr);
       
   234 			}
       
   235 		else
       
   236 			{
       
   237 			// We couldn't pre-allocate as much as we needed... destroy as 
       
   238 			// many TRemConAddresses as we have already made and leave. 
       
   239 			for ( TUint jj = 0 ; jj < ii ; ++jj )
       
   240 				{
       
   241 				ASSERT_DEBUG(!iAddressPool.IsEmpty());
       
   242 				TRemConAddress* removeAddr = iAddressPool.Last();
       
   243 				ASSERT_DEBUG(removeAddr);
       
   244 				iAddressPool.Remove(*removeAddr);
       
   245 				delete removeAddr;
       
   246 				}
       
   247 			// This leave will clean up the pre-allocated CConnections and the 
       
   248 			// new 'real' CConnections.
       
   249 			LEAVEIFERRORL(KErrNoMemory);
       
   250 			}
       
   251 		}
       
   252 
       
   253 	// If we got to here, do some sanity checking, clean up the cleanup stack 
       
   254 	// (!) and finally add the new history item.
       
   255 	CLEANUPSTACK_POP1(conn);
       
   256 	iConnectionsPool.AddLast(*conn);
       
   257 	
       
   258 	ASSERT_DEBUG(newSetOfConnections->Count() == Last().Count() + 1);
       
   259 	
       
   260 	CLEANUPSTACK_POP1(newSetOfConnections);
       
   261 	iHistory.AddLast(*newSetOfConnections); 
       
   262 	}
       
   263 
       
   264 void CConnectionHistory::Disconnection(const TRemConAddress& aAddr)
       
   265 	{
       
   266 	LOG_FUNC;
       
   267 	LogConnectionHistory();
       
   268 	LOGCOLLECTIONPOOL;
       
   269 	LOGADDRESSPOOL;
       
   270 	
       
   271 	// Called to handle a disconnection by updating the connection history.
       
   272 	// In order for this operation to be guaranteed to work, we must take a 
       
   273 	// CConnections from the pool iConnectionsPool, and, for each 
       
   274 	// TRemConAddress in Last, _except the one being disconnected in this 
       
   275 	// call_, copy it into a TRemConAddress taken from the iAddressPool, and 
       
   276 	// add it to the reclaimed CConnections. Finally add the CConnections to 
       
   277 	// the history.
       
   278 	// At the end the history will have a new Last with one less remote in it 
       
   279 	// than the previous.
       
   280 
       
   281 	// Get a CConnections from the pool.
       
   282 	ASSERT_DEBUG(!iConnectionsPool.IsEmpty());
       
   283 	CConnections* newSetOfConnections = iConnectionsPool.Last();
       
   284 	ASSERT_DEBUG(newSetOfConnections);
       
   285 	iConnectionsPool.Remove(*newSetOfConnections);
       
   286 
       
   287 	// Copy addresses from Last into newSetOfConnections, except the one being 
       
   288 	// disconnected.
       
   289 	CConnections& last = Last();
       
   290 	TSglQueIter<TRemConAddress>& iter = last.SetToFirst();
       
   291 	TRemConAddress* conn = NULL;
       
   292 	while ( ( conn = iter++ ) != NULL )
       
   293 		{
       
   294 		if ( !(*conn == aAddr) )
       
   295 			{
       
   296 			ASSERT_DEBUG(!iAddressPool.IsEmpty());
       
   297 			TRemConAddress* const newAddr = iAddressPool.Last();
       
   298 			ASSERT_DEBUG(newAddr);
       
   299 			iAddressPool.Remove(*newAddr);
       
   300 			*newAddr = *conn;
       
   301 			newSetOfConnections->Append(*newAddr);
       
   302 			}
       
   303 		}
       
   304 
       
   305 	// Sanity check and finally add the new CConnections to the history.
       
   306 	ASSERT_DEBUG(newSetOfConnections->Count() == Last().Count() - 1);
       
   307 	iHistory.AddLast(*newSetOfConnections);
       
   308 
       
   309 	LogConnectionHistory();
       
   310 	LOGCOLLECTIONPOOL;
       
   311 	LOGADDRESSPOOL;
       
   312 	}
       
   313 
       
   314 void CConnectionHistory::LogConnectionHistory() const
       
   315 	{
       
   316 #ifdef __FLOG_ACTIVE
       
   317 
       
   318 	const TUint count = Count();
       
   319 	LOG1(_L("\tNumber of items in history = %d"), count);
       
   320 	TSglQueIter<CConnections> iter(const_cast<CConnectionHistory*>(this)->iHistory);
       
   321 	CConnections* conns;
       
   322 	while ( ( conns = iter++ ) != NULL )
       
   323 		{
       
   324 		LOG1(_L("\t\titem [0x%08x]:"), conns);
       
   325 		conns->LogConnections();
       
   326 		}
       
   327 
       
   328 #endif // __FLOG_ACTIVE
       
   329 	}
       
   330 
       
   331 #ifdef __FLOG_ACTIVE
       
   332 void CConnectionHistory::LogCollectionPool() const
       
   333 	{
       
   334 	LOG(_L("\tLogging pre-allocated connections pool"));
       
   335 	TSglQueIter<CConnections> iter(const_cast<CConnectionHistory*>(this)->iConnectionsPool);
       
   336 	CConnections* conns;
       
   337 	while ( ( conns = iter++ ) != NULL )
       
   338 		{
       
   339 		LOG1(_L("\t\titem [0x%08x]:"), conns);
       
   340 		}
       
   341 	}
       
   342 
       
   343 void CConnectionHistory::LogAddressPool() const
       
   344 	{
       
   345 	LOG(_L("\tLogging pre-allocated address pool"));
       
   346 	TSglQueIter<TRemConAddress> iter(const_cast<CConnectionHistory*>(this)->iAddressPool);
       
   347 	TRemConAddress* addr;
       
   348 	while ( ( addr = iter++ ) != NULL )
       
   349 		{
       
   350 		LOG1(_L("\t\titem [0x%08x]:"), addr);
       
   351 		}
       
   352 	}
       
   353 #endif // __FLOG_ACTIVE