//===========================================================================//
// File:	apptask.cpp                                                         //
// Project: MUNGA           Brick: Application								  		  //
// Contents: Interface specification for Application								  //
//---------------------------------------------------------------------------//
//   Date    Who  Modification                                               //
// --------  ---  ---------------------------------------------------------- //
// 08/24/94  ECH  Initial coding.                                            //
//---------------------------------------------------------------------------//
// Copyright (C) 1994-1995, Virtual World Entertainment, Inc.                //
// All Rights reserved worldwide                                             //
// This unpublished sourcecode is PROPRIETARY and CONFIDENTIAL               //
//===========================================================================//

#include <munga.hpp>
#pragma hdrstop

#if !defined(APPTASK_HPP)
#	include <apptask.hpp>
#endif

#if !defined(RENDERER_HPP)
#	include	<renderer.hpp>
#endif

#if !defined(AUDREND_HPP)
#	include	<audrend.hpp>
#endif

#if !defined(APP_HPP)
#	include <app.hpp>
#endif

#if !defined(NTTMGR_HPP)
#	include <nttmgr.hpp>
#endif

#if !defined(GAUGEREND_HPP)
#	include <gaugrend.hpp>
#endif

#if defined(TRACE_COMPLETE_CYCLES)
	BitTrace Complete_Cycles("Complete Cycles");
#endif

#if defined(TRACE_DEATH_ROW)
	BitTrace Death_Row("Death Row");
#endif

//#############################################################################
//###########################    ApplicationTask    ###########################
//#############################################################################

ApplicationTask::ApplicationTask(ClassID class_ID):
	Component(class_ID)
{
}

ApplicationTask::~ApplicationTask()
{
}

//#############################################################################
//###########################    BackgroundTasks    ###########################
//#############################################################################

BackgroundTasks::BackgroundTasks():
	taskSocket(NULL)
{
	taskIterator = new SChainIteratorOf<ApplicationTask*>(&taskSocket);
	Register_Object(taskIterator);
}

BackgroundTasks::~BackgroundTasks()
{
	Check(taskIterator);
   taskIterator->DeletePlugs();
	Unregister_Object(taskIterator);
	delete taskIterator;
}

Logical
	BackgroundTasks::TestInstance() const
{
	Component::TestInstance();
	Check(&taskSocket);
	Check(taskIterator);
	return True;
}

void
	BackgroundTasks::AddTask(ApplicationTask *task)
{
	Check(this);
   Check(task);
	taskSocket.Add(task);
}

void
	BackgroundTasks::Execute()
{
	Check(this);

	Check(taskIterator);
	if (taskIterator->GetCurrent() == NULL)
	{
		taskIterator->First();
	}

	ApplicationTask *task = taskIterator->GetCurrent();
	Check(task);
	task->Execute();
	taskIterator->Next();
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~ NetworkManagerTask ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

void
	NetworkManagerTask::Execute()
{
	Check(this);
	Check(application);
	Check(application->GetNetworkManager());
	application->GetNetworkManager()->ExecuteBackground();
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~ RoutePacketTask ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

void
	RoutePacketTask::Execute()
{
	Check(this);

	Check(application);
	Check(application->GetNetworkManager());
	application->GetNetworkManager()->RoutePacket();
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ProcessEventTask ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

void
	ProcessEventTask::Execute()
{
	Check(this);
	Check(application);
	application->ProcessOneEvent();
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~ AudioRendererTask ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

void
	AudioRendererTask::Execute()
{
	Check(this);
	Check(application);
	if (application->GetAudioRenderer() != NULL)
	{
		Check(application->GetAudioRenderer());
		application->GetAudioRenderer()->ExecuteBackground();
	}
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GaugeRendererTask ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

void
	GaugeRendererTask::Execute()
{
	Check(this);
	Check(application);
	if (application->GetGaugeRenderer() != NULL)
	{
		Check(application->GetGaugeRenderer());
		application->GetGaugeRenderer()->ExecuteBackground();
	}
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~ CompleteCyclesTask ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

void
	CompleteCyclesTask::Execute()
{
	SET_COMPLETE_CYCLES();

	Check(this);
	Check(application);
	Check(application->GetRendererManager());
	application->GetRendererManager()->CompleteCycles();

	CLEAR_COMPLETE_CYCLES();
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ FryDeathRowTask ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

void
	FryDeathRowTask::Execute()
{
	SET_DEATH_ROW();

	Check(this);
	Check(application);
	Check(application->GetEntityManager());
	application->GetEntityManager()->FryDeathRow();

	CLEAR_DEATH_ROW();
}

