00001 #ifndef HEARTBEAT_IMPLEMENTATION_FILE
00002 #define HEARTBEAT_IMPLEMENTATION_FILE
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "heartbeat.h"
00019 #include "time_stamp.h"
00020
00021 #include <basis/function.h>
00022 #include <basis/istring.h>
00023
00024 heartbeat::heartbeat(int misses_allowed, int check_interval)
00025 : _next_heartbeat(new time_stamp()),
00026 _check_interval(0),
00027 _misses_allowed(0),
00028 _misses(0)
00029 { reset(misses_allowed, check_interval); }
00030
00031 heartbeat::heartbeat(const heartbeat &to_copy)
00032 : object_base(),
00033 _next_heartbeat(new time_stamp()),
00034 _check_interval(0),
00035 _misses_allowed(0),
00036 _misses(0)
00037 { *this = to_copy; }
00038
00039 heartbeat::~heartbeat() { WHACK(_next_heartbeat); }
00040
00041 time_stamp heartbeat::heartbeat_time() const { return *_next_heartbeat; }
00042
00043 bool heartbeat::due() const { return time_left() <= 0; }
00044
00045 void heartbeat::made_request() { _misses++; reset_next_beat(); }
00046
00047 void heartbeat::kabump() { _misses = 0; reset_next_beat(); }
00048
00049 void heartbeat::reset_next_beat()
00050 { *_next_heartbeat = time_stamp(_check_interval); }
00051
00052 int heartbeat::time_left() const
00053 { return int(_next_heartbeat->value() - time_stamp().value()); }
00054
00055 bool heartbeat::dead() const
00056 {
00057
00058
00059 return (_misses > _misses_allowed)
00060 || (due() && (_misses >= _misses_allowed));
00061 }
00062
00063 void heartbeat::reset(int misses_allowed, int check_interval)
00064 {
00065 _misses_allowed = misses_allowed;
00066 _misses = 0;
00067 _check_interval = check_interval;
00068 reset_next_beat();
00069 }
00070
00071 istring heartbeat::text_form(bool detailed) const
00072 {
00073 istring to_return = (dead()? istring("expired, ") : istring("alive, "));
00074 to_return += (!dead() && due() ? istring("due now, ")
00075 : istring::empty_string());
00076 to_return += isprintf("beats left=%d", misses_left());
00077 if (detailed) {
00078 to_return += isprintf(", missed=%d, interval=%d, ",
00079 missed_so_far(), checking_interval());
00080 to_return += istring("next=") + heartbeat_time().text_form();
00081 }
00082 return to_return;
00083 }
00084
00085 heartbeat &heartbeat::operator =(const heartbeat &to_copy)
00086 {
00087 if (this == &to_copy) return *this;
00088 _check_interval = to_copy._check_interval;
00089 _misses_allowed = to_copy._misses_allowed;
00090 _misses = to_copy._misses;
00091 *_next_heartbeat = *to_copy._next_heartbeat;
00092 return *this;
00093 }
00094
00095
00096 #endif //HEARTBEAT_IMPLEMENTATION_FILE
00097