Subversion Repositories HomeAutomation

Rev

Rev 1646 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. /*
  2.  *
  3.  *  Copyright (C) 2010  Mattias Runge
  4.  *
  5.  *  This program is free software; you can redistribute it and/or modify
  6.  *  it under the terms of the GNU General Public License as published by
  7.  *  the Free Software Foundation; either version 2 of the License, or
  8.  *  (at your option) any later version.
  9.  *
  10.  *  This program is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  *  GNU General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU General Public License along
  16.  *  with this program; if not, write to the Free Software Foundation, Inc.,
  17.  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18.  *
  19.  */
  20.  
  21. #include "Timer.h"
  22.  
  23. namespace atom {
  24. namespace timer {
  25.  
  26. Timer::Timer(boost::asio::io_service& io_service, TimerId id, unsigned int timeout, bool repeat) : instance_(io_service)
  27. {
  28.     this->id_ = id;
  29.     this->timeout_ = timeout;
  30.     this->repeat_ = repeat;
  31. }
  32.  
  33. Timer::~Timer()
  34. {
  35.     this->Cancel();
  36. }
  37.  
  38. void Timer::ConnectSlots(const SignalOnTimeout::slot_type& slot_on_timeout)
  39. {
  40.     this->signal_on_timeout_.connect(slot_on_timeout);
  41. }
  42.  
  43. void Timer::Start()
  44. {
  45.     this->instance_.expires_from_now(boost::posix_time::millisec(this->timeout_));
  46.    
  47.     this->instance_.async_wait(boost::bind(&Timer::TimeoutHandler,
  48.                                            this,
  49.                                            boost::asio::placeholders::error));
  50. }
  51.  
  52. void Timer::Cancel()
  53. {
  54.     this->instance_.cancel();
  55. }
  56.  
  57. void Timer::TimeoutHandler(const boost::system::error_code& error)
  58. {
  59.     if (this->repeat_)
  60.     {
  61.         this->Start();
  62.     }
  63.    
  64.     this->signal_on_timeout_(this->id_);
  65. }
  66.  
  67. TimerId Timer::GetId()
  68. {
  69.     return this->id_;
  70. }
  71.  
  72. bool Timer::GetRepeat()
  73. {
  74.     return this->repeat_;
  75. }
  76.  
  77. }; // namespace timer
  78. }; // namespace atom
  79.