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

#include <iostream>
#include <string>
#include "LqrSMACStdOutLogger.h"
#include "LqrSMACObjectBroker.h"
#include "LqrSMACIObjectBroker-inline.h"
#include "LqrSMACObjectBroker-inline.h"
#include "PayloadManager.h"
#include "PayloadManagerSettings.h"
#include <stdlib.h>
#include <boost/foreach.hpp>
#include "LqrSMACIdl.h"

using namespace std;
using namespace LiquidRobotics::SMACFramework;
using namespace CORBA;

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

	try
	{
		PayloadManagerSettings settings;
		settings.Load("PayloadManager-config.xml");

		const string serverName = settings.GetServerName();
		const string serverKind = settings.GetServerKind();
		StdOutLogger logger(settings.GetLoggerSettings());

		ObjectBroker broker(true);
		broker.Initialize(argc, argv);

		PayloadManager mgr(broker, settings, logger);

		PortableServer::ServantBase_var theServer = 0;
		theServer = new ::POA_LiquidRobotics::SMAC::IPayloadManagerProcess_tie<PayloadManager>(&mgr);

		Object_var serverRef = broker.AdvertizeInstance(theServer.in(), serverName.c_str(), serverKind.c_str());
		mgr.Initialize(serverRef.in());
		broker.ListenForRequests();
	}
	catch(CORBA::Exception& ex)
	{
		cerr << "Caught CORBA exception: " << DecodeCORBAException(ex) << endl;
		retcode = 1;
	}
	catch(exception& ex)
	{
		cerr << "Caught " << typeid(ex).name() << ": " << ex.what() << endl;
		retcode = 1;
	}

	return retcode;
}
