#include "StdAfx.h"
#include "ServerControlPipeServer.h"
#include "04WebServer.h"

enum
{
	//!	コマンドに日対応
	CONTROL_ERROR_COMMAND_NOT_SUPPORTED		= 1000,

	//!	サーバ開始失敗
	CONTROL_ERROR_START_SERVER				= 2000,

	//!	サーバが停止中で実行不能
	CONTROL_ERROR_SERVER_IS_STOPPED			= 3000,
};



CServerControlPipeServer::CServerControlPipeServer(void)
{
}

CServerControlPipeServer::~CServerControlPipeServer(void)
{
}

//////////////////////////////////////////////////////////////////////////////////
//	コントロール
//////////////////////////////////////////////////////////////////////////////////
/*!
	開始
*/
int CServerControlPipeServer::Start()
{
	//	制御パイプ初期化
	if(m_pipeServer.Start(CONTROL_PIPE_NAME,	CONTROL_PIPE_MAX,	this))
		return(-1);
	return(0);
}

/*!
	停止
*/
void CServerControlPipeServer::Stop()
{
	m_pipeServer.Stop();
}

/*!
	現在パイプが存在するかチェック
*/
int CServerControlPipeServer::CheckOther()
{
	CString pipeName = CONTROL_PIPE_NAME;
	if(pipeName.Left(2) != "\\\\")
		pipeName = "\\\\.\\pipe\\" + pipeName;

	//	開く
	HANDLE hPipe = ::CreateFile (pipeName,	GENERIC_READ | GENERIC_WRITE, 0, NULL,
							OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	if(hPipe == INVALID_HANDLE_VALUE)
		return(FALSE);

	CloseHandle(hPipe);
	return(TRUE);
}


//////////////////////////////////////////////////////////////////////////////////
//	CPipeCommandExecuteBase
//////////////////////////////////////////////////////////////////////////////////
/*!
	クライアントからのコマンドを実行
*/
int CServerControlPipeServer::OnRecvCommand(CBuffer &recvData, CBuffer &sendData)
{
	//	保護 (現在、Commandは単一スレッドで処理されるが、念のため)
	SCOPE_LOCK();

	//	パケット解析
	CCommandPacket	recvPacket, sendPacket;
	if(recvPacket.FromBuffer(recvData))
		return(-1);

//	TRACE(_T("Command : %s\n"), recvPacket.GetCommand());

	//	コマンドの実行
	int	ret = 0;
	if(recvPacket.GetCommand() == _T("StartServer"))
	{
		//	サーバ起動・再起動
		ret = DoStartServer(recvPacket, sendPacket);
	}
	else if(recvPacket.GetCommand() == _T("StopServer"))
	{
		//	サーバ停止
		ret = DoStopServer(recvPacket, sendPacket);
	}
	else if(recvPacket.GetCommand() == _T("GetServerErrorString"))
	{
		//	サーバエラー情報取得
		ret = DoGetServerErrorString(recvPacket, sendPacket);
	}
	else if(recvPacket.GetCommand() == _T("GetServerRoot"))
	{
		//	サーバルート取得
		ret = DoGetServerRoot(recvPacket, sendPacket);
	}
	else if(recvPacket.GetCommand() == _T("GetConnectionCount"))
	{
		//	接続数取得
		ret = DoGetConnectCount(recvPacket, sendPacket);
	}
	else if(recvPacket.GetCommand() == _T("IsRunning"))
	{
		//	実行状態取得
		ret = DoIsRunning(recvPacket, sendPacket);
	}
	else if(recvPacket.GetCommand() == _T("IsService"))
	{
		//	起動モード取得
		ret = DoIsService(recvPacket, sendPacket);
	}
	else if(recvPacket.GetCommand() == _T("GetServerInfo"))
	{
		//	サーバ情報取得
		ret = DoGetServerInfo(recvPacket, sendPacket);
	}
	else if(recvPacket.GetCommand() == _T("GetServerSetting"))
	{
		//	サーバ設定取得
		ret = DoGetServerSetting(recvPacket, sendPacket);
	}
	else if(recvPacket.GetCommand() == _T("GetServerContext"))
	{
		//	接続情報取得
		ret = DoGetServerContext(recvPacket, sendPacket);
	}
	else
	{
		//	サポート外
		ret = 0;
		sendPacket.SetErrorPacket(CONTROL_ERROR_COMMAND_NOT_SUPPORTED, _T("Command not Supported"));
	}

	//	返信
	if(ret == 0)
	{
		sendPacket.ToBuffer(sendData);
		return(0);
	}
	else
	{
		return(ret);
	}

	return(0);
}


//////////////////////////////////////////////////////////////////////////////////
//	コマンド実行
//////////////////////////////////////////////////////////////////////////////////
/*!
	サーバ起動・再起動
*/
int CServerControlPipeServer::DoStartServer(CCommandPacket &recvPacket, CCommandPacket &sendPacket)
{
	//	サーバ起動
	if(GET_APP()->StartServer() == 0)
		sendPacket.SetSuccessPacket();
	else
		sendPacket.SetErrorPacket(CONTROL_ERROR_START_SERVER, GET_APP()->GetServerErrorInfo());

	return(0);
}

/*!
	サーバ停止
*/
int CServerControlPipeServer::DoStopServer(CCommandPacket &recvPacket, CCommandPacket &sendPacket)
{
	GET_APP()->StopServer();

	//	エラーになる事はない
	sendPacket.SetSuccessPacket();

	return(0);
}

/*!
	サーバエラー情報取得
*/
int CServerControlPipeServer::DoGetServerErrorString(CCommandPacket &recvPacket, CCommandPacket &sendPacket)
{
	//	成功
	sendPacket.SetSuccessPacket();
	sendPacket.SetCommandOption(_T("ServerErrorString"), GET_APP()->GetServerErrorInfo());

	return(0);
}

/*!
	サーバルート取得
*/
int CServerControlPipeServer::DoGetServerRoot(CCommandPacket &recvPacket, CCommandPacket &sendPacket)
{
	//	成功
	sendPacket.SetSuccessPacket();
	sendPacket.SetCommandOption(_T("ServerRoot"), GET_APP()->GetServerRoot());

	return(0);
}

/*!
	接続数取得
*/
int CServerControlPipeServer::DoGetConnectCount(CCommandPacket &recvPacket, CCommandPacket &sendPacket)
{
	//	成功
	sendPacket.SetSuccessPacket();
	sendPacket.SetCommandOptionInt(_T("ConnectionCount"), GET_APP()->GetConnectCount());

	return(0);
}

/*!
	実行状態取得
*/
int CServerControlPipeServer::DoIsRunning(CCommandPacket &recvPacket, CCommandPacket &sendPacket)
{
	//	成功
	sendPacket.SetSuccessPacket();
	sendPacket.SetCommandOptionInt(_T("IsRunning"), GET_APP()->ServerRunning());

	return(0);
}

/*!
	起動モード取得
*/
int CServerControlPipeServer::DoIsService(CCommandPacket &recvPacket, CCommandPacket &sendPacket)
{
	//	成功
	sendPacket.SetSuccessPacket();
	sendPacket.SetCommandOptionInt(_T("IsService"), GET_APP()->IsService());

	return(0);
}

/*!
	サーバ情報取得
*/
int CServerControlPipeServer::DoGetServerInfo(CCommandPacket &recvPacket, CCommandPacket &sendPacket)
{
	if(GET_APP()->GetServerInfo(sendPacket.GetAttachedDataAccess()) == 0)
		sendPacket.SetSuccessPacket();
	else
		sendPacket.SetErrorPacket(CONTROL_ERROR_SERVER_IS_STOPPED, GET_APP()->GetServerErrorInfo());
		
	return(0);
}

/*!
	サーバ設定取得
*/
int CServerControlPipeServer::DoGetServerSetting(CCommandPacket &recvPacket, CCommandPacket &sendPacket)
{
	if(GET_APP()->GetServerSetting(sendPacket.GetAttachedDataAccess()) == 0)
		sendPacket.SetSuccessPacket();
	else
		sendPacket.SetErrorPacket(CONTROL_ERROR_SERVER_IS_STOPPED, GET_APP()->GetServerErrorInfo());
		
	return(0);
}

/*!
	接続情報取得
*/
int CServerControlPipeServer::DoGetServerContext(CCommandPacket &recvPacket, CCommandPacket &sendPacket)
{
	if(GET_APP()->GetServerContext(sendPacket.GetAttachedDataAccess()) == 0)
		sendPacket.SetSuccessPacket();
	else
		sendPacket.SetErrorPacket(CONTROL_ERROR_SERVER_IS_STOPPED, GET_APP()->GetServerErrorInfo());
		
	return(0);
}

