]> git.vomp.tv Git - vompclient.git/blob - timers.h
Add -s option to select server
[vompclient.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 #include <list>
26
27 #ifndef WIN32
28 #include "threadp.h"
29 #else
30 #include "threadwin.h"
31 #endif
32
33 #include "defines.h"
34
35
36 class Log;
37 class TimerReceiver;
38
39 /*
40
41 Timers documentation
42
43 Call setTimer to set a timer.... cancelTimer to delete a running timer.
44 Derive your object from TimerReceiver (timerreceiver.h), implement timercall() in your class
45 and supply your 'this' pointer to setTimer.
46
47 Once a timer has fired it does not exist anymore, you have to keep creating them if you want
48 a constant pulse.
49
50 clientReference is any int of your choice. It will be supplied back to you in the timercall()
51 so you can identify which timer has fired if you have more than one.
52
53 You can reset a timer by calling setTimer again. This will not create 2 timers, it will overwrite the first one.
54
55 You must not allow a timer to fire on an object that has been deleted already, unless you want
56 segfaulty hell.
57
58 ??
59
60 You must call cancelTimer before deleting object. cancelTimer guarantees that timercall
61 will not be called again.
62
63 */
64
65 class TimerEvent : public Thread_TYPE
66 {
67   public:
68     TimerEvent();
69
70     virtual void run();
71     virtual void threadMethod();
72     virtual void threadPostStopCleanup() {};
73
74     TimerReceiver* client;
75     int clientReference;
76     struct timespec requestedTime;
77
78     bool running;
79     bool restartAfterFinish;
80 };
81
82
83 using namespace std;
84
85 typedef list<TimerEvent*> TimerList;
86
87 class Timers : public Thread_TYPE
88 {
89   public:
90     Timers();
91     virtual ~Timers();
92     static Timers* getInstance();
93
94     int init();
95     int shutdown();
96
97     bool setTimerT(TimerReceiver* client, int clientReference, long int requestedTime, long int requestedTimeNSEC=0);
98     bool setTimerD(TimerReceiver* client, int clientReference, long int requestedSecs, long int requestedNSecs=0);
99     bool cancelTimer(TimerReceiver* client, int clientReference);
100
101     // Thread stuff
102     virtual void threadMethod();
103     virtual void threadPostStopCleanup() {};
104
105     void timerEventFinished(TimerEvent* timerEvent); // internal use only, does not return
106
107   private:
108     static Timers* instance;
109     Log* logger;
110     bool initted;
111     TimerList timerList;
112     bool resetThreadFlag;
113 };
114
115 #endif