bluetoothengine/headsetsimulator/core/src/Server/hsclientmanager.cpp
author michal.sulewski
Wed, 15 Sep 2010 15:59:44 +0200
branchheadsetsimulator
changeset 60 90dbfc0435e3
permissions -rw-r--r--
source code commit

/*
 * Component Name: Headset Simulator
 * Author: Comarch S.A.
 * Version: 1.0
 * Copyright (c) 2010 Comarch S.A.
 *  
 * This Software is submitted by Comarch S.A. to Symbian Foundation Limited on 
 * the basis of the Member Contribution Agreement entered between Comarch S.A. 
 * and Symbian Foundation Limited on 5th June 2009 (“Agreement”) and may be 
 * used only in accordance with the terms and conditions of the Agreement. 
 * Any other usage, duplication or redistribution of this Software is not 
 * allowed without written permission of Comarch S.A.
 * 
 */

#include "hsclientmanager.h"
#include "hsclientobservers.h"
#include "hsclient.h"
#include "debug.h"

CHsClientManager* CHsClientManager::NewL()
    {

    CHsClientManager *self = CHsClientManager::NewLC();
    CleanupStack::Pop( self );
    return self;
    }

CHsClientManager* CHsClientManager::NewLC()
    {
    CHsClientManager *self = new ( ELeave ) CHsClientManager;
    CleanupStack::PushL( self );
    self->ConstructL();
    return self;
    }

CHsClientManager::~CHsClientManager()
    {
    TRACE_FUNC_ENTRY

    iClientArray.ResetAndDestroy();
    iObserverArray.Close();

    TRACE_FUNC_EXIT
    }

void CHsClientManager::SetClientStateNotfierL(
        MHsClientStateNotifier &aClientStateNotifier, TInt aNumber )
    {
    TRACE_FUNC_ENTRY
    TRACE_INFO( ( _L("aNumber = %d"), aNumber) );

    if ( aNumber < 0 )
        {
        User::Leave( KErrUnderflow );
        }
    else if ( aNumber >= iClientArray.Count() )
        {
        User::Leave( KErrOverflow );
        }
    iClientArray[aNumber]->SetClientStateNotfier( aClientStateNotifier );
    iObserverArray.InsertL( &aClientStateNotifier, aNumber );

    TRACE_FUNC_EXIT
    }

TInt CHsClientManager::Send( const TDesC8 &aAt, TInt aNumber )
    {
    TRACE_FUNC_ENTRY
    TRACE_INFO((_L8("Data to be send: %S"), &aAt))

    TInt err = KErrNone;

    if ( aNumber >= 0 && aNumber < iClientArray.Count() )
        {
        iClientArray[aNumber]->Send( aAt );
        }
    else
        {
        err = KErrNotFound;
        }

    TRACE_INFO( ( _L("Returned value = %d"), err) );
    TRACE_FUNC_EXIT
    return err;
    }

void CHsClientManager::DisconnectClients()
    {
    TRACE_FUNC_ENTRY
    //free memory
    iClientArray.ResetAndDestroy();
    iObserverArray.Reset();
    TRACE_FUNC_EXIT
    }

void CHsClientManager::HandleNewClientL( CHsClient *aClient, TInt aErr )
    {
    TRACE_FUNC_ENTRY
    TRACE_INFO( ( _L("aErr = %d"), aErr) );

    if ( aClient && aErr == KErrNone )
        {
        iClientArray.AppendL( aClient );
        aClient->SetClientDisconnectNotfier( *this );
        iObserverArray.AppendL( aClient->GetClientStateNotifer() );
        }

    TRACE_FUNC_EXIT
    }

void CHsClientManager::HandleClientDisconnect( CHsClient *aClient, 
        TInt /** aErr */)
    {
    TRACE_FUNC_ENTRY
    TInt idx = iClientArray.Find( aClient );
    if ( KErrNotFound != idx )
        {
        iClientArray.Remove( idx );
        iObserverArray.Remove( idx );
        }

    delete aClient;

    TRACE_FUNC_EXIT
    }

CHsClientManager::CHsClientManager() :
    iClientArray( KHsClientManagerArrayMinSize ), iObserverArray(
            KHsClientManagerArrayMinSize )
    {

    }

void CHsClientManager::ConstructL()
    {

    }