]> git.vomp.tv Git - vompclient.git/blob - timers.h
Rename TCP class to TCPOld
[vompclient.git] / timers.h
1 /*
2     Copyright 2004-2020 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, see <https://www.gnu.org/licenses/>.
18 */
19
20 /*
21
22 Timers documentation
23
24 Call setTimer to set a timer.... cancelTimer to delete a running timer.
25 Derive your object from TimerReceiver, implement timercall() in your class
26 and supply your 'this' pointer to setTimer.
27
28 Once a timer has fired it does not exist anymore, you have to keep creating them if you want
29 a constant pulse.
30
31 clientReference is any int of your choice. It will be supplied back to you in the timercall()
32 so you can identify which timer has fired if you have more than one.
33
34 You can reset a timer by calling setTimer again. This will not create 2 timers, it will overwrite the first one.
35
36 You must not allow a timer to fire on an object that has been deleted already.
37
38 You must call cancelTimer before deleting object. cancelTimer guarantees that timercall
39 will not be called again.
40
41 */
42
43 #ifndef TIMERS_H
44 #define TIMERS_H
45
46 #include <thread>
47 #include <mutex>
48 #include <condition_variable>
49 #include <list>
50 #include <chrono>
51
52 #include "defines.h"
53
54 class TimerReceiver
55 {
56   public:
57     virtual ~TimerReceiver() {}
58     virtual void timercall(int clientReference)=0;
59 };
60
61 class TimerEvent
62 {
63   friend class Timers;
64   public:
65   private:
66     TimerReceiver* client{};
67     int clientReference{};
68     std::chrono::system_clock::time_point requestedTime;
69     bool running{};
70     bool completed{};
71     bool restartAfterFinish{};
72     std::thread timerThread;
73     std::mutex threadStartProtect;
74
75     void run();
76 };
77
78 class Log;
79 typedef std::list<TimerEvent*> TimerList;
80
81 class Timers
82 {
83   public:
84     Timers();
85     virtual ~Timers();
86     static Timers* getInstance();
87
88     int init();
89     void shutdown();
90
91     bool setTimerT(TimerReceiver* client, int clientReference, long int requestedTime, long int requestedTimeNSEC=0);
92     bool setTimerD(TimerReceiver* client, int clientReference, long int requestedSecs, long int requestedNSecs=0);
93     bool setTimerC(TimerReceiver* client, int clientReference, std::chrono::system_clock::time_point& requestedTime);
94     bool cancelTimer(TimerReceiver* client, int clientReference);
95
96     void reapTimerEvent(TimerEvent*); // Internal only
97   private:
98     static Timers* instance;
99     Log* logger{};
100     bool initted{};
101     bool quitThread{};
102     bool recalc{};
103     bool doReap{};
104     TimerList timerList;
105
106     std::thread timersThread;
107     std::mutex timersMutex;
108     std::condition_variable timersCond;
109
110     void masterLoop();
111     void reap();
112 };
113
114 #endif