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

Last change on this file since 2190 was 2143, checked in by ansari, 23 years ago

MAJ periodic.cc pour autodoc (doc PI) - Reza 29/7/2002

File size: 3.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//++
20// Class Periodic
21// Lib SysTools
22// include periodic.h
23//
24// Classe d'objets avec une méthode "DoPeriodic()" appelée périodiquement
25// à intervalle régulier.
26//--
27
28//++
29// Titre Constructeur, méthodes
30//--
31//++
32// Periodic(int dt, UsPeriodicAction act=NULL, void * usp=NULL)
33// Constructeur avec un intervalle de temps en secondes "dt".
34// Il est possible de spécifier une fonction "UsPeriodicAction" avec
35// un argument "usp" qui sera appelée régulierement.
36//| typedef void (* UsPeriodicAction) (void *);
37//--
38
39//++
40// void Start(int dt=-1)
41// Activation de l'objet périodique.
42// On peut spécifier l'intervalle en temps (en secondes) si "dt > 0".
43// void Stop()
44// Désactivation de l'objet périodique.
45// void DoPeriodic()
46// Méthode virtuelle qui peut être surchargée dans les classes dérivées.
47// Elle est appelée lorsque l'intervalle de temps est écoulé. Par défaut,
48// elle appelle la fonction "UsPeriodicAction" associée si "!= NULL"
49//--
50
51
52/* --Methode-- */
53/*! Constructor with the definition of the interval (in seconds)
54 and an optional user action */
55Periodic::Periodic(int dt, UsPeriodicAction act, void * usp)
56{
57mDt = dt;
58mAct = act;
59mUsp = usp;
60if (mDt < 1) mDt = 5;
61mDtms = 1000*mDt;
62mFgact = false;
63it = -1;
64}
65
66/* --Methode-- */
67Periodic::~Periodic()
68{
69if (mFgact) Stop();
70}
71
72/* --Methode-- */
73/*! Sets the user action function and its data \c usp */
74void Periodic::SetAction(UsPeriodicAction act, void * usp)
75{
76mAct = act;
77mUsp = usp;
78}
79
80/* --Methode-- */
81/*! Changes the interval \c dt specified in seconds */
82void Periodic::SetInterval(int dt)
83{
84mDt = dt;
85if (mDt < 1) mDt = 5;
86mDtms = 1000*mDt;
87}
88
89/* --Methode-- */
90/*! Changes the interval \c dtms specified in milli-seconds */
91void Periodic::SetIntervalms(int dtms)
92{
93if (dtms < 1) dtms = 5;
94mDtms = dtms;
95mDt = mDtms/1000;
96if (mDt < 1) mDt = 1;
97}
98
99/* --Methode-- */
100/*! Activate the object */
101void Periodic::Start(int dt)
102{
103if (mFgact) return;
104if (dt > 0) { mDt = dt; mDtms = dt*1000; }
105if (!actifs) actifs = new PeriodicList;
106it = 0;
107if (actifs->size() == 0) {
108 signal(SIGALRM, CallBack);
109 alarm(1);
110 }
111actifs->push_back(this);
112mFgact = true;
113return;
114}
115
116/* --Methode-- */
117/*! Deactivate the object */
118void Periodic::Stop()
119{
120if (!mFgact) return;
121
122//remove(actifs->begin(), actifs->end(), this); // $CHECK$ - Reza
123actifs->remove(this); // $CHECK$
124if (actifs->size() == 0) {
125 signal(SIGALRM, SIG_IGN);
126 alarm(0);
127 delete actifs;
128 actifs = NULL;
129 }
130it = -1; mFgact = false;
131return;
132}
133
134/* --Methode-- */
135/*! This method should be redefined for sub-classes of Periodic
136 The default implementation calls the user action function */
137void Periodic::DoPeriodic()
138{
139if (mAct) mAct(mUsp);
140}
141
142PeriodicList* Periodic::actifs = NULL;
143
144void Periodic::CallBack(int)
145{
146 if (!actifs) return;
147
148 for (PeriodicList::iterator i = actifs->begin(); i != actifs->end(); i++) {
149 Periodic* p = (Periodic*) *i;
150 p->it ++;
151 if (p->it >= p->mDt) {
152 p->it = 0;
153 p->DoPeriodic();
154 }
155 }
156 alarm(1);
157}
158
Note: See TracBrowser for help on using the repository browser.