//-------------------------------------------------------------------------------------------------
// <copyright file="LWCloseLuaInternalCmd.cpp" company="LiquidRobotics">
//  Copyright (c) Liquid Robotics Corporation, confidential and proprietary.  All rights reserved. 
// </copyright>
//
// <summary>
//	Implementation of the LWCloseLuaInternalCmd class, which is queued up to be
//	executed on a thread specific to a lua interpreter.  It calls back to the
//	controller to actually close a lua instance.
// </summary>
//
// <owner>Mike Cookson</owner>
//-------------------------------------------------------------------------------------------------

#include "LWController.h"
#include "LWCloseLuaInternalCmd.h"
#include "LqrSMACLogger.h"
#include <boost/algorithm/string.hpp>

using namespace std;
using namespace LiquidRobotics::SMACFramework;

namespace LiquidRobotics {
namespace LuaWrapper {

LWCloseLuaInternalCmd::LWCloseLuaInternalCmd(TCon& _ctrl, const string& _cmd, const string& _instanceName)
: LWConcreteCmd(_ctrl)
, ctrl(_ctrl)
, cmd(_cmd)
, instanceName(_instanceName)
{
}

LWCloseLuaInternalCmd::~LWCloseLuaInternalCmd()
{
}

// this method is called by the background thread to execute the command
void LWCloseLuaInternalCmd::Execute()
{

	try
	{
		SignalStarted();
		dynamic_cast<LWController&>(ctrl).CloseLuaInternal(instanceName);
		SetException(TEx::NoException, "");
	}
	catch(exception& ex)
	{
		const int exNum = TEx::ExceptionToNumber(ex);

		stringstream msg;
		msg << "caught exception " << typeid(ex).name() << " '" << ex.what() << "'";

		SetException(exNum, msg.str());
	}
	catch(...)
	{
		TEx::NotReady exc("");
		const int exNum = TEx::ExceptionToNumber(exc);

		SetException(exNum, "caught unknown exception");
	}

	if ((exType == TEx::NoException) && !cmdResponse.length())
		cmdResponse = "OK";
}

// get the output of the command
string LWCloseLuaInternalCmd::GetResult()
{
	DoThrow();
	return cmdResponse;
}


}
}
