#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; }