////////////////////////////////////////////////////// // // // Incremental Clock Syncronization. // // 236503(20) Summer 2001 // // Advanced Programming Project. // // Technion. Computer Science Department. // // // // Written by: Reitman Anna // // Fidelman Greg // // Supervisor: Hagit Attya // // Saar Pilosof // // // ////////////////////////////////////////////////////// // SyncAlgorithm.cpp #include "stdafx.h" #include #include "SyncAlgorithm.h" #include "Process.h" ProcessHolder* SyncAlgorithm::m_pProcHolder = NULL; bool SyncAlgorithm::ExecuteHagitAlg(Process* proc) { if( !checkParams_(proc) ) exit(1); //read messages received at the previous time slot and update own clock for( MessageBufferList::iterator itProcMsg = (proc->m_Hagit).m_CurrMsgBuff->begin(); itProcMsg != (proc->m_Hagit).m_CurrMsgBuff->end(); ++itProcMsg ) { if( (*itProcMsg).second > (proc->m_Hagit).m_Clock ) { (proc->m_Hagit).m_Clock++; } else{ if( (*itProcMsg).second < (proc->m_Hagit).m_Clock ) { (proc->m_Hagit).m_Clock--; } } } //clean current MessageBufferList (proc->m_Hagit).m_CurrMsgBuff->clear(); // send own clock value to other processes if( !m_pProcHolder->sendMessage(proc->m_ProcID,(proc->m_Hagit).m_Clock) ){ std::cout << "SyncAlgorithm: Cannot send message\n"; return false; } return true; } bool SyncAlgorithm::ExecuteSTAlg(Process* proc) { if( !checkParams_(proc) ) exit(1); //broadcast request for next Round //maybe broadcast if round != clock ???? if( !m_pProcHolder->broadcastMessage(proc->m_ProcID,(proc->m_ST).m_Round+1) ){ std::cout << "SyncAlgorithm: Cannot broadcast message\n"; return false; } //read messages received at the previous time slot for( MessageBufferList::iterator itProcMsg = (proc->m_ST).m_CurrMsgBuff->begin(); itProcMsg != (proc->m_ST).m_CurrMsgBuff->end(); itProcMsg++ ) { ((proc->m_ST).m_mapSignedMsges[(*itProcMsg).second]).insert((*itProcMsg).first); } //clean current MessageBufferList (proc->m_Hagit).m_CurrMsgBuff->clear(); //check if we can accept some message(Round) ( count of distinct processes >= N/2 ) for( MapOfSets::iterator itSet = (proc->m_ST).m_mapSignedMsges.begin(); itSet != (proc->m_ST).m_mapSignedMsges.end(); ++itSet ) { if( m_pProcHolder->halfProc() <= (*itSet).second.size() ){ (proc->m_ST).m_Round = (*itSet).first; (proc->m_ST).m_Clock = (proc->m_ST).m_Round; (*itSet).second.clear(); } } return true; } bool SyncAlgorithm::checkParams_(Process* proc) { if( m_pProcHolder == NULL ){ std::cout << "SyncAlgorithm: ProcessHolder is NULL\n"; return false; } if( proc == NULL ){ std::cout << "SyncAlgorithm: Process passed as NULL\n"; return false; } return true; }