Subversion Repositories HomeAutomation

Rev

Rev 1646 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1646 Rev 1746
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)
82
TimerId Manager::SetAlarm(std::string time)
83
{
83
{
84
    this->mutex_timers_.lock();
84
    this->mutex_timers_.lock();
85
   
85
   
86
    Timer::Pointer timer = Timer::Pointer(new Timer(this->io_service_, this->GetFreeId(), time));
86
    Timer::Pointer timer = Timer::Pointer(new Timer(this->io_service_, this->GetFreeId(), time));
87
   
87
   
88
    this->timers_[timer->GetId()] = timer;
88
    this->timers_[timer->GetId()] = timer;
89
   
89
   
90
    this->mutex_timers_.unlock();
90
    this->mutex_timers_.unlock();
91
   
91
   
92
    timer->ConnectSlots(Timer::SignalOnTimeout::slot_type(&Manager::SlotOnTimeout, this, _1).track(Manager::instance_));
92
    timer->ConnectSlots(Timer::SignalOnTimeout::slot_type(&Manager::SlotOnTimeout, this, _1).track(Manager::instance_));
93
   
93
   
94
    timer->Start();
94
    timer->Start();
95
   
95
   
96
    return timer->GetId();
96
    return timer->GetId();
97
}
97
}
98
 
98
 
99
TimerId Manager::SetTimer(unsigned int timeout, bool repeat)
99
TimerId Manager::SetTimer(unsigned int timeout, bool repeat)
100
{
100
{
101
    this->mutex_timers_.lock();
101
    this->mutex_timers_.lock();
102
   
102
   
103
    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));
104
 
104
 
105
    this->timers_[timer->GetId()] = timer;
105
    this->timers_[timer->GetId()] = timer;
106
   
106
   
107
    this->mutex_timers_.unlock();
107
    this->mutex_timers_.unlock();
108
   
108
   
109
    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_));
110
   
110
   
111
    timer->Start();
111
    timer->Start();
112
   
112
   
113
    return timer->GetId();
113
    return timer->GetId();
114
}
114
}
115
 
115
 
116
void Manager::Cancel(TimerId id)
116
void Manager::Cancel(TimerId id)
117
{
117
{
118
    this->io_service_.post(boost::bind(&Manager::CancelHandler, this, id));
118
    this->io_service_.post(boost::bind(&Manager::CancelHandler, this, id));
119
}
119
}
120
 
120
 
121
void Manager::CancelHandler(TimerId id)
121
void Manager::CancelHandler(TimerId id)
122
{
122
{
123
    this->mutex_timers_.lock();
123
    this->mutex_timers_.lock();
124
   
124
   
125
    TimerList::iterator it = this->timers_.find(id);
125
    TimerList::iterator it = this->timers_.find(id);
126
   
126
   
127
    if (it != this->timers_.end())
127
    if (it != this->timers_.end())
128
    {
128
    {
129
        it->second->Cancel();
129
        this->timers_.erase(id);
130
    }
130
    }
131
   
131
   
132
    this->mutex_timers_.unlock();
132
    this->mutex_timers_.unlock();
133
}
133
}
134
 
134
 
135
TimerId Manager::GetFreeId()
135
TimerId Manager::GetFreeId()
136
{
136
{
137
    TimerId id = 1;
137
    TimerId id = 1;
138
       
138
       
139
    while (this->timers_.find(id) != this->timers_.end())
139
    while (this->timers_.find(id) != this->timers_.end())
140
    {
140
    {
141
        id++;
141
        id++;
142
    }
142
    }
143
       
143
       
144
    return id;
144
    return id;
145
}
145
}
146
 
146
 
147
}; // namespace timer
147
}; // namespace timer
148
}; // namespace atom
148
}; // namespace atom
149
 
149