m2.mbcp/src/clock_cb.cc
2006-03-06 16:40:15 +00:00

87 lines
2.2 KiB
C++

#include "macros.h"
#include "clock_cb.h"
ClockCb::ClockCb(size_t size, size_t index){
Glib::Mutex::Lock lock(_mutex);
printf("ClockCb::ClockCb -- constructor\n");
_cur_index = index;
//_ticks = 0;
// lock jusqu'a la fin de la fonction
if ((index < 0) || (index > size)) {
throw ClockInitError();
// throw exception
}
for (int idx = 0; idx < size; idx++){
printf ("ClockCb::ClockCb -- extending vector to %d\n", idx);
_ticks.push_back (0);
}
}
bool ClockCb::adjust(TimeStamp ts){
bool result = true;
short emit_idx = ts.getIndex();
// lock jusqu'a la fin de la fonction
Glib::Mutex::Lock lock(_mutex);
// si les conditions sont remplies, alors on peut modifier l'horloge
printf("ClockCb::adjust -- Trying to adjust local (%d):\n", _cur_index);
this->val().display();
printf("ClockCb::adjust -- ... with remote (%d)\n", ts.getIndex());
ts.display();
if (ts[emit_idx] == _ticks[emit_idx] + 1){
printf("ClockCb::adjust -- index TS_m[j] == TS_i[j] + 1 (FIFO property)\n");
// Condition "le site i a recu au moins tout ce que l'emetteur à recu"
for (int idx = 0; idx < _ticks.size(); idx++){
if (idx != emit_idx){
if (ts[idx] > _ticks[idx]) {
result = false;
break;
}
}
}
} else {
// Condition FIFO non respectée
result = false;
}
if (result){
// si les deux conditions sont respectées, on met à jour l'horloge...
_ticks[emit_idx] = _ticks[emit_idx] + 1;
printf("ClockCb::adjust -- time update _ticks[%d] = %d\n", emit_idx, _ticks[emit_idx]);
} else {
printf("ClockCb::adding -- %scannot adjust%s ... \n", COLOR_RED, COLOR_NORMAL);
}
return result;
}
TimeStamp ClockCb::val(){
TimeStamp ts(Protocol::TYPE_CBCAST, _cur_index);
// on push tous les ticks de l'horloge
for (int idx = 0; idx < _ticks.size(); idx++){
ts.push_back (_ticks.at(idx));
}
return ts;
}
TimeStamp ClockCb::inc(){
// lock jusqu'a la fin de la fonction
Glib::Mutex::Lock lock(_mutex);
printf("ClockCb::inc -- cur_index from %d ", _ticks[_cur_index]);
_ticks[_cur_index] = _ticks[_cur_index] + 1;
printf("to %d\n", _ticks[_cur_index]);
TimeStamp ts(Protocol::TYPE_CBCAST, _cur_index);
// on push tous les ticks de l'horloge
for (int idx = 0; idx < _ticks.size(); idx++){
ts.push_back (_ticks.at(idx));
}
return ts;
}