sensorservices/sensorserver/src/server/sensrvtransactionqueue.cpp
changeset 0 4e1aa6a622a0
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 /*
       
     2 * Copyright (c) 2006 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 "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:  Sensor server transaction queue.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include "sensrvdefines.h"
       
    21 #include "sensrvtransactionqueue.h"
       
    22 #include "sensrvtransaction.h"
       
    23 #include "sensrvsession.h"
       
    24 
       
    25 // ---------------------------------------------------------------------------
       
    26 // 2-phase constructor
       
    27 // ---------------------------------------------------------------------------
       
    28 //
       
    29 CSensrvTransactionQueue* CSensrvTransactionQueue::NewL(TBool aOwned)
       
    30     {
       
    31     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvTransactionQueue::NewL()" ) ) );
       
    32 
       
    33     CSensrvTransactionQueue* self = new( ELeave ) CSensrvTransactionQueue(aOwned);
       
    34     CleanupStack::PushL(self);
       
    35     self->ConstructL();
       
    36     CleanupStack::Pop(self); 
       
    37     
       
    38     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvTransactionQueue::NewL - return 0x%x" ), self ) );
       
    39 
       
    40     return self;
       
    41     }
       
    42 
       
    43 // ---------------------------------------------------------------------------
       
    44 // C++ constructor
       
    45 // ---------------------------------------------------------------------------
       
    46 //
       
    47 CSensrvTransactionQueue::CSensrvTransactionQueue(TBool aOwned)
       
    48     : iTransactionPtrList(_FOFF(TLinkableTransactionPtr,iLink)),
       
    49       iTransactionPtrIter(iTransactionPtrList),
       
    50       iOwned(aOwned)
       
    51     {
       
    52     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvTransactionQueue::CSensrvTransactionQueue()" ) ) );
       
    53 
       
    54     // Nothing to do
       
    55 
       
    56     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvTransactionQueue::CSensrvTransactionQueue - return" ) ) );
       
    57     }
       
    58     
       
    59 // ---------------------------------------------------------------------------
       
    60 // 2nd phase of construction
       
    61 // ---------------------------------------------------------------------------
       
    62 //
       
    63 void CSensrvTransactionQueue::ConstructL()
       
    64     {
       
    65     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvTransactionQueue::ConstructL()" ) ) );
       
    66 
       
    67     iHeap = &User::Heap();
       
    68     
       
    69     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvTransactionQueue::ConstructL - return" ) ) );
       
    70     }
       
    71 
       
    72 
       
    73 // ---------------------------------------------------------------------------
       
    74 // Destructor
       
    75 // ---------------------------------------------------------------------------
       
    76 //
       
    77 CSensrvTransactionQueue::~CSensrvTransactionQueue()
       
    78     {
       
    79     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvTransactionQueue::~CSensrvTransactionQueue()" ) ) );
       
    80 
       
    81     TLinkableTransactionPtr* ptr = NULL;
       
    82     
       
    83     while (!iTransactionPtrList.IsEmpty())
       
    84         {
       
    85         ptr = iTransactionPtrList.First();
       
    86         iTransactionPtrList.Remove(*ptr);
       
    87         CSensrvTransaction* transaction = ptr->iTransaction;
       
    88         
       
    89         // If transactions are owned, they are all deleted. 
       
    90         if (iOwned)
       
    91             {
       
    92             transaction->SetErrorCode(KErrCancel);
       
    93             transaction->Complete();
       
    94             // Transactions are always created in same heap as queue with regular new,
       
    95             // so regular delete is ok
       
    96             delete transaction;
       
    97             }
       
    98         
       
    99         // Pointers are allocated directly on heap, so free them directly also just to be on safe side.
       
   100         if (iHeap)
       
   101             {
       
   102             iHeap->Free(ptr);
       
   103             }
       
   104         }
       
   105        
       
   106     // Reset to clear memory used by the list
       
   107     iTransactionPtrList.Reset();
       
   108         
       
   109     // iHeap is not owned
       
   110         
       
   111     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvTransactionQueue::~CSensrvTransactionQueue - return" ) ) );
       
   112     }
       
   113 
       
   114 
       
   115 // ---------------------------------------------------------------------------
       
   116 // Adds transaction to the end of the queue
       
   117 // ---------------------------------------------------------------------------
       
   118 //
       
   119 TInt CSensrvTransactionQueue::Add(CSensrvTransaction* aTransaction, TBool aLast)
       
   120     {
       
   121     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvTransactionQueue::Add(aTransaction: 0x%x, aLast: %d)" ), aTransaction, aLast ) );
       
   122 
       
   123     TInt err(KErrNone);
       
   124 
       
   125     if (aTransaction)
       
   126         {
       
   127         // Allocate linkable transaction pointer in same heap as queue
       
   128         TLinkableTransactionPtr* newPtr = reinterpret_cast<TLinkableTransactionPtr*>(iHeap->Alloc(sizeof(TLinkableTransactionPtr)));
       
   129 
       
   130         if (newPtr)
       
   131             {
       
   132             newPtr->iTransaction = aTransaction;
       
   133             if (aLast)
       
   134                 {
       
   135                 iTransactionPtrList.AddLast(*newPtr);
       
   136                 }
       
   137             else
       
   138                 {
       
   139                 iTransactionPtrList.AddFirst(*newPtr);
       
   140                 }
       
   141             }
       
   142         else
       
   143             {
       
   144             ERROR_TRACE( ( _L( "Sensor Server - CSensrvTransactionQueue::Add - ERROR: No memory to add item" ) ) );
       
   145             err = KErrNoMemory;
       
   146             }
       
   147         }
       
   148     else
       
   149         {
       
   150         ERROR_TRACE( ( _L( "Sensor Server - CSensrvTransactionQueue::Add - ERROR: Attempted to add NULL transaction" ) ) );
       
   151         err = KErrArgument;
       
   152         }
       
   153 
       
   154 #ifdef MEMORY_TRACE_DEBUG
       
   155     TInt count(0);
       
   156     if (!iTransactionPtrList.IsEmpty())
       
   157         {
       
   158         TLinkableTransactionPtr* ptr = NULL;
       
   159         iTransactionPtrIter.SetToFirst(); 
       
   160         while ((ptr = iTransactionPtrIter++) != NULL)
       
   161             {
       
   162             count++;
       
   163             // Do not print the entire queue if it has many items.
       
   164             if (count < 5)
       
   165                 {
       
   166                 MEMORY_TRACE( ( _L( "@TT: %d" ), ptr->iTransaction->Type() ) );
       
   167                 }
       
   168             else if (count == 5)
       
   169                 {
       
   170                 MEMORY_TRACE( ( _L( "@..." ) ) );
       
   171                 }
       
   172             };
       
   173         }
       
   174     MEMORY_TRACE( ( _L( "@Queue count %d" ), count ) );
       
   175 #endif
       
   176 
       
   177     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvTransactionQueue::Add - return %d" ), err ) );
       
   178     
       
   179     return err;
       
   180     }
       
   181 
       
   182 // ---------------------------------------------------------------------------
       
   183 // Remove transaction from queue
       
   184 // ---------------------------------------------------------------------------
       
   185 //
       
   186 void CSensrvTransactionQueue::Remove(CSensrvTransaction* aTransaction,
       
   187                                      TRemovalType aRemovalType)
       
   188     {
       
   189     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvTransactionQueue::Remove(aTransaction: 0x%x, %d)" ), aTransaction, aRemovalType ) );
       
   190 
       
   191     if (aTransaction)
       
   192         {
       
   193         TLinkableTransactionPtr* ptr = FindTransactionPtr(aTransaction);
       
   194         
       
   195         if (ptr)
       
   196             {
       
   197             RemovePtr(ptr, aRemovalType);
       
   198             }
       
   199         else
       
   200             {
       
   201             COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvTransactionQueue::Remove - Attempted to remove nonexisting transaction" ) ) );
       
   202             }
       
   203         }
       
   204     else
       
   205         {
       
   206         COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvTransactionQueue::Remove - Attempted to remove NULL transaction" ) ) );
       
   207         }
       
   208     }
       
   209 
       
   210 // ---------------------------------------------------------------------------
       
   211 // Remove all of session's transactions from queue.
       
   212 // ---------------------------------------------------------------------------
       
   213 //
       
   214 void CSensrvTransactionQueue::Remove(CSensrvSession* aSession)
       
   215     {
       
   216     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvTransactionQueue::Remove(aSession: 0x%x)" ), aSession ) );
       
   217 
       
   218     if (aSession)
       
   219         {
       
   220         TLinkableTransactionPtr* ptr = NULL;
       
   221         iTransactionPtrIter.SetToFirst(); 
       
   222         while ((ptr = iTransactionPtrIter++) != NULL)
       
   223             {
       
   224             if ( ptr->iTransaction->Message() )
       
   225                 {
       
   226                 if (ptr->iTransaction->Message()->Session() == aSession)    
       
   227                     {
       
   228                     RemovePtr(ptr, ERemovalTypeCancel);
       
   229                     }
       
   230                 }
       
   231             };
       
   232         }
       
   233     else
       
   234         {
       
   235         COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvTransactionQueue::Remove - Attempted to remove transactions for NULL session" ) ) );
       
   236         }
       
   237     }
       
   238 
       
   239 // ---------------------------------------------------------------------------
       
   240 // Remove all obsolete transactions from queue
       
   241 // ---------------------------------------------------------------------------
       
   242 //
       
   243 TInt CSensrvTransactionQueue::RemoveObsolete(const TTime& aCutoffTime)
       
   244     {
       
   245     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvTransactionQueue::RemoveObsolete(aCutoffTime: %LD)" ), aCutoffTime.Int64() ) );
       
   246 
       
   247     TInt count(0);
       
   248     TLinkableTransactionPtr* ptr = NULL;
       
   249     iTransactionPtrIter.SetToFirst(); 
       
   250     while ((ptr = iTransactionPtrIter++) != NULL)
       
   251         {
       
   252         if ( ptr->iTransaction->TimeStamp() <= aCutoffTime )
       
   253             {
       
   254             COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvTransactionQueue::RemoveObsolete - Removing obsolete transaction: 0x%x" ), ptr->iTransaction) );
       
   255 
       
   256             count++;
       
   257             
       
   258             RemovePtr(ptr, ERemovalTypeCancel);
       
   259             }
       
   260         };
       
   261 
       
   262     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvTransactionQueue::RemoveObsolete - return %d" ), count ) );
       
   263     
       
   264     return count;
       
   265     }
       
   266 
       
   267 // ---------------------------------------------------------------------------
       
   268 // Remove all transactions from queue
       
   269 // ---------------------------------------------------------------------------
       
   270 //
       
   271 void CSensrvTransactionQueue::RemoveAll()
       
   272     {
       
   273     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvTransactionQueue::RemoveAll()" ) ) );
       
   274 
       
   275     TLinkableTransactionPtr* ptr = NULL;
       
   276     iTransactionPtrIter.SetToFirst(); 
       
   277     while ((ptr = iTransactionPtrIter++) != NULL)
       
   278         {
       
   279         RemovePtr(ptr, ERemovalTypeCancel);
       
   280         };
       
   281     }
       
   282 
       
   283 
       
   284 // ---------------------------------------------------------------------------
       
   285 // Gets pointer to first transaction in queue.
       
   286 // ---------------------------------------------------------------------------
       
   287 //
       
   288 CSensrvTransaction* CSensrvTransactionQueue::First()
       
   289     {
       
   290     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvTransactionQueue::First()" ) ) );
       
   291 
       
   292     CSensrvTransaction* retval = NULL;
       
   293 
       
   294     if (!iTransactionPtrList.IsEmpty())
       
   295         {
       
   296         retval = iTransactionPtrList.First()->iTransaction;
       
   297         }
       
   298 
       
   299     COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvTransactionQueue::First - return 0x%x" ), retval ) );
       
   300     
       
   301     return retval;
       
   302     }
       
   303 
       
   304 
       
   305 // ---------------------------------------------------------------------------
       
   306 // Finds the queue item which contains pointer to specified transaction.
       
   307 // ---------------------------------------------------------------------------
       
   308 //
       
   309 CSensrvTransactionQueue::TLinkableTransactionPtr* CSensrvTransactionQueue::FindTransactionPtr(CSensrvTransaction* aTransaction)
       
   310     {
       
   311     TLinkableTransactionPtr* ptr = NULL;
       
   312 
       
   313     if (aTransaction)
       
   314         {
       
   315         TBool found(EFalse);
       
   316         
       
   317         iTransactionPtrIter.SetToFirst(); 
       
   318         while (!found && (ptr = iTransactionPtrIter++) != NULL )
       
   319             {
       
   320             if ( ptr->iTransaction == aTransaction )
       
   321                 {    
       
   322                 COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvTransactionQueue::FindTransactionPtr - Pointer found" ) ) );
       
   323                 found = ETrue;
       
   324                 }
       
   325             };
       
   326         
       
   327         if (!found)
       
   328             {
       
   329             ptr = NULL;
       
   330             }
       
   331         }
       
   332     else
       
   333         {
       
   334         COMPONENT_TRACE( ( _L( "Sensor Server - CSensrvTransactionQueue::FindTransactionPtr - Pointer not found" ) ) );
       
   335         }
       
   336     
       
   337     return ptr;    
       
   338     }
       
   339 
       
   340 // ---------------------------------------------------------------------------
       
   341 // Removes linkable pointer object from queue.
       
   342 // ---------------------------------------------------------------------------
       
   343 //
       
   344 void CSensrvTransactionQueue::RemovePtr(TLinkableTransactionPtr* aPtr, TRemovalType aRemovalType)
       
   345     {
       
   346     if (aPtr)
       
   347         {
       
   348         if (iOwned && aRemovalType != ERemovalTypeTransfer)
       
   349             {
       
   350             __ASSERT_ALWAYS(aRemovalType != ERemovalTypeUndefined, User::Panic(KSensrvPanicCategory, ESensrvPanicInvalidRemoveType));
       
   351 
       
   352             if (aRemovalType == ERemovalTypeCancel)
       
   353                 {
       
   354                 aPtr->iTransaction->SetErrorCode(KErrCancel);    
       
   355                 }
       
   356             aPtr->iTransaction->Complete();
       
   357             // Owned queues are always owned by server main thread and transactions are
       
   358             // only removed from those queues within server main thread,
       
   359             // so regular delete is ok.
       
   360             delete aPtr->iTransaction;
       
   361             }
       
   362 
       
   363         iTransactionPtrList.Remove(*aPtr);
       
   364         iHeap->Free(aPtr);
       
   365         aPtr = NULL;
       
   366         }
       
   367     }
       
   368