summaryrefslogtreecommitdiff
path: root/src/generic
diff options
context:
space:
mode:
authorDaniel_Burrows@alumni.brown.edu <Daniel_Burrows@alumni.brown.edu>2010-07-03 18:48:03 -0700
committerDaniel Burrows <Daniel Burrows Daniel_Burrows@alumni.brown.edu>2010-07-03 23:49:21 -0700
commitb2aa333c7b9af41a0aa0122d9d976a4dee2be6ba (patch)
tree0055c766ede64ead4adca121d9f7ab02ee7254a2 /src/generic
parent3fe709bebbfdb81c5354cf6e30c6762390569918 (diff)
downloadaptitude-b2aa333c7b9af41a0aa0122d9d976a4dee2be6ba.tar.gz
Move the throttle object to src/generic/util.
Diffstat (limited to 'src/generic')
-rw-r--r--src/generic/util/Makefile.am4
-rw-r--r--src/generic/util/SConscript4
-rw-r--r--src/generic/util/mocks/Makefile.am1
-rw-r--r--src/generic/util/mocks/SConscript3
-rw-r--r--src/generic/util/mocks/throttle.h47
-rw-r--r--src/generic/util/throttle.cc123
-rw-r--r--src/generic/util/throttle.h59
7 files changed, 241 insertions, 0 deletions
diff --git a/src/generic/util/Makefile.am b/src/generic/util/Makefile.am
index c754e805..6652afc5 100644
--- a/src/generic/util/Makefile.am
+++ b/src/generic/util/Makefile.am
@@ -4,6 +4,8 @@ localedir = $(datadir)/locale
INCLUDES = -I$(top_builddir) -I$(srcdir) -I$(top_srcdir) -I$(top_srcdir)/src
DEFS = -DLOCALEDIR=\"$(localedir)\" @DEFS@
+SUBDIRS = mocks
+
noinst_LIBRARIES = libgeneric-util.a
libgeneric_util_a_SOURCES = \
compare3.h \
@@ -40,6 +42,8 @@ libgeneric_util_a_SOURCES = \
sqlite.h \
temp.cc \
temp.h \
+ throttle.cc \
+ throttle.h \
undo.cc \
undo.h \
util.cc \
diff --git a/src/generic/util/SConscript b/src/generic/util/SConscript
index fe9eeaf5..0615eac0 100644
--- a/src/generic/util/SConscript
+++ b/src/generic/util/SConscript
@@ -34,6 +34,8 @@ srcs = map(File, [
'sqlite.h',
'temp.cc',
'temp.h',
+ 'throttle.cc',
+ 'throttle.h',
'undo.cc',
'undo.h',
'util.cc',
@@ -42,4 +44,6 @@ srcs = map(File, [
programs_env.Dist('Makefile.am', 'SConscript')
+SConscript(dirs = [ 'mocks' ])
+
Return('srcs')
diff --git a/src/generic/util/mocks/Makefile.am b/src/generic/util/mocks/Makefile.am
new file mode 100644
index 00000000..6b88c48d
--- /dev/null
+++ b/src/generic/util/mocks/Makefile.am
@@ -0,0 +1 @@
+EXTRA_DIST = throttle.h \ No newline at end of file
diff --git a/src/generic/util/mocks/SConscript b/src/generic/util/mocks/SConscript
new file mode 100644
index 00000000..025e0f15
--- /dev/null
+++ b/src/generic/util/mocks/SConscript
@@ -0,0 +1,3 @@
+Import('programs_env')
+
+programs_env.Dist('throttle.h')
diff --git a/src/generic/util/mocks/throttle.h b/src/generic/util/mocks/throttle.h
new file mode 100644
index 00000000..0e852f6e
--- /dev/null
+++ b/src/generic/util/mocks/throttle.h
@@ -0,0 +1,47 @@
+/** \file throttle.h */ // -*-c++-*-
+
+
+// Copyright (C) 2010 Daniel Burrows
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License as
+// published by the Free Software Foundation; either version 2 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; see the file COPYING. If not, write to
+// the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+// Boston, MA 02111-1307, USA.
+
+#ifndef APTITUDE_UTIL_MOCKS_THROTTLE_H
+#define APTITUDE_UTIL_MOCKS_THROTTLE_H
+
+// Local includes:
+#include <generic/util/throttle.h>
+
+// System includes:
+#include <gmock/gmock.h>
+
+namespace aptitude
+{
+ namespace util
+ {
+ namespace mocks
+ {
+ /** \brief Mock implementation of aptitude::util::throttle. */
+ class throttle : public aptitude::util::throttle
+ {
+ public:
+ MOCK_METHOD0(update_required, bool());
+ MOCK_METHOD0(reset_timer, void());
+ };
+ }
+ }
+}
+
+#endif // APTITUDE_UTIL_MOCKS_THROTTLE_H
diff --git a/src/generic/util/throttle.cc b/src/generic/util/throttle.cc
new file mode 100644
index 00000000..9697df36
--- /dev/null
+++ b/src/generic/util/throttle.cc
@@ -0,0 +1,123 @@
+/** \file throttle.cc */ // -*-c++-*-
+
+#include "throttle.h"
+
+// Local includes:
+#include <loggers.h>
+
+// System includes:
+#include <boost/make_shared.hpp>
+#include <boost/optional.hpp>
+
+#include <generic/util/util.h>
+
+#include <errno.h>
+#include <sys/time.h>
+
+using aptitude::Loggers;
+using boost::make_shared;
+using boost::optional;
+using boost::shared_ptr;
+using logging::LoggerPtr;
+
+namespace aptitude
+{
+ namespace util
+ {
+ namespace
+ {
+ class throttle_impl : public throttle
+ {
+ boost::optional<struct timeval> last_update;
+
+ logging::LoggerPtr logger;
+
+ // Used to ensure that we only warn once about gettimeofday()
+ // failing.
+ bool wrote_time_error;
+
+ static const double update_interval = 0.7;
+
+ void write_time_error(int errnum);
+
+ public:
+ throttle_impl();
+
+ /** \return \b true if the timer has expired. */
+ bool update_required();
+
+ /** \brief Reset the timer that controls when the display is
+ * updated.
+ */
+ void reset_timer();
+ };
+
+ const double throttle_impl::update_interval;
+
+ void throttle_impl::write_time_error(int errnum)
+ {
+ if(!wrote_time_error)
+ {
+ LOG_ERROR(logger,
+ "gettimeofday() failed: " <<
+ sstrerror(errnum));
+ wrote_time_error = true;
+ }
+ }
+
+ throttle_impl::throttle_impl()
+ : logger(Loggers::getAptitudeCmdlineThrottle()),
+ wrote_time_error(false)
+ {
+ }
+
+ bool throttle_impl::update_required()
+ {
+ if(!last_update)
+ return true;
+ else
+ {
+ // Time checking code shamelessly stolen from apt, since
+ // we know theirs works.
+ struct timeval now;
+ if(gettimeofday(&now, 0) != 0)
+ {
+ write_time_error(errno);
+ return false;
+ }
+ else
+ {
+ const struct timeval &last_update_time = *last_update;
+ double diff =
+ now.tv_sec - last_update_time.tv_sec +
+ (now.tv_usec - last_update_time.tv_usec)/1000000.0;
+
+ bool rval = diff >= update_interval;
+
+ return rval;
+ }
+ }
+ }
+
+ void throttle_impl::reset_timer()
+ {
+ LOG_TRACE(logger, "Resetting the update timer.");
+
+ struct timeval now;
+ if(gettimeofday(&now, 0) != 0)
+ write_time_error(errno);
+ else
+ last_update = now;
+ }
+ }
+
+ throttle::~throttle()
+ {
+ }
+
+ shared_ptr<throttle> create_throttle()
+ {
+ return make_shared<throttle_impl>();
+ }
+ }
+}
diff --git a/src/generic/util/throttle.h b/src/generic/util/throttle.h
new file mode 100644
index 00000000..5535b8a2
--- /dev/null
+++ b/src/generic/util/throttle.h
@@ -0,0 +1,59 @@
+/** \file throttle.h */ // -*-c++-*-
+
+// Copyright (C) 2010 Daniel Burrows
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License as
+// published by the Free Software Foundation; either version 2 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; see the file COPYING. If not, write to
+// the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+// Boston, MA 02111-1307, USA.
+
+#ifndef APTITUDE_UTIL_THROTTLE_H
+#define APTITUDE_UTIL_THROTTLE_H
+
+// System includes:
+#include <boost/shared_ptr.hpp>
+
+namespace aptitude
+{
+ namespace util
+ {
+ /** \brief Used to ensure that an expensive operation (such as
+ * updating a progress indicator) doesn't run too often.
+ *
+ * To test code that uses this class, use the mock that's
+ * available in mocks/.
+ */
+ class throttle
+ {
+ public:
+ virtual ~throttle() = 0;
+
+ /** \return \b true if the timer has expired. */
+ virtual bool update_required() = 0;
+
+ /** \brief Reset the timer that controls when the display is
+ * updated.
+ */
+ virtual void reset_timer() = 0;
+ };
+
+ /** \brief Create a throttle object.
+ *
+ * \todo This should take an argument giving the update interval
+ * in seconds.
+ */
+ boost::shared_ptr<throttle> create_throttle();
+ }
+}
+
+#endif // APTITUDE_UTIL_THROTTLE_H