4 /*****************************************************************************\
7 * Author : Chris Koeritz *
9 *******************************************************************************
10 * Copyright (c) 1996-$now By Author. This program is free software; you can *
11 * redistribute it and/or modify it under the terms of the GNU General Public *
12 * License as published by the Free Software Foundation; either version 2 of *
13 * the License or (at your option) any later version. This is online at: *
14 * http://www.fsf.org/copyleft/gpl.html *
15 * Please send any updates to: fred@gruntose.com *
16 \*****************************************************************************/
18 #include "heartbeat.h"
20 #include <basis/astring.h>
21 #include <basis/functions.h>
22 #include <timely/time_stamp.h>
24 using namespace basis;
25 using namespace timely;
29 heartbeat::heartbeat(int misses_allowed, int check_interval)
30 : _next_heartbeat(new time_stamp()),
34 { reset(misses_allowed, check_interval); }
36 heartbeat::heartbeat(const heartbeat &to_copy)
38 _next_heartbeat(new time_stamp()),
44 heartbeat::~heartbeat() { WHACK(_next_heartbeat); }
46 time_stamp heartbeat::heartbeat_time() const { return *_next_heartbeat; }
48 bool heartbeat::due() const { return time_left() <= 0; }
50 void heartbeat::made_request() { _misses++; reset_next_beat(); }
52 void heartbeat::kabump() { _misses = 0; reset_next_beat(); }
54 void heartbeat::reset_next_beat()
55 { *_next_heartbeat = time_stamp(_check_interval); }
57 int heartbeat::time_left() const
58 { return int(_next_heartbeat->value() - time_stamp().value()); }
60 bool heartbeat::dead() const
62 // two cases mean the timer's dead; (1) if the misses are already too high,
63 // or (2) if the heartbeat is due and the misses are as many as allowed.
64 return (_misses > _misses_allowed)
65 || (due() && (_misses >= _misses_allowed));
68 void heartbeat::reset(int misses_allowed, int check_interval)
70 _misses_allowed = misses_allowed;
72 _check_interval = check_interval;
76 astring heartbeat::text_form(bool detailed) const
78 astring to_return = (dead()? astring("expired, ") : astring("alive, "));
79 to_return += (!dead() && due() ? astring("due now, ")
80 : astring::empty_string());
81 to_return += a_sprintf("beats left=%d", misses_left());
83 to_return += a_sprintf(", missed=%d, interval=%d, ",
84 missed_so_far(), checking_interval());
85 to_return += astring("next=") + heartbeat_time().text_form();
90 heartbeat &heartbeat::operator =(const heartbeat &to_copy)
92 if (this == &to_copy) return *this;
93 _check_interval = to_copy._check_interval;
94 _misses_allowed = to_copy._misses_allowed;
95 _misses = to_copy._misses;
96 *_next_heartbeat = *to_copy._next_heartbeat;