diff options
author | Remko Tronçon <git@el-tramo.be> | 2009-11-12 19:48:32 (GMT) |
---|---|---|
committer | Remko Tronçon <git@el-tramo.be> | 2009-11-12 20:41:59 (GMT) |
commit | 8231ecc07c1c7d5e260a2795b0660de157501850 (patch) | |
tree | 935baa855914d632086fcac5f26330c2f0261216 /Swiften/Network | |
parent | 48bb7441f913144d6a20687ff79264e4631156f0 (diff) | |
download | swift-8231ecc07c1c7d5e260a2795b0660de157501850.zip swift-8231ecc07c1c7d5e260a2795b0660de157501850.tar.bz2 |
Created a TimerFactory, and abstracted Timer.
Diffstat (limited to 'Swiften/Network')
-rw-r--r-- | Swiften/Network/BoostTimer.cpp | 34 | ||||
-rw-r--r-- | Swiften/Network/BoostTimer.h | 25 | ||||
-rw-r--r-- | Swiften/Network/BoostTimerFactory.cpp | 13 | ||||
-rw-r--r-- | Swiften/Network/BoostTimerFactory.h | 20 | ||||
-rw-r--r-- | Swiften/Network/SConscript | 3 | ||||
-rw-r--r-- | Swiften/Network/Timer.cpp | 29 | ||||
-rw-r--r-- | Swiften/Network/Timer.h | 22 | ||||
-rw-r--r-- | Swiften/Network/TimerFactory.cpp | 8 | ||||
-rw-r--r-- | Swiften/Network/TimerFactory.h | 14 |
9 files changed, 121 insertions, 47 deletions
diff --git a/Swiften/Network/BoostTimer.cpp b/Swiften/Network/BoostTimer.cpp new file mode 100644 index 0000000..fdbd45d --- /dev/null +++ b/Swiften/Network/BoostTimer.cpp @@ -0,0 +1,34 @@ +#include "Swiften/Network/BoostTimer.h" + +#include <boost/date_time/posix_time/posix_time.hpp> +#include <boost/asio.hpp> + +#include "Swiften/EventLoop/MainEventLoop.h" + +namespace Swift { + +BoostTimer::BoostTimer(int milliseconds, boost::asio::io_service* service) : + timeout(milliseconds), timer(*service) { +} + +void BoostTimer::start() { + timer.expires_from_now(boost::posix_time::milliseconds(timeout)); + timer.async_wait(boost::bind(&BoostTimer::handleTimerTick, shared_from_this(), boost::asio::placeholders::error)); +} + +void BoostTimer::stop() { + timer.cancel(); +} + +void BoostTimer::handleTimerTick(const boost::system::error_code& error) { + if (error) { + assert(error == boost::asio::error::operation_aborted); + } + else { + MainEventLoop::postEvent(boost::bind(boost::ref(onTick)), shared_from_this()); + timer.expires_from_now(boost::posix_time::milliseconds(timeout)); + timer.async_wait(boost::bind(&BoostTimer::handleTimerTick, shared_from_this(), boost::asio::placeholders::error)); + } +} + +} diff --git a/Swiften/Network/BoostTimer.h b/Swiften/Network/BoostTimer.h new file mode 100644 index 0000000..9b27cf9 --- /dev/null +++ b/Swiften/Network/BoostTimer.h @@ -0,0 +1,25 @@ +#pragma once + +#include <boost/asio.hpp> +#include <boost/thread.hpp> +#include <boost/enable_shared_from_this.hpp> + +#include "Swiften/EventLoop/EventOwner.h" +#include "Swiften/Network/Timer.h" + +namespace Swift { + class BoostTimer : public Timer, public EventOwner, public boost::enable_shared_from_this<BoostTimer> { + public: + BoostTimer(int milliseconds, boost::asio::io_service* service); + + virtual void start(); + virtual void stop(); + + private: + void handleTimerTick(const boost::system::error_code& error); + + private: + int timeout; + boost::asio::deadline_timer timer; + }; +} diff --git a/Swiften/Network/BoostTimerFactory.cpp b/Swiften/Network/BoostTimerFactory.cpp new file mode 100644 index 0000000..bbcd83f --- /dev/null +++ b/Swiften/Network/BoostTimerFactory.cpp @@ -0,0 +1,13 @@ +#include "Swiften/Network/BoostTimerFactory.h" +#include "Swiften/Network/BoostTimer.h" + +namespace Swift { + +BoostTimerFactory::BoostTimerFactory(boost::asio::io_service* ioService) : ioService(ioService) { +} + +boost::shared_ptr<Timer> BoostTimerFactory::createTimer(int milliseconds) { + return boost::shared_ptr<Timer>(new BoostTimer(milliseconds, ioService)); +} + +} diff --git a/Swiften/Network/BoostTimerFactory.h b/Swiften/Network/BoostTimerFactory.h new file mode 100644 index 0000000..e98c9de --- /dev/null +++ b/Swiften/Network/BoostTimerFactory.h @@ -0,0 +1,20 @@ +#pragma once + +#include <boost/asio.hpp> + +#include "Swiften/Network/TimerFactory.h" +#include "Swiften/Network/BoostTimer.h" + +namespace Swift { + class BoostTimer; + + class BoostTimerFactory : public TimerFactory { + public: + BoostTimerFactory(boost::asio::io_service*); + + virtual boost::shared_ptr<Timer> createTimer(int milliseconds); + + private: + boost::asio::io_service* ioService; + }; +} diff --git a/Swiften/Network/SConscript b/Swiften/Network/SConscript index 652bda1..475d6e4 100644 --- a/Swiften/Network/SConscript +++ b/Swiften/Network/SConscript @@ -12,10 +12,13 @@ objects = myenv.StaticObject([ "ConnectionFactory.cpp", "ConnectionServer.cpp", "Connector.cpp", + "TimerFactory.cpp", + "BoostTimerFactory.cpp", "DomainNameResolver.cpp", "PlatformDomainNameResolver.cpp", "StaticDomainNameResolver.cpp", "HostAddress.cpp", "Timer.cpp", + "BoostTimer.cpp", ]) swiften_env.Append(SWIFTEN_OBJECTS = [objects]) diff --git a/Swiften/Network/Timer.cpp b/Swiften/Network/Timer.cpp index d6120e3..a8d17c3 100644 --- a/Swiften/Network/Timer.cpp +++ b/Swiften/Network/Timer.cpp @@ -1,37 +1,8 @@ #include "Swiften/Network/Timer.h" -#include <boost/date_time/posix_time/posix_time.hpp> - -#include "Swiften/EventLoop/MainEventLoop.h" - namespace Swift { -Timer::Timer(int milliseconds, boost::asio::io_service* service) : - timeout(milliseconds), timer(*service) { -} - Timer::~Timer() { - stop(); -} - -void Timer::start() { - timer.expires_from_now(boost::posix_time::milliseconds(timeout)); - timer.async_wait(boost::bind(&Timer::handleTimerTick, shared_from_this(), boost::asio::placeholders::error)); -} - -void Timer::stop() { - timer.cancel(); -} - -void Timer::handleTimerTick(const boost::system::error_code& error) { - if (error) { - assert(error == boost::asio::error::operation_aborted); - } - else { - MainEventLoop::postEvent(boost::bind(boost::ref(onTick)), shared_from_this()); - timer.expires_from_now(boost::posix_time::milliseconds(timeout)); - timer.async_wait(boost::bind(&Timer::handleTimerTick, shared_from_this(), boost::asio::placeholders::error)); - } } } diff --git a/Swiften/Network/Timer.h b/Swiften/Network/Timer.h index 6474fe9..9b01a0d 100644 --- a/Swiften/Network/Timer.h +++ b/Swiften/Network/Timer.h @@ -1,29 +1,15 @@ #pragma once -#include <boost/asio.hpp> #include <boost/signals.hpp> -#include <boost/thread.hpp> -#include <boost/enable_shared_from_this.hpp> - -#include "Swiften/EventLoop/EventOwner.h" namespace Swift { - class Timer : public EventOwner, public boost::enable_shared_from_this<Timer> { + class Timer { public: - Timer(int milliseconds, boost::asio::io_service* service); - ~Timer(); + virtual ~Timer(); - void start(); - void stop(); + virtual void start() = 0; + virtual void stop() = 0; - public: boost::signal<void ()> onTick; - - private: - void handleTimerTick(const boost::system::error_code& error); - - private: - int timeout; - boost::asio::deadline_timer timer; }; } diff --git a/Swiften/Network/TimerFactory.cpp b/Swiften/Network/TimerFactory.cpp new file mode 100644 index 0000000..642ac52 --- /dev/null +++ b/Swiften/Network/TimerFactory.cpp @@ -0,0 +1,8 @@ +#include "Swiften/Network/TimerFactory.h" + +namespace Swift { + +TimerFactory::~TimerFactory() { +} + +} diff --git a/Swiften/Network/TimerFactory.h b/Swiften/Network/TimerFactory.h new file mode 100644 index 0000000..f72a8fc --- /dev/null +++ b/Swiften/Network/TimerFactory.h @@ -0,0 +1,14 @@ +#pragma once + +#include <boost/shared_ptr.hpp> + +namespace Swift { + class Timer; + + class TimerFactory { + public: + virtual ~TimerFactory(); + + virtual boost::shared_ptr<Timer> createTimer(int milliseconds) = 0; + }; +} |