1
/** \file throttle.cc */ // -*-c++-*-
9
#include <boost/make_shared.hpp>
10
#include <boost/optional.hpp>
12
#include <generic/util/util.h>
17
using aptitude::Loggers;
18
using boost::make_shared;
19
using boost::optional;
20
using boost::shared_ptr;
21
using logging::LoggerPtr;
29
class throttle_impl : public throttle
31
boost::optional<struct timeval> last_update;
33
logging::LoggerPtr logger;
35
// Used to ensure that we only warn once about gettimeofday()
37
bool wrote_time_error;
39
static const double update_interval = 0.7;
41
void write_time_error(int errnum);
46
/** \return \b true if the timer has expired. */
47
bool update_required();
49
/** \brief Reset the timer that controls when the display is
55
const double throttle_impl::update_interval;
57
void throttle_impl::write_time_error(int errnum)
62
"gettimeofday() failed: " <<
64
wrote_time_error = true;
68
throttle_impl::throttle_impl()
69
: logger(Loggers::getAptitudeCmdlineThrottle()),
70
wrote_time_error(false)
74
bool throttle_impl::update_required()
80
// Time checking code shamelessly stolen from apt, since
81
// we know theirs works.
83
if(gettimeofday(&now, 0) != 0)
85
write_time_error(errno);
90
const struct timeval &last_update_time = *last_update;
92
now.tv_sec - last_update_time.tv_sec +
93
(now.tv_usec - last_update_time.tv_usec)/1000000.0;
95
bool rval = diff >= update_interval;
102
void throttle_impl::reset_timer()
104
LOG_TRACE(logger, "Resetting the update timer.");
107
if(gettimeofday(&now, 0) != 0)
108
write_time_error(errno);
114
throttle::~throttle()
118
shared_ptr<throttle> create_throttle()
120
return make_shared<throttle_impl>();