#include "tstatuscontrol_amcs.hpp"

TStatusControl_AMCS::TStatusControl_AMCS()
{		
}
TStatusControl_AMCS::~TStatusControl_AMCS()
{
}

//Define the Launch method
bool TStatusControl_AMCS::Launch()
{
	bool returns;	
	int i=0;	
	//call the tryConnect method
	returns = amcsConnection.setUp(vspSettings, clientBulletinAndEvent);	
	//loop to find a valid and pingable connection	
	while(amcsConnection.ping(amcsConnection.enabledIPs[i])!=true)
	{
		if (amcsConnection.enabledIPs[i] == 0)
		{
			//return false
			return false;
		}
		i++;
	}
	//set attribute to the connection
	amcsConnection.currentIP = i;		
	//Establish Connection to the AMCS
	if (amcsConnection.establish(vspSettings.port_AMCS, i)== true)
	{
		//call softwareCOMM function
		if (amcsConnection.softwareComm(vspSettings)== true)
		{			
			//start statusMonitor, it should never return, unless there is a problem		
			if (statusMonitor(i)== false)
			{
				return false;
			}
		}
		
	}
	//close socket and proceed with reconnectFAIL
	amcsConnection.closeSocket();
	if (amcsConnection.reconnectFail(vspSettings)!= true)
	{
		return false;
	}		
	//restart statusmonitor, it should never return, unless there is a problem
	if (statusMonitor(i) == false)
	{
		return false;
	}
}

//Status Monitor Method
//Method responsible for checking periodically the bulletin and send appropriate messages
bool TStatusControl_AMCS::statusMonitor(int i)
{
	//cast the timeout and set the counter maximum
	int maxCounts = (atoi(vspSettings.timeoutSeconds))/10;	
	printf("\n%i\n", i);	
	fflush(stdout);
	int counter = 0;	
	bool returning;
	//TEMPORARY VARIABLE TO SET NO EVENT	
	int numberOfEvents = 0;		
	
	printf("%i", maxCounts);

	while(true)
	{
		counter++;				
		//Check the bulletin for events
		//If there are events
		numberOfEvents = clientBulletinAndEvent.isAMCSEvent();
		if (numberOfEvents>0)
		{
			//Verify AMCS is still there
			//Check the presence of AMCS
			if (amcsConnection.presenceOfAMCS(vspSettings,i)!= true)
			{
				//Close socket, and return false
				amcsConnection.closeSocket();					
				//reconnectFail
				if (amcsConnection.reconnectFail(vspSettings)==false)
				{
					return false;
				}				
				//reset everything to zero and restart loop
				counter = 0;
				continue;
			}
			//create a table sized with the number of events
			TMessages_AMCS * statuses = new TMessages_AMCS();
			//generate the message
			returning = statuses->generateStatusMsg(vspSettings.hostName, vspSettings.rasSystemIdent, vspSettings.rasSiteIdent, clientBulletinAndEvent.eventArray, numberOfEvents);
			//Send Status Messages
			fflush(stdout);
			cout<< statuses->completeMessage;
			if (amcsConnection.sendMessage(statuses->completeMessage)== false)
			{
				delete statuses;				
				//Close socket, and reconnect
				amcsConnection.closeSocket();					
				//reconnectFail
				if (amcsConnection.reconnectFail(vspSettings)==false)
				{
					return false;
				}				
				//reset everything to zero and restart loop
				counter = 0;
				continue;
			}
			//delete the message;
			delete statuses;
			//send timeout message
			TMessages_AMCS * timeout = new TMessages_AMCS();
			fflush(stdout);
			returning = timeout->generateTimeoutMsg(vspSettings.hostName, vspSettings.rasSystemIdent, vspSettings.rasSiteIdent, vspSettings.timeoutSeconds);
			//send timeout
			if (amcsConnection.sendMessage(timeout->completeMessage)== false)
			{
				delete timeout;
				//Close socket, and reconnect
				amcsConnection.closeSocket();					
				//reconnectFail
				if (amcsConnection.reconnectFail(vspSettings)==false)
				{
					return false;
				}				
				//reset everything to zero and restart loop
				counter = 0;
				continue;
			}
			counter = 0;
			//reset the events
			numberOfEvents = 0;
		}
		//Else if there are no events
		else if (clientBulletinAndEvent.isAMCSEvent() == 0)
		{
			if (counter == (maxCounts/2))
			{
				//Check the presence of AMCS
				if (amcsConnection.presenceOfAMCS(vspSettings,i)!= true)
				{
					//Close socket, and return false
					amcsConnection.closeSocket();					
					//reconnectFail
					if (amcsConnection.reconnectFail(vspSettings)==false)
					{
						return false;
					}					
					//reset everything to zero and restart loop
					counter = 0;
					continue;
				}
				//Generate the timeout sequence		
				TMessages_AMCS * timeout = new TMessages_AMCS();
				//Format that message as a timeout message
				returning = timeout->generateTimeoutMsg(vspSettings.hostName,vspSettings.rasSystemIdent, vspSettings.rasSiteIdent, vspSettings.timeoutSeconds);	
				//send Timeout
				if (amcsConnection.sendMessage(timeout->completeMessage)== false)
				{
					delete timeout;
					//Close socket, and return false
					amcsConnection.closeSocket();					
					//reconnectFail
					if (amcsConnection.reconnectFail(vspSettings)==false)
					{
						return false;
					}					
					//reset everything to zero and restart loop
					counter = 0;
					continue;
				}
				//delete the message;
				delete timeout;
				counter = 0;
			}			
			else if (counter == (int)(maxCounts/4))
			{	
				//Check the presence of AMCS
				if (amcsConnection.presenceOfAMCS(vspSettings,i)!= true)
				{
					//Close socket, and return false
					amcsConnection.closeSocket();					
					//reconnectFail
					if (amcsConnection.reconnectFail(vspSettings)==false)
					{
						return false;
					}					
					//reset everything to zero and restart loop
					counter = 0;
					continue;
				}
			}
		}
		//Sleep until next need for call	
		sleep(10);		
	}//End While
	return false;
}



