www.pudn.com > SendMsgOCX.rar > WMsgSentHistory.cpp


// WMsgSentHistory.cpp: implementation of the CWMsgSentHistory class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#include "stdafx.h" 
//tts #include "WSMModule.h" 
#include "WMsgSentHistory.h" 
 
#ifdef _DEBUG 
#undef THIS_FILE 
static char THIS_FILE[]=__FILE__; 
#define new DEBUG_NEW 
#endif 
 
#define DefaultTimeOut CTimeSpan(0,0,10,0) 
 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
 
CWMsgLastFrameNo::CWMsgLastFrameNo(LPCSTR lpcszPhoneNumber) 
{				 
	strcpy(m_szPhoneNumber, lpcszPhoneNumber); 
	m_lastFrameNo = MIN_FRAME_NO; 
} 
 
int CWMsgLastFrameNo::Compare( const CWMsgLastFrameNo& toCmp) const 
{ 
	return strcmp(m_szPhoneNumber, toCmp.m_szPhoneNumber); 
} 
 
int CWMsgLastFrameNo::operator - (const CWMsgLastFrameNo& toMinus) const 
{ 
	return Compare(toMinus); 
} 
 
BYTE CWMsgLastFrameNo::GenerateNextFrameNo() 
{ 
	m_lastFrameNo++; 
 
	BOOL bRecycle = m_lastFrameNo > MAX_FRAME_NO; 
	if(bRecycle) 
		m_lastFrameNo = MIN_FRAME_NO; 
 
	return m_lastFrameNo; 
} 
 
BYTE CWMsgLastFrameNo::GetFrameNo() const 
{ 
	return m_lastFrameNo; 
} 
 
CWMsgSentHistory::CWMsgSentHistory() 
{ 
	m_bLocked = FALSE; 
	m_timeOut = DefaultTimeOut; 
} 
 
CWMsgSentHistory::~CWMsgSentHistory() 
{ 
 
} 
 
POSITION CWMsgSentHistory::FindDuplicateItem(const CWMsgSent& toCheck) 
{ 
	enum CWMsgSent::MsgMatchType prevType = CWMsgSent::SetMatchType(CWMsgSent::PhoneNumberAndFrameNo); 
	POSITION pos = Find(toCheck, NULL); 
	CWMsgSent::SetMatchType(prevType);		// restore 
	 
	return pos; 
} 
 
BOOL CWMsgSentHistory::HasDuplicateItem(const CWMsgSent& toCheck) 
{ 
	return FindDuplicateItem(toCheck) != NULL; 
} 
 
void CWMsgSentHistory::ResetSaveTime() 
{ 
	Lock(); 
 
	POSITION pos = GetHeadPosition(); 
	MsgSaveTimeType currTime = GetSaveTime(); 
 
	while(NULL!=pos) 
	{ 
		CWMsgSent& item = GetNext(pos); 
		item.SetSaveTime(currTime); 
	} 
 
	Unlock(); 
} 
 
BOOL CWMsgSentHistory::SaveMsg(CWMsgSent& msg) 
{ 
 
	Lock(); 
	 
	RemoveTimeOutItem(); 
 
	BYTE newFrameNo = GenerateNextFrameNo(msg.GetPhoneNumber()); 
	msg.SetFrameNo(newFrameNo); 
 
	BOOL bHasDuplicate = HasDuplicateItem(msg); 
	if ( !bHasDuplicate )  
	{ 
		msg.SetSaveTime(GetSaveTime()); 
		AddTail(msg); 
	} 
 
	Unlock(); 
	 
	return !bHasDuplicate; 
} 
 
BOOL CWMsgSentHistory::IsLocked() const 
{ 
	return m_bLocked; 
} 
 
void CWMsgSentHistory::Lock() 
{ 
	ASSERT(!IsLocked()); 
 
	m_criticalSection.Lock(); 
	m_bLocked = TRUE; 
} 
 
void CWMsgSentHistory::Unlock() 
{ 
	ASSERT(IsLocked()); 
 
	m_criticalSection.Unlock(); 
	m_bLocked = FALSE; 
} 
 
BYTE CWMsgSentHistory::GenerateNextFrameNo(LPCSTR lpcszPhoneNumber) 
{ 
	CWMsgLastFrameNo toFind(lpcszPhoneNumber); 
	int idxPrev = m_frameNoHistory.Find(toFind); 
	 
	if (idxPrev >= 0)  
		return m_frameNoHistory[idxPrev].GenerateNextFrameNo(); 
	else  
	{ 
		m_frameNoHistory.Add(toFind);			// save 
		return toFind.GetFrameNo(); 
	} 
	 
} 
 
MsgSaveTimeType CWMsgSentHistory::GetSaveTime() 
{ 
	return CTime::GetCurrentTime(); 
} 
 
BOOL CWMsgSentHistory::CheckTimeOut(const MsgSaveTimeType& currTime,const CWMsgSent& toCheck) const 
{ 
	return ( currTime - toCheck.GetSaveTime() ) > m_timeOut; 
} 
 
void CWMsgSentHistory::RemoveTimeOutItem() 
{ 
 
	ASSERT(IsLocked()); 
 
	MsgSaveTimeType currTime = GetSaveTime(); 
 
	POSITION pos = GetHeadPosition(); 
	while(pos != NULL) 
	{ 
		POSITION posItem = pos;					// save for delete 
		CWMsgSent& historyItem = GetNext(pos);	// get element at pos and make pos point to next 
		if( CheckTimeOut(currTime, historyItem) )  
			RemoveAt(posItem); 
	} 
} 
 
BOOL CWMsgSentHistory::GetACopy(CWMsgSent& toFindAndGet, BOOL bResetSaveTime) 
{ 
	Lock(); 
 
	POSITION pos = FindDuplicateItem(toFindAndGet); 
 
	BOOL bFound = pos != NULL; 
	if (bFound) 
	{ 
		CWMsgSent& historyItem = GetAt(pos); 
		toFindAndGet = historyItem; 
 
		if(bResetSaveTime)  
			toFindAndGet.SetSaveTime(GetSaveTime()); 
	} 
 
	Unlock(); 
 
	return bFound; 
} 
 
void CWMsgSentHistory::SetTimeOut(const MsgTimeOutType& timeOut) 
{ 
	m_timeOut = timeOut; 
}