Subversion Repositories HomeAutomation

Rev

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