示例#1
0
unsigned int WINAPI TaskSynch::SynchThreadProc(void *pContext)
{
	TaskSynch *pSynch = reinterpret_cast<TaskSynch *>(pContext);
	WaitForSingleObject(pSynch->m_synchMap.m_synchEvent, INFINITE);
	ResetEvent(pSynch->m_synchMap.m_synchEvent);
	while(pSynch->m_synchMap.GetCount())
	{
		SynchData *pSynchData = pSynch->m_synchMap.Get(0);
		if(pSynchData)
		{
			Group *pGroup = pSynch->m_pDataManager->GetGroupManagerInstance()->GetGroup(pSynchData->m_sbGroup);
			if(pGroup)
			{
				int count = 0;
				unsigned __int64 *pIDs = pGroup->GetTaskIDs(&count);
				//TODO: build sets then use diffSet to get the missing items.. synch the existing items
				//and request the missing items
				SortedSet<unsigned __int64> localIDs;
				localIDs.addAll(pIDs, count);
				SortedSet<unsigned __int64> foreignIDs;
				delete [] pIDs;
				foreignIDs.addAll(pSynchData->m_pIDs, pSynchData->m_taskCount);
				CSet<unsigned __int64, 32> remainingIDs = localIDs.diff(foreignIDs);
				IterWrapper<Task> iter(pGroup->IterateTasks());
				while(iter.Next())
				{
					pSynch->SynchTaskTreeSeg(iter.Get(), pSynchData->m_ipSet[0]);
				}
				IterWrapper<unsigned __int64> idIter(remainingIDs.Iterate());
				int len = 0;
				char * pMsg = NULL;
				while(idIter.Next())
				{
					pMsg = MakeReqTask(*idIter.Get(), &len, true);
					pSynch->m_pNetwork->Send(pMsg, len, pSynchData->m_ipSet[0]);
				}
			}
		}
		pSynch->m_synchMap.RemoveItem(0);
	}
	pSynch->m_threadID = 0;
	return 0;
}
示例#2
0
void TaskSynch::HandleGroupSynch(unsigned char *pData, unsigned long len, unsigned long ip, unsigned long response)
{
	if(response)return ProcessGroupSynchResponse(pData, len, ip);
	else
	{
		SendMessage(mainAppHWND, WM_STOREPLAN, 0, 0);

		unsigned long *pHashLen = reinterpret_cast<unsigned long *>(pData);
		int *pCount = reinterpret_cast<int *>(pData + sizeof(unsigned long) + *pHashLen);
		int *pGroupNameLen = pCount + 1;
		StringBuffer sbGroup(reinterpret_cast<TCHAR *>(pGroupNameLen + 1), *pGroupNameLen / sizeof(TCHAR));
		int *pKeyLen = reinterpret_cast<int *>(pData + sizeof(unsigned long) + *pHashLen + (sizeof(int) * 2) + *pGroupNameLen);
		StringBuffer sbKey(reinterpret_cast<TCHAR *>(pKeyLen + 1), *pKeyLen / sizeof(TCHAR));
		Group *pGroup = m_pDataManager->GetGroupManagerInstance()->GetGroup(sbGroup);
		unsigned char *pHashData = (pData + sizeof(unsigned long));
		if(pGroup && _tcscmp(pGroup->GetGroupKey(), sbKey) == 0)
		{
			int taskCount = 0;
			StringBuffer sbGroupXML;
			GetGroupTasksXML(pGroup, &taskCount, sbGroupXML);
			unsigned long ghashLen = 0;
			unsigned char *pGroupHash = HashTaskTreeSeg(sbGroupXML, &ghashLen);
			if(*pHashLen != ghashLen)
			{
				OutputDebugString(_T("Hash Lengths don't match, this should not happen... throw exception\n"));
				throw _T("Unmatched Hash Lengths");
			}
			if(memcmp(pHashData, pGroupHash, *pHashLen))
			{
				int msgLen = 0;
				unsigned __int64 *pIDs = pGroup->GetTaskIDs(&taskCount);
				char *pMsg = MakeGroupSynchReq(pGroupHash, ghashLen, pIDs, sbGroup, sbKey, taskCount, true, &msgLen);
				delete [] pIDs;
				m_pNetwork->Send(pMsg, msgLen, ip);
			}
			delete [] pGroupHash;
		}
	}
}