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