[IOMultiplexerV2] added CIOTimer class to c++ interface && added some features to...
[NextIRCd.git] / src / IOHandler_test / timer++ / iotest.cpp
1 /* main.c - IOMultiplexer
2  * Copyright (C) 2012  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
18 #include <stdio.h>
19 #include "../../IOHandler++/IOHandler.h"
20 #include "../../IOHandler++/IOTimer.h"
21
22 class IOTestTimer : public CIOTimer {
23 public:
24         IOTestTimer(int test_duration) : CIOTimer() {
25                 this->tick_count = 0;
26                 this->test_duration = test_duration;
27                 gettimeofday(&this->test_clock1, NULL);
28                 gettimeofday(&this->test_clock2, NULL);
29                 
30                 this->setRelativeTimeoutSeconds(this->test_duration / 1000.0, 1);
31                 this->setActive(1);
32                 
33                 printf("[timer 0] %ld.%ld\n", test_clock1.tv_sec, test_clock1.tv_usec);
34         };
35         
36 protected:
37         virtual void timeout() {
38                 this->tick_count++;
39                 this->tick();
40         };
41         
42 private:
43         timeval test_clock1, test_clock2;
44         int tick_count, test_duration;
45         
46         void tick() {
47                 timeval curr_time;
48                 int diff1;
49                 double diff2;
50                 
51                 gettimeofday(&curr_time, NULL);
52                 diff1 = (curr_time.tv_sec - this->test_clock1.tv_sec) * 1000 + ((curr_time.tv_usec - this->test_clock1.tv_usec) / 1000);
53                 diff2 = (curr_time.tv_sec - this->test_clock2.tv_sec) * 1000 + ((curr_time.tv_usec - this->test_clock2.tv_usec) / 1000.0);
54                 diff2 -= (this->tick_count * this->test_duration);
55                 gettimeofday(&this->test_clock1, NULL);
56                 printf("[timer %03d] %ld.%06ld [%d ms]  accuracy: %f ms\n", this->tick_count, curr_time.tv_sec, curr_time.tv_usec, diff1, diff2);
57         };
58 };
59
60
61 int main(int argc, char *argv[]) {
62         CIOHandler *iohandler = new CIOHandler();
63         IOTestTimer *timer = new IOTestTimer(100);
64         
65         iohandler->start();
66 }