applayerprotocols/httpexamples/nwsswsptrhnd/CNwssTransLookUpTable.cpp
changeset 0 b16258d2340f
equal deleted inserted replaced
-1:000000000000 0:b16258d2340f
       
     1 // Copyright (c) 2002-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 // System includes
       
    15 // 
       
    16 //
       
    17 
       
    18 // Local includes
       
    19 #include "cnwsswsptrhnddatasupplier.h"
       
    20 #include "tnwsswsptrhndpanic.h"
       
    21 
       
    22 // Class signature
       
    23 #include "cnwsstranslookuptable.h"
       
    24 
       
    25 
       
    26 
       
    27 CNwssTransLookUpTable::CNwssTransLUTEntry* CNwssTransLookUpTable::CNwssTransLUTEntry::NewL()
       
    28 	{
       
    29 	return new(ELeave) CNwssTransLookUpTable::CNwssTransLUTEntry;
       
    30 	}
       
    31 
       
    32 CNwssTransLookUpTable::CNwssTransLUTEntry::CNwssTransLUTEntry()
       
    33 	{
       
    34 	}
       
    35 
       
    36 CNwssTransLookUpTable::CNwssTransLUTEntry::~CNwssTransLUTEntry()
       
    37 	{
       
    38 	delete iResponseBodyHandler;
       
    39 	}
       
    40 
       
    41 CNwssTransLookUpTable* CNwssTransLookUpTable::NewL(TInt aTableSize)
       
    42 	{
       
    43 	CNwssTransLookUpTable* me = new(ELeave)CNwssTransLookUpTable();
       
    44 	CleanupStack::PushL(me);
       
    45 	me->ConstructL(aTableSize);
       
    46 	CleanupStack::Pop(me);
       
    47 	return me;
       
    48 	}
       
    49 
       
    50 CNwssTransLookUpTable::CNwssTransLookUpTable()
       
    51 	{
       
    52 	}
       
    53 
       
    54 CNwssTransLookUpTable::~CNwssTransLookUpTable()
       
    55 	{
       
    56 	// Flush the look-up table.  Entries which are unused will delete OK since
       
    57 	// when removed, their iResponseBodyHandler pointer is reset to NULL.
       
    58 	iEntries.ResetAndDestroy();
       
    59 	}
       
    60 
       
    61 CNwssTransLookUpTable::CNwssTransLUTEntry& CNwssTransLookUpTable::NewEntry()
       
    62 	{
       
    63 	TInt numTableEntries = iEntries.Count();
       
    64 #ifdef __UNIT_TESTING__
       
    65 	if (numTableEntries == 0)
       
    66 		// dummy return for testing only
       
    67 		return *(REINTERPRET_CAST(CNwssTransLookUpTable::CNwssTransLUTEntry*, 1));
       
    68 #endif
       
    69 	__ASSERT_DEBUG(numTableEntries > 0, User::Invariant());
       
    70 
       
    71 	CNwssTransLookUpTable::CNwssTransLUTEntry* retVal = NULL;
       
    72 	if (iNumActiveLUTEntries < numTableEntries)
       
    73 		retVal = iEntries[iNumActiveLUTEntries++];
       
    74 #ifdef __UNIT_TESTING__
       
    75 	else
       
    76 		// dummy return for testing only
       
    77 		return *(REINTERPRET_CAST(CNwssTransLookUpTable::CNwssTransLUTEntry*, 1));
       
    78 #else
       
    79 	else
       
    80 		TNwssWspTrHndPanic::Panic(TNwssWspTrHndPanic::ETransLUTOverflow);
       
    81 #endif
       
    82 	return *retVal;
       
    83 	}
       
    84 
       
    85 CNwssTransLookUpTable::CNwssTransLUTEntry&
       
    86 CNwssTransLookUpTable::LookUpByTransId(RWSPCOTrans::TTransID aTransId, TBool& aFound)
       
    87 	{
       
    88 	// Iterate the look-up-table until we find the entry whose transaction ID matches
       
    89 	// the supplied value
       
    90 	aFound = EFalse;
       
    91 	TInt idx = 0;
       
    92 	while (!aFound && (idx < iNumActiveLUTEntries))
       
    93 		{
       
    94 		if (iEntries[idx]->iStackTransID == aTransId)
       
    95 			aFound = ETrue;
       
    96 		else
       
    97 			++idx;
       
    98 		}
       
    99 	if (aFound)
       
   100 		return *(iEntries[idx]);
       
   101 
       
   102 	// dummy return
       
   103 	CNwssTransLookUpTable::CNwssTransLUTEntry* retVal = NULL;
       
   104 	return *retVal;
       
   105 	}
       
   106 
       
   107 CNwssTransLookUpTable::CNwssTransLUTEntry&
       
   108 CNwssTransLookUpTable::LookUpByCallback(MWspCOMethodCallback& aCallback, TBool& aFound)
       
   109 	{
       
   110 	// Locate the item in the table
       
   111 	aFound = EFalse;
       
   112 	TInt unused = KErrNotFound;
       
   113 	CNwssTransLookUpTable::CNwssTransLUTEntry* lutEntry = LookUp(aCallback, unused);
       
   114 	if (lutEntry)
       
   115 		{
       
   116 		aFound = ETrue;
       
   117 		return *lutEntry;
       
   118 		}
       
   119 
       
   120 	// dummy return
       
   121 	CNwssTransLookUpTable::CNwssTransLUTEntry* retVal = NULL;
       
   122 	return *retVal;
       
   123 	}
       
   124 
       
   125 void CNwssTransLookUpTable::RemoveEntry(MWspCOMethodCallback& aCallback)
       
   126 	{
       
   127 	// Locate the item in the table
       
   128 	TInt idx = KErrNotFound;
       
   129 	CNwssTransLookUpTable::CNwssTransLUTEntry* lutEntry = LookUp(aCallback, idx);
       
   130 	if (lutEntry)
       
   131 		{
       
   132 		iEntries.Remove(idx);
       
   133 		--iNumActiveLUTEntries;
       
   134 
       
   135 		// Reset the entry, so it can be recycled
       
   136 		lutEntry->iCallback = NULL;
       
   137 		delete lutEntry->iResponseBodyHandler;
       
   138 		lutEntry->iResponseBodyHandler = NULL;
       
   139 		lutEntry->iStackTrans = RWSPCOTrans();
       
   140 		lutEntry->iStackTransID = 0;
       
   141 		lutEntry->iAborted = EFalse;
       
   142 
       
   143 		// Move the removed entry to the end of the table, to ensure the table remains
       
   144 		// of constant size.  Note, this append will not leave, since the table size
       
   145 		// was preallocated in ConstructL.
       
   146 #if defined (_DEBUG) && defined(__UNIT_TESTING__)
       
   147 		TInt err =
       
   148 #endif
       
   149 			iEntries.Append(lutEntry);
       
   150 #ifdef __UNIT_TESTING__
       
   151 		__ASSERT_DEBUG(err == KErrNone, 
       
   152 					TNwssWspTrHndPanic::Panic(TNwssWspTrHndPanic::ETransLUTOverflow));
       
   153 #endif
       
   154 		}
       
   155 	}
       
   156 
       
   157 TBool CNwssTransLookUpTable::IsEmpty() const
       
   158 	{
       
   159 	return iNumActiveLUTEntries == 0;
       
   160 	}
       
   161 
       
   162 CNwssTransLookUpTable::CNwssTransLUTEntry& CNwssTransLookUpTable::Head(TBool& aFound)
       
   163 	{
       
   164 	// Are there any entries
       
   165 	aFound = iNumActiveLUTEntries > 0;
       
   166 
       
   167 	// Return the first entry
       
   168 	if (aFound)
       
   169 		return *(iEntries[0]);
       
   170 
       
   171 	// dummy return
       
   172 	CNwssTransLookUpTable::CNwssTransLUTEntry* retVal = NULL;
       
   173 	return *retVal;
       
   174 	}
       
   175 
       
   176 void CNwssTransLookUpTable::ConstructL(TInt aTableSize)
       
   177 	{
       
   178 	CNwssTransLookUpTable::CNwssTransLUTEntry* entry;
       
   179 
       
   180 	// Pre-allocate the look-up table
       
   181 	for (TInt ii = 0; ii < aTableSize; ++ii)
       
   182 		{
       
   183 		entry = CNwssTransLookUpTable::CNwssTransLUTEntry::NewL();
       
   184 		CleanupStack::PushL(entry);
       
   185 		User::LeaveIfError(	iEntries.Append(entry) );
       
   186 		CleanupStack::Pop(entry);
       
   187 		}
       
   188 	}
       
   189 
       
   190 void CNwssTransLookUpTable::ResizeTableL(TInt aNewSize)
       
   191 	{
       
   192 	// Bail out now if the size doesn't change!
       
   193 	const TInt oldSize = iEntries.Count();
       
   194 	if (aNewSize == oldSize)
       
   195 		return;
       
   196 	CNwssTransLookUpTable::CNwssTransLUTEntry* entry = NULL;
       
   197 	if (aNewSize > oldSize)
       
   198 		{
       
   199 		// Table is being enlarged - pre-allocate new entries
       
   200 		for (TInt ii = 0; ii < (aNewSize - oldSize); ++ii)
       
   201 			{
       
   202 			entry = CNwssTransLookUpTable::CNwssTransLUTEntry::NewL();
       
   203 			CleanupStack::PushL(entry);
       
   204 			User::LeaveIfError(	iEntries.Append(entry) );
       
   205 			CleanupStack::Pop(entry);
       
   206 			}
       
   207 
       
   208 		// High-water mark doesn't change in this situation
       
   209 		}
       
   210 	else
       
   211 		{
       
   212 		// Table is being shrunk - remove surplus entries
       
   213 		for (TInt ii = oldSize - 1; ii >= aNewSize; --ii)
       
   214 			{
       
   215 			delete iEntries[ii];
       
   216 			iEntries.Remove(ii);
       
   217 			}
       
   218 
       
   219 		// Adjust high-water mark
       
   220 		if (iNumActiveLUTEntries > aNewSize)
       
   221 			iNumActiveLUTEntries = aNewSize;
       
   222 		}
       
   223 	}
       
   224 
       
   225 CNwssTransLookUpTable::CNwssTransLUTEntry*
       
   226 CNwssTransLookUpTable::LookUp(MWspCOMethodCallback& aCallback, TInt& aIndex)
       
   227 	{
       
   228 	// Iterate the look-up-table until we find the entry whose transaction callback
       
   229 	// matches the supplied value
       
   230 	TBool found = EFalse;
       
   231 	aIndex = 0;
       
   232 	CNwssTransLookUpTable::CNwssTransLUTEntry* lutEntry = NULL;
       
   233 	while (!found && (aIndex < iNumActiveLUTEntries))
       
   234 		{
       
   235 		lutEntry = iEntries[aIndex];
       
   236 		if (lutEntry->iCallback == &aCallback)
       
   237 			found = ETrue;
       
   238 		else
       
   239 			++aIndex;
       
   240 		}
       
   241 	if (!found)
       
   242 		{
       
   243 		aIndex = KErrNotFound;
       
   244 		lutEntry = NULL;
       
   245 		}
       
   246 	return lutEntry;
       
   247 	}