1 | /* Classe d'objets avec une methode (DoPeriodic()) appelee periodiquement */
|
---|
2 | /* Eric Aubourg 04/96 */
|
---|
3 | /* Reza Ansari 06/96 */
|
---|
4 | /* LAL/IN2P3 (Orsay) DAPNIA/CEA (Saclay) */
|
---|
5 |
|
---|
6 | #include "machdefs.h"
|
---|
7 | #include <signal.h>
|
---|
8 | #include <unistd.h>
|
---|
9 |
|
---|
10 | #include "periodic.h"
|
---|
11 | /*!
|
---|
12 | \class SOPHYA::Periodic
|
---|
13 | \ingroup SysTools
|
---|
14 | This class provide the service of calling a given function at
|
---|
15 | regular intervals. It can also be subclassed with the method
|
---|
16 | \b DoPeriodic redefined to perform a periodic action.
|
---|
17 | */
|
---|
18 |
|
---|
19 | /* --Methode-- */
|
---|
20 | /*! Constructor with the definition of the interval (in seconds)
|
---|
21 | and an optional user action */
|
---|
22 | Periodic::Periodic(int dt, UsPeriodicAction act, void * usp)
|
---|
23 | {
|
---|
24 | mDt = dt;
|
---|
25 | mAct = act;
|
---|
26 | mUsp = usp;
|
---|
27 | if (mDt < 1) mDt = 5;
|
---|
28 | mDtms = 1000*mDt;
|
---|
29 | mFgact = false;
|
---|
30 | it = -1;
|
---|
31 | }
|
---|
32 |
|
---|
33 | /* --Methode-- */
|
---|
34 | Periodic::~Periodic()
|
---|
35 | {
|
---|
36 | if (mFgact) Stop();
|
---|
37 | }
|
---|
38 |
|
---|
39 | /* --Methode-- */
|
---|
40 | /*! Sets the user action function and its data \c usp */
|
---|
41 | void Periodic::SetAction(UsPeriodicAction act, void * usp)
|
---|
42 | {
|
---|
43 | mAct = act;
|
---|
44 | mUsp = usp;
|
---|
45 | }
|
---|
46 |
|
---|
47 | /* --Methode-- */
|
---|
48 | /*! Changes the interval \c dt specified in seconds */
|
---|
49 | void Periodic::SetInterval(int dt)
|
---|
50 | {
|
---|
51 | mDt = dt;
|
---|
52 | if (mDt < 1) mDt = 5;
|
---|
53 | mDtms = 1000*mDt;
|
---|
54 | }
|
---|
55 |
|
---|
56 | /* --Methode-- */
|
---|
57 | /*! Changes the interval \c dtms specified in milli-seconds */
|
---|
58 | void Periodic::SetIntervalms(int dtms)
|
---|
59 | {
|
---|
60 | if (dtms < 1) dtms = 5;
|
---|
61 | mDtms = dtms;
|
---|
62 | mDt = mDtms/1000;
|
---|
63 | if (mDt < 1) mDt = 1;
|
---|
64 | }
|
---|
65 |
|
---|
66 | /* --Methode-- */
|
---|
67 | /*! Activate the object */
|
---|
68 | void Periodic::Start(int dt)
|
---|
69 | {
|
---|
70 | if (mFgact) return;
|
---|
71 | if (dt > 0) { mDt = dt; mDtms = dt*1000; }
|
---|
72 | if (!actifs) actifs = new PeriodicList;
|
---|
73 | it = 0;
|
---|
74 | if (actifs->size() == 0) {
|
---|
75 | signal(SIGALRM, CallBack);
|
---|
76 | alarm(1);
|
---|
77 | }
|
---|
78 | actifs->push_back(this);
|
---|
79 | mFgact = true;
|
---|
80 | return;
|
---|
81 | }
|
---|
82 |
|
---|
83 | /* --Methode-- */
|
---|
84 | /*! Deactivate the object */
|
---|
85 | void Periodic::Stop()
|
---|
86 | {
|
---|
87 | if (!mFgact) return;
|
---|
88 |
|
---|
89 | //remove(actifs->begin(), actifs->end(), this); // $CHECK$ - Reza
|
---|
90 | actifs->remove(this); // $CHECK$
|
---|
91 | if (actifs->size() == 0) {
|
---|
92 | signal(SIGALRM, SIG_IGN);
|
---|
93 | alarm(0);
|
---|
94 | delete actifs;
|
---|
95 | actifs = NULL;
|
---|
96 | }
|
---|
97 | it = -1; mFgact = false;
|
---|
98 | return;
|
---|
99 | }
|
---|
100 |
|
---|
101 | /* --Methode-- */
|
---|
102 | /*! This method should be redefined for sub-classes of Periodic
|
---|
103 | The default implementation calls the user action function */
|
---|
104 | void Periodic::DoPeriodic()
|
---|
105 | {
|
---|
106 | if (mAct) mAct(mUsp);
|
---|
107 | }
|
---|
108 |
|
---|
109 | PeriodicList* Periodic::actifs = NULL;
|
---|
110 |
|
---|
111 | void Periodic::CallBack(int)
|
---|
112 | {
|
---|
113 | if (!actifs) return;
|
---|
114 |
|
---|
115 | for (PeriodicList::iterator i = actifs->begin(); i != actifs->end(); i++) {
|
---|
116 | Periodic* p = (Periodic*) *i;
|
---|
117 | p->it ++;
|
---|
118 | if (p->it >= p->mDt) {
|
---|
119 | p->it = 0;
|
---|
120 | p->DoPeriodic();
|
---|
121 | }
|
---|
122 | }
|
---|
123 | alarm(1);
|
---|
124 | }
|
---|
125 |
|
---|