/***************************************************************************
                HostInfo.cpp
                -----------------------------------------
    begin      : 2011/10/18
    copyright  : (C) 2005 SolutionBox Inc.
    author     : Service 1 Team
    email      : svc1@solbox.com

    CopyRight(C) 2005 SolutionBox Inc. All Rights reserved.
    Redistribution and use in source and binary forms, with or with out
    modification, are not permitted in outside of SolutionBox Inc.
***************************************************************************/
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/resource.h>
#include <time.h>
#include <assert.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <pwd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <string>
#include <map>
#include <vector>
#include <list>
#include <queue>

#include "HostInfo.h"
#include "String.h"
#include "Logger.h"


#define HOSTNAME_SIZE		256

using namespace std;

CHostInfo::CHostInfo()
{
	m_szHostname	= "";
	m_nHostNumber	= -1;

	MakeHostInfos();
}

CHostInfo::~CHostInfo()
{
}

void 
CHostInfo::MakeHostInfos()
{
	MakeHostname();
	MakeShortHostname();
	MakeHostNumber();
}

void CHostInfo::MakeHostname()
{
    char hostname[HOSTNAME_SIZE];
    memset( hostname, 0x00, HOSTNAME_SIZE );
    
	/// 호스트 이름 얻기.
    if( gethostname( hostname, HOSTNAME_SIZE - 1 ) != 0 )
    {
        LOG( LERR, "can't get hostname. errno=%d, err=%s", errno, strerror(errno) );
        return;
    }   
    
	/// 호스트 이름 설정.
    m_szHostname = hostname;
}

void CHostInfo::MakeShortHostname()
{
	string shortHostName = "";

	/// bd-01-fhs001.ktsh[x-cdn].co.kr 형식에서 '.'을 기준으로 토큰화 시킴.
	vector<string> tokens = CString::Tokenize( m_szHostname, "." );
	if( tokens.size() == 0 )
	{
		LOG( LERR, "invalid hostname(%s)", m_szHostname.c_str() );
		return;
	}   

	/// bd-01-fhs001.ktsh[x-cdn].co.kr 형식에서 bd-01-fhs001만 추출.
	m_szShortHostname = tokens[0];
}

void CHostInfo::MakeHostNumber()
{
	string hostnumber = "";

	/// bd-01-fhs001.ktsh[x-cdn].co.kr 형식에서 '.'을 기준으로 토큰화 시킴.
	vector<string> tokens = CString::Tokenize( m_szHostname, "." );
	if( tokens.size() == 0 )
	{
		LOG( LERR, "invalid hostname(%s)", m_szHostname.c_str() );
		return;
	}   
	/// bd-01-fhs001.ktsh[x-cdn].co.kr 형식에서 bd-01-fhs001만 추출.
	hostnumber = tokens[0];

	/// bd-01-fhs001에서 '-'을 기준으로 토큰화 시킴.
	tokens.clear();
	tokens = CString::Tokenize( hostnumber, "-" );
	if( tokens.size() == 0 )
	{
		LOG( LERR, "invalid hostname(%s)", m_szHostname.c_str() );
		return;
	}   
	/// bd-01-fhs001에서 fhs001만 추출. 
	hostnumber = tokens[ tokens.size() - 1 ];
	
	/// fhs001에서 fhs 제거
	hostnumber = CString::Replace( hostnumber, "fhs", "" );

	/// 혹시 있을지 모르는 공백 제거
	hostnumber = CString::Trim( hostnumber );
	if( hostnumber.empty() == true )
	{
		LOG( LERR, "invalid hostname(%s)", m_szHostname.c_str() );
		return;
	}

	m_nHostNumber = atoi( hostnumber.c_str() );

	LOG( LDEV, "host info: hostname(%s), hostnumber(%d)", m_szHostname.c_str(), m_nHostNumber );
}
