AAO DRAMA/DRAMA2 C++ Interface
DRAMA C++11 and later interface
threadmonitor.hh
Go to the documentation of this file.
1#ifndef _DRAMA2_THREADMONITOR_INC
2#define _DRAMA2_THREADMONITOR_INC
23/*
24 * History:
25 08-May-2014 - TJF - Original version
26 05-Mar-2015 - TJF - Revamp to support canceling.
27 23-Oct-2018 - TJF - Add new Monitor constructor which takes a vector
28 of strings. (As you can initialize a vector
29 from an initialize list, but if you already
30 have a vector, you can't initialize it with
31 a vector.)
32
33 * The above ID is for Doxygen, this one has the format ACMM is looking for.
34 * "@(#) $Id$"
35 */
37#include "drama/thread.hh"
38#include "drama/path.hh"
39#include "Sdp.h"
40#include <vector>
41#include <string>
42namespace drama {
43
44 namespace thread {
49 class Monitor;
74 class MonitorMessageHandler : public MessageEventHandler {
75 private :
76 Monitor &_theMonitor;
77
78 public:
79 MonitorMessageHandler(Monitor &theMon) : _theMonitor(theMon) {}
89 void TriggerReceived(thread::ProcessInfo messInfo,
90 StatusType status, const sds::IdPtr &arg) override final;
91
109 virtual bool KickReceived(thread::ProcessInfo messInfo,
110 const sds::IdPtr &arg) override;
111
112 }; // class MonitorMessageHandler
113
114
140 class Monitor {
141 private:
142 /*
143 * Pointer to the DRAMA task we are part of.
144 */
145 std::weak_ptr<Task> _theTask;
146
147 Path *_thePath = nullptr;
148 thread::TMessHandler *_action = nullptr;
149
150 std::vector <std::string> _params;// Parameters being monitored.
151 int _monId; // The monitor ID
152 bool _running; // Are we running (may be starting)
153
154 bool _canceling; // Are we canceling
155 std::future<void> _cancelFuture; // Future used for cancel.
156
157 /*
158 * Send the message to cancel the monitor. Executed
159 * as a thread by the Cancel() method.
160 */
161 void SendCancel();
162
163
164 public:
165
179 Monitor(std::weak_ptr<Task> theTask,
180 std::initializer_list<std::string> pars):
181 _theTask(theTask),
182 _thePath(nullptr), _action(nullptr),
183 _params{pars}, _monId(-1), _running(false),_canceling(false) {
184
185 }
186
200 Monitor(std::weak_ptr<Task> theTask,
201 std::vector<std::string> pars):
202 _theTask(theTask),
203 _thePath(nullptr), _action(nullptr),
204 _params{pars}, _monId(-1), _running(false),_canceling(false) {
205
207
227 template <typename EventProcType = MonitorMessageHandler>
229
230 static_assert(
231 std::is_base_of<MonitorMessageHandler,
232 EventProcType>(),
233 "EventProcType must be a sub-class of drama::thread::MonitorMessageHandler");
234
235
236
237 EventProcType handler(*this);
238
239
241 }
242
243
259 void Run(Path *thePath,
262
263
268 Monitor& operator=(const Monitor &rhs) = delete;
273 Monitor(const Monitor &source) = delete;
274
275
276 /* Invoked by a helper class when a monitor is started.
277 *
278 * Not intended for use by user code.
279 */
280 void _StartedEvent(const sds::IdPtr &arg);
281
282 /* Invoked by a helper class when a monitor is started.
283 *
284 * Not intended for use by user code.
285 */
286 void _ChangedEvent(const sds::IdPtr &arg);
287
302 void Cancel();
303
307 virtual ~Monitor() {}
308
313 bool IsRunning() const {
314 return _running;
315 }
320 bool IsCancelling() const {
321 return _canceling;
322 }
334 int GetMonitorId() const {
335 return _monId;
336 }
337
338 protected:
355 virtual void ParameterChanged(const sds::IdPtr &arg) = 0;
356
357
366 virtual std::shared_ptr<Task> GetTask() const {
367 return std::shared_ptr<Task>(_theTask);
368 }
369
370 }; // class Monitor
371
388 class MonitorToParam : public Monitor {
389 using Monitor::Monitor; // Inherit constructor.
390 public:
394 virtual ~MonitorToParam();
395
409 virtual std::string Transform(const std::string &in);
410
411 protected:
426 virtual void ParameterChanged(const sds::IdPtr &arg) override final;
427
428 }; // class MonitorToParam
429
430
431
459 class MonitorByType : public Monitor {
460 using Monitor::Monitor; // Inherit constructor.
461 private:
462 // See below for details.
463 enum class RtnSupType { checkNeeded, yes, viaString, no };
464
465 /*
466 * We have a scheme for fallback from integer/unsigned integer/real
467 * to string. These enums are used to determine if we use that
468 * fallback. On the first parameter update of each time, a check
469 * is needed to confirm we have a user implemented routine. If not,
470 * then we will use the string routine, if it exists, converting the
471 * value to a string.
472 */
473 RtnSupType _intRoutineSupported = RtnSupType::checkNeeded;
474 RtnSupType _uintRoutineSupported = RtnSupType::checkNeeded;
475 RtnSupType _realRoutineSupported = RtnSupType::checkNeeded;
476
477 /*
478 * Implementation methods.
479 */
480 void DoIntParam(const std::string &name, const sds::IdPtr &arg);
481 void DoUintParam(const std::string &name, const sds::IdPtr &arg);
482 void DoRealParam(const std::string &name, const sds::IdPtr &arg);
483
484 public:
488 virtual ~MonitorByType();
489
490 protected:
505 virtual void ParameterChanged(const sds::IdPtr &arg) override final;
506
529 virtual void ParamChanged(const std::string &name, long value);
530
554 virtual void ParamChanged(const std::string &name, unsigned long value);
555
572 virtual void ParamChanged(const std::string &name, double value);
573
587 virtual void ParamChanged(const std::string &name, const std::string & value);
600 virtual void ParamChanged(const std::string &name, const sds::IdPtr &value);
601
602 }; // class MonitorByType
603
604
605
606 } // namespace thread
607} // namespace drama
608
609#endif
Message event handling objects.
Definition path.hh:194
A Class which provides access to DRAMA's message sending facilities.
Definition path.hh:689
virtual void ParamChanged(const std::string &name, const sds::IdPtr &value)
Method invoked when the value of complex parameter has been changed.
virtual void ParamChanged(const std::string &name, unsigned long value)
Method invoked when the value of an unsigned integer type parameter has been changed.
virtual ~MonitorByType()
Destructor.
virtual void ParameterChanged(const sds::IdPtr &arg) override final
Method invoked when a parameter is changed.
virtual void ParamChanged(const std::string &name, double value)
Method invoked when the value of a real type parameter has been changed.
virtual void ParamChanged(const std::string &name, const std::string &value)
Method invoked when the value of a string type parameter has been changed.
virtual void ParamChanged(const std::string &name, long value)
Method invoked when the value of a signed integer type parameter has been changed.
A class implementing Monitor which invokes particular methods for parameters of given base types.
Definition threadmonitor.hh:486
virtual bool KickReceived(thread::ProcessInfo messInfo, const sds::IdPtr &arg) override
Invoked if an action waiting on a monitor message receives a kick message during the wait.
void TriggerReceived(thread::ProcessInfo messInfo, StatusType status, const sds::IdPtr &arg) override final
Invoked if a trigger message is received.
Handle replies to DRAMA Monitor Messages.
Definition threadmonitor.hh:101
virtual void ParameterChanged(const sds::IdPtr &arg) override final
Method invoked when a parameter is changed.
virtual std::string Transform(const std::string &in)
Transform method, default implementation.
virtual ~MonitorToParam()
Destructor.
A class implementing Monitor which copies the value of a monitored parameter into one of the same nam...
Definition threadmonitor.hh:415
virtual ~Monitor()
Destructor.
Definition threadmonitor.hh:334
Monitor(std::weak_ptr< Task > theTask, std::initializer_list< std::string > pars)
Create a Monitor object.
Definition threadmonitor.hh:206
int GetMonitorId() const
Return the monitor ID.
Definition threadmonitor.hh:361
Monitor(const Monitor &source)=delete
Copy constructor - deleted.
void Run(Path *thePath, thread::TMessHandler *action, MonitorMessageHandler *evtProcessor)
Run a monitor operation - handler by address.
virtual std::shared_ptr< Task > GetTask() const
Get a reference to the task we are part of.
Definition threadmonitor.hh:393
void Cancel()
Cancel the monitor.
bool IsRunning() const
Indicate if the monitor is running.
Definition threadmonitor.hh:340
void Run(Path *thePath, thread::TMessHandler *action)
Run a monitor operation - handler by type.
Definition threadmonitor.hh:255
Monitor(std::weak_ptr< Task > theTask, std::vector< std::string > pars)
Create a Monitor object.
Definition threadmonitor.hh:227
Monitor & operator=(const Monitor &rhs)=delete
Assignment operator - deleted.
virtual void ParameterChanged(const sds::IdPtr &arg)=0
Abstract method invoked when a parameter is changed.
bool IsCancelling() const
Indicate if the monitor is being canceled.
Definition threadmonitor.hh:347
An abstract class which implements a DRAMA Parameter monitor in a thread.
Definition threadmonitor.hh:167
A class with information used by the TransEvtProcessor methods.
Definition thread.hh:147
This interface class must be implemented by classes which have threads waiting for messages.
Definition thread.hh:308
DRAMA 2 include file - Message Handler class definition.
std::shared_ptr< Id > IdPtr
A shared pointer for sds::Id items.
Definition sds.hh:3318
void CreateRunDramaTask()
Create and run a DRAMA task, with standard exception handling.
Definition task.hh:1322
The drama namespace contains all the classes, types etc of the DRAMA 2 implementation.
Definition drama.hh:93
DRAMA 2 include file.
DRAMA 2 include file - Code common to DRAMA 2 features supporting threading.