AAO DRAMA C++ Interface (Old style)
DRAMA C++, Depreciated, don't use for new code
dcpphandler.h
1/*
2 * Name: filter.m4
3 *
4 * Description:
5 * DOXYGEN Input file filter for DRAMA source files.
6 *
7 * This file must be complatible with both Solaris and GNU m4. All
8 * unused macros are deleted and this comment must end up as a C/C++
9 * compatible comment which is not picked up by DOXYGEN.
10 *
11 * Synopsis:
12 * DCF(func) => Produces a link to a DRAMA C source file func.html
13 * in ../routines.
14 * DDL(link,text) => Produce a link to ../link.html, specified link text.
15 *
16 * Language: m4 macro.
17 *
18 * Author: Tony Farrell, AAO.
19 *
20 * "@(#) $Id$"
21 *
22 * History:
23 * 10-Nov-2004 - TJF - Original Version.
24 */
25
26
27
28#ifndef dcpphandler_h
29#define dcpphandler_h
30/* d c p p h a n d l e r
31
32 * Module name:
33 dcpphandler.h
34
35 * Function:
36 A class that completly hides the details of action reschedule.
37
38 * Description:
39 When a DcppHandler object is installed for the current action,
40 it calls DitsPutObeyHandler supplying its own routine to handle
41 future reschedules of the action. The idea is that the various
42 handlers will be invoked when events occur. By returning
43 DcppReschedule, they indicate that reschedule should occur,
44 Otherwise they should return DcppFinished.
45
46 This file now uses DOXYGEN comments to generate the C++ web pages.
47 m4 macros of the form @htmlonly <a href="../routines/function.html">function()</a>@endhtmlonly are used in the comments
48 to refer to DRAMA C function documention (see DramaHtml/Makefile,
49 DramaHtml/doxygen.config and DramaHtml/filter.m4 for detials)
50
51 * Language:
52 C++
53
54 * Support: Tony Farrell, AAO
55
56 * Copyright (c) Anglo-Australian Telescope Board, 1995.
57 Not to be used for commercial purposes without AATB permission.
58
59 * @(#) $Id$
60
61 * History:
62 12-Oct-1995 - TJF - Original version. Taken from dcpp.h
63 25-Sep-1996 - TJF - Extensively modified to support multiple
64 threads.
65 09-Dec-1997 - TJF - Declare class with DPUBLICCLASS to allow
66 export from WIN32 DLL.
67 02-Feb-1998 - TJF - Declare ReschedHandler static function using
68 DSTATICMEM macro to support WIN32 DLL correctly.
69 23-Nov-1998 - TJF - SetTimeout now does DitsPutDelay() to ensure
70 we can install and then to SetTimeout(). But
71 unsetting timeout does not work.
72 17-Nov-1999 - TJF - Add NewThread method. The increment operators
73 are now obsolete and will be removed.
74 19-Nov-1999 - TJF - Add ability to set a timeout handler.
75 12-Sep-2003 - TJF - Add new ThreadCount() function to help in debugging.
76 17-Sep-2003 - TJF - Use Dits___Log for logging and log handler being
77 installed.
78 26-Sep-2003 - TJF - GCC 3.x complains if printf style arguments for
79 the %p formater are not cast to void *. Fixed.
80 19-Jan-2004 - TJF - The external interface is now virtual and
81 all significant virtual functions are now moved
82 to dcpphandler.C.
83 19-Aug-2004 - MVV - Added a member data that is a pointer to
84 DcppCallbackBase type.
85 18-Oct-2004 - TJF - Include DitsFix.h - it is needed by IsUface() to get
86 DitsGetContext().
87 01-Dec-2004 - TJF - Improve Wait and WaitAll headers.
88
89 */
90#include "dcpp.h"
91#include "DitsFix.h"
92#include "dcppcallbackbase.h"
148DPUBLICCLASS DcppHandler {
149 private:
150 DcppHandlerRoutine finishedHandler; /* User supplied routine to invoke when messages complete*/
151 DcppHandlerRoutine errorHandler; /* User supplied routine to inovked on errors */
152 DcppHandlerRoutine timeoutHandler; /* User supplied routine to inovked on timeout */
153 DcppVoidPnt oldActData; /* actData is saved here */
154 DcppVoidPnt clientData; /* Client data item for handlers */
155 DitsDeltaTimeType delay; /* Timeout between reschedules */
156 bool timeoutEnabled; /* Is the timeout enabled */
157 StatusType errorStat; /* Status of first error in current sequence */
158 int threadCount; /* Number of outstanding threads */
159 int initThreads; /* When reinitialising threads, use this value */
160 int errorCount; /* Number of threads which completed in error */
161 bool errorMode; /* If true errorHandler is invoked for each */
162 /* thread on which an error occurs instead of */
163 /* when all threads have completed */
164 bool *destroyedPtr; /* IF non-null, then contains a pointer to
165 a bool which should be set true by the
166 destructor - allowing HandleReschedule()
167 to work out if the handler was destroyed
168 whilst it was run
169 */
170
171 DcppCallbackBase* callbackObj;
172
173 void ErrorHandler(StatusType * status);
174 void TimeoutHandler(StatusType * status);
175 void FinishedHandler(StatusType * status);
176
182 bool IsUface() const
183 {
184 return ((DitsGetContext() == DITS_CTX_UFACE) ? true : false);
185 }
186
187/*
188 * Prevent Assignment and copying of this type by redefining the
189 * assignment and copy operators to be private.
190 * This ensures we can't get multiple threads controlling the one
191 * task unless done intentionally by the caller.
192 *
193 * Disallow the default constructor.
194 */
195 DcppHandler& operator=(const DcppHandler&);
196 DcppHandler(const DcppHandler&);
197
198
199 public:
227 DcppHandler(const DcppHandlerRoutine FinishedHandler = 0,
228 const DcppHandlerRoutine ErrorHandler = 0,
229 const DcppVoidPnt ClientData = 0,
230 const float Timeout = -1);
231
256 DcppHandler(DcppCallbackBase* CallbackObj,
257 const float Timeout = -1);
258
259
273 virtual void Install(StatusType * status, int threads = 1);
274
283 virtual void DeInstall(StatusType * const status);
284
294 virtual void SetFinished(const DcppHandlerRoutine FinishedHandler) {
295 finishedHandler = FinishedHandler;
296 };
297
350 void Wait(DitsTransIdType transId, StatusType* status, double timeout = 0);
351
374 void WaitAll(StatusType* status, double timeout = 0);
375
382 virtual void SetError (const DcppHandlerRoutine ErrorHandler) {
383 errorHandler = ErrorHandler;
384 };
389 virtual void SetData (const DcppVoidPnt ClientData) {
390 clientData = ClientData;
391 };
400 virtual void SetTimeout(const float Timeout);
401
408 virtual void SetTimeout (const DcppHandlerRoutine TimeoutHandler,
409 const float Timeout) {
410 SetTimeout(Timeout);
411 timeoutHandler = TimeoutHandler;
412 };
418 virtual void SetMultiCallErrorMode(int enable = true) {
419 errorMode = enable;
420 }
423 virtual StatusType ErrorStatus() const { return errorStat; }
424
428 virtual DcppVoidPnt GetData() const { return clientData; }
429
432 virtual int ThreadCount() const { return threadCount; }
437 void operator++() { /* Preincrement operator */
438 if (TheTask(debug) & DITS_LOG_LIBS)
439 Dits___Log("DcppHd::++[pre]",
440 "Thread count incremented for %p from %d to %d",
441 (void *)this, threadCount, threadCount+1);
442 ++threadCount;
443 }
448 void operator++(int) { /* Post increment operator */
449 if (TheTask(debug) & DITS_LOG_LIBS)
450 Dits___Log("DcppHd::++[post]",
451 "Thread count incremented for %p from %d to %d",
452 (void *)this, threadCount, threadCount+1);
453 ++threadCount;
454 }
462 virtual void NewThread() {
463 if (TheTask(debug) & DITS_LOG_LIBS)
464 Dits___Log("DcppHd::NewThread",
465 "Thread count incremented for %p from %d to %d",
466 (void *)this, threadCount, threadCount+1);
467
468 ++threadCount;
469 }
470
473 virtual ~DcppHandler();
474
478 void HandleReschedule(StatusType * status);
479
483 void SetupTimeout(const float Timeout);
484
485};
486
487
488#endif
void Wait(DitsTransIdType transId, StatusType *status, double timeout=0)
Wait for the completion of the action supplied with the transaction id.
virtual StatusType ErrorStatus() const
Return the status associated with the first error event.
Definition dcpphandler.h:423
virtual void Install(StatusType *status, int threads=1)
Install this handler object for the current action.
virtual void SetFinished(const DcppHandlerRoutine FinishedHandler)
Change the finished handler routine.
Definition dcpphandler.h:294
void HandleReschedule(StatusType *status)
Used internally to handle a reschedule event.
virtual int ThreadCount() const
Return the number messages being controlled at the moment.
Definition dcpphandler.h:432
virtual void SetTimeout(const float Timeout)
Change the timeout.
void SetupTimeout(const float Timeout)
Used internally.
DcppHandler(DcppCallbackBase *CallbackObj, const float Timeout=-1)
Construct a DcppHandler object.
virtual void SetError(const DcppHandlerRoutine ErrorHandler)
Change the error handler routine.
Definition dcpphandler.h:382
void operator++()
Increase the thread count by one.
Definition dcpphandler.h:437
void WaitAll(StatusType *status, double timeout=0)
Wait for all outstanding messages to complete.
void operator++(int)
Increase the thread count by one.
Definition dcpphandler.h:448
virtual void SetMultiCallErrorMode(int enable=true)
Set the multi error call mode.
Definition dcpphandler.h:418
virtual void SetData(const DcppVoidPnt ClientData)
Change the handler routine client data item.
Definition dcpphandler.h:389
virtual void DeInstall(StatusType *const status)
Remove this handler.
virtual void SetTimeout(const DcppHandlerRoutine TimeoutHandler, const float Timeout)
Set a timeout handler routine and new timeout.
Definition dcpphandler.h:408
virtual void NewThread()
Increase the thread count by one.
Definition dcpphandler.h:462
virtual DcppVoidPnt GetData() const
Return the client data value.
Definition dcpphandler.h:428
virtual ~DcppHandler()
Destructor.
DcppHandler(const DcppHandlerRoutine FinishedHandler=0, const DcppHandlerRoutine ErrorHandler=0, const DcppVoidPnt ClientData=0, const float Timeout=-1)
Construct a DcppHandler object.
A DRAMA Action reschedule handler used in conjunction with DcppTask.
Definition dcpphandler.h:148
void * DcppVoidPnt
A pointer to void
Definition dcpp.h:59
DcppHandlerRet(* DcppHandlerRoutine)(DcppVoidPnt ClientData, StatusType *status)
Type for call backs when messages arrive.
Definition dcpp.h:92