Subversion Repositories HomeAutomation

Rev

Rev 1595 | Rev 1601 | 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 "Manager.h"
  22.  
  23. namespace atom {
  24. namespace timer {
  25.  
  26. Manager::Pointer Manager::instance_;
  27.  
  28. Manager::Manager() : io_service_work_(io_service_)
  29. {
  30.     boost::thread thread(boost::bind(&boost::asio::io_service::run, &this->io_service_));
  31.     this->thread_ = thread.move();
  32. }
  33.  
  34. Manager::~Manager()
  35. {
  36.     this->mutex_timers_.lock();
  37.     this->timers_.clear();
  38.     this->mutex_timers_.unlock();
  39.    
  40.     this->io_service_.stop();
  41.    
  42.     this->thread_.interrupt();
  43.     this->thread_.join();
  44. }
  45.  
  46. Manager::Pointer Manager::Instance()
  47. {
  48.     return Manager::instance_;
  49. }
  50.  
  51. void Manager::Create()
  52. {
  53.     Manager::instance_ = Manager::Pointer(new Manager());
  54. }
  55.  
  56. void Manager::Delete()
  57. {
  58.     Manager::instance_.reset();
  59. }
  60.  
  61. void Manager::ConnectSlots(const SignalOnTimeout::slot_type& slot_on_timeout)
  62. {
  63.     this->signal_on_timeout_.connect(slot_on_timeout);
  64. }
  65.  
  66. void Manager::SlotOnTimeout(TimerId id)
  67. {
  68.     this->mutex_timers_.lock();
  69.    
  70.     TimerList::iterator it = this->timers_.find(id);
  71.    
  72.     if (it != this->timers_.end())
  73.     {
  74.         if (!it->second->GetRepeat())
  75.         {
  76.             this->timers_.erase(id);
  77.         }
  78.     }
  79.    
  80.     this->mutex_timers_.unlock();
  81.    
  82.     this->signal_on_timeout_(id);
  83. }
  84.  
  85. TimerId Manager::Set(unsigned int timeout, bool repeat)
  86. {
  87.     this->mutex_timers_.lock();
  88.    
  89.     Timer::Pointer timer = Timer::Pointer(new Timer(this->io_service_, this->GetFreeId(), timeout, repeat));
  90.  
  91.     this->timers_[timer->GetId()] = timer;
  92.    
  93.     this->mutex_timers_.unlock();
  94.    
  95.     timer->ConnectSlots(Timer::SignalOnTimeout::slot_type(&Manager::SlotOnTimeout, this, _1).track(Manager::instance_));
  96.    
  97.     timer->Start();
  98.    
  99.     return timer->GetId();
  100. }
  101.  
  102. void Manager::Cancel(TimerId id)
  103. {
  104.     this->io_service_.post(boost::bind(&Manager::CancelHandler, this, id));
  105. }
  106.  
  107. void Manager::CancelHandler(TimerId id)
  108. {
  109.     this->mutex_timers_.lock();
  110.    
  111.     TimerList::iterator it = this->timers_.find(id);
  112.    
  113.     if (it != this->timers_.end())
  114.     {
  115.         it->second->Cancel();
  116.     }
  117.    
  118.     this->mutex_timers_.unlock();
  119. }
  120.  
  121. TimerId Manager::GetFreeId()
  122. {
  123.     TimerId id = 1;
  124.        
  125.     while (this->timers_.find(id) != this->timers_.end())
  126.     {
  127.         id++;
  128.     }
  129.        
  130.     return id;
  131. }
  132.  
  133. }; // namespace timer
  134. }; // namespace atom
  135.