source: Sophya/trunk/SophyaLib/SysTools/periodic.cc@ 2124

Last change on this file since 2124 was 913, checked in by ansari, 25 years ago

Documentation + Modifs mineures (namespace, etc..) - Reza 13/4/2000

File size: 2.6 KB
Line 
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 */
22Periodic::Periodic(int dt, UsPeriodicAction act, void * usp)
23{
24mDt = dt;
25mAct = act;
26mUsp = usp;
27if (mDt < 1) mDt = 5;
28mDtms = 1000*mDt;
29mFgact = false;
30it = -1;
31}
32
33/* --Methode-- */
34Periodic::~Periodic()
35{
36if (mFgact) Stop();
37}
38
39/* --Methode-- */
40/*! Sets the user action function and its data \c usp */
41void Periodic::SetAction(UsPeriodicAction act, void * usp)
42{
43mAct = act;
44mUsp = usp;
45}
46
47/* --Methode-- */
48/*! Changes the interval \c dt specified in seconds */
49void Periodic::SetInterval(int dt)
50{
51mDt = dt;
52if (mDt < 1) mDt = 5;
53mDtms = 1000*mDt;
54}
55
56/* --Methode-- */
57/*! Changes the interval \c dtms specified in milli-seconds */
58void Periodic::SetIntervalms(int dtms)
59{
60if (dtms < 1) dtms = 5;
61mDtms = dtms;
62mDt = mDtms/1000;
63if (mDt < 1) mDt = 1;
64}
65
66/* --Methode-- */
67/*! Activate the object */
68void Periodic::Start(int dt)
69{
70if (mFgact) return;
71if (dt > 0) { mDt = dt; mDtms = dt*1000; }
72if (!actifs) actifs = new PeriodicList;
73it = 0;
74if (actifs->size() == 0) {
75 signal(SIGALRM, CallBack);
76 alarm(1);
77 }
78actifs->push_back(this);
79mFgact = true;
80return;
81}
82
83/* --Methode-- */
84/*! Deactivate the object */
85void Periodic::Stop()
86{
87if (!mFgact) return;
88
89//remove(actifs->begin(), actifs->end(), this); // $CHECK$ - Reza
90actifs->remove(this); // $CHECK$
91if (actifs->size() == 0) {
92 signal(SIGALRM, SIG_IGN);
93 alarm(0);
94 delete actifs;
95 actifs = NULL;
96 }
97it = -1; mFgact = false;
98return;
99}
100
101/* --Methode-- */
102/*! This method should be redefined for sub-classes of Periodic
103 The default implementation calls the user action function */
104void Periodic::DoPeriodic()
105{
106if (mAct) mAct(mUsp);
107}
108
109PeriodicList* Periodic::actifs = NULL;
110
111void 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
Note: See TracBrowser for help on using the repository browser.