[IOMultiplexerV2] added CIOTimer class to c++ interface && added some features to...
[NextIRCd.git] / src / IOHandler++ / IOTimer.cpp
1 /* IOTimer.cpp - IOMultiplexer v2
2  * Copyright (C) 2014  Philipp Kreil (pk910)
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License 
15  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
16  */
17 extern "C" {
18         #include "../IOHandler/IOTimer.h"
19 }
20 #include "IOTimer.h"
21 #include <cstdarg>
22 #include <cstdio>
23 #include <cstring>
24
25 static IOTIMER_CALLBACK(c_timer_callback) {
26         CIOTimer *ciotimer = (CIOTimer *) iotimer->data;
27         ciotimer->timer_callback();
28 }
29
30 CIOTimer::CIOTimer() {
31         this->iotimer = iotimer_create(NULL);
32         this->iotimer->data = this;
33         iotimer_set_callback(this->iotimer, c_timer_callback);
34         iotimer_set_persistent(this->iotimer, 1);
35 }
36
37 CIOTimer::~CIOTimer() {
38         iotimer_destroy(this->iotimer);
39 }
40
41 void CIOTimer::setTimeout(timeval timeout) {
42         iotimer_set_timeout(this->iotimer, &timeout);
43 }
44
45 void CIOTimer::setRelativeTimeout(timeval timeout) {
46         timeval now;
47         gettimeofday(&now, NULL);
48         
49         timeout.tv_sec += now.tv_sec;
50         timeout.tv_usec += now.tv_usec;
51         while(timeout.tv_usec > 1000000) {
52                 timeout.tv_usec -= 1000000;
53                 timeout.tv_sec++;
54         }
55         
56         this->setTimeout(timeout);
57 }
58
59 void CIOTimer::setRelativeTimeout(timeval timeout, int auto_reload) {
60         this->setRelativeTimeout(timeout);
61         if(auto_reload)
62                 this->setAutoReload(timeout);
63 }
64
65 void CIOTimer::setRelativeTimeoutSeconds(double seconds) {
66         this->setRelativeTimeoutSeconds(seconds, 0);
67 }
68
69 void CIOTimer::setRelativeTimeoutSeconds(double seconds, int auto_reload) {
70         timeval tout;
71         tout.tv_sec = (int) seconds;
72         tout.tv_usec = ((int) (seconds * 1000000)) % 1000000;
73         this->setRelativeTimeout(tout);
74         if(auto_reload)
75                 this->setAutoReload(tout);
76 }
77
78 timeval CIOTimer::getTimeout() {
79         return iotimer_get_timeout(this->iotimer);
80 }
81
82 timeval CIOTimer::getRelativeTimeout() {
83         timeval tout, now;
84         gettimeofday(&now, NULL);
85         
86         tout = iotimer_get_timeout(this->iotimer);
87         
88         if(tout.tv_sec || tout.tv_usec) {
89                 tout.tv_sec = tout.tv_sec - now.tv_sec;
90                 tout.tv_usec = tout.tv_usec - now.tv_usec;
91                 if(tout.tv_usec < 0) {
92                         tout.tv_usec += 1000000;
93                         tout.tv_sec--;
94                 }
95         }
96         
97         return tout;
98 }
99
100 double CIOTimer::getRelativeTimeoutSeconds() {
101         timeval tout = this->getRelativeTimeout();
102         return tout.tv_sec + (tout.tv_usec / 1000000);
103 }
104
105
106 void CIOTimer::setAutoReload(timeval timeout) {
107         iotimer_set_autoreload(this->iotimer, &timeout);
108 }
109
110 void CIOTimer::clearAutoReload() {
111         iotimer_set_autoreload(this->iotimer, NULL);
112 }
113
114 timeval CIOTimer::getAutoReloadTime() {
115         return iotimer_get_autoreload(this->iotimer);
116 }
117
118 int CIOTimer::getAutoReloadState() {
119         timeval timeout = this->getAutoReloadTime();
120         if(timeout.tv_sec == 0 && timeout.tv_usec == 0)
121                 return 0;
122         else
123                 return 1;
124 }
125
126 void CIOTimer::setActive(int active) {
127         if(active)
128                 iotimer_start(this->iotimer);
129         else
130                 iotimer_stop(this->iotimer);
131 }
132
133 int CIOTimer::getActive() {
134         return iotimer_state(this->iotimer);
135 }
136
137 void CIOTimer::timer_callback() {
138         this->timeout();
139 }
140