heartbeat.cpp

Go to the documentation of this file.
00001 #ifndef HEARTBEAT_IMPLEMENTATION_FILE
00002 #define HEARTBEAT_IMPLEMENTATION_FILE
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : heartbeat                                                         *
00007 *  Author : Chris Koeritz                                                     *
00008 *                                                                             *
00009 *******************************************************************************
00010 * Copyright (c) 1996-$now By Author.  This program is free software; you can  *
00011 * redistribute it and/or modify it under the terms of the GNU General Public  *
00012 * License as published by the Free Software Foundation; either version 2 of   *
00013 * the License or (at your option) any later version.  This is online at:      *
00014 *     http://www.fsf.org/copyleft/gpl.html                                    *
00015 * Please send any updates to: fred@gruntose.com                               *
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   // two cases mean the timer's dead; (1) if the misses are already too high,
00058   // or (2) if the heartbeat is due and the misses are as many as allowed.
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 

Generated on Thu Nov 20 04:28:59 2008 for HOOPLE Libraries by  doxygen 1.5.1