Subversion Repositories HomeAutomation

Rev

Rev 1599 | Rev 1646 | Go to most recent revision | 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
 
23
namespace atom {
24
namespace timer {
25
 
1599 runge 26
Manager::Pointer Manager::instance_;
1593 runge 27
 
1601 runge 28
Manager::Manager()
1593 runge 29
{
30
}
31
 
32
Manager::~Manager()
33
{
1599 runge 34
    this->io_service_.stop();
35
 
1601 runge 36
    this->timers_.clear();
1593 runge 37
}
38
 
39
Manager::Pointer Manager::Instance()
40
{
41
    return Manager::instance_;
42
}
43
 
1599 runge 44
void Manager::Create()
45
{
46
    Manager::instance_ = Manager::Pointer(new Manager());
47
}
48
 
1593 runge 49
void Manager::Delete()
50
{
51
    Manager::instance_.reset();
52
}
53
 
54
void Manager::ConnectSlots(const SignalOnTimeout::slot_type& slot_on_timeout)
55
{
56
    this->signal_on_timeout_.connect(slot_on_timeout);
57
}
58
 
59
void Manager::SlotOnTimeout(TimerId id)
60
{
1601 runge 61
    bool repeat = false;
62
 
1595 runge 63
    this->mutex_timers_.lock();
64
 
1593 runge 65
    TimerList::iterator it = this->timers_.find(id);
66
 
67
    if (it != this->timers_.end())
68
    {
1601 runge 69
        repeat = it->second->GetRepeat();
70
 
71
        if (!repeat)
1593 runge 72
        {
73
            this->timers_.erase(id);
74
        }
75
    }
76
 
1595 runge 77
    this->mutex_timers_.unlock();
78
 
1601 runge 79
    this->signal_on_timeout_(id, repeat);
1593 runge 80
}
81
 
82
TimerId Manager::Set(unsigned int timeout, bool repeat)
83
{
1595 runge 84
    this->mutex_timers_.lock();
85
 
1593 runge 86
    Timer::Pointer timer = Timer::Pointer(new Timer(this->io_service_, this->GetFreeId(), timeout, repeat));
87
 
88
    this->timers_[timer->GetId()] = timer;
89
 
1595 runge 90
    this->mutex_timers_.unlock();
91
 
1593 runge 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
 
99
void Manager::Cancel(TimerId id)
100
{
101
    this->io_service_.post(boost::bind(&Manager::CancelHandler, this, id));
102
}
103
 
104
void Manager::CancelHandler(TimerId id)
105
{
1595 runge 106
    this->mutex_timers_.lock();
107
 
1593 runge 108
    TimerList::iterator it = this->timers_.find(id);
109
 
110
    if (it != this->timers_.end())
111
    {
112
        it->second->Cancel();
113
    }
1595 runge 114
 
115
    this->mutex_timers_.unlock();
1593 runge 116
}
117
 
118
TimerId Manager::GetFreeId()
119
{
120
    TimerId id = 1;
121
 
122
    while (this->timers_.find(id) != this->timers_.end())
123
    {
124
        id++;
125
    }
126
 
127
    return id;
128
}
129
 
130
}; // namespace timer
131
}; // namespace atom