| 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 */
|
|---|
| 55 | Periodic::Periodic(int dt, UsPeriodicAction act, void * usp)
|
|---|
| 56 | {
|
|---|
| 57 | mDt = dt;
|
|---|
| 58 | mAct = act;
|
|---|
| 59 | mUsp = usp;
|
|---|
| 60 | if (mDt < 1) mDt = 5;
|
|---|
| 61 | mDtms = 1000*mDt;
|
|---|
| 62 | mFgact = false;
|
|---|
| 63 | it = -1;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | /* --Methode-- */
|
|---|
| 67 | Periodic::~Periodic()
|
|---|
| 68 | {
|
|---|
| 69 | if (mFgact) Stop();
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | /* --Methode-- */
|
|---|
| 73 | /*! Sets the user action function and its data \c usp */
|
|---|
| 74 | void Periodic::SetAction(UsPeriodicAction act, void * usp)
|
|---|
| 75 | {
|
|---|
| 76 | mAct = act;
|
|---|
| 77 | mUsp = usp;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | /* --Methode-- */
|
|---|
| 81 | /*! Changes the interval \c dt specified in seconds */
|
|---|
| 82 | void Periodic::SetInterval(int dt)
|
|---|
| 83 | {
|
|---|
| 84 | mDt = dt;
|
|---|
| 85 | if (mDt < 1) mDt = 5;
|
|---|
| 86 | mDtms = 1000*mDt;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | /* --Methode-- */
|
|---|
| 90 | /*! Changes the interval \c dtms specified in milli-seconds */
|
|---|
| 91 | void Periodic::SetIntervalms(int dtms)
|
|---|
| 92 | {
|
|---|
| 93 | if (dtms < 1) dtms = 5;
|
|---|
| 94 | mDtms = dtms;
|
|---|
| 95 | mDt = mDtms/1000;
|
|---|
| 96 | if (mDt < 1) mDt = 1;
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | /* --Methode-- */
|
|---|
| 100 | /*! Activate the object */
|
|---|
| 101 | void Periodic::Start(int dt)
|
|---|
| 102 | {
|
|---|
| 103 | if (mFgact) return;
|
|---|
| 104 | if (dt > 0) { mDt = dt; mDtms = dt*1000; }
|
|---|
| 105 | if (!actifs) actifs = new PeriodicList;
|
|---|
| 106 | it = 0;
|
|---|
| 107 | if (actifs->size() == 0) {
|
|---|
| 108 | signal(SIGALRM, CallBack);
|
|---|
| 109 | alarm(1);
|
|---|
| 110 | }
|
|---|
| 111 | actifs->push_back(this);
|
|---|
| 112 | mFgact = true;
|
|---|
| 113 | return;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | /* --Methode-- */
|
|---|
| 117 | /*! Deactivate the object */
|
|---|
| 118 | void Periodic::Stop()
|
|---|
| 119 | {
|
|---|
| 120 | if (!mFgact) return;
|
|---|
| 121 |
|
|---|
| 122 | //remove(actifs->begin(), actifs->end(), this); // $CHECK$ - Reza
|
|---|
| 123 | actifs->remove(this); // $CHECK$
|
|---|
| 124 | if (actifs->size() == 0) {
|
|---|
| 125 | signal(SIGALRM, SIG_IGN);
|
|---|
| 126 | alarm(0);
|
|---|
| 127 | delete actifs;
|
|---|
| 128 | actifs = NULL;
|
|---|
| 129 | }
|
|---|
| 130 | it = -1; mFgact = false;
|
|---|
| 131 | return;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | /* --Methode-- */
|
|---|
| 135 | /*! This method should be redefined for sub-classes of Periodic
|
|---|
| 136 | The default implementation calls the user action function */
|
|---|
| 137 | void Periodic::DoPeriodic()
|
|---|
| 138 | {
|
|---|
| 139 | if (mAct) mAct(mUsp);
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | PeriodicList* Periodic::actifs = NULL;
|
|---|
| 143 |
|
|---|
| 144 | void 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 |
|
|---|