//******************************************************************************
//
// Restricted Rights
// WARNING: This is a restricted distribution Zeligsoft file.
//
//******************************************************************************
//
// Copyright (C) 2006 Zeligsoft Inc. - All Rights Reserved
//

//******************************************************************************
//
//     FILE: zceAssemblyControllerWorker.cpp
//
//******************************************************************************

#include "zceAssemblyControllerWorker.h"

#include "zceAssemblyControllerPorts.h"

void zceAssemblyControllerWorker::setAudioDeviceRunningStatus()
{
	if (mPropApplicationType == 0)
	{
		// Voice
		if (portAudioDevice_->is_connected())
		{
			portAudioDevice_->start();
		}
	}
	else if (mPropApplicationType == 1)
	{
		// Data
		if (portAudioDevice_->is_connected())
		{
			portAudioDevice_->stop();
		}
	}
}

zceAssemblyControllerWorker::zceAssemblyControllerWorker( const AssemblyController_params& execParams, zceCF_ResourceUsesPort* portSendReceiveData, zceCF_DeviceUsesPort* portAudioDevice, zceCF_ResourceUsesPort* portProcessRx, zceCF_ResourceUsesPort* portProcessTx, zceCF_ResourceUsesPort* portCaptureData, zceCF_ResourceUsesPort* portWriteData, zceCF_ResourceUsesPort* portReadData, zceCF_ResourceUsesPort* portPlayData ZCE_EXC_ENV_ARG ):
	portSendReceiveData_(portSendReceiveData), portAudioDevice_(portAudioDevice), portProcessRx_(portProcessRx), portProcessTx_(portProcessTx), portCaptureData_(portCaptureData), portWriteData_(portWriteData), portReadData_(portReadData), portPlayData_(portPlayData)
	#if defined(DEBUG_MSGS)
	, debug_(0)
	#endif

{
	ZCE_ASSERT_EXCEPTION_VOID;
	#if defined(DEBUG_MSGS)
	debug_.rdbuf(std::cout.rdbuf());
	#endif


	initExecParams(execParams ZCE_EXC_ENV_PARAM );

	// TODO : Implement constructor for zceAssemblyControllerWorker
}

zceAssemblyControllerWorker::~zceAssemblyControllerWorker()
{
	// TODO : Implement destructor for zceAssemblyControllerWorker
}

void zceAssemblyControllerWorker::initExecParams( const AssemblyController_params& params ZCE_EXC_ENV_ARG)
{

	ZCE_ASSERT_EXCEPTION_VOID;
	execParams_.NAMING_CONTEXT_IOR = params.NAMING_CONTEXT_IOR;
	execParams_.NAME_BINDING = params.NAME_BINDING;
	execParams_.COMPONENT_IDENTIFIER = params.COMPONENT_IDENTIFIER;

	// TODO: perform some action based on these exec parameters
}

void zceAssemblyControllerWorker::config_propQoS( const CORBA::Long& propqos  ZCE_EXC_ENV_ARG )
{
	ZCE_ASSERT_EXCEPTION_VOID;

	mPropQoS = propqos;

	CORBA::String_var propQoSProcessTx = CORBA::string_dup("QoS");
	CORBA::String_var propQoSProcessRx = CORBA::string_dup("QoS");

	CF::Properties propQoS(1);
	propQoS.length(1);
	propQoS[0].value <<= propqos;

	// Configure ProcessTx
	propQoS[0].id = propQoSProcessTx;
	if(portProcessTx_->is_connected())
	{
		portProcessTx_->configure(propQoS);
	}

	// Configure ProcessRx
	propQoS[0].id = propQoSProcessRx;
	if(portProcessRx_->is_connected())
	{
		portProcessRx_->configure(propQoS);
	}
}

void zceAssemblyControllerWorker::config_propDestinationId( const CORBA::String_var& propdestinationid  ZCE_EXC_ENV_ARG )
{
	ZCE_ASSERT_EXCEPTION_VOID;

	mPropDestinationId = propdestinationid;

	CORBA::String_var propDestinationIdProcessTx = CORBA::string_dup("DestinationId");
	CORBA::String_var propDestinationIdProcessRx = CORBA::string_dup("DestinationId");

	CF::Properties propDestinationId(1);
	propDestinationId.length(1);
	propDestinationId[0].value <<= propdestinationid;

	// Configure ProcessTx
	propDestinationId[0].id = propDestinationIdProcessTx;
	if(portProcessTx_->is_connected())
	{
		portProcessTx_->configure(propDestinationId);
	}

	// Configure ProcessRx
	propDestinationId[0].id = propDestinationIdProcessRx;
	if(portProcessRx_->is_connected())
	{
		portProcessRx_->configure(propDestinationId);
	}
}

void zceAssemblyControllerWorker::config_propModulation( const CORBA::Long& propmodulation  ZCE_EXC_ENV_ARG )
{
	ZCE_ASSERT_EXCEPTION_VOID;

	mPropModulation = propmodulation;

	CORBA::String_var propModulationProcessTx = CORBA::string_dup("Modulation");
	CORBA::String_var propModulationProcessRx = CORBA::string_dup("Modulation");

	CF::Properties propModulation(1);
	propModulation.length(1);
	propModulation[0].value <<= propmodulation;

	// Configure ProcessTx
	propModulation[0].id = propModulationProcessTx;
	if(portProcessTx_->is_connected())
	{
		portProcessTx_->configure(propModulation);
	}

	// Configure ProcessRx
	propModulation[0].id = propModulationProcessRx;
	if(portProcessRx_->is_connected())
	{
		portProcessRx_->configure(propModulation);
	}
}

void zceAssemblyControllerWorker::config_propInputFilePath( const CORBA::String_var& propinputfilepath  ZCE_EXC_ENV_ARG )
{
	ZCE_ASSERT_EXCEPTION_VOID;

	mPropInputFilePath = propinputfilepath;

	CORBA::String_var propInputFilePathReadData = CORBA::string_dup("InputFilePath");

	CF::Properties propInputFilePath(1);
	propInputFilePath.length(1);
	propInputFilePath[0].value <<= propinputfilepath;

	// Configure ReadData
	propInputFilePath[0].id = propInputFilePathReadData;
	if(portReadData_->is_connected())
	{
		portReadData_->configure(propInputFilePath);
	}
}

void zceAssemblyControllerWorker::config_propApplicationType( const CORBA::Long& propapplicationtype  ZCE_EXC_ENV_ARG )
{
	ZCE_ASSERT_EXCEPTION_VOID;

	mPropApplicationType = propapplicationtype;

	CORBA::String_var propApplicationTypeProcessTx = CORBA::string_dup("ApplicationType");
	CORBA::String_var propApplicationTypeProcessRx = CORBA::string_dup("ApplicationType");
	CORBA::String_var propApplicationTypeReadData = CORBA::string_dup("ApplicationType");
	CORBA::String_var propApplicationTypeCaptureData = CORBA::string_dup("ApplicationType");

	CF::Properties propApplicationType(1);
	propApplicationType.length(1);
	propApplicationType[0].value <<= propapplicationtype;

	// Configure ProcessTx
	propApplicationType[0].id = propApplicationTypeProcessTx;
	if(portProcessTx_->is_connected())
	{
		portProcessTx_->configure(propApplicationType);
	}

	// Configure ProcessRx
	propApplicationType[0].id = propApplicationTypeProcessRx;
	if(portProcessRx_->is_connected())
	{
		portProcessRx_->configure(propApplicationType);
	}

	// Configure ReadData
	propApplicationType[0].id = propApplicationTypeReadData;
	if(portReadData_->is_connected())
	{
		portReadData_->configure(propApplicationType);
	}

	// Configure CaptureData
	propApplicationType[0].id = propApplicationTypeCaptureData;
	if(portCaptureData_->is_connected())
	{
		portCaptureData_->configure(propApplicationType);
	}

	setAudioDeviceRunningStatus();
}


void zceAssemblyControllerWorker::query_propQoS( CORBA::Long& propqos  ZCE_EXC_ENV_ARG  )
{
	ZCE_ASSERT_EXCEPTION_VOID;

    propqos = mPropQoS;
}

void zceAssemblyControllerWorker::query_propDestinationId( CORBA::String_var& propdestinationid  ZCE_EXC_ENV_ARG  )
{
	ZCE_ASSERT_EXCEPTION_VOID;

	propdestinationid = mPropDestinationId;
}

void zceAssemblyControllerWorker::query_propModulation( CORBA::Long& propmodulation  ZCE_EXC_ENV_ARG  )
{
	ZCE_ASSERT_EXCEPTION_VOID;

	propmodulation = mPropModulation;
}

void zceAssemblyControllerWorker::query_propInputFilePath( CORBA::String_var& propinputfilepath  ZCE_EXC_ENV_ARG  )
{
	ZCE_ASSERT_EXCEPTION_VOID;

    propinputfilepath = mPropInputFilePath;
}

void zceAssemblyControllerWorker::query_propApplicationType( CORBA::Long& propapplicationtype  ZCE_EXC_ENV_ARG  )
{
	ZCE_ASSERT_EXCEPTION_VOID;

	propapplicationtype = mPropApplicationType;
}



/*!	\brief
 *
 *	 Describe operation here.
 *
 *	\throw ::CF::LifeCycle::InitializeError
 *
 */
void zceAssemblyControllerWorker::CF_LifeCycle_initialize( int portIdentifier ZCE_EXC_ENV_ARG )
{
	ZCE_ASSERT_EXCEPTION_VOID;

	mPropApplicationType = 0;
}

/*!	\brief
 *
 *	 Describe operation here.
 *
 *	\throw ::CF::LifeCycle::ReleaseError
 *
 */
void zceAssemblyControllerWorker::CF_LifeCycle_releaseObject( int portIdentifier ZCE_EXC_ENV_ARG )
{
	ZCE_ASSERT_EXCEPTION_VOID;
}

/*!
 * \brief The start operation is provided to command a Resource implementing this
 * interface to start internal processing.
 *
 * Specification reference:  3.1.3.1.6.5.2
 *
 * The start operation puts the Resource in an operating condition.
 *
 * \return This operation does not return a value.
 *
 * \throw StartError The start operation shall raise the StartError exception if
 * an error occurs while starting the resource.
 *
 */
void zceAssemblyControllerWorker::CF_Resource_start( int portIdentifier ZCE_EXC_ENV_ARG )
{
	ZCE_ASSERT_EXCEPTION_VOID;

    setAudioDeviceRunningStatus();
}

/*!
 * \brief The stop operation is provided to command a Resource implementing this
 * interface to stop internal processing.
 *
 * Specification reference:  3.1.3.1.6.5.1
 *
 * The stop operation shall disable all current operations and put the Resource in
 * a non-operating condition. Subsequent configure, query, and start operations
 * are not inhibited by the stop operation.
 *
 * \return This operation does not return a value.
 *
 * \throw StopError The stop operation shall raise the StopError exception if an
 * error occurs while stopping the resource.
 *
 */
void zceAssemblyControllerWorker::CF_Resource_stop( int portIdentifier ZCE_EXC_ENV_ARG )
{
	ZCE_ASSERT_EXCEPTION_VOID;

	portAudioDevice_->stop();
}


void zceAssemblyControllerWorker::setportSendReceiveData( zceCF_ResourceUsesPort& portSendReceiveData )
{
	portSendReceiveData_ = &portSendReceiveData;
}
void zceAssemblyControllerWorker::setportAudioDevice( zceCF_DeviceUsesPort& portAudioDevice )
{
	portAudioDevice_ = &portAudioDevice;
}
void zceAssemblyControllerWorker::setportProcessRx( zceCF_ResourceUsesPort& portProcessRx )
{
	portProcessRx_ = &portProcessRx;
}
void zceAssemblyControllerWorker::setportProcessTx( zceCF_ResourceUsesPort& portProcessTx )
{
	portProcessTx_ = &portProcessTx;
}
void zceAssemblyControllerWorker::setportCaptureData( zceCF_ResourceUsesPort& portCaptureData )
{
	portCaptureData_ = &portCaptureData;
}
void zceAssemblyControllerWorker::setportWriteData( zceCF_ResourceUsesPort& portWriteData )
{
	portWriteData_ = &portWriteData;
}
void zceAssemblyControllerWorker::setportReadData( zceCF_ResourceUsesPort& portReadData )
{
	portReadData_ = &portReadData;
}
void zceAssemblyControllerWorker::setportPlayData( zceCF_ResourceUsesPort& portPlayData )
{
	portPlayData_ = &portPlayData;
}

#if defined(DEBUG_MSGS)
void zceAssemblyControllerWorker::setDebugStream(const std::ostream& stream)
{
	debug_.rdbuf(stream.rdbuf());
}

void zceAssemblyControllerWorker::debugMsg(const std::string& msg)
{
	if(debug_ != NULL)
	{
		debug_ << msg << std::endl;
	}
}
#endif

// End-Of-File
//------------------------------------------------------------------------------
