m2.mbcp/src/clock_cb.cc

87 lines
2.2 KiB
C++
Raw Normal View History

2006-02-05 17:43:00 +00:00
2006-03-06 16:17:28 +00:00
#include "macros.h"
2006-02-05 17:43:00 +00:00
#include "clock_cb.h"
ClockCb::ClockCb(size_t size, size_t index){
2006-03-04 14:24:03 +00:00
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);
}
2006-02-05 17:43:00 +00:00
}
2006-03-06 12:42:03 +00:00
bool ClockCb::adjust(TimeStamp ts){
2006-03-06 14:06:03 +00:00
bool result = true;
short emit_idx = ts.getIndex();
2006-03-06 12:42:03 +00:00
// lock jusqu'a la fin de la fonction
Glib::Mutex::Lock lock(_mutex);
// si les conditions sont remplies, alors on peut modifier l'horloge
2006-03-06 16:40:15 +00:00
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();
2006-03-06 14:06:03 +00:00
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 <20> 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<63>e
result = false;
}
if (result){
// si les deux conditions sont respect<63>es, on met <20> jour l'horloge...
_ticks[emit_idx] = _ticks[emit_idx] + 1;
printf("ClockCb::adjust -- time update _ticks[%d] = %d\n", emit_idx, _ticks[emit_idx]);
2006-03-06 16:17:28 +00:00
} else {
printf("ClockCb::adding -- %scannot adjust%s ... \n", COLOR_RED, COLOR_NORMAL);
2006-03-06 14:06:03 +00:00
}
2006-03-06 12:42:03 +00:00
return result;
2006-02-05 17:43:00 +00:00
}
2006-03-06 16:40:15 +00:00
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;
}
2006-02-05 17:43:00 +00:00
TimeStamp ClockCb::inc(){
2006-03-04 14:24:03 +00:00
// lock jusqu'a la fin de la fonction
Glib::Mutex::Lock lock(_mutex);
2006-03-04 22:24:36 +00:00
printf("ClockCb::inc -- cur_index from %d ", _ticks[_cur_index]);
2006-03-04 14:24:03 +00:00
_ticks[_cur_index] = _ticks[_cur_index] + 1;
2006-03-04 22:24:36 +00:00
printf("to %d\n", _ticks[_cur_index]);
2006-03-04 14:24:03 +00:00
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;
2006-02-05 17:43:00 +00:00
}
2006-03-04 22:24:36 +00:00