Subversion Repositories HomeAutomation

Rev

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()
  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::SetAlarm(std::string time)
  83. {
  84.     this->mutex_timers_.lock();
  85.    
  86.     Timer::Pointer timer = Timer::Pointer(new Timer(this->io_service_, this->GetFreeId(), time));
  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. TimerId Manager::SetTimer(unsigned int timeout, bool repeat)
  100. {
  101.     this->mutex_timers_.lock();
  102.    
  103.     Timer::Pointer timer = Timer::Pointer(new Timer(this->io_service_, this->GetFreeId(), timeout, repeat));
  104.  
  105.     this->timers_[timer->GetId()] = timer;
  106.    
  107.     this->mutex_timers_.unlock();
  108.    
  109.     timer->ConnectSlots(Timer::SignalOnTimeout::slot_type(&Manager::SlotOnTimeout, this, _1).track(Manager::instance_));
  110.    
  111.     timer->Start();
  112.    
  113.     return timer->GetId();
  114. }
  115.  
  116. void Manager::Cancel(TimerId id)
  117. {
  118.     this->io_service_.post(boost::bind(&Manager::CancelHandler, this, id));
  119. }
  120.  
  121. void Manager::CancelHandler(TimerId id)
  122. {
  123.     this->mutex_timers_.lock();
  124.    
  125.     TimerList::iterator it = this->timers_.find(id);
  126.    
  127.     if (it != this->timers_.end())
  128.     {
  129.         it->second->Cancel();
  130.     }
  131.    
  132.     this->mutex_timers_.unlock();
  133. }
  134.  
  135. TimerId Manager::GetFreeId()
  136. {
  137.     TimerId id = 1;
  138.        
  139.     while (this->timers_.find(id) != this->timers_.end())
  140.     {
  141.         id++;
  142.     }
  143.        
  144.     return id;
  145. }
  146.  
  147. }; // namespace timer
  148. }; // namespace atom
  149.