//-------------------------------------------------------------------------------
// <copyright file="CommonSendCommand.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>
// 	Declaration of CommonSendCommand, which handles most of the SendCommand CORBA
//  method
// </summary>
//
// <owner>Mike Cookson</owner>
//-------------------------------------------------------------------------------

#ifndef __PAYLOADINTERFACELIB_COMMONSENDCOMMAND_H__
#define __PAYLOADINTERFACELIB_COMMONSENDCOMMAND_H__

#include "AlarmSubsystem.h"
#include "EventTransceiver.h"
#include "LocalTransactionCounter.h"
#include "LqrSMACAsynchronousProcessExecutor.h"
#include "LqrSMACByteArray.h"
#include "LqrSMACIdl.h"
#include "LqrSMACLogger.h"
#include "LqrSMACSharedLib.h"
#include "LqrSMACScopedConnection.h"
#include "LqrSMACTempFile.h"
#include "PayloadInterfaceController_Config.h"
#include "PayloadManagerClient.h"

namespace LiquidRobotics {
namespace PayloadInterface {

class SHARED_LIB_PUBLIC CommonSendCommand
: public LiquidRobotics::SMACFramework::ICommandableBase
, public AlarmSubsystem
{
public:

	typedef LiquidRobotics::EventLib::EventTransceiver EventTransceiver;
	typedef LiquidRobotics::SMACFramework::Logger Logger;
	typedef LiquidRobotics::EventLib::PayloadManagerClient PayloadManagerClient;
	typedef LiquidRobotics::SMACFramework::ScopedConnectionPtr ScopedConnectionPtr;
	typedef std::string string;

	CommonSendCommand(
			LiquidRobotics::SMACFramework::IObjectBroker& _broker,
			EventTransceiver& _et,
			Logger& _logger,
			LocalTransactionCounter& _ltc,
			const PayloadInterfaceController_Config& _config,
			PayloadManagerClient& _pmClient);
	virtual ~CommonSendCommand();

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

protected:

    virtual bool HandleAdditionalCommands(const string& command, const string& requester, const string& transactionID, string& result);

private:

    typedef LiquidRobotics::SMACFramework::AsynchronousProcessExecutor AsynchronousProcessExecutor;
	typedef LiquidRobotics::SMACFramework::LockableConceptMutex LockableConceptMutex;

	void APEExit(AsynchronousProcessExecutor& sender, int resultCode);
	void APEReport(AsynchronousProcessExecutor& sender, const LiquidRobotics::SMACFramework::ByteArray& dataReceived, bool fromStdErr);
	void DumpPayloadManagerConfig();

protected:

	void ThrowIfCommandExecuting();
	void ThrowIfEmptyOrNull(const char* paramName, const char* ptr);

private:

	LiquidRobotics::SMACFramework::IObjectBroker& broker;
    EventTransceiver& et;
    Logger& logger;
    LocalTransactionCounter& ltc;
    const PayloadInterfaceController_Config& config;
    PayloadManagerClient& pmClient;
    LockableConceptMutex firingMutex;
    AsynchronousProcessExecutor::Ptr apePtr;
	ScopedConnectionPtr connectionToAPEExit;
	ScopedConnectionPtr connectionToAPEReport;

	struct BackgroundProcessData
	{
		typedef std::deque<LiquidRobotics::SMACFramework::ByteArray::byte> ByteDQ;
		typedef std::string string;
		typedef LiquidRobotics::SMACFramework::TempFile TempFile;
		typedef boost::shared_ptr<TempFile> TempFilePtr;

		BackgroundProcessData() : active(false) { }

		void reset()
		{
			resultingData.clear();
			active = false;
			tempFile.reset();
		}

		void reset(const char* _requester, const char* _transactionID)
		{
			reset();
			requester = _requester;
			transactionID = _transactionID;
			active = true;
		}

		string requester;
		string transactionID;
		ByteDQ resultingData;
		TempFilePtr tempFile;
		bool active;
	};

	BackgroundProcessData backgroundProcessData;
};

}
}

#endif
