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

//******************************************************************************
//
//     FILE: zceProcessTxWorker.cpp
//
//******************************************************************************

#include "zceProcessTxWorker.h"

#include "zceProcessTxPorts.h"

#include "../../Constants.h"
#include "../../Support.h"

zceProcessTxWorker::zceProcessTxWorker( const ProcessTx_params& execParams, zcePushPorts_OctetSeqConsumerUsesPort* portDataOut ZCE_EXC_ENV_ARG ):
	portDataOut_(portDataOut)
	#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 zceProcessTxWorker
}

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

void zceProcessTxWorker::initExecParams( const ProcessTx_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 zceProcessTxWorker::config_propDestinationId( const CORBA::String_var& propdestinationid  ZCE_EXC_ENV_ARG )
{
	ZCE_ASSERT_EXCEPTION_VOID;

	mPropDestinationId = propdestinationid;
}

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

	mPropQoS = propqos;
}

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

	mPropModulation = propmodulation;
}

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

	mPropApplicationType = propapplicationtype;
}


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

	propdestinationid = mPropDestinationId;
}

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

	propqos = mPropQoS;
}

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

	propmodulation = mPropModulation;
}

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

	propapplicationtype = mPropApplicationType;
}



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

/*!	\brief
 *
 *	 Describe operation here.
 * 
 *	\throw ::CF::LifeCycle::ReleaseError
 * 
 */
void zceProcessTxWorker::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 zceProcessTxWorker::CF_Resource_start( int portIdentifier ZCE_EXC_ENV_ARG )
{
	ZCE_ASSERT_EXCEPTION_VOID;
}

/*!
 * \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 zceProcessTxWorker::CF_Resource_stop( int portIdentifier ZCE_EXC_ENV_ARG )
{
	ZCE_ASSERT_EXCEPTION_VOID;
}

/*!	\brief
 *
 *	 Describe operation here.
 * 
 *	\param msg ( ::CF::OctetSequence ) 
 * 
 *	\param options ( ::CF::Properties ) 
 * 
 */
void zceProcessTxWorker::PushPorts_OctetSeqConsumer_processOctetMsg( int portIdentifier, const CF::OctetSequence& msg, const CF::Properties& options ZCE_EXC_ENV_ARG )
{
	ZCE_ASSERT_EXCEPTION_VOID;
	
	ostringstream oss;
	string headerStr;
	unsigned int headerLength;
	unsigned int processedMsgLength;
	unsigned int realDataLengthSize = msg.length();

	// Generate header
	ostringstream destinationIDStream;
	destinationIDStream << mPropDestinationId;
	string adjustedDestinationIDStr = adjustStringLenght(destinationIDStream.str(), CONST_HEADER_DESTINATION_ID_SIZE);

	ostringstream applicationTypeStream;
	applicationTypeStream << mPropApplicationType;
	string adjustedApplicationTypeStr = adjustStringLenght(applicationTypeStream.str(), CONST_HEADER_APPLICATION_TYPE_SIZE);

	ostringstream qosStream;
	qosStream << mPropQoS;
	string adjustedQoSStr = adjustStringLenght(qosStream.str(), CONST_HEADER_QOS_SIZE);

	ostringstream realDataLengthSizeStream;
	realDataLengthSizeStream << realDataLengthSize;
	string adjustedRealDataLengthSizeStr = adjustStringLenght(realDataLengthSizeStream.str(), CONST_HEADER_REAL_DATA_LENGTH_SIZE);

	oss << adjustedDestinationIDStr << adjustedApplicationTypeStr << adjustedQoSStr << adjustedRealDataLengthSizeStr;
	headerStr = oss.str();
	headerLength = headerStr.length();
	processedMsgLength = CONST_PACKET_SIZE;

	// Generate message
	CF::OctetSequence processedMsg(processedMsgLength + 1);
	processedMsg.length(processedMsgLength);

	for(unsigned int i = 0; i < processedMsg.length(); i++)
	{
		processedMsg[i] = 0;
	}

	for(unsigned int i = 0; i < headerLength; i++)
	{
		processedMsg[i] = char(headerStr[i]);
	}

	for(unsigned int i = 0; i < msg.length(); i++)
	{
		// Shift the data Modulation times
		CORBA::Octet tmp = msg[i];
		tmp = tmp + mPropModulation;
		processedMsg[i + headerLength] = tmp;
	}

	portDataOut_->processOctetMsg(processedMsg, options);
}


void zceProcessTxWorker::setportDataOut( zcePushPorts_OctetSeqConsumerUsesPort& portDataOut )
{
	portDataOut_ = &portDataOut;
}

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

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

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