// -----------------------------------------------------------------------------
// CMediatorServerCommandHandler::CancelCommands
//  
// (other items were commented in a header).
// -----------------------------------------------------------------------------
//
void CMediatorServerCommandHandler::CancelCommands( MMediatorCommandObserver* aObserver,
                                                    MMediatorCommandResponseObserver* aResponseObserver )
    {
    LOG(_L("[Mediator Server]\t CMediatorServerCommandHandler::CancelCommand"));
    
    // loop through list of pending commands
    for ( TInt i = iCommandPendingList.Count()-1; i >= 0; i-- )
        {
        
        CCommand* commandPtr = iCommandPendingList[i];
        
        if ( commandPtr )
            {
            
            TBool commandCanceled = EFalse;
            
            // This client has registered the command, and some other client has issued it
            // -> give an error response to the issuer
            if ( aObserver == commandPtr->Observer() )
                {
                TRAP_IGNORE( commandPtr->ResponseObserver()->CommandResponseL( commandPtr->Domain(),
                                                                               commandPtr->Category(),
                                                                               commandPtr->Id(),
                                                                               KErrMediatorCommandRemoved,
                                                                               KNullDesC8 ) );
                commandCanceled = ETrue;                                                                               
                }
            
            // This client has issued the command
            // -> inform the client which registered the command that it is canceled
            if ( aResponseObserver == commandPtr->ResponseObserver() )
                {
                TRAP_IGNORE( commandPtr->Observer()->CancelMediatorCommandL( commandPtr->Domain(),
                                                                             commandPtr->Category(),
                                                                             commandPtr->Id() ) );
                commandCanceled = ETrue;                                                                             
                }
            
            // pending command was registered and/or issued by this client, so now it can be removed
            // from the list
            if ( commandCanceled )
                {
                iCommandPendingList.Remove( i );
                delete commandPtr;
                }
            
            }
        
        }

    }
// -----------------------------------------------------------------------------
// CMediatorServerCommandHandler::TimerCallBack
//  
// (other items were commented in a header).
// -----------------------------------------------------------------------------
//
void CMediatorServerCommandHandler::TimerCallBack( TUid aDomain, 
                                                   TUid aCategory, 
                                                   TInt aCommandId )
    {
    LOG(_L("[Mediator Server]\t CMediatorServerCommandHandler::TimerCallBack"));
    TBool found = EFalse;
    for ( TInt index = 0; index < iCommandPendingList.Count() && !found; index ++ )
        {
        CCommand* commandPtr = iCommandPendingList[index];
        if ( commandPtr )
            {
            if ( ( aDomain == commandPtr->Domain() )
                 && ( aCategory == commandPtr->Category() )
                 && ( aCommandId == commandPtr->Id() ) )
                {
                // We have found the correct command --> ignore the leave 
                // Send the response to initiator with timeout error
                TRAP_IGNORE( commandPtr->ResponseObserver()->CommandResponseL( aDomain,
                                                                               aCategory,
                                                                               aCommandId,
                                                                               KErrMediatorTimeout,
                                                                               KNullDesC8 ) );
                // And clear the responder side also                                                                  
                TRAP_IGNORE( commandPtr->Observer()->MediatorCommandTimeoutL( aDomain,
                                                                              aCategory,
                                                                              aCommandId ) );                                                                  
                // Remove the command from list, free memory && stop looping
                iCommandPendingList.Remove( index );
                delete commandPtr;
                commandPtr = NULL;
                found = ETrue;                                                                  
                }
            }
        }
    }
// -----------------------------------------------------------------------------
// CMediatorServerCommandHandler::IssueResponse
//  
// (other items were commented in a header).
// -----------------------------------------------------------------------------
//
TInt CMediatorServerCommandHandler::IssueResponse( 
                                        TMediatorCategory aCategory, 
                                        MediatorService::TCommand aCommand,
                                        const TDesC8& aData,
                                        TInt aStatus )
    {
    LOG(_L("[Mediator Server]\t CMediatorServerCommandHandler::IssueResponse"));
    TBool found = EFalse;
    for ( TInt index = 0; index < iCommandPendingList.Count() && !found; index ++ )
        {
        CCommand* commandPtr = iCommandPendingList[index];
        if ( commandPtr )
            {
            TUid domain = aCategory.iDomain;
            TUid category = aCategory.iCategory;
            TInt commandId = aCommand.iCommandId;
            if ( ( domain == commandPtr->Domain() )
                 && ( category == commandPtr->Category() )
                 && ( commandId == commandPtr->Id() ) )
                {
                // We have found the correct command --> ignore the leave 
                TRAP_IGNORE( commandPtr->ResponseObserver()->CommandResponseL( domain,
                                                                               category,
                                                                               commandId,
                                                                               aStatus,
                                                                               aData ) );
                // Remove the command from list, free memory && stop looping
                iCommandPendingList.Remove( index );
                delete commandPtr;
                commandPtr = NULL;
                found = ETrue;                                                                  
                }
            }
        }

    if ( !found )
        {
        ERROR_TRACE(Print(_L("[Mediator] CMediatorServerCommandHandler::IssueResponse: Command %d not found in category %d of domain %d\n"), aCommand.iCommandId, 
                                                                                                                                             aCategory.iDomain.iUid ) );    
        }
            
    return found ? KErrNone : KErrMediatorCommandNotFound;

    }