Merge branch 'release-2.140.101'
[feisty_meow.git] / nucleus / library / processes / heartbeat.cpp
1
2
3
4 /*****************************************************************************\
5 *                                                                             *
6 *  Name   : heartbeat                                                         *
7 *  Author : Chris Koeritz                                                     *
8 *                                                                             *
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 \*****************************************************************************/
17
18 #include "heartbeat.h"
19
20 #include <basis/astring.h>
21 #include <basis/functions.h>
22 #include <timely/time_stamp.h>
23
24 using namespace basis;
25 using namespace timely;
26
27 namespace processes {
28
29 heartbeat::heartbeat(int misses_allowed, int check_interval)
30 : _next_heartbeat(new time_stamp()),
31   _check_interval(0),
32   _misses_allowed(0),
33   _misses(0)
34 { reset(misses_allowed, check_interval); }
35
36 heartbeat::heartbeat(const heartbeat &to_copy)
37 : root_object(),
38   _next_heartbeat(new time_stamp()),
39   _check_interval(0),
40   _misses_allowed(0),
41   _misses(0)
42 { *this = to_copy; }
43
44 heartbeat::~heartbeat() { WHACK(_next_heartbeat); }
45
46 time_stamp heartbeat::heartbeat_time() const { return *_next_heartbeat; }
47
48 bool heartbeat::due() const { return time_left() <= 0; }
49
50 void heartbeat::made_request() { _misses++; reset_next_beat(); }
51
52 void heartbeat::kabump() { _misses = 0; reset_next_beat(); }
53
54 void heartbeat::reset_next_beat()
55 { *_next_heartbeat = time_stamp(_check_interval); }
56
57 int heartbeat::time_left() const
58 { return int(_next_heartbeat->value() - time_stamp().value()); }
59
60 bool heartbeat::dead() const
61 {
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));
66 }
67
68 void heartbeat::reset(int misses_allowed, int check_interval)
69 {
70   _misses_allowed = misses_allowed;
71   _misses = 0;
72   _check_interval = check_interval;
73   reset_next_beat();
74 }
75
76 astring heartbeat::text_form(bool detailed) const
77 {
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());
82   if (detailed) {
83     to_return += a_sprintf(", missed=%d, interval=%d, ",
84         missed_so_far(), checking_interval());
85     to_return += astring("next=") + heartbeat_time().text_form();
86   }
87   return to_return;
88 }
89
90 heartbeat &heartbeat::operator =(const heartbeat &to_copy)
91 {
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;
97   return *this;
98 }
99
100 } //namespace.
101
102