testexecfw/stf/stffw/eventsystem/client/src/stfasynceventactive.cpp
changeset 2 8bb370ba6d1d
equal deleted inserted replaced
1:bbd31066657e 2:8bb370ba6d1d
       
     1 /*
       
     2 * Copyright (c) 2010 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: 
       
    15 *
       
    16 */
       
    17 #include <e32std.h>
       
    18 #include <e32svr.h>
       
    19 
       
    20 #include <stfeventsystemerrorcodes.h>
       
    21 #include "stfasynceventactive.h"
       
    22 #include <stfasynceventlist.h>
       
    23 
       
    24 
       
    25 // Implementation of asynchronous event Active Object
       
    26 
       
    27 /**
       
    28 * NewL is first phase of two-phased constructor.
       
    29 */
       
    30 EXPORT_C CAsyncEventActive* CAsyncEventActive::NewL(const TInt aOwnerId)
       
    31     {
       
    32     //RDebug::Print(_L("STF [ESC]: CAsyncEventActive::NewL()"));
       
    33     CAsyncEventActive* self = new (ELeave) CAsyncEventActive(aOwnerId);
       
    34     CleanupStack::PushL(self);
       
    35 
       
    36     // Construct the object
       
    37     self->ConstructL();
       
    38 
       
    39     // Remove from cleanup stack
       
    40     CleanupStack::Pop(self);
       
    41 
       
    42     return self;
       
    43     }
       
    44 
       
    45 /** 
       
    46 * C++ default constructor.
       
    47 */
       
    48 CAsyncEventActive::CAsyncEventActive(const TInt aOwnerId): CActive(CActive::EPriorityStandard), iOwnerId(aOwnerId)
       
    49     {
       
    50     CActiveScheduler::Add(this);
       
    51     RDebug::Print(_L("STF [ESC]: CAsyncEventActive::CAsyncEventActive() creating active object [%x]"), this);
       
    52     }
       
    53 
       
    54 /**
       
    55 * By default Symbian OS constructor is private.
       
    56 */
       
    57 void CAsyncEventActive::ConstructL()
       
    58     {
       
    59     //RDebug::Print(_L("STF [ESC]: CAsyncEventActive::ConstructL()"));
       
    60     }
       
    61 
       
    62 /**
       
    63 * Destructor.
       
    64 */
       
    65 CAsyncEventActive::~CAsyncEventActive()
       
    66     {
       
    67     Cancel();
       
    68     iEventSystem.Close();
       
    69     delete iEventName;
       
    70     RDebug::Print(_L("STF [ESC]: CAsyncEventActive::~CAsyncEventActive() deleting active object [%x]"), this);
       
    71     }
       
    72 
       
    73 /** 
       
    74 * Start active object.
       
    75 * If list is provided, active object will cooperate with it adding and deleting itself from the list.
       
    76 * Also active object will delete itself if it's on the list.
       
    77 */
       
    78 EXPORT_C void CAsyncEventActive::StartL(TRequestStatus* aStatus, TThreadId aStatusThreadId, const TDesC& aEventName, CAsyncEventList* aAsyncEventList)
       
    79     {
       
    80     RDebug::Print(_L("STF [ESC]: CAsyncEventActive::StartL() eventName=[%S]"), &aEventName);
       
    81     // Check if list of asynchronous objects and event name are not set.
       
    82     // It would mean that method StartL was already called.
       
    83     __ASSERT_ALWAYS(iAsyncEventList == NULL, User::Panic(_L("CAsyncEventActive1"), EEventSystemListAlreadySet));
       
    84     __ASSERT_ALWAYS(iEventName == NULL, User::Panic(_L("CAsyncEventActive2"), EEventSystemListAlreadySet));
       
    85     
       
    86     // Set caller's status and other data
       
    87     RDebug::Print(_L("STF [ESC]: CAsyncEventActive::StartL() registering status [%x]"), aStatus);
       
    88     iCallerStatus = aStatus;
       
    89     *iCallerStatus = KRequestPending;
       
    90     iStatusThreadId = aStatusThreadId;
       
    91     iAsyncEventList = aAsyncEventList;
       
    92     iEventName = aEventName.AllocL();
       
    93      
       
    94     // Open connection to server
       
    95     User::LeaveIfError(iEventSystem.Connect());
       
    96     
       
    97     // Add object to list
       
    98     if(iAsyncEventList)
       
    99         {
       
   100         iAsyncEventList->AddAsyncEventL(this);
       
   101         }
       
   102     
       
   103     // Call wait event
       
   104     RDebug::Print(_L("STF [ESC]: CAsyncEventActive::StartL() SendReceive eventName=[%S]"), iEventName);
       
   105     TIpcArgs args(iEventName, iOwnerId);
       
   106     iEventSystem.SendReceive(EEventSystemWaitEvent, args, iStatus);
       
   107     
       
   108     // Activate object    
       
   109     SetActive();
       
   110     }
       
   111 
       
   112 /**
       
   113 * Handles active object and deletes itself!
       
   114 */
       
   115 void CAsyncEventActive::RunL()
       
   116     {
       
   117     RDebug::Print(_L("STF [ESC]: CAsyncEventActive::RunL()"));
       
   118     // Complete request from caller
       
   119     RDebug::Print(_L("STF [ESC]: CAsyncEventActive::RunL() completing status [%x] with [%d]"), iCallerStatus, iStatus.Int());
       
   120     User::LeaveIfError(CompleteCallerStatus(iStatus.Int()));
       
   121     
       
   122     // Remove from list
       
   123     if(iAsyncEventList)
       
   124         {
       
   125         iAsyncEventList->RemoveAsyncEventL(this);
       
   126         iAsyncEventList = NULL;
       
   127         }
       
   128     
       
   129     // Close connection to server, as it's not longer required
       
   130     iEventSystem.Close();
       
   131     }
       
   132 
       
   133 /** 
       
   134 * DoCancel
       
   135 */
       
   136 void CAsyncEventActive::DoCancel()
       
   137     {
       
   138     RDebug::Print(_L("STF [ESC]: CAsyncEventActive::DoCancel()"));
       
   139 
       
   140     // Cancelling wait event on the event server
       
   141     TIpcArgs args(iEventName, iOwnerId);
       
   142     if(iEventSystem.Handle())
       
   143         {
       
   144         TInt ret = iEventSystem.SendReceive(EEventSystemCancelWaitEvent, args);
       
   145         }
       
   146 
       
   147     // Complete request from caller
       
   148     RDebug::Print(_L("STF [ESC]: CAsyncEventActive::DoCancel() completing (KErrCancel) status [%x]"), iCallerStatus);
       
   149     CompleteCallerStatus(KErrCancel);
       
   150 
       
   151     // Remove from list
       
   152     if(iAsyncEventList)
       
   153         {
       
   154         iAsyncEventList->RemoveAsyncEventL(this);
       
   155         iAsyncEventList = NULL;
       
   156         }
       
   157     }
       
   158 
       
   159 /** 
       
   160 * RunError
       
   161 */
       
   162 TInt CAsyncEventActive::RunError(TInt aError)
       
   163     {
       
   164     RDebug::Print(_L("STF [ESC]: CAsyncEventActive::RunError() aError=[%d]"), aError);    
       
   165     return aError;
       
   166     }
       
   167 
       
   168 /**
       
   169 * Checks if request to event server is in pending state
       
   170 */
       
   171 EXPORT_C TBool CAsyncEventActive::IsPending()
       
   172     {
       
   173     return (iCallerStatus != NULL);
       
   174     }
       
   175 
       
   176 /**
       
   177 * Complete caller status with specified code
       
   178 */
       
   179 TInt CAsyncEventActive::CompleteCallerStatus(TInt aCode)
       
   180     {
       
   181     RThread thread;
       
   182     TInt ret = thread.Open(iStatusThreadId);
       
   183     
       
   184     if(ret == KErrNone)
       
   185         {
       
   186         thread.RequestComplete(iCallerStatus, aCode);
       
   187         iCallerStatus = NULL;
       
   188         thread.Close();
       
   189         }
       
   190         
       
   191     return ret;
       
   192     }
       
   193