Rev 1593 | Rev 1599 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1593 | runge | 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_ = Manager::Pointer(new Manager()); |
||
| 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 | { |
||
| 1595 | runge | 36 | this->mutex_timers_.lock(); |
| 1593 | runge | 37 | this->timers_.clear(); |
| 1595 | runge | 38 | this->mutex_timers_.unlock(); |
| 1593 | runge | 39 | |
| 40 | this->thread_.interrupt(); |
||
| 41 | this->thread_.join(); |
||
| 42 | } |
||
| 43 | |||
| 44 | Manager::Pointer Manager::Instance() |
||
| 45 | { |
||
| 46 | return Manager::instance_; |
||
| 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 | { |
||
| 1595 | runge | 61 | this->mutex_timers_.lock(); |
| 62 | |||
| 1593 | runge | 63 | TimerList::iterator it = this->timers_.find(id); |
| 64 | |||
| 65 | if (it != this->timers_.end()) |
||
| 66 | { |
||
| 67 | if (!it->second->GetRepeat()) |
||
| 68 | { |
||
| 69 | this->timers_.erase(id); |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 1595 | runge | 73 | this->mutex_timers_.unlock(); |
| 74 | |||
| 1593 | runge | 75 | this->signal_on_timeout_(id); |
| 76 | } |
||
| 77 | |||
| 78 | TimerId Manager::Set(unsigned int timeout, bool repeat) |
||
| 79 | { |
||
| 1595 | runge | 80 | this->mutex_timers_.lock(); |
| 81 | |||
| 1593 | runge | 82 | Timer::Pointer timer = Timer::Pointer(new Timer(this->io_service_, this->GetFreeId(), timeout, repeat)); |
| 83 | |||
| 84 | this->timers_[timer->GetId()] = timer; |
||
| 85 | |||
| 1595 | runge | 86 | this->mutex_timers_.unlock(); |
| 87 | |||
| 1593 | runge | 88 | timer->ConnectSlots(Timer::SignalOnTimeout::slot_type(&Manager::SlotOnTimeout, this, _1).track(Manager::instance_)); |
| 89 | |||
| 90 | timer->Start(); |
||
| 91 | |||
| 92 | return timer->GetId(); |
||
| 93 | } |
||
| 94 | |||
| 95 | void Manager::Cancel(TimerId id) |
||
| 96 | { |
||
| 97 | this->io_service_.post(boost::bind(&Manager::CancelHandler, this, id)); |
||
| 98 | } |
||
| 99 | |||
| 100 | void Manager::CancelHandler(TimerId id) |
||
| 101 | { |
||
| 1595 | runge | 102 | this->mutex_timers_.lock(); |
| 103 | |||
| 1593 | runge | 104 | TimerList::iterator it = this->timers_.find(id); |
| 105 | |||
| 106 | if (it != this->timers_.end()) |
||
| 107 | { |
||
| 108 | it->second->Cancel(); |
||
| 109 | } |
||
| 1595 | runge | 110 | |
| 111 | this->mutex_timers_.unlock(); |
||
| 1593 | runge | 112 | } |
| 113 | |||
| 114 | TimerId Manager::GetFreeId() |
||
| 115 | { |
||
| 116 | TimerId id = 1; |
||
| 117 | |||
| 118 | while (this->timers_.find(id) != this->timers_.end()) |
||
| 119 | { |
||
| 120 | id++; |
||
| 121 | } |
||
| 122 | |||
| 123 | return id; |
||
| 124 | } |
||
| 125 | |||
| 126 | }; // namespace timer |
||
| 127 | }; // namespace atom |