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

Last change on this file since 3993 was 2615, checked in by cmv, 21 years ago

using namespace sophya enleve de machdefs.h, nouveau sopnamsp.h cmv 10/09/2004

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