/***************************************************************************
                ContentList.cpp
                -----------------------------------------
    begin      : 2015/05/29
    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 "TransferStatDataList.h"
#include "MutexLock.hpp"

////////////////////////////////////////////////////////////////////////////
//
// CTransferStatDataList Class
//
////////////////////////////////////////////////////////////////////////////
CTransferStatDataList::CTransferStatDataList()
{
	pthread_mutex_init(&m_mutex, NULL);
}

CTransferStatDataList::~CTransferStatDataList()
{
	ProtectedMutex mutex(m_mutex);
	pthread_mutex_destroy(&m_mutex);
}

void CTransferStatDataList::Push( CTransferStatData& statData )
{
	ProtectedMutex mutex(m_mutex);
	
	// 동일한 파일에 대해서 insert 방지 및 메모리 절약 목적으로 map 사용
	m_TransferStatMap.insert(std::map< std::string, CTransferStatData>::value_type(statData.GetFilenameHash(), CTransferStatData(statData)));
}

void CTransferStatDataList::Pop(CTransferStatData & data)
{
	ProtectedMutex mutex(m_mutex);
	//CTransferStatData data;
	// 큐가 비었으면 빈 데이터 반환.
	if (m_TransferStatMap.empty() == true)
	{
		LOG( LDBG, "no element in the queue of TransferStatDataList." );
		return;
	}
	
	std::map< std::string, CTransferStatData>::iterator it = m_TransferStatMap.begin();
	data.SetTransferStatData( it->second.GetData());
	m_TransferStatMap.erase(it);

	return;
}

unsigned int CTransferStatDataList::GetSize()
{
	ProtectedMutex mutex(m_mutex);
	unsigned int r = 0;
	r = m_TransferStatMap.size();
	return r;
}

void CTransferStatDataList::Clear()
{
	ProtectedMutex mutex(m_mutex);
	while (m_TransferStatMap.size() > 0)
	{
		m_TransferStatMap.erase(m_TransferStatMap.begin());
	}
}
