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

//******************************************************************************
//
//     FILE: zcePlayDataWorker.cpp
//
//******************************************************************************

#include "zcePlayDataWorker.h"

#include "zcePlayDataPorts.h"

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

zcePlayDataWorker::zcePlayDataWorker( const PlayData_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 zcePlayDataWorker
}

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

void zcePlayDataWorker::initExecParams( const PlayData_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
}

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

	mIsDone=0;
	mIsStarted = 0;
}

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

	mIsStarted=0;
	mIsDone=1;

	ostringstream oss;
	oss << "PlayData releasedObject. \nPacketID = " << mPacketID;
	logToFile("Debug", "PlayData", oss.str());
}

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

	mIsStarted = 1;

	ostringstream oss;
	oss << "PlayData started. \nPacketID = " << mPacketID;
	logToFile("Debug", "PlayData", oss.str());
}

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

	mIsStarted = 0;

	ostringstream oss;
	oss << "PlayData stopped. \nPacketID = " << mPacketID;
	logToFile("Debug", "PlayData", oss.str());
}

/*!	\brief
 *
 *	 Describe operation here.
 *
 *	\param msg ( ::CF::OctetSequence )
 *
 *	\param options ( ::CF::Properties )
 *
 */
void zcePlayDataWorker::PushPorts_OctetSeqConsumer_processOctetMsg( int portIdentifier, const CF::OctetSequence& msg, const CF::Properties& options ZCE_EXC_ENV_ARG )
{
	ZCE_ASSERT_EXCEPTION_VOID;

	if(mIsStarted == 1)
	{
		mPacketID++;
		portDataOut_->processOctetMsg(msg, options);

		ostringstream oss;
		oss << "PlayData send message. \nPacketID = " << mPacketID;
		oss << msg[0] << msg[1] << msg[2] << msg[3] << msg[4] << msg[5] << msg[6] << msg[7] << msg[8] << msg[9];
		logToFile("Debug", "PlayData", oss.str());

		usleep(50);
	}
}

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

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

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

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