//------------------------------------------------------------------------------
// <copyright file="SSQProgram.h" company="LiquidRobotics">
//
// Copyright (c) 2014 Liquid Robotics, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// This software is associated with a release.  Liquid Robotics makes no
// representation that the design, interfaces or component distribution will
// remain unchanged in future releases.
//
// </copyright>
//
// <summary>
// Declarations for SSQProgram, which orchestrates program startup and shutdown
//  and interfaces the Controller class to CORBA
// </summary>
//
// <owner>Mike Cookson</owner>
//------------------------------------------------------------------------------

#ifndef __SSQProgram_h__
#define __SSQProgram_h__

#include "EventTransceiver.h"
#include "LqrSMACIdl.h"
#include "LqrSMACSharedLib.h"
#include "SSQController.h"
#include "SSQProgramSettings.h"
#include "PayloadManagerClient.h"
#include "SensorProgram.h"

namespace LiquidRobotics {

namespace EventLib {
	class GPSSampleEvent;
}

namespace SimpleSBDQueue {

class SHARED_LIB_PUBLIC SSQProgram
: public LiquidRobotics::EventLib::SensorProgram
{
public:

	typedef LiquidRobotics::EventLib::SensorProgram Base;
	typedef LiquidRobotics::EventLib::GPSSampleEvent GPSSampleEvent;
	typedef LiquidRobotics::SMACFramework::ByteArray ByteArray;
	typedef LiquidRobotics::SMACFramework::PayloadDescription Desc;
	typedef LiquidRobotics::EventLib::PayloadManagerClient PayloadManagerClient;

	SSQProgram(IObjectBroker& _broker, const SSQProgramSettings& _settings, Logger& _logger);
	virtual ~SSQProgram();

	virtual void Initialize(Object_ptr _pServer, PayloadManagerClient::Ptr _pmClient);
	virtual void Shutdown();

	virtual void SendCommand(const ::CORBA::Any& command, const char* requester, const char* transactionID, ::CORBA::Any_OUT_arg result);

protected:

	typedef LiquidRobotics::EventLib::IEventBase IEventBase;

	void GPSSampleHandlerAsSample(EventTransceiver& sender, const GPSSampleEvent& receivedEvent);
	void OnCommandCompleted(const EventLib::SensorControllerBaseCommandID& cmdID, const string& result, bool success);
	void OnPacketReceived(SSQController& sender, const ByteArray& packetData, unsigned MTMSN);
	void OnPacketSendDisposition(SSQController& sender, uint32_t priority, uint16_t senderID, uint32_t senderTransaction, unsigned MOMSN, SSQController::SDSDisposition disposition);
	void OnChannelStats(SSQController& sender, const SSQCommunicator::Statistics& stats);
	void OnEnqueueEvent(EventTransceiver& sender, const IEventBase& receivedEvent);

	IObjectBroker& broker;
	const SSQProgramSettings& settings;
	Logger& logger;
	double latSave, lonSave;
	SSQControllerPtr ctrl;
	SSQController::ScopedConnectionPtr commandCompletedConnection;
	SSQController::ScopedConnectionPtr packetReceivedConnection;
	SSQController::ScopedConnectionPtr packetSendDispositionConnection;
	SSQController::ScopedConnectionPtr channelStatsConnection;
	SSQController::ScopedConnectionPtr connectionToEnqueueEvent ;
};

}
}

#endif
