Rev 1601 | Rev 1746 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1601 | Rev 1646 | ||
|---|---|---|---|
| 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() |
28 | Manager::Manager() |
| 29 | { |
29 | { |
| 30 | } |
30 | } |
| 31 | 31 | ||
| 32 | Manager::~Manager() |
32 | Manager::~Manager() |
| 33 | { |
33 | { |
| 34 | this->io_service_.stop(); |
34 | this->io_service_.stop(); |
| 35 | 35 | ||
| 36 | this->timers_.clear(); |
36 | this->timers_.clear(); |
| 37 | } |
37 | } |
| 38 | 38 | ||
| 39 | Manager::Pointer Manager::Instance() |
39 | Manager::Pointer Manager::Instance() |
| 40 | { |
40 | { |
| 41 | return Manager::instance_; |
41 | return Manager::instance_; |
| 42 | } |
42 | } |
| 43 | 43 | ||
| 44 | void Manager::Create() |
44 | void Manager::Create() |
| 45 | { |
45 | { |
| 46 | Manager::instance_ = Manager::Pointer(new Manager()); |
46 | Manager::instance_ = Manager::Pointer(new Manager()); |
| 47 | } |
47 | } |
| 48 | 48 | ||
| 49 | void Manager::Delete() |
49 | void Manager::Delete() |
| 50 | { |
50 | { |
| 51 | Manager::instance_.reset(); |
51 | Manager::instance_.reset(); |
| 52 | } |
52 | } |
| 53 | 53 | ||
| 54 | void Manager::ConnectSlots(const SignalOnTimeout::slot_type& slot_on_timeout) |
54 | void Manager::ConnectSlots(const SignalOnTimeout::slot_type& slot_on_timeout) |
| 55 | { |
55 | { |
| 56 | this->signal_on_timeout_.connect(slot_on_timeout); |
56 | this->signal_on_timeout_.connect(slot_on_timeout); |
| 57 | } |
57 | } |
| 58 | 58 | ||
| 59 | void Manager::SlotOnTimeout(TimerId id) |
59 | void Manager::SlotOnTimeout(TimerId id) |
| 60 | { |
60 | { |
| 61 | bool repeat = false; |
61 | bool repeat = false; |
| 62 | 62 | ||
| 63 | this->mutex_timers_.lock(); |
63 | this->mutex_timers_.lock(); |
| 64 | 64 | ||
| 65 | TimerList::iterator it = this->timers_.find(id); |
65 | TimerList::iterator it = this->timers_.find(id); |
| 66 | 66 | ||
| 67 | if (it != this->timers_.end()) |
67 | if (it != this->timers_.end()) |
| 68 | { |
68 | { |
| 69 | repeat = it->second->GetRepeat(); |
69 | repeat = it->second->GetRepeat(); |
| 70 | 70 | ||
| 71 | if (!repeat) |
71 | if (!repeat) |
| 72 | { |
72 | { |
| 73 | this->timers_.erase(id); |
73 | this->timers_.erase(id); |
| 74 | } |
74 | } |
| 75 | } |
75 | } |
| 76 | 76 | ||
| 77 | this->mutex_timers_.unlock(); |
77 | this->mutex_timers_.unlock(); |
| 78 | 78 | ||
| 79 | this->signal_on_timeout_(id, repeat); |
79 | this->signal_on_timeout_(id, repeat); |
| 80 | } |
80 | } |
| 81 | 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 | ||
| 82 | TimerId Manager:: |
99 | TimerId Manager::SetTimer(unsigned int timeout, bool repeat) |
| 83 | { |
100 | { |
| 84 | this->mutex_timers_.lock(); |
101 | this->mutex_timers_.lock(); |
| 85 | 102 | ||
| 86 | Timer::Pointer timer = Timer::Pointer(new Timer(this->io_service_, this->GetFreeId(), timeout, repeat)); |
103 | Timer::Pointer timer = Timer::Pointer(new Timer(this->io_service_, this->GetFreeId(), timeout, repeat)); |
| 87 | 104 | ||
| 88 | this->timers_[timer->GetId()] = timer; |
105 | this->timers_[timer->GetId()] = timer; |
| 89 | 106 | ||
| 90 | this->mutex_timers_.unlock(); |
107 | this->mutex_timers_.unlock(); |
| 91 | 108 | ||
| 92 | timer->ConnectSlots(Timer::SignalOnTimeout::slot_type(&Manager::SlotOnTimeout, this, _1).track(Manager::instance_)); |
109 | timer->ConnectSlots(Timer::SignalOnTimeout::slot_type(&Manager::SlotOnTimeout, this, _1).track(Manager::instance_)); |
| 93 | 110 | ||
| 94 | timer->Start(); |
111 | timer->Start(); |
| 95 | 112 | ||
| 96 | return timer->GetId(); |
113 | return timer->GetId(); |
| 97 | } |
114 | } |
| 98 | 115 | ||
| 99 | void Manager::Cancel(TimerId id) |
116 | void Manager::Cancel(TimerId id) |
| 100 | { |
117 | { |
| 101 | this->io_service_.post(boost::bind(&Manager::CancelHandler, this, id)); |
118 | this->io_service_.post(boost::bind(&Manager::CancelHandler, this, id)); |
| 102 | } |
119 | } |
| 103 | 120 | ||
| 104 | void Manager::CancelHandler(TimerId id) |
121 | void Manager::CancelHandler(TimerId id) |
| 105 | { |
122 | { |
| 106 | this->mutex_timers_.lock(); |
123 | this->mutex_timers_.lock(); |
| 107 | 124 | ||
| 108 | TimerList::iterator it = this->timers_.find(id); |
125 | TimerList::iterator it = this->timers_.find(id); |
| 109 | 126 | ||
| 110 | if (it != this->timers_.end()) |
127 | if (it != this->timers_.end()) |
| 111 | { |
128 | { |
| 112 | it->second->Cancel(); |
129 | it->second->Cancel(); |
| 113 | } |
130 | } |
| 114 | 131 | ||
| 115 | this->mutex_timers_.unlock(); |
132 | this->mutex_timers_.unlock(); |
| 116 | } |
133 | } |
| 117 | 134 | ||
| 118 | TimerId Manager::GetFreeId() |
135 | TimerId Manager::GetFreeId() |
| 119 | { |
136 | { |
| 120 | TimerId id = 1; |
137 | TimerId id = 1; |
| 121 | 138 | ||
| 122 | while (this->timers_.find(id) != this->timers_.end()) |
139 | while (this->timers_.find(id) != this->timers_.end()) |
| 123 | { |
140 | { |
| 124 | id++; |
141 | id++; |
| 125 | } |
142 | } |
| 126 | 143 | ||
| 127 | return id; |
144 | return id; |
| 128 | } |
145 | } |
| 129 | 146 | ||
| 130 | }; // namespace timer |
147 | }; // namespace timer |
| 131 | }; // namespace atom |
148 | }; // namespace atom |
| 132 | 149 | ||