#include "StdAfx.h"
#include "FilterControl.h"

CFilterControl::CFilterControl(void)
{
}

CFilterControl::~CFilterControl(void)
{
	DetachFilters();
}



//////////////////////////////////////////////////////////////////////////////////////////////
//	初期化
//////////////////////////////////////////////////////////////////////////////////////////////
/*!
	初期化
*/
void CFilterControl::AttachFilters(IServerToolPtr serverTool, IConnectionThreadStatusPtr threadStatus, IConnectionControlPtr connectionControl, CContext &context)
{
	m_serverTool = serverTool;
	m_threadStatus = threadStatus;
	m_connectionControl = connectionControl;
	m_context = context;

	//	フィルタアレイ取得
	m_serverTool->GetIFilterManager()->GetFilterArray(m_filters);

	//!	開始
	for(int i=0;i<m_filters.GetSize();i++)
	{
		m_threadStatus->SetCurrentModuleFunction(m_filters[i]->GetFilterName(), FUNCTION_FILTER_START);
		m_filters[i]->Start(GetIFilterContext());
	}
}

/*!
	終了
*/
void CFilterControl::DetachFilters()
{
	//	停止
	for(int i=0;i<m_filters.GetSize();i++)
	{
		m_threadStatus->SetCurrentModuleFunction(m_filters[i]->GetFilterName(), FUNCTION_FILTER_STOP);
		m_filters[i]->Stop(GetIFilterContext());
	}

	m_filters.RemoveAll();
}

//////////////////////////////////////////////////////////////////////////////////////////////
//	フィルタ
//////////////////////////////////////////////////////////////////////////////////////////////
/*!
	リクエストヘッダ
*/
void CFilterControl::FilterRequestHeader()
{
	CContextRequestInfo requestInfo = m_context.GetRequestInfo();
	for(int i=0;i<m_filters.GetSize();i++)
	{
		m_threadStatus->SetCurrentModuleFunction(m_filters[i]->GetFilterName(), FUNCTION_FILTER_REQUEST_HEADER);
		m_filters[i]->FilterRequestHeader(GetIFilterContext(), requestInfo);
	}
}

/*!
	ボディを受信、繰り返し(throw)
*/
void CFilterControl::FilterRequestBody(CBinaryData &body)
{
	for(int i=0;i<m_filters.GetSize();i++)
	{
		m_threadStatus->SetCurrentModuleFunction(m_filters[i]->GetFilterName(), FUNCTION_FILTER_REQUEST_BODY);
		m_filters[i]->FilterRequestBody(GetIFilterContext(), body);
	}
}

/*!
	受信処理完了
*/
void CFilterControl::FilterEndRequest()
{
	for(int i=0;i<m_filters.GetSize();i++)
	{
		m_threadStatus->SetCurrentModuleFunction(m_filters[i]->GetFilterName(), FUNCTION_FILTER_END_REQUEST);
		m_filters[i]->FilterEndRequest(GetIFilterContext());
	}
}

/*!
	レスポンス生成
*/
void CFilterControl::FilterBuildResponse()
{
	for(int i=0;i<m_filters.GetSize();i++)
	{
		m_threadStatus->SetCurrentModuleFunction(m_filters[i]->GetFilterName(), FUNCTION_FILTER_BUILD_RESPONSE);
		m_filters[i]->FilterBuildResponse(GetIFilterContext());
	}
}

/*!
	レスポンスヘッダ
*/
void CFilterControl::FilterResponseHeader()
{
	CContextResponseInfo responseInfo = m_context.GetResponseInfo();
	for(int i=0;i<m_filters.GetSize();i++)
	{
		m_threadStatus->SetCurrentModuleFunction(m_filters[i]->GetFilterName(), FUNCTION_FILTER_RESPONSE_HEADER);
		m_filters[i]->FilterResponseHeader(GetIFilterContext(), responseInfo);
	}
}

/*!
	ボディを送信、繰り返し(throw)
*/
void CFilterControl::FilterResponseBody(CBinaryData &body)
{
	for(int i=0;i<m_filters.GetSize();i++)
	{
		m_threadStatus->SetCurrentModuleFunction(m_filters[i]->GetFilterName(), FUNCTION_FILTER_RESPONSE_BODY);
		m_filters[i]->FilterResponseBody(GetIFilterContext(), body);
	}
}

/*!
	送信処理完了
*/
void CFilterControl::FilterEndResponse()
{
	for(int i=0;i<m_filters.GetSize();i++)
	{
		m_threadStatus->SetCurrentModuleFunction(m_filters[i]->GetFilterName(), FUNCTION_FILTER_END_RESPONSE);
		m_filters[i]->FilterEndResponse(GetIFilterContext());
	}
}

/*!
	レスポンスヘッダ(例外発生厳禁！)
*/
void CFilterControl::FilterServerResponse(CBinaryData &body)
{
	CContextResponseInfo responseInfo = m_context.GetResponseInfo();
	for(int i=0;i<m_filters.GetSize();i++)
	{
		m_threadStatus->SetCurrentModuleFunction(m_filters[i]->GetFilterName(), FUNCTION_FILTER_SERVER_RESPONSE);
		m_filters[i]->FilterServerResponse(GetIFilterContext(), responseInfo, body);
	}
}
