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