Rev 1595 | Rev 1601 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1595 | Rev 1599 | ||
|---|---|---|---|
| 1 | /* |
1 | /* |
| 2 | * |
2 | * |
| 3 | * Copyright (C) 2010 Mattias Runge |
3 | * Copyright (C) 2010 Mattias Runge |
| 4 | * |
4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
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 |
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 |
7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
8 | * (at your option) any later version. |
| 9 | * |
9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
13 | * GNU General Public License for more details. |
| 14 | * |
14 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
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., |
16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
18 | * |
| 19 | */ |
19 | */ |
| 20 | 20 | ||
| 21 | #include "Manager.h" |
21 | #include "Manager.h" |
| 22 | 22 | ||
| 23 | namespace atom { |
23 | namespace atom { |
| 24 | namespace timer { |
24 | namespace timer { |
| 25 | 25 | ||
| 26 | Manager::Pointer Manager::instance_ |
26 | Manager::Pointer Manager::instance_; |
| 27 | 27 | ||
| 28 | Manager::Manager() : io_service_work_(io_service_) |
28 | Manager::Manager() : io_service_work_(io_service_) |
| 29 | { |
29 | { |
| 30 | boost::thread thread(boost::bind(&boost::asio::io_service::run, &this->io_service_)); |
30 | boost::thread thread(boost::bind(&boost::asio::io_service::run, &this->io_service_)); |
| 31 | this->thread_ = thread.move(); |
31 | this->thread_ = thread.move(); |
| 32 | } |
32 | } |
| 33 | 33 | ||
| 34 | Manager::~Manager() |
34 | Manager::~Manager() |
| 35 | { |
35 | { |
| 36 | this->mutex_timers_.lock(); |
36 | this->mutex_timers_.lock(); |
| 37 | this->timers_.clear(); |
37 | this->timers_.clear(); |
| 38 | this->mutex_timers_.unlock(); |
38 | this->mutex_timers_.unlock(); |
| - | 39 | ||
| - | 40 | this->io_service_.stop(); |
|
| 39 | 41 | ||
| 40 | this->thread_.interrupt(); |
42 | this->thread_.interrupt(); |
| 41 | this->thread_.join(); |
43 | this->thread_.join(); |
| 42 | } |
44 | } |
| 43 | 45 | ||
| 44 | Manager::Pointer Manager::Instance() |
46 | Manager::Pointer Manager::Instance() |
| - | 47 | { |
|
| - | 48 | return Manager::instance_; |
|
| - | 49 | } |
|
| - | 50 | ||
| - | 51 | void Manager::Create() |
|
| 45 | { |
52 | { |
| 46 |
|
53 | Manager::instance_ = Manager::Pointer(new Manager()); |
| 47 | } |
54 | } |
| 48 | 55 | ||
| 49 | void Manager::Delete() |
56 | void Manager::Delete() |
| 50 | { |
57 | { |
| 51 | Manager::instance_.reset(); |
58 | Manager::instance_.reset(); |
| 52 | } |
59 | } |
| 53 | 60 | ||
| 54 | void Manager::ConnectSlots(const SignalOnTimeout::slot_type& slot_on_timeout) |
61 | void Manager::ConnectSlots(const SignalOnTimeout::slot_type& slot_on_timeout) |
| 55 | { |
62 | { |
| 56 | this->signal_on_timeout_.connect(slot_on_timeout); |
63 | this->signal_on_timeout_.connect(slot_on_timeout); |
| 57 | } |
64 | } |
| 58 | 65 | ||
| 59 | void Manager::SlotOnTimeout(TimerId id) |
66 | void Manager::SlotOnTimeout(TimerId id) |
| 60 | { |
67 | { |
| 61 | this->mutex_timers_.lock(); |
68 | this->mutex_timers_.lock(); |
| 62 | 69 | ||
| 63 | TimerList::iterator it = this->timers_.find(id); |
70 | TimerList::iterator it = this->timers_.find(id); |
| 64 | 71 | ||
| 65 | if (it != this->timers_.end()) |
72 | if (it != this->timers_.end()) |
| 66 | { |
73 | { |
| 67 | if (!it->second->GetRepeat()) |
74 | if (!it->second->GetRepeat()) |
| 68 | { |
75 | { |
| 69 | this->timers_.erase(id); |
76 | this->timers_.erase(id); |
| 70 | } |
77 | } |
| 71 | } |
78 | } |
| 72 | 79 | ||
| 73 | this->mutex_timers_.unlock(); |
80 | this->mutex_timers_.unlock(); |
| 74 | 81 | ||
| 75 | this->signal_on_timeout_(id); |
82 | this->signal_on_timeout_(id); |
| 76 | } |
83 | } |
| 77 | 84 | ||
| 78 | TimerId Manager::Set(unsigned int timeout, bool repeat) |
85 | TimerId Manager::Set(unsigned int timeout, bool repeat) |
| 79 | { |
86 | { |
| 80 | this->mutex_timers_.lock(); |
87 | this->mutex_timers_.lock(); |
| 81 | 88 | ||
| 82 | Timer::Pointer timer = Timer::Pointer(new Timer(this->io_service_, this->GetFreeId(), timeout, repeat)); |
89 | Timer::Pointer timer = Timer::Pointer(new Timer(this->io_service_, this->GetFreeId(), timeout, repeat)); |
| 83 | 90 | ||
| 84 | this->timers_[timer->GetId()] = timer; |
91 | this->timers_[timer->GetId()] = timer; |
| 85 | 92 | ||
| 86 | this->mutex_timers_.unlock(); |
93 | this->mutex_timers_.unlock(); |
| 87 | 94 | ||
| 88 | timer->ConnectSlots(Timer::SignalOnTimeout::slot_type(&Manager::SlotOnTimeout, this, _1).track(Manager::instance_)); |
95 | timer->ConnectSlots(Timer::SignalOnTimeout::slot_type(&Manager::SlotOnTimeout, this, _1).track(Manager::instance_)); |
| 89 | 96 | ||
| 90 | timer->Start(); |
97 | timer->Start(); |
| 91 | 98 | ||
| 92 | return timer->GetId(); |
99 | return timer->GetId(); |
| 93 | } |
100 | } |
| 94 | 101 | ||
| 95 | void Manager::Cancel(TimerId id) |
102 | void Manager::Cancel(TimerId id) |
| 96 | { |
103 | { |
| 97 | this->io_service_.post(boost::bind(&Manager::CancelHandler, this, id)); |
104 | this->io_service_.post(boost::bind(&Manager::CancelHandler, this, id)); |
| 98 | } |
105 | } |
| 99 | 106 | ||
| 100 | void Manager::CancelHandler(TimerId id) |
107 | void Manager::CancelHandler(TimerId id) |
| 101 | { |
108 | { |
| 102 | this->mutex_timers_.lock(); |
109 | this->mutex_timers_.lock(); |
| 103 | 110 | ||
| 104 | TimerList::iterator it = this->timers_.find(id); |
111 | TimerList::iterator it = this->timers_.find(id); |
| 105 | 112 | ||
| 106 | if (it != this->timers_.end()) |
113 | if (it != this->timers_.end()) |
| 107 | { |
114 | { |
| 108 | it->second->Cancel(); |
115 | it->second->Cancel(); |
| 109 | } |
116 | } |
| 110 | 117 | ||
| 111 | this->mutex_timers_.unlock(); |
118 | this->mutex_timers_.unlock(); |
| 112 | } |
119 | } |
| 113 | 120 | ||
| 114 | TimerId Manager::GetFreeId() |
121 | TimerId Manager::GetFreeId() |
| 115 | { |
122 | { |
| 116 | TimerId id = 1; |
123 | TimerId id = 1; |
| 117 | 124 | ||
| 118 | while (this->timers_.find(id) != this->timers_.end()) |
125 | while (this->timers_.find(id) != this->timers_.end()) |
| 119 | { |
126 | { |
| 120 | id++; |
127 | id++; |
| 121 | } |
128 | } |
| 122 | 129 | ||
| 123 | return id; |
130 | return id; |
| 124 | } |
131 | } |
| 125 | 132 | ||
| 126 | }; // namespace timer |
133 | }; // namespace timer |
| 127 | }; // namespace atom |
134 | }; // namespace atom |
| 128 | 135 | ||