vpnengine/sit/src/taskhandler.cpp
changeset 0 33413c0669b9
equal deleted inserted replaced
-1:000000000000 0:33413c0669b9
       
     1 /*
       
     2 * Copyright (c) 2003 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: Base class for all task handlers.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include "taskhandler.h"
       
    21 #include "log.h"
       
    22 
       
    23 CTaskHandler::CTaskHandler(MTaskHandlerManager* aManager, const TTaskArrivedEventData& aTaskInfo,
       
    24                            TEventType aCancelEventType, TDes8* aEventSpecPtr)
       
    25     : CActive(EPriorityNormal), iManager(aManager), iTaskInfo(aTaskInfo),
       
    26       iCancelEventType(aCancelEventType), iEventSpecPtr(aEventSpecPtr)
       
    27     {
       
    28     LOG(Log::Printf(_L("CTaskHandler::CTaskHandler - iCancelEventType = %d\n"), iCancelEventType));
       
    29     }
       
    30 
       
    31 void CTaskHandler::Start()
       
    32     {
       
    33     LOG(Log::Printf(_L("CTaskHandler::Start\n")));
       
    34     // First fetch the event specification of the
       
    35     // task request that we are supposed to fulfill
       
    36     TPckg<TTaskArrivedEventData> taskInfoDes(iTaskInfo);
       
    37     iEventMediator.ListenToEvent(EFetchTaskInfoEvent, taskInfoDes, *this);
       
    38     // Operation continues from CTaskHandler::EventOccured
       
    39     // that calls StartTaskHandlingL if all goes well
       
    40     }
       
    41 
       
    42 void CTaskHandler::RunL()
       
    43     {
       
    44     ChangeStateL();
       
    45     }
       
    46 
       
    47 void CTaskHandler::GotoState(TInt aState)
       
    48     {
       
    49     SetNextState(aState);
       
    50     SetActive();
       
    51     TRequestStatus* status = &iStatus;
       
    52     User::RequestComplete(status, KErrNone);
       
    53     }
       
    54     
       
    55 void CTaskHandler::SetCurrState(TInt aState)
       
    56     {
       
    57     iCurrState = aState;
       
    58     }
       
    59 
       
    60 void CTaskHandler::SetNextState(TInt aState)
       
    61     {
       
    62     iNextState = aState;
       
    63     }
       
    64 
       
    65 TInt CTaskHandler::CurrState()
       
    66     {
       
    67     return iCurrState;
       
    68     }
       
    69 
       
    70 TInt CTaskHandler::NextState()
       
    71     {
       
    72     return iNextState;
       
    73     }
       
    74 
       
    75 void CTaskHandler::DoCancel()
       
    76     {
       
    77     LOG(Log::Printf(_L("CTaskHandler::DoCancel\n")));
       
    78     // Cancel listening to the task cancellation
       
    79     iEventMediator.CancelListening(iCancelEventType, *iEventSpecPtr);
       
    80 
       
    81     CancelOngoingOperation();
       
    82             
       
    83     TaskDone();
       
    84     }
       
    85 
       
    86 TInt CTaskHandler::RunError(TInt aError)
       
    87     {
       
    88     LOG(Log::Printf(_L("CTaskHandler::RunError - error = %d\n"), aError));
       
    89     TaskComplete(aError);
       
    90     return KErrNone;
       
    91     }
       
    92 
       
    93 void CTaskHandler::EventOccured(TInt aStatus, TEventType aType, TDesC8* aData)
       
    94     {
       
    95     LOG(Log::Printf(_L("CTaskHandler::EventOccured\n")));
       
    96     if (aType == EFetchTaskInfoEvent)
       
    97         {
       
    98         OnFetchTaskInfoEvent(aStatus, aData);
       
    99         }
       
   100     else if (aType == iCancelEventType)
       
   101         {
       
   102         OnTaskRequestCancelledEvent(aStatus);
       
   103         }
       
   104     else
       
   105         {
       
   106         User::Panic(KSitName, EPanicUnexpectedEventOccured);
       
   107         }
       
   108     }
       
   109 
       
   110 void CTaskHandler::OnFetchTaskInfoEvent(TInt aStatus, TDesC8* aData)
       
   111     {
       
   112     LOG(Log::Printf(_L("CTaskHandler::OnFetchTaskInfoEvent\n")));
       
   113     if (aStatus == KErrNone && aData)
       
   114         {
       
   115         // Copy the task request event specification
       
   116         // so that the actual task handler can start using it
       
   117         iEventSpecPtr->Copy(*aData);
       
   118         
       
   119         // Listen to the cancellation of this task
       
   120         iEventMediator.ListenToEvent(iCancelEventType, *iEventSpecPtr, *this);
       
   121         
       
   122         StartTaskHandling();
       
   123         }
       
   124     else if (aStatus == KErrNotFound)
       
   125         {
       
   126         // The task request was cancelled before 
       
   127         // we had the change to get its data
       
   128         TaskDone();
       
   129         }
       
   130     else
       
   131         {
       
   132         FatalError(aStatus);
       
   133         }
       
   134     }
       
   135 
       
   136 void CTaskHandler::OnTaskRequestCancelledEvent(TInt aStatus)
       
   137     {
       
   138     LOG(Log::Printf(_L("CTaskHandler::OnTaskRequestCancelledEvent\n")));
       
   139     if (aStatus == KErrNone)
       
   140         {
       
   141         // Event Mediator tells that the task request
       
   142         // has been cancelled so we stop performing the task
       
   143         Cancel();
       
   144         }
       
   145     else if (aStatus != KErrCancel)
       
   146         {
       
   147         FatalError(aStatus);
       
   148         }
       
   149     }
       
   150 
       
   151 void CTaskHandler::TaskComplete(TInt aError)
       
   152     {
       
   153     LOG(Log::Printf(_L("CTaskHandler::TaskComplete - aError = %d\n"), aError));
       
   154     // Cancel listening to the task cancellation
       
   155     iEventMediator.CancelListening(iCancelEventType, *iEventSpecPtr);
       
   156 
       
   157     ReportResult(aError);
       
   158     }
       
   159     
       
   160 void CTaskHandler::ReportEvent(TEventType aEventType, TDesC8& aEventSpec, TDesC8& aEventData)
       
   161     {
       
   162     LOG(Log::Printf(_L("CTaskHandler::ReportEvent - aEventType = %d\n"), aEventType));
       
   163     TInt ret = iEventMediator.ReportEvent(aEventType, aEventSpec, aEventData);
       
   164 
       
   165     if (ret != KErrNone)
       
   166         {
       
   167         FatalError(ret);
       
   168         }
       
   169     else
       
   170         {
       
   171         TaskDone();
       
   172         }
       
   173     }
       
   174 
       
   175 void CTaskHandler::TaskDone()
       
   176     {
       
   177     LOG(Log::Printf(_L("CTaskHandler::TaskDone\n")));
       
   178     if (!iDelayedTaskEnd)
       
   179         {
       
   180         LOG(Log::Printf(_L("CTaskHandler::TaskDone - calling iManager->TaskHandlerComplete\n")));
       
   181         iManager->TaskHandlerComplete(this);
       
   182         }
       
   183     }
       
   184 
       
   185 void CTaskHandler::FatalError(TInt aError)
       
   186     {
       
   187     LOG(Log::Printf(_L("CTaskHandler::FatalError - error = %d\n"), aError));
       
   188     iManager->TaskHandlerFatalError(this, aError);
       
   189     }
       
   190 
       
   191 void CTaskHandler::SetDelayedTaskEnd(TBool aDelayedTaskEnd)
       
   192     {
       
   193     iDelayedTaskEnd = aDelayedTaskEnd;
       
   194     }