]> git.vomp.tv Git - vompclient-marten.git/blob - timers.h
New timers method
[vompclient-marten.git] / timers.h
1 /*
2     Copyright 2004-2005 Chris Tallon
3
4     This file is part of VOMP.
5
6     VOMP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     VOMP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with VOMP; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #ifndef TIMERS_H
22 #define TIMERS_H
23
24 #include <stdio.h>
25 #ifndef WIN32
26 #include <pthread.h>
27 #endif
28 #include <list>
29
30 #include "defines.h"
31 #include "log.h"
32 #include "command.h"
33 #include "timerreceiver.h"
34
35 #ifdef WIN32
36 #include "threadwin.h"
37 #else
38 #include "threadp.h"
39 #endif
40
41 /*
42
43 Timers documentation
44
45 Call setTimer to set a timer.... cancelTimer to delete a running timer.
46 Derive your object from TimerReceiver (timerreceiver.h), implement timercall() in your class
47 and supply your 'this' pointer to setTimer.
48
49 Once a timer has fired it does not exist anymore, you have to keep creating them if you want
50 a constant pulse.
51
52 clientReference is any int of your choice. It will be supplied back to you in the timercall()
53 so you can identify which timer has fired if you have more than one.
54
55 You can reset a timer by calling setTimer again. This will not create 2 timers, it will overwrite the first one.
56
57 You must not allow a timer to fire on an object that has been deleted already, unless you want
58 segfaulty hell.
59
60 ??
61
62 You must call cancelTimer before deleting object. cancelTimer guarantees that timercall
63 will not be called again.
64
65 */
66
67 class TimerEvent : public Thread_TYPE
68 {
69   public:
70     TimerEvent();
71
72     virtual void run();
73     virtual void threadMethod();
74     virtual void threadPostStopCleanup() {};
75
76     TimerReceiver* client;
77     int clientReference;
78     struct timespec requestedTime;
79
80     bool running;
81     bool restartAfterFinish;
82 };
83
84
85 using namespace std;
86
87 typedef list<TimerEvent*> TimerList;
88
89 class Timers : public Thread_TYPE
90 {
91   public:
92     Timers();
93     virtual ~Timers();
94     static Timers* getInstance();
95
96     int init();
97     int shutdown();
98
99     bool setTimerT(TimerReceiver* client, int clientReference, long int requestedTime, long int requestedTimeNSEC=0);
100     bool setTimerD(TimerReceiver* client, int clientReference, long int requestedSecs, long int requestedNSecs=0);
101     bool cancelTimer(TimerReceiver* client, int clientReference);
102
103     // Thread stuff
104     virtual void threadMethod();
105     virtual void threadPostStopCleanup() {};
106
107     void timerEventFinished(TimerEvent* timerEvent); // internal use only, does not return
108
109   private:
110     static Timers* instance;
111     Log* logger;
112     bool initted;
113     TimerList timerList;
114     bool resetThreadFlag;
115 };
116
117 #endif