Rev 1595 | Go to most recent revision | Details | 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 | { |
||
| 36 | this->timers_.clear(); |
||
| 37 | |||
| 38 | this->thread_.interrupt(); |
||
| 39 | this->thread_.join(); |
||
| 40 | } |
||
| 41 | |||
| 42 | Manager::Pointer Manager::Instance() |
||
| 43 | { |
||
| 44 | return Manager::instance_; |
||
| 45 | } |
||
| 46 | |||
| 47 | void Manager::Delete() |
||
| 48 | { |
||
| 49 | Manager::instance_.reset(); |
||
| 50 | } |
||
| 51 | |||
| 52 | void Manager::ConnectSlots(const SignalOnTimeout::slot_type& slot_on_timeout) |
||
| 53 | { |
||
| 54 | this->signal_on_timeout_.connect(slot_on_timeout); |
||
| 55 | } |
||
| 56 | |||
| 57 | void Manager::SlotOnTimeout(TimerId id) |
||
| 58 | { |
||
| 59 | TimerList::iterator it = this->timers_.find(id); |
||
| 60 | |||
| 61 | if (it != this->timers_.end()) |
||
| 62 | { |
||
| 63 | if (!it->second->GetRepeat()) |
||
| 64 | { |
||
| 65 | this->timers_.erase(id); |
||
| 66 | } |
||
| 67 | } |
||
| 68 | |||
| 69 | this->signal_on_timeout_(id); |
||
| 70 | } |
||
| 71 | |||
| 72 | TimerId Manager::Set(unsigned int timeout, bool repeat) |
||
| 73 | { |
||
| 74 | Timer::Pointer timer = Timer::Pointer(new Timer(this->io_service_, this->GetFreeId(), timeout, repeat)); |
||
| 75 | |||
| 76 | this->timers_[timer->GetId()] = timer; |
||
| 77 | |||
| 78 | timer->ConnectSlots(Timer::SignalOnTimeout::slot_type(&Manager::SlotOnTimeout, this, _1).track(Manager::instance_)); |
||
| 79 | |||
| 80 | timer->Start(); |
||
| 81 | |||
| 82 | return timer->GetId(); |
||
| 83 | } |
||
| 84 | |||
| 85 | void Manager::Cancel(TimerId id) |
||
| 86 | { |
||
| 87 | this->io_service_.post(boost::bind(&Manager::CancelHandler, this, id)); |
||
| 88 | } |
||
| 89 | |||
| 90 | void Manager::CancelHandler(TimerId id) |
||
| 91 | { |
||
| 92 | TimerList::iterator it = this->timers_.find(id); |
||
| 93 | |||
| 94 | if (it != this->timers_.end()) |
||
| 95 | { |
||
| 96 | it->second->Cancel(); |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | TimerId Manager::GetFreeId() |
||
| 101 | { |
||
| 102 | TimerId id = 1; |
||
| 103 | |||
| 104 | while (this->timers_.find(id) != this->timers_.end()) |
||
| 105 | { |
||
| 106 | id++; |
||
| 107 | } |
||
| 108 | |||
| 109 | return id; |
||
| 110 | } |
||
| 111 | |||
| 112 | |||
| 113 | }; // namespace timer |
||
| 114 | }; // namespace atom |