remotestoragefw/remotefileengine/src/rsfwrfestatemachine.cpp
branchRCL_3
changeset 19 88ee4cf65e19
parent 16 87c71b25c937
child 20 1aa8c82cb4cb
equal deleted inserted replaced
16:87c71b25c937 19:88ee4cf65e19
     1 /*
       
     2 * Copyright (c) 2005-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:  Base class for all operation state machines
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "mdebug.h"
       
    20 #include "rsfwrfestatemachine.h"
       
    21 #include "rsfwinterface.h"
       
    22 #include "rsfwfiletable.h"
       
    23 #include "rsfwrferequest.h"
       
    24 #include "rsfwfileengine.h"
       
    25 
       
    26 // ----------------------------------------------------------------------------
       
    27 // CRsfwRfeStateMachine::BaseConstructL
       
    28 // ----------------------------------------------------------------------------
       
    29 //
       
    30 void CRsfwRfeStateMachine::BaseConstructL()
       
    31     {
       
    32     DEBUGSTRING(("CRsfwRfeStateMachine::BaseConstructL"));
       
    33     iCompleteAndDestroyState =
       
    34         new (ELeave) CRsfwRfeStateMachine::TCompleteAndDestroyState(this);   
       
    35     }
       
    36 
       
    37 // ----------------------------------------------------------------------------
       
    38 // CRsfwRfeStateMachine::~CRsfwRfeStateMachine
       
    39 // Note that normally when we come here iState = iCompleteAndDestroyState,
       
    40 // only delete once
       
    41 // ----------------------------------------------------------------------------
       
    42 //
       
    43 CRsfwRfeStateMachine::~CRsfwRfeStateMachine()
       
    44     {
       
    45     DEBUGSTRING(("CRsfwRfeStateMachine::~CRsfwRfeStateMachine"));
       
    46     if (CurrentState() == CompleteAndDestroyState()) 
       
    47         {
       
    48         delete iState;
       
    49         }
       
    50     else 
       
    51         {
       
    52         delete iState;
       
    53         delete iCompleteAndDestroyState;
       
    54         }
       
    55     }
       
    56 
       
    57 // ----------------------------------------------------------------------------
       
    58 // CRsfwRfeStateMachine::ErrorOnStateEntry
       
    59 // Entering the current state failed, so loop in EnterState() will replace 
       
    60 // it by CompleteAndDestroy state as we return that state.
       
    61 // Deleting the current state happens in EnterState()
       
    62 // The state machine logic does not do that, as it allows us to eg resolve
       
    63 // the problem by fixing the current state. 
       
    64 // ----------------------------------------------------------------------------
       
    65 //
       
    66 CRsfwRfeStateMachine::TState* CRsfwRfeStateMachine::ErrorOnStateEntry(TInt aError) 
       
    67     {
       
    68     DEBUGSTRING(("CRsfwRfeStateMachine::ErrorOnStateEntry"));
       
    69     iCompleteAndDestroyState->SetErrorCode(aError);
       
    70     return iCompleteAndDestroyState;
       
    71     }
       
    72 
       
    73 // ----------------------------------------------------------------------------
       
    74 // CRsfwRfeStateMachine::ChangeState
       
    75 // Enters aNextState, and makes it our current state.
       
    76 // Deletes the previous state
       
    77 // (assumes we never go backwards in the state machine)
       
    78 // ----------------------------------------------------------------------------
       
    79 //
       
    80 void CRsfwRfeStateMachine::ChangeState(CRsfwRfeStateMachine::TState* aNextState)
       
    81     {
       
    82     DEBUGSTRING(("CRsfwRfeStateMachine::ChangeState"));
       
    83     if (iState && (iState != aNextState) )
       
    84         {
       
    85         delete iState;
       
    86         iState = NULL;
       
    87         }
       
    88     EnterState(aNextState);
       
    89     }
       
    90 
       
    91 // ----------------------------------------------------------------------------
       
    92 // CRsfwRfeStateMachine::EnterState
       
    93 // Enters aNextState, and makes it our current state.
       
    94 // ----------------------------------------------------------------------------
       
    95 //
       
    96 void CRsfwRfeStateMachine::EnterState(CRsfwRfeStateMachine::TState* aNextState)
       
    97     {
       
    98     DEBUGSTRING(("CRsfwRfeStateMachine::EnterState"));
       
    99     TInt err;
       
   100     while (aNextState) 
       
   101         {// State change required.
       
   102         iState = aNextState;
       
   103         TRAP(err, iState->EnterL());
       
   104         if (err)
       
   105             {
       
   106             aNextState = ErrorOnStateEntry(err);
       
   107             delete iState;
       
   108             iState = NULL;
       
   109             }
       
   110         else
       
   111             {
       
   112             aNextState = NULL;
       
   113             }
       
   114         }
       
   115     }
       
   116 
       
   117 // ----------------------------------------------------------------------------
       
   118 // CRsfwRfeStateMachine::SetNextState
       
   119 // Sets the current state, but doesn't actually enter it.
       
   120 // ----------------------------------------------------------------------------
       
   121 //
       
   122 void CRsfwRfeStateMachine::SetNextState(TState* aNextState)
       
   123     {
       
   124     DEBUGSTRING(("CRsfwRfeStateMachine::SetNextState"));
       
   125     iState = aNextState;
       
   126     }
       
   127 
       
   128 // ----------------------------------------------------------------------------
       
   129 // CRsfwRfeStateMachine::ReEnterCurrentState
       
   130 // Re-enters the state we're currently in.
       
   131 // ----------------------------------------------------------------------------
       
   132 //
       
   133 void CRsfwRfeStateMachine::ReEnterCurrentState()  
       
   134     {
       
   135     DEBUGSTRING(("CRsfwRfeStateMachine::ReEnterCurrentState"));
       
   136     EnterState(iState);
       
   137     }
       
   138 
       
   139 // ----------------------------------------------------------------------------
       
   140 // CRsfwRfeStateMachine::SetRequest
       
   141 // Sets the backpointer to our request
       
   142 // ----------------------------------------------------------------------------
       
   143 //
       
   144 void CRsfwRfeStateMachine::SetRequest(CRsfwRfeRequest* aRequest)
       
   145     {
       
   146     DEBUGSTRING(("CRsfwRfeStateMachine::SetRequest"));
       
   147     iRFeRequest = aRequest;
       
   148     }
       
   149     
       
   150 // ----------------------------------------------------------------------------
       
   151 // CRsfwRfeStateMachine::Request 
       
   152 // ----------------------------------------------------------------------------
       
   153 //
       
   154 CRsfwRfeRequest* CRsfwRfeStateMachine::Request()
       
   155     {
       
   156     DEBUGSTRING(("CRsfwRfeStateMachine::Request"));
       
   157     return iRFeRequest;
       
   158     }
       
   159 
       
   160 // ----------------------------------------------------------------------------
       
   161 // CRsfwRfeStateMachine::SetVolumes
       
   162 // ----------------------------------------------------------------------------
       
   163 //    
       
   164 void CRsfwRfeStateMachine::SetVolumes(CRsfwVolumeTable* aImplementor)
       
   165     {
       
   166     DEBUGSTRING(("CRsfwRfeStateMachine::SetVolumes"));
       
   167     iImplementor = aImplementor;
       
   168     }
       
   169 
       
   170 // ----------------------------------------------------------------------------
       
   171 // CRsfwRfeStateMachine::SetFileEngine
       
   172 // ----------------------------------------------------------------------------
       
   173 //
       
   174 void CRsfwRfeStateMachine::SetFileEngine(CRsfwFileEngine* aFileEngine) 
       
   175     {
       
   176     DEBUGSTRING(("CRsfwRfeStateMachine::SetFileEngine"));
       
   177     iFileEngine = aFileEngine;
       
   178     }
       
   179 
       
   180 // ----------------------------------------------------------------------------
       
   181 // CRsfwRfeStateMachine::SetArguments
       
   182 // ----------------------------------------------------------------------------
       
   183 //
       
   184 void CRsfwRfeStateMachine::SetArguments(TRfeInArgs* aInArgs, TRfeOutArgs* aOutArgs)
       
   185     {
       
   186     DEBUGSTRING(("CRsfwRfeStateMachine::SetArguments"));
       
   187     iInArgs = aInArgs;
       
   188     iOutArgs = aOutArgs;
       
   189     
       
   190     // Set the target node for this operation.
       
   191     if (iInArgs && iFileEngine) 
       
   192         {
       
   193         TFid* fidp = &(iInArgs->iFid);
       
   194         iFep = iFileEngine->iFileTable->Lookup(*fidp);  
       
   195         }
       
   196     else 
       
   197         {
       
   198         iFep = NULL;
       
   199         }
       
   200     }
       
   201 
       
   202 // ----------------------------------------------------------------------------
       
   203 // CRsfwRfeStateMachine::Volumes
       
   204 // ----------------------------------------------------------------------------
       
   205 //    
       
   206 CRsfwVolumeTable* CRsfwRfeStateMachine::Volumes()
       
   207     {
       
   208     DEBUGSTRING(("CRsfwRfeStateMachine::Volumes"));
       
   209     return iImplementor;
       
   210     }
       
   211   
       
   212 // ----------------------------------------------------------------------------
       
   213 // CRsfwRfeStateMachine::FileEngine
       
   214 // ----------------------------------------------------------------------------
       
   215 //    
       
   216 CRsfwFileEngine* CRsfwRfeStateMachine::FileEngine()
       
   217     {
       
   218     return iFileEngine;
       
   219     }
       
   220 
       
   221 // ----------------------------------------------------------------------------
       
   222 // CRsfwRfeStateMachine::Node
       
   223 // ----------------------------------------------------------------------------
       
   224 //
       
   225 CRsfwFileEntry* CRsfwRfeStateMachine::Node() 
       
   226     {
       
   227     return iFep;
       
   228     }
       
   229 
       
   230 // ----------------------------------------------------------------------------
       
   231 // CRsfwRfeStateMachine::ErrorOnStateExit
       
   232 // ----------------------------------------------------------------------------
       
   233 //
       
   234 CRsfwRfeStateMachine::TState* CRsfwRfeStateMachine::ErrorOnStateExit(TInt aError) 
       
   235     {
       
   236     DEBUGSTRING(("CRsfwRfeStateMachine::ErrorOnStateExit %d", aError));
       
   237     iCompleteAndDestroyState->SetErrorCode(aError);
       
   238     return iCompleteAndDestroyState;
       
   239     }
       
   240         
       
   241 // ----------------------------------------------------------------------------
       
   242 // CRsfwRfeStateMachine::HandleRemoteAccessResponse
       
   243 // ----------------------------------------------------------------------------
       
   244 //    
       
   245 void CRsfwRfeStateMachine::HandleRemoteAccessResponse(TUint /*aId*/, TInt aStatus)
       
   246     {
       
   247     DEBUGSTRING(("CRsfwRfeStateMachine::HandleRemoteAccessResponse"));
       
   248     TState* nextState = NULL;
       
   249     TRAPD(err, nextState = aStatus ? iState->ErrorL(aStatus)
       
   250           : iState->CompleteL());
       
   251     if (err)
       
   252         {
       
   253         nextState = ErrorOnStateExit(err);
       
   254         }
       
   255     ChangeState(nextState);
       
   256     }
       
   257 
       
   258 // empty defaults for CRsfwRfeStateMachine::TState functions CompleteL(), ErrorL() and Cancel()
       
   259 
       
   260 // ----------------------------------------------------------------------------
       
   261 // CRsfwRfeStateMachine::TState::CompleteL
       
   262 // ----------------------------------------------------------------------------
       
   263 //
       
   264 CRsfwRfeStateMachine::TState* CRsfwRfeStateMachine::TState::CompleteL()
       
   265     {
       
   266     DEBUGSTRING(("CRsfwRfeStateMachine::TState::CompleteL"));
       
   267     return NULL;
       
   268     }
       
   269 
       
   270 // ----------------------------------------------------------------------------
       
   271 // CRsfwRfeStateMachine::TState::ErrorL
       
   272 // ----------------------------------------------------------------------------
       
   273 //
       
   274 CRsfwRfeStateMachine::TState* CRsfwRfeStateMachine::TState::ErrorL(TInt /*aCode*/) 
       
   275     {
       
   276     DEBUGSTRING(("CRsfwRfeStateMachine::TState::ErrorL"));
       
   277     return NULL;
       
   278     }
       
   279 
       
   280 // ----------------------------------------------------------------------------
       
   281 // CRsfwRfeStateMachine::TState::Cancel
       
   282 // ----------------------------------------------------------------------------
       
   283 //
       
   284 void CRsfwRfeStateMachine::TState::Cancel() 
       
   285     {
       
   286     DEBUGSTRING(("CRsfwRfeStateMachine::TState::Cancel"));
       
   287     }
       
   288 
       
   289 // ----------------------------------------------------------------------------
       
   290 // CRsfwRfeStateMachine::TCompleteAndDestroyState::TCompleteAndDestroyState
       
   291 // Completes the remote request with error code aErrCode and
       
   292 //  destroys it (CRsfwRfeRequest) 
       
   293 // as well as the classes pointed to by it, that it the instances of 
       
   294 // CRsfwRfeAsyncOperation, CRsfwRfeStateMachine and CRsfwRfeStateMachine::TState classes.
       
   295 //
       
   296 // In RemoteFS requests error code is 0,
       
   297 // as the error is returned in iOutArgs->iResult
       
   298 // For RemoteEngine API requests aErrCode should have the error code
       
   299 // ----------------------------------------------------------------------------
       
   300 //
       
   301 CRsfwRfeStateMachine::
       
   302 TCompleteAndDestroyState::TCompleteAndDestroyState(CRsfwRfeStateMachine* aParent,
       
   303                                                    TInt aErrCode)
       
   304     : iOperation(aParent), iErrCode(aErrCode)
       
   305     {
       
   306     DEBUGSTRING(("TCompleteAndDestroyState::TCompleteAndDestroyState"));
       
   307     }
       
   308 
       
   309 // ----------------------------------------------------------------------------
       
   310 // CRsfwRfeStateMachine::TCompleteAndDestroyState::EnterL
       
   311 // ----------------------------------------------------------------------------
       
   312 //
       
   313 void CRsfwRfeStateMachine::TCompleteAndDestroyState::EnterL()
       
   314     {
       
   315     DEBUGSTRING(("CRsfwRfeStateMachine::TCompleteAndDestroyState::EnterL"));
       
   316     iOperation->Request()->CompleteAndDestroy(iErrCode);   
       
   317     // nothing can be called here as we have been deallocated!!!    
       
   318     }
       
   319 
       
   320 // ----------------------------------------------------------------------------
       
   321 // CRsfwRfeStateMachine::TCompleteAndDestroyState::SetErrorCode
       
   322 // ----------------------------------------------------------------------------
       
   323 //
       
   324 void CRsfwRfeStateMachine::TCompleteAndDestroyState::SetErrorCode(TInt aErrorCode) 
       
   325     {
       
   326     DEBUGSTRING(("CRsfwRfeStateMachine::TCompleteAndDestroyState::SetErrorCode"));
       
   327     iErrCode = aErrorCode;
       
   328     }
       
   329