Files
2022-10-26 12:25:11 +08:00

212 lines
4.2 KiB
C++

#include "StdAfx.h"
#include ".\agentinfoparser.h"
#include "AgentInfo.h"
AgentInfoParser::AgentInfoParser(void)
{
m_XMLParser.Init();
m_mapAgentInfo.clear();
m_KeyGenerator.Create( 1, 1000 );
}
AgentInfoParser::~AgentInfoParser(void)
{
UnLoad();
}
VOID AgentInfoParser::UnLoad()
{
AgentInfo* pAgentInfo = NULL;
for( AGENT_INFO_MAP_ITER iter = m_mapAgentInfo.begin(); iter != m_mapAgentInfo.end(); ++iter )
{
pAgentInfo = iter->second;
if( pAgentInfo )
{
delete pAgentInfo;
pAgentInfo = NULL;
}
}
m_mapAgentInfo.clear();
}
BOOL AgentInfoParser::Load( char* pszFileName )
{
if( !m_XMLParser.Load( pszFileName ) )
return FALSE;
long nAgentNum = 0;
NODELISTPtr pAgentNodeList = m_XMLParser.FindNodeList( L"//AGENT", nAgentNum );
if( pAgentNodeList == NULL )
return FALSE;
DWORD dwAgentKey;
std::string strIP;
for( int nAgent = 0; nAgent < nAgentNum; nAgent++ )
{
NODEPtr pAgentNode = m_XMLParser.FindNode( pAgentNodeList, nAgent );
if( pAgentNode == NULL )
continue;
dwAgentKey = 0; strIP.clear();
if( !m_XMLParser.GetAttriDataDWORD( pAgentNode, L"KEY", dwAgentKey ) ) continue;
if( !m_XMLParser.GetAttriDataStr( pAgentNode, L"IP", strIP ) ) continue;
if( GetAgentInfo( dwAgentKey ) )
{
FASSERT( !"데이터에 오류가 있습니다." );
return FALSE;
}
// 서버 정보를 추가한다.
AgentInfo *pAgentInfo = new AgentInfo;
pAgentInfo->SetKey( dwAgentKey );
pAgentInfo->SetIP( strIP.c_str() );
m_KeyGenerator.ReserveKey( dwAgentKey );
m_mapAgentInfo.insert( std::make_pair( dwAgentKey, pAgentInfo ) );
}
return TRUE;
}
AgentInfo* AgentInfoParser::GetAgentInfo( const TCHAR* pszIP )
{
AgentInfo * pAgentInfo = NULL;
for( AGENT_INFO_MAP_ITER it = m_mapAgentInfo.begin(); it != m_mapAgentInfo.end(); ++it )
{
pAgentInfo = it->second;
if( pAgentInfo )
{
if( pAgentInfo->CompareIP( pszIP ) )
return pAgentInfo;
}
}
return NULL;
}
VOID AgentInfoParser::SerializeStream( AGENT_TOTAL_INFO& info, BOOL bSave )
{
if( bSave )
{
}
else
{
AgentInfo * pAgentInfo = NULL;
for( AGENT_INFO_MAP_ITER it = m_mapAgentInfo.begin(); it != m_mapAgentInfo.end(); ++it )
{
pAgentInfo = it->second;
if( pAgentInfo )
{
info.m_Data[info.m_Count] = pAgentInfo->GetAgentInfo();
info.m_Count++;
}
}
}
}
BOOL AgentInfoParser::Add( const TCHAR* pszIP, DWORD& dwKey )
{
if( !pszIP ) return FALSE;
//새로운 Agent 생성
AgentInfo* pAgentInfo = new AgentInfo;
pAgentInfo->SetIP( pszIP );
dwKey = m_KeyGenerator.GetKey();
pAgentInfo->SetKey( dwKey );
// 파일에 추가
ELEMENTPtr pNewElem = m_XMLParser.InsertElement( L"//AGENT_INFO", L"AGENT" );
if( pNewElem )
{
if( !m_XMLParser.SetAttributeDWORD( pNewElem, L"KEY", dwKey ) ) return FALSE;
if( !m_XMLParser.SetAttributeStr( pNewElem, L"IP", pAgentInfo->GetIP() ) ) return FALSE;
}
if( !m_XMLParser.Save() )
{
delete pAgentInfo;
return FALSE;
}
m_mapAgentInfo.insert( std::make_pair( dwKey, pAgentInfo) );
return TRUE;
}
BOOL AgentInfoParser::Del( DWORD dwAgentKey )
{
AGENT_INFO_MAP_ITER iter = m_mapAgentInfo.find( dwAgentKey );
if( iter != m_mapAgentInfo.end() )
{
AgentInfo* pAgentInfo = iter->second;
if( pAgentInfo )
{
//1. xml 파일에서 삭제하고,
_variant_t varKey;
varKey.ChangeType( VT_UI4 );
varKey.ulVal = dwAgentKey;
if( !m_XMLParser.RemoveElement( "AGENT", "KEY", varKey ) )
return FALSE;
//2. map에서 삭제한다.
m_mapAgentInfo.erase( iter );
}
}
if( !m_XMLParser.Save() ) return FALSE;
return TRUE;
}
AgentInfo* AgentInfoParser::GetAgentInfo( DWORD dwAgentKey )
{
AGENT_INFO_MAP_ITER iter = m_mapAgentInfo.find( dwAgentKey );
if( iter != m_mapAgentInfo.end() )
{
AgentInfo* pAgentInfo = iter->second;
if( pAgentInfo )
return pAgentInfo;
}
return NULL;
}
BOOL AgentInfoParser::Modify( DWORD dwKey, TCHAR* pszIP )
{
if( !GetAgentInfo( dwKey ) )
return FALSE;
// XML파일 변경
_variant_t varKey, varIP;
varKey.ChangeType( VT_UI4 );
varKey.ulVal = dwKey;
varIP.ChangeType( VT_BSTR );
varIP.bstrVal = _com_util::ConvertStringToBSTR( pszIP );
if( !m_XMLParser.UpdateElement( "AGENT", "KEY", varKey, "IP", varIP ) )
return FALSE;
if( !m_XMLParser.Save() ) return FALSE;
return TRUE;
}