00001
00002 #ifndef __DAQpp_RunState_h__
00003 #define __DAQpp_RunState_h__
00004 #include <algorithm>
00005 #include <functional>
00006 #include <string>
00007 #include <set>
00008 #include <DAQ++/GTimer.h>
00009 #include <DAQ++/ObserverModel.h>
00010 #include <DAQ++/exception.h>
00011
00012 namespace DAQpp
00013 {
00022 class RunStateAction
00023 {
00024 public:
00026 virtual ~RunStateAction()
00027 {}
00029 virtual void execute() = 0;
00030 };
00031
00097 class RunState : public Observable, public Observer
00098 {
00099 private:
00100 void cpy(const RunState &s);
00101 public:
00103 RunState();
00104
00106 RunState(const RunState &s)
00107 {
00108 cpy(s);
00109 }
00110
00112 RunState &operator=(const RunState &s)
00113 {
00114 if (this != &s) cpy(s);
00115 return *this;
00116 }
00117
00119 virtual ~RunState();
00120
00122 enum StatusType {
00123 NotDefined,
00124 NotReady,
00125 GettingReady,
00126 Ready,
00127 Starting,
00128 Paused,
00129 Running,
00130 Stopping,
00131 Stopped,
00132 TimeOut,
00133 last_state
00134 };
00135
00137 const std::string &get_name() const
00138 {
00139 return name;
00140 }
00142 void set_name(const std::string &s)
00143 {
00144 name = s;
00145 }
00146
00151 void reset();
00153 void start();
00155 void stop();
00157 int Ntrig() const
00158 {
00159 return ntrig;
00160 }
00162 void new_trigger()
00163 {
00164 ntrig++;
00165 }
00167 void new_writen()
00168 {
00169 nwriten++;
00170 }
00172 int Nwriten() const
00173 {
00174 return nwriten;
00175 }
00181 double eff() const;
00183
00188 double rate(bool average = false) const;
00190 double time() const
00191 {
00192 return timer();
00193 }
00195 double get_throughput() const
00196 {
00197 return throughput;
00198 }
00200 void reset_throughput()
00201 {
00202 throughput = 0;
00203 }
00205 void set_throughput(int x)
00206 {
00207 throughput += x;
00208 }
00209
00211 StatusType get_status() const
00212 {
00213 return status;
00214 }
00215
00217 bool isRunning() const
00218 {
00219 return status == Running || status == Paused;
00220 }
00221
00223 bool isReady() const
00224 {
00225 return status == Ready;
00226 }
00227
00229 bool isPaused() const
00230 {
00231 return status == Paused;
00232 }
00233
00235 bool isNotReady() const
00236 {
00237 return status == NotReady;
00238 }
00239
00241 bool isTimeout() const
00242 {
00243 return status == TimeOut;
00244 }
00245
00247 StatusType set_status(StatusType st) ;
00248
00256 void update(Observable *o, Argument *arg);
00257
00259 static std::string str_state[last_state];
00261 static std::string const &status_str(StatusType st)
00262 {
00263 return str_state[st];
00264 }
00265 private:
00267 std::string name;
00269 int ntrig;
00271 int nwriten;
00273 int throughput;
00275 StatusType status;
00277 GTimer timer;
00278
00280 int ntrg0;
00282 double tim0;
00283
00285 typedef std::set<RunStateAction *> ActionList;
00289 ActionList action_list;
00294 struct executeAction : public std::unary_function<RunStateAction *, void>
00295 {
00297 executeAction()
00298 {}
00300 void operator()( RunStateAction *action )
00301 {
00302 action->execute();
00303 }
00304 };
00305 public:
00307 void addAction(RunStateAction *);
00309 void deleteAction(RunStateAction *);
00311 void clearActionList();
00313 void executeActionList();
00314 };
00315 }
00316
00317
00318 #endif