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

CFilterControl::CFilterControl(void)
{
}

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



//////////////////////////////////////////////////////////////////////////////////////////////
//	初期化
//////////////////////////////////////////////////////////////////////////////////////////////
/*!
	初期化
*/
void CFilterControl::AttachFilters(CIConnectionToolWrap connectionTool)
{
	m_connectionTool = connectionTool;
	m_threadStatus = m_connectionTool.GetIConnectionThreadStatus();

	//	フィルタアレイ取得
	((IFilterManagerPtr)m_connectionTool.GetIServerTool().
		GetManagerInterface(FILTER_MANAGER_INTERFACE_NAME, FILTER_MANAGER_INTERFACE_VER))->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]->FilterStart(m_connectionTool.GetIConnectionTool());
	}
}

/*!
	終了
*/
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]->FilterStop(m_connectionTool.GetIConnectionTool());
	}

	m_filters.RemoveAll();
}

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

/*!
	ボディを受信、繰り返し(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(m_connectionTool.GetIConnectionTool(), body.GetCoreInterface());
	}
}

/*!
	受信処理完了
*/
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(m_connectionTool.GetIConnectionTool());
	}
}

/*!
	レスポンス生成
*/
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(m_connectionTool.GetIConnectionTool());
	}
}

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

/*!
	ボディを送信、繰り返し(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(m_connectionTool.GetIConnectionTool(), body.GetCoreInterface());
	}
}

/*!
	送信処理完了
*/
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(m_connectionTool.GetIConnectionTool());
	}
}

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