//-------------------------------------------------------------------------------------------------
// <copyright file="main.cpp" company="LiquidRobotics">
//  Copyright (c) Liquid Robotics Corporation, confidential and proprietary.  All rights reserved. 
// </copyright>
//
// <summary>
//   Implementation of the main() function for the PayloadInterface process
// </summary>
//
// <owner>Mike Cookson</owner>
//-------------------------------------------------------------------------------------------------

#include "EncryptedPayloadController.h"
#include "EncryptedStartupSettings.h"
#include "LqrSMACStdOutLogger.h"
#include <iostream>
#include <boost/thread.hpp>

using namespace std;
using namespace LiquidRobotics::EncryptedStartup;
using namespace LiquidRobotics::PayloadInterface;
using namespace LiquidRobotics::SMACFramework;

int main(int argc, char* argv[])
{
	int retcode = 0;					// program exit code

	// init time zone variables
	tzset();

	try
	{
		EncryptedStartupSettings settings;
		settings.Load("EncryptedStartup-config.xml");

		// get the server name and kind from the setings, initialize the logger
#ifdef INCLUDE_LOGGER
		StdOutLogger logger(settings.GetServerName().c_str(), 1, Logger::LT_DEBUG);
#else
		StdOutLogger logger(settings.GetServerName().c_str(), 1, Logger::LT_NONE);
#endif

		// create a semaphore to signal the main thread to shut down
		Semaphore runSem(0);

		// construct the controller and begin running the background thread
		EncryptedPayloadController controller(settings, logger, runSem);
		HDLCPort::Ptr pHDLC = controller.InitHDLC();

		// start the protocol asynchronously
		boost::thread runThread(boost::bind(&EncryptedPayloadController::Run, &controller, pHDLC));

		// wait for exit
		runSem.Wait();
	}
	catch(exception& ex)
	{
		cerr << argv[0] << " caught exception " << typeid(ex).name() << ": " << ex.what() << " (program will exit)" << endl;
		retcode = 1;
	}

	return retcode;
}
