// DWBS ------------------------------------------------------------------ DWBS
//
// PLATFORM		: ALL
// PRODUCT		: BIT_DEMON
// VISIBILITY	: PUBLIC
//
// DWBS ------------------------------------------------------------------ DWBS

#include <bdLobby/bdRemoteTaskManager/bdRemoteTask.h>

#define BD_LOG_LEVEL "lobbyservice/Remote task"

bdRemoteTask::bdRemoteTask()
: m_timeout(0.f),
  m_status(BD_EMPTY),
  m_transactionID(0)
{
	m_timer.reset();
}

bdRemoteTask::~bdRemoteTask()
{
}

void bdRemoteTask::close()
{
	delete this;
}

bdRemoteTask::bdStatus bdRemoteTask::getStatus()
{
	// if pending check the timeout
	if(m_status == BD_PENDING)
	{
		const bdFloat32 elapsedTime = m_timer.getElapsedTimeInSeconds();
		if((m_timeout > 0) && (elapsedTime >= m_timeout))
		{
			m_status = BD_TIMED_OUT;
			m_results = BD_NULL;
			BD_INFO(BD_LOG_LEVEL, "Remote task timed out after %.3fs.", m_timeout);
		}
	}
	

	return m_status;
}

bdBitBufferRef bdRemoteTask::getResults() const
{
	return m_results;
}


bdFloat32 bdRemoteTask::getTimeout() const
{
	return m_timeout;
}

void bdRemoteTask::setTimeout(const bdFloat32 timeout)
{
	m_timeout = timeout;
}

bdUInt64 bdRemoteTask::getTransactionID() const
{
	return m_transactionID;
}


void bdRemoteTask::start(const bdFloat32 timeout)
{
	m_timer.start();
	m_timeout = timeout;
	m_status = BD_PENDING;
	m_results = BD_NULL;
}

void bdRemoteTask::stop(const bdBitBufferRef results, const bdUInt64 id)
{	
	m_status = BD_DONE;
	m_results = results;
	m_transactionID = id;
}

