phoneclientserver/phoneserver/Src/CommandHandler/Commandhandler_Aiw/CPhSrvComHandRequestManager.cpp
changeset 46 2fa1fa551b0b
parent 42 35488577e233
child 48 78df25012fda
equal deleted inserted replaced
42:35488577e233 46:2fa1fa551b0b
     1 /*
       
     2 * Copyright (c) 2003-2004 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:  Command Handler Request Manager.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDES
       
    21 #include "cphsrvcomhandrequestmanager.h" 
       
    22 #include "cphsrvcomhandrequest.h" 
       
    23 #include "cphsrvsubsessionbase.h" 
       
    24 #include "phsrvutils.h" 
       
    25 #include "cphsrvserver.h" 
       
    26 #include "mphsrvemergencynumbermanager.h" 
       
    27 
       
    28 
       
    29 // CONSTANTS
       
    30 
       
    31 // The size of the Notification manager storage.
       
    32 const TInt KPhSrvComHandManagerPendingCallGranularity = 4;
       
    33 
       
    34 
       
    35 // ============================ MEMBER FUNCTIONS ===============================
       
    36 
       
    37 // -----------------------------------------------------------------------------
       
    38 // CPhSrvComHandRequestManager::CPhSrvComHandRequestManager
       
    39 // 
       
    40 // Constructor.
       
    41 // -----------------------------------------------------------------------------
       
    42 //
       
    43 CPhSrvComHandRequestManager::CPhSrvComHandRequestManager( 
       
    44     CPhSrvServer& aServer )
       
    45 :   iServer( aServer )
       
    46     {
       
    47     }
       
    48 
       
    49 
       
    50 // -----------------------------------------------------------------------------
       
    51 // CPhSrvComHandRequestManager::~CPhSrvComHandRequestManager
       
    52 // 
       
    53 // Destructor.
       
    54 // -----------------------------------------------------------------------------
       
    55 //
       
    56 CPhSrvComHandRequestManager::~CPhSrvComHandRequestManager()
       
    57     {
       
    58     if ( iOutstandingComHandRequests )
       
    59         {
       
    60         iOutstandingComHandRequests->ResetAndDestroy();
       
    61         iOutstandingComHandRequests->Close();
       
    62 
       
    63         delete iOutstandingComHandRequests;
       
    64         }
       
    65     }
       
    66 
       
    67 
       
    68 // -----------------------------------------------------------------------------
       
    69 // CPhSrvComHandRequestManager::ConstructL
       
    70 // 
       
    71 // Complete construction.
       
    72 // -----------------------------------------------------------------------------
       
    73 //
       
    74 void CPhSrvComHandRequestManager::ConstructL()
       
    75     {
       
    76     iOutstandingComHandRequests = new ( ELeave ) RPointerArray< 
       
    77         CPhSrvComHandRequest >( KPhSrvComHandManagerPendingCallGranularity );
       
    78 
       
    79     CPhSrvComHandRequest* emergencyRequest = CPhSrvComHandRequest::NewL();
       
    80     CleanupStack::PushL( emergencyRequest );
       
    81     User::LeaveIfError( 
       
    82         iOutstandingComHandRequests->Append( emergencyRequest ) );
       
    83     CleanupStack::Pop(); // emergencyRequest
       
    84     }
       
    85 
       
    86 
       
    87 // -----------------------------------------------------------------------------
       
    88 // CPhSrvComHandRequestManager::NegotiatorRequestL
       
    89 // 
       
    90 // Initiate a command handler request - called by the external command handler
       
    91 // interface.
       
    92 // -----------------------------------------------------------------------------
       
    93 //
       
    94 void CPhSrvComHandRequestManager::NegotiatorRequestL( 
       
    95     const RMessage2& aMessage, 
       
    96     const TPhCltComHandCommandParameters& aParams, 
       
    97     CPhSrvSubSessionBase& aSubSession )
       
    98     {
       
    99     // If we have no registered initiator then we can't perform the request.
       
   100     if ( !iComHandInitiator )
       
   101         {
       
   102         User::Leave( KErrNotReady );
       
   103         }
       
   104 
       
   105     // If the request is DTMF stop, it is handled as emergency request.
       
   106     TBool isDtmfCancel = 
       
   107         ( 
       
   108             ( aParams.iCommandHandlerCommand == EPhCltCommandVts ) &&
       
   109             ( aParams.iDtmfAction == EPhCltDtmfStop ) 
       
   110         );
       
   111 
       
   112     // If the request is dial and the number is emergencynumber, it is
       
   113     // handled as emergency request.
       
   114     TBool isEmergencyCall = 
       
   115         ( aParams.iCommandHandlerCommand == EPhCltCommandAtd );
       
   116     
       
   117     if( isEmergencyCall )
       
   118         {
       
   119         isEmergencyCall = iServer.EmergencyNumberManagerL().
       
   120         EmergencyNumberManagerIsEmergencyNumberL(
       
   121             ( TDes16& ) aParams.iTelNumber, 
       
   122             EPhCltEmergencyNumberQueryModeNormal );
       
   123         }
       
   124 
       
   125     // Panic if this is not the only request from this subsession.
       
   126     if ( RequestAlreadyExists( aSubSession ) )
       
   127         {
       
   128         // Cancel all ongoing requests first.
       
   129         NegotiatorRequestCancel( aSubSession );
       
   130 
       
   131         PhoneServerUtils::PanicClient( 
       
   132             EPhoneServerPanicTooManyRequestsOngoing,
       
   133             aMessage );
       
   134         }
       
   135 
       
   136     // Normal request handling, request is ok.
       
   137     else
       
   138         {
       
   139         CPhSrvComHandRequest* temp = NULL;
       
   140 
       
   141         // Emergency request handling.
       
   142         if ( ( isDtmfCancel || isEmergencyCall ) && 
       
   143              ( *iOutstandingComHandRequests )[0]->SubSessionHandle() == 0 )
       
   144             {
       
   145             ( *iOutstandingComHandRequests )[0]->UpdateEmergencyRequest( 
       
   146                 aMessage, 
       
   147                 aParams,
       
   148                 aSubSession.SubSessionUniqueHandle() );
       
   149             temp = ( *iOutstandingComHandRequests )[0];
       
   150             }
       
   151         else
       
   152             {
       
   153             // Create pending request
       
   154             CPhSrvComHandRequest* request = CPhSrvComHandRequest::NewLC( 
       
   155                 aMessage, 
       
   156                 aParams,
       
   157                 aSubSession.SubSessionUniqueHandle() );
       
   158 
       
   159             User::LeaveIfError( 
       
   160                 iOutstandingComHandRequests->Append( request ) );
       
   161             CleanupStack::Pop( request );
       
   162             temp = request;
       
   163             }
       
   164 
       
   165         // Now see if the command handler notifier (initiator) is ready 
       
   166         if ( iComHandInitiator->InitiatorIsReadyToPerformRequest() )
       
   167             {
       
   168             // Ask the call initiator to perform the request 
       
   169             // (basically completes the phone app engine's outstanding 
       
   170             // ComHand Notifier request).
       
   171             iComHandInitiator->InitiatorMakeAsynchronousComHandRequest( 
       
   172                 aParams );
       
   173 
       
   174             iActiveRequest = temp;
       
   175             }
       
   176         }
       
   177     }
       
   178 
       
   179 
       
   180 // -----------------------------------------------------------------------------
       
   181 // CPhSrvComHandRequestManager::NegotiatorRequestCancel
       
   182 // 
       
   183 // Cancel a pending command handler request.
       
   184 // -----------------------------------------------------------------------------
       
   185 //
       
   186 void CPhSrvComHandRequestManager::NegotiatorRequestCancel( 
       
   187     CPhSrvSubSessionBase& aSubSession )
       
   188     {
       
   189     // Obtain the subsessions handle
       
   190     const TInt handle = aSubSession.SubSessionUniqueHandle();
       
   191 
       
   192     // Now look for any pending requests that match.
       
   193     const TInt count = iOutstandingComHandRequests->Count();
       
   194     for( TInt i = 0; i < count; i++ )
       
   195         {
       
   196         CPhSrvComHandRequest* entry = ( *iOutstandingComHandRequests )[i];
       
   197         if ( entry->SubSessionHandle() == handle )
       
   198             {
       
   199             if ( entry == iActiveRequest )
       
   200                 {
       
   201                 // This request is active, so inform that it is deleted.
       
   202                 iActiveRequest = NULL;
       
   203                 }
       
   204 
       
   205             // Found matching entry, so cancel it and clean up
       
   206             if ( i == 0 ) // 0 stands for emergency request
       
   207                 {
       
   208                 entry->Cancel();
       
   209                 entry->ClearEmergencyRequest();
       
   210                 }
       
   211             else
       
   212                 {
       
   213                 iOutstandingComHandRequests->Remove( i );
       
   214                 entry->Cancel();
       
   215                 delete entry;
       
   216                 }
       
   217             }
       
   218         }
       
   219     UpdateQueue();
       
   220     }
       
   221 
       
   222 
       
   223 // -----------------------------------------------------------------------------
       
   224 // CPhSrvComHandRequestManager::SetNegotiatorReadyRequestL
       
   225 // 
       
   226 // Updates the negotiator so that it knows if the notifier
       
   227 // session is able to perform the request.
       
   228 // Does not leave.
       
   229 // -----------------------------------------------------------------------------
       
   230 //
       
   231 void CPhSrvComHandRequestManager::SetNegotiatorReadyRequestL(
       
   232     MPhSrvComHandInitiator& aInitiator )
       
   233     {
       
   234     // There mustn't already be a command handler initiator, or the existing 
       
   235     // initiator must be the same as the new one (otherwise, panic).
       
   236     __ASSERT_ALWAYS( !iComHandInitiator || &aInitiator == iComHandInitiator, 
       
   237         PhoneServerUtils::Panic( 
       
   238         EPhoneServerPanicExistingComHandInitiatorFault ) );
       
   239 
       
   240     // Update our pointer
       
   241     iComHandInitiator = &aInitiator;
       
   242 
       
   243     UpdateQueue();
       
   244     }
       
   245 
       
   246 
       
   247 // -----------------------------------------------------------------------------
       
   248 // CPhSrvComHandRequestManager::SetNegotiatorReadyRequestCancel
       
   249 // 
       
   250 // Indicates that the initiator is no longer available.
       
   251 // -----------------------------------------------------------------------------
       
   252 //
       
   253 void CPhSrvComHandRequestManager::SetNegotiatorReadyRequestCancel( 
       
   254     MPhSrvComHandInitiator& aInitiator )
       
   255     {
       
   256     if ( iComHandInitiator && iComHandInitiator == &aInitiator )
       
   257         {
       
   258         iComHandInitiator = NULL;
       
   259         }
       
   260     }
       
   261 
       
   262 // -----------------------------------------------------------------------------
       
   263 // CPhSrvComHandRequestManager::SetNegotiatorAttemptedRequestResponseValue
       
   264 // 
       
   265 // Handle a response from the phone app engine - this is the 
       
   266 // result of attempting to perform a command handler request 
       
   267 // (based upon a previous clients external command handler request).
       
   268 // -----------------------------------------------------------------------------
       
   269 //
       
   270 void CPhSrvComHandRequestManager::SetNegotiatorAttemptedRequestResponseValue( 
       
   271     TPhCltPhoneResults aResult )
       
   272     {
       
   273     // Handle response event only if there is request that waits response.
       
   274     if ( iActiveRequest )
       
   275         {
       
   276         // Get the first item in the list - this is the oldest, and has the
       
   277         // details of the command handler request we had previously initiated.
       
   278         CPhSrvComHandRequest* head = NULL;
       
   279         if ( ( *iOutstandingComHandRequests )[0]->SubSessionHandle() != 0 )
       
   280             {
       
   281             head = ( *iOutstandingComHandRequests )[0];
       
   282             }
       
   283         else
       
   284             {
       
   285             head = ( *iOutstandingComHandRequests )[1];
       
   286             }
       
   287         // Complete the request so that the external command handler client 
       
   288         // knows the result of the request attempt
       
   289         head->InformOfComHandRequestAttemptResult( aResult );
       
   290 
       
   291         // Delete the pending request (now that it's been processed) and tidy
       
   292         // up
       
   293         if ( ( *iOutstandingComHandRequests )[0]->SubSessionHandle() != 0 )
       
   294             {
       
   295             ( *iOutstandingComHandRequests )[0]->ClearEmergencyRequest();
       
   296             }
       
   297         else
       
   298             {
       
   299             iOutstandingComHandRequests->Remove( 1 );
       
   300             delete head;
       
   301             }
       
   302 
       
   303         // Reset the iActiveRequest variable.
       
   304         iActiveRequest = NULL;
       
   305         }
       
   306     }
       
   307 
       
   308 
       
   309 // -----------------------------------------------------------------------------
       
   310 // CPhSrvComHandRequestManager::UpdateQueue
       
   311 // 
       
   312 // Update the request queue.
       
   313 // -----------------------------------------------------------------------------
       
   314 //
       
   315 void CPhSrvComHandRequestManager::UpdateQueue()
       
   316     {
       
   317     // If we have no registered initiator then we do nothing here.
       
   318     if ( iComHandInitiator )
       
   319         {
       
   320         // If we have more than one item in the queue, then we
       
   321         // try and fire of the next item here.
       
   322         if ( iComHandInitiator->InitiatorIsReadyToPerformRequest() && 
       
   323             ( iOutstandingComHandRequests->Count() > 1 ||
       
   324             ( *iOutstandingComHandRequests )[0]->SubSessionHandle() != 0 ) )
       
   325             {
       
   326             // Get the request at the top of the list (head of the queue)
       
   327             CPhSrvComHandRequest* head = NULL;
       
   328             if ( ( *iOutstandingComHandRequests )[0]->SubSessionHandle() != 0 )
       
   329                 {
       
   330                 head = ( *iOutstandingComHandRequests )[0];
       
   331                 }
       
   332             else
       
   333                 {
       
   334                 head = ( *iOutstandingComHandRequests )[1];
       
   335                 }
       
   336 
       
   337             iComHandInitiator->InitiatorMakeAsynchronousComHandRequest(
       
   338                 head->ComHandParameters() );
       
   339 
       
   340             iActiveRequest = head;
       
   341             }
       
   342         }
       
   343     }
       
   344 
       
   345 
       
   346 // -----------------------------------------------------------------------------
       
   347 // CPhSrvComHandRequestManager::RequestAlreadyExists
       
   348 // 
       
   349 // Check whether there already is request(s) for the subsession given as 
       
   350 // parameter.
       
   351 // -----------------------------------------------------------------------------
       
   352 //
       
   353 TBool CPhSrvComHandRequestManager::RequestAlreadyExists( 
       
   354     CPhSrvSubSessionBase& aSubSession )
       
   355     {
       
   356     TBool isThereRequest = EFalse;
       
   357 
       
   358     // Obtain the subsessions handle
       
   359     const TInt handle = aSubSession.SubSessionUniqueHandle();
       
   360 
       
   361     // Look for any pending requests that match the handle.
       
   362     const TInt count = iOutstandingComHandRequests->Count();
       
   363     TInt i = 0;
       
   364     for( i = 0; i < count; i++ )
       
   365         {
       
   366         CPhSrvComHandRequest* entry = 
       
   367             ( *iOutstandingComHandRequests )[i];
       
   368         if ( entry->SubSessionHandle() == handle )
       
   369             {
       
   370             isThereRequest = ETrue;
       
   371             }
       
   372         }
       
   373     return isThereRequest;
       
   374     }
       
   375 
       
   376 // End of File