// MATLAB2Network.cpp: implementation of the CMATLAB2Network class.
//
//////////////////////////////////////////////////////////////////////
 /*      
 *          Copyright (C) 2000 RasSimTech Ltd. 
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version. 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details. 
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
 */

#include <stdio.h>	//snprintf
#include "MATLAB2Network.h"
#include "UserNetwork.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CMATLAB2Network::CMATLAB2Network()
{
	m_bFirstTimerTime = TRUE;
	m_dSendRateHz = 20.0;	// defaults to 20 Hz
	m_dSendPeriodSec = 1.0/m_dSendRateHz;
	m_dSendTimerSec = 0.0;

}

CMATLAB2Network::~CMATLAB2Network()
{

}

BOOL CMATLAB2Network::bOpenConnection()
{
	if (!m_bConnectionOpen)	// only need to open the connection once
	{
		int iCount = 1;	/* one address included in this open call */
		char* strAddress[1];
		strAddress[0] = strdup("224.0.5.20:2267");/* this is the default AVDS multicast address*/
		if(OpenAVDSNetwork(iCount,strAddress) != 0)
		{
			m_bConnectionOpen = FALSE;
		}
		else
		{
			m_bConnectionOpen = TRUE;
		}
	}
	return (m_bConnectionOpen);
}

BOOL CMATLAB2Network::bInitializeVehicle(int iCraftType,double dInitLongitude,double dInitLatitude,double dInitAltitude, char* pcVehicleName)
{
	BOOL bNoError;


	m_iNumberVehicles++;

	char pcIdentifier[PACKET_HANDLE_SIZE];
	if(pcVehicleName == NULL)
	{
		pcVehicleName = pcIdentifier;
		// build identifiyer name
		_snprintf(pcVehicleName,PACKET_HANDLE_SIZE,"MATLAB_AC_%0.2d",m_iNumberVehicles);
	}

	//TODO:: to be precise these need to be doubles!!!
	float fInitLatitude = dInitLatitude;
	float fInitLongitude = dInitLongitude;
	float fInitAltitude = dInitAltitude;
	bNoError = Init_Aircraft(&m_acftVehicleSend,pcVehicleName,0,iCraftType,&fInitLongitude,&fInitLatitude,&fInitAltitude);

	sprintf(m_acftVehicleSend.packet.home_port,"none");

	for(int iCount=0;iCount<16;iCount++)
	{
		m_acftVehicleSend.packet.dUserData[iCount] = 0.0;
	}


	int iHostIDCraftID = m_iNumberVehicles + (rand()<<24) + ((GetTickCount()<<8)&0x00ffff00); /* need unique numbers */
	m_acftVehicleSend.packet.craftid = m_acftVehicleSend.packet.hostid = LOWORD(iHostIDCraftID);

	m_bFirstTimerTime = TRUE;

	if(bOpenConnection() && bNoError)
	{
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

BOOL CMATLAB2Network::bSendData()
{
	if(bOpenConnection())
	{
		return !Put_Aircraft(&m_acftVehicleSend);
	}
	else
	{
		return FALSE;
	}
}

BOOL CMATLAB2Network::bReceiveData()
{
	if(bOpenConnection())
	{
		Aircraft acftVehicle;
		return !Get_Aircraft(PacketHandler, &acftVehicle);
	}
	else
	{
		return FALSE;
	}
}

void CALLBACK CMATLAB2Network::PacketHandler(Aircraft *pacftVehicle)
{
	//TODO:: this probably won't work, need a copy constructor
	m_lacftVehiclesReceive.push_back(*pacftVehicle);
}

void CMATLAB2Network::CloseConnection()
{
	if(bOpenConnection())
	{
		CloseAVDSNetwork();	// closes all open connections
	}
	m_bConnectionOpen = FALSE;
	m_lacftVehiclesReceive.clear();
	m_iNumberVehicles = 0;
	m_bFirstTimerTime = TRUE;
}

double CMATLAB2Network::dGetDT()
{
	if (m_bFirstTimerTime)
	{
		elapsedTime.iStartElapsedTimer();
		m_bFirstTimerTime = FALSE;
	}
	return(elapsedTime.dGetElapsedTimeSec());
}


BOOL CMATLAB2Network::bTimeToSendPacket(double dDeltaTime)
{
	BOOL bSendPacket = FALSE;

	m_dSendTimerSec += dDeltaTime;
	if(m_dSendTimerSec >= m_dSendPeriodSec)
	{
		m_dSendTimerSec = 0.0;
		bSendPacket = TRUE;
	}
	return (bSendPacket);
}
