Rev 1595 | Rev 1601 | 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 | |||
| 1599 | runge | 26 | Manager::Pointer Manager::instance_; |
| 1593 | runge | 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 | |
| 1599 | runge | 40 | this->io_service_.stop(); |
| 41 | |||
| 1593 | runge | 42 | this->thread_.interrupt(); |
| 43 | this->thread_.join(); |
||
| 44 | } |
||
| 45 | |||
| 46 | Manager::Pointer Manager::Instance() |
||
| 47 | { |
||
| 48 | return Manager::instance_; |
||
| 49 | } |
||
| 50 | |||
| 1599 | runge | 51 | void Manager::Create() |
| 52 | { |
||
| 53 | Manager::instance_ = Manager::Pointer(new Manager()); |
||
| 54 | } |
||
| 55 | |||
| 1593 | runge | 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 | { |
||
| 1595 | runge | 68 | this->mutex_timers_.lock(); |
| 69 | |||
| 1593 | runge | 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 | |||
| 1595 | runge | 80 | this->mutex_timers_.unlock(); |
| 81 | |||
| 1593 | runge | 82 | this->signal_on_timeout_(id); |
| 83 | } |
||
| 84 | |||
| 85 | TimerId Manager::Set(unsigned int timeout, bool repeat) |
||
| 86 | { |
||
| 1595 | runge | 87 | this->mutex_timers_.lock(); |
| 88 | |||
| 1593 | runge | 89 | Timer::Pointer timer = Timer::Pointer(new Timer(this->io_service_, this->GetFreeId(), timeout, repeat)); |
| 90 | |||
| 91 | this->timers_[timer->GetId()] = timer; |
||
| 92 | |||
| 1595 | runge | 93 | this->mutex_timers_.unlock(); |
| 94 | |||
| 1593 | runge | 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 | { |
||
| 1595 | runge | 109 | this->mutex_timers_.lock(); |
| 110 | |||
| 1593 | runge | 111 | TimerList::iterator it = this->timers_.find(id); |
| 112 | |||
| 113 | if (it != this->timers_.end()) |
||
| 114 | { |
||
| 115 | it->second->Cancel(); |
||
| 116 | } |
||
| 1595 | runge | 117 | |
| 118 | this->mutex_timers_.unlock(); |
||
| 1593 | runge | 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 |