Subversion Repositories HomeAutomation

Rev

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