AAO DRAMA/DRAMA2 C++ Interface
DRAMA C++11 and later interface
action.hh
1#ifndef _DRAMA2_ACTION_INC
2#define _DRAMA2_ACTION_INC
3
4/*
5 *+ a c t i o n . h h
6
7 * Include File name:
8 drama/action.hh
9
10 * Function:
11 DRAMA 2 include file - Action Implementation class definition.
12
13 * Description:
14 The drama::Action class is used to manage the implementation of
15 actions in the Drama2 system.
16
17 Internal support class - not to be used by user code.
18
19 * Language:
20 C++
21
22 * Support: Tony Farrell, AAO
23
24 *-
25
26 * "@(#) $Id$"
27
28 * History:
29 31-Jan-2014 - TJF - Original version
30
31 * Copyright (c) Australian Astronomical Observatory, 2014.
32 Not to be used for commercial purposes without permission.
33 No warranty of any form unless subject to a specific agreement.
34
35 */
36#include "drama/task.hh"
37#include "drama/action.hh"
39//#include "util/heaptracked.hh"
40#include "drama/entryinfo.hh"
41#include "drama/request.hh"
42#include "status.h"
43#include <memory>
44namespace drama {
45
46 class Task; // Needed by Action below.
47 class Action;
48 class ActionHandler;
49 class Spawnable;
50
57 using ActionHandlerPtr = std::shared_ptr<ActionHandler> ;
58
67 class ActionHandler {
68 friend class drama::Action; // Action is allow to call our private methods.
69 friend class drama::Spawnable;
70 private:
71
72 /* MessageEvent is invoked when a message is received. Must invoke
73 * the sub-class's method.
74 */
75 virtual Request MessageEvent(std::weak_ptr<Task> task, Action *action) = 0;
76 /* This is invoked when the action ends. Invokes the user overideable
77 * method ActionEnd().
78 */
79 virtual void InvokeActionEnd(std::weak_ptr<Task> task, Action *action,
80 bool taskExiting,
82
83 /*
84 * The action we are part of.
85 */
86 Action *_actionPtr = nullptr;
87 public:
88
89
90 /*
91 * Destructor.
92 */
93 virtual ~ActionHandler();
102 void SetAction( Action *action);
103
104 };
105
106
115 class Action {
116 private:
117
118 // We store the valid ID_CODE in every object we create. This
119 // is to allow us to check in static handlers invoked by DRAMA
120 // code that we have the right type of object. Not a perfect thing,
121 // but helps find issues.
122 static const long int ID_CODE = 0x2398434;
123 const long int _checkIdCode = ID_CODE;
124
125 friend class drama::Task; // Task is allow to call some functions here.
126 friend class drama::Spawnable; // Spawnable is allow to create these.
127
128 /*
129 * Pointer to the DRAMA task this action is part of.
130 */
131 std::weak_ptr<Task> _theTask;
132 /*
133 * Name of this action. Never changed, so is const.
134 */
135 const std::string _actionName;
136
137 /* Non-static obey, kick and action end handlers */
138 virtual void ObeyHandler(StatusType *status);
139 virtual void KickHandler(StatusType *status);
140
141 /*
142 * ActionHandlerPtr is typedef-ed to "std::shared_ptr<ActionHandler>".
143 */
144
145 /*
146 * Object specified by the user to be invoked to implement the action.
147 */
148 ActionHandlerPtr _origActionHandler=nullptr;
149 /*
150 * Object specified by the user to implement the next Obey reschedule event.
151 * Can be changed.
152 */
153 ActionHandlerPtr _currentObeyHandler=nullptr;
154 /*
155 * Object specified by the user to implement the next Kick event.
156 * Can be changed.
157 */
158 ActionHandlerPtr _currentKickHandler=nullptr;
159
160 /*
161 * We need to know if we are currently in an obey or kick
162 */
163 bool _runningAction=false;
164
165 // *************
166 // Methods that drama::Task is allowed to access.
167
168
169
170
171 // Static routines invoked by drama::Task for Obey and Kick messages.
172 // DitsGetCode() should point to the object.
173 static void ObeyHandlerStatic(StatusType *status);
174 static void KickHandlerStatic(StatusType *status);
175
176
177 // Invoked when the action object is deleted as the task shuts down.
178 static void Cleanup(long int code, StatusType * const status);
179
180
181 // Invoked when the action ends.
182 static void ActEndHandlerStatic(DVOIDP client_data,
183 int taskExit,
186
187
188 // Invoke the obey method.
189 Request InvokeCurrentObey();
190
191 protected:
196 virtual ~Action();
197
217 Action(ActionHandlerPtr obj, std::weak_ptr<Task> task, const std::string &name) :
218 _theTask(task),
219 _actionName(name),
220 _origActionHandler(obj),
221 _currentObeyHandler(obj)
222 {
223 obj->SetAction(this);
224 }
225
226 // End of methods drama::Task is allowed to access
227 // *************
228
229 virtual void ActEndHandler(bool taskExit,
232
233 public:
239 void PutObeyHandler(ActionHandlerPtr obj) {
240 if (!_runningAction)
242 "Tried to change Obey handler when not in action.");
243 _currentObeyHandler = obj;
244 }
249 void PutKickHandler(ActionHandlerPtr obj) {
250 if (!_runningAction)
252 "Tried to change Kick handler when not in action.");
253 _currentKickHandler = obj;
254 }
255
256
257 /*
258 * Used by ~ActionHandler to indicate the action handler object
259 * has been destroyed.
260 */
261 void _HandlerGone() {
262#if 0
263 fprintf(stderr,"Action:_HandlerGone - %p\n", (void *)(this));
264#endif
265 _origActionHandler = nullptr;
266 }
267
268 /*
269 * Get Name
270 */
271 std::string GetActionName() const {
272 return _actionName;
273 }
274 };
275
276
277} // namespace drama
278
279#endif
A class which implements a Spawnable DRAMA Message Handler.
Definition spawnable.hh:84
A class which implements a DRAMA task.
Definition task.hh:441
DRAMA 2 include file - implements a class providing access to details on an action entry.
#define DramaTHROW(status_, message_)
Throw a Drama exception.
Definition exception.hh:93
DRAMA 2 include file - Message Handler class definition.
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 - Request class definition.
DRAMA 2 include file - Task class definition.