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

#include <iostream>
#include <string>
#include "LqrSMACObjectBroker.h"
#include "LqrSMACIObjectBroker-inline.h"
#include "LqrSMACObjectBroker-inline.h"
#include <stdlib.h>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/compare.hpp>
#include <boost/foreach.hpp>
#include "LqrSMACIdl.h"
#include "EventManagerImpl.h"
#include "EventManagerSettings.h"
#include "LqrSMACStdOutLogger.h"

using namespace std;
using namespace boost::algorithm;
using namespace LiquidRobotics::EventManager;
using namespace LiquidRobotics::SMACFramework;

int main(int argc, char* argv[])
{
	const string settingsFileName = "EventManager-config.xml";

	if (argc == 2)
	{
		const string arg1 = argv[1];
		const string write = "write";

		if (boost::iequals(arg1, write))
		{
			EventManagerSettings settings;
			settings.Save(settingsFileName);
			return 0;
		}
	}

	try
	{
		EventManagerSettings settings;
		settings.Load(settingsFileName);

		string eventManagerName = settings.GetEventManagerName();
		StdOutLogger logger(settings.GetLoggerSettings());

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

		EventManagerImpl evmgr(broker, logger, settings.GetMaxSubscribers());
		PortableServer::ServantBase_var theServer = 0;
		theServer = new ::POA_LiquidRobotics::SMAC::IEventManagerProcess_tie<EventManagerImpl>(&evmgr);

		broker.AdvertizeInstance(theServer.in(), eventManagerName.c_str());
		broker.ListenForRequests();
	}
	catch(CORBA::Exception& ex)
	{
		cerr << "Caught CORBA exception: " << DecodeCORBAException(ex) << endl;
	}
}
