/********************************************************************\
|*	Portons Copyright © 1999 RasSimTech Ltd.                       	*|
|*	Printed in the United States of America.                       	*|
|*	All rights reserved.                                           	*|
|*	                                                               	*|
|*	This product or document is protected by copyright and         	*|
|*	distributed under licenses restricting its use,                	*|
|*	copyrightand distribution under licenses restricting           	*|
|*	its use, copying, distribution and decompilation.              	*|
|*	                                                               	*|
|*	No part of this product or document may be reproduced in       	*|
|*	any form by any means without prior written authorization of   	*|
|*	RasSimTech Ltd.							                     	*|
|*	                                                               	*|
\********************************************************************/



#include <PacketSnooper.h>

#include "windows.h"
#include <stdio.h>

#include <UserNetwork.h>

#define PRINT_TIME_MS 1000
#define RUN_TIME_MS 20
#define TOTAL_TIME_SEC 100.0  /*total run time inseconds*/

void CALLBACK PacketHandler(Aircraft *);

void
main(int argc, char *argv)
{

	Aircraft AircraftReceived;
	int iCount = 1;
	char* strAddress[1];
	strAddress[0] = strdup("224.0.5.20:2267");/* this is the default AVDS multicast address*/
	OpenAVDSNetwork(iCount,strAddress);

	DWORD dwCurrentTimeMs,dwLastTimeMs;/*MILISECONDS*/
	float dwTotalTimeSec;
	DWORD dwElapsedTimeMs,dwRunTimeMs,dwSendTimeMs;

	dwLastTimeMs = GetTickCount();
	dwRunTimeMs = 0;
	dwSendTimeMs=0;

	dwTotalTimeSec = 0;
	
	while (TRUE){
		dwCurrentTimeMs = GetTickCount();
		dwElapsedTimeMs = dwCurrentTimeMs-dwLastTimeMs;
		dwTotalTimeSec+=(float)(dwElapsedTimeMs/1000.0);
		dwRunTimeMs+=dwElapsedTimeMs;
		dwSendTimeMs+=dwElapsedTimeMs;
		if (dwTotalTimeSec>TOTAL_TIME_SEC){
			break;
		}
		dwLastTimeMs = dwCurrentTimeMs;
		Get_Aircraft(PacketHandler,&AircraftReceived);
	}
	CloseAVDSNetwork();
	fprintf(stderr,"Finished:Total Time = %f \n",dwTotalTimeSec);


}

BOOL Get_Aircraft(void CALLBACK pAircraftCallback ( Aircraft *aP),Aircraft * aP);

void CALLBACK PacketHandler(Aircraft *aP)
{
	fprintf(stdout,"%i %i %4.1g %4.1g %4.1f %4.1f %4.1f %4.1f %s %s\n",
					aP->packet.hostid,
					aP->packet.craftid,
					aP->pos_X,aP->pos_Y,aP->pos_Z,
					aP->rot_X,aP->rot_Y,aP->rot_Z,
					aP->packet.handle,
					aP->packet.home_port
					);

}
