Subversion Repositories HomeAutomation

Rev

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