summaryrefslogtreecommitdiff
path: root/examples/Timer_Queue/Reactor_Timer_Queue_Test.h
blob: 536732b52b86d36be8ccb0da53d9dbe9dfbf16b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/* -*- C++ -*- */


//=============================================================================
/**
 *  @file    Reactor_Timer_Queue_Test.h
 *
 *  $Id$
 *
 *  This code is an implementation of a test driver for a reactor based
 *  timer queue.
 *
 *
 *  @author Nanbor Wang <nw1@cs.wustl.edu> and Sergio Flores-Gaitan <sergio@cs.wustl.edu>
 */
//=============================================================================


#ifndef _REACTOR_TIMER_QUEUE_TEST_H_
#define _REACTOR_TIMER_QUEUE_TEST_H_

#include "Driver.h"

#if !defined (ACE_LACKS_PRAGMA_ONCE)
# pragma once
#endif /* ACE_LACKS_PRAGMA_ONCE */

/// @@todo: Not sure why this needs to be included. But am sure that,
/// there is some circular dependency setup. Needs to be
/// fixed. Atleast on g++
#include "ace/Timer_Queue.h"
#include "ace/Timer_Heap.h"
#include "ace/svc_export.h"

class Reactor_Timer_Queue_Test_Driver;

/**
 * @class Input_Handler
 *
 * @brief Implements the handler to be called for input events.  Also has
 * the logic to handle the different timer queue operations (i.e.,
 * schedule, cancel, list, shutdown).
 *
 * This class handles the reading of user input from stdin.  Also
 * has the logic to handle the commands that are to be invoked in
 * response to the user input.
 */
class Input_Handler : public ACE_Event_Handler
{
public:
  typedef int (Input_Handler::*ACTION) (void *);

  /// Sets <done_> flag to 0, <driver_> to <timer_queue_driver> and
  /// timer queue <tq_> to <tq>
  Input_Handler (ACE_Timer_Queue *tq,
                 Reactor_Timer_Queue_Test_Driver &timer_queue_driver);

  /// Hook method for the <ACE_Reactor> to call whenever there is input
  /// ready to be read.
  int handle_input (ACE_HANDLE);

  /**
   * returns the value for <done_> that indicates whether we are
   * exiting the program.A value of 0 indicates that we are NOT done,
   * 1 otherwise.
   */
  int done (void);

  // = Hook methods to be called from <Reactor_Timer_Queue_Test_Driver>

  /// Schedule a timer.  The (void *) will be mapped to the delay
  /// parameter for the timer queue schedule method.
  int schedule_timer (void *argument);

  /// Cancel a timer.  The (void *) will be mapped to the ID of the
  /// timer being cancelled.
  int cancel_timer (void *argument);

  /// Dump the timers in the queue.  The argument is ignored.
  int list_timer (void *argument);

  /// Processes the request to exit the timer queue application.
  /// argument is ignored.
  int shutdown_timer (void *argument);

private:
  /// Keep a pointer to the timer queue we are using so we can traverse
  /// the queue.
  ACE_Timer_Queue *tq_;

  /// Flag used to close down program.
  int done_;

  /// Test driver.  Used to call hook methods that are common code for
  /// all drivers.
  Reactor_Timer_Queue_Test_Driver &driver_;
};

/**
 * @class Reactor_Timer_Queue_Test_Driver
 *
 * @brief Implements a test driver for a reactive timer queue using
 * <ACE_Reactor>.
 *
 * This class implements the logic to test the reactor
 * implementation of timer queue, using an <ACE_Timer_Heap>.
 */
class ACE_Svc_Export Reactor_Timer_Queue_Test_Driver : public Timer_Queue_Test_Driver <ACE_Timer_Heap, Input_Handler, Input_Handler::ACTION>
{
public:
  /**
   * Sets the input handler <thandler_> with <timer_queue_> from the
   * <Timer_Queue_Test_Driver> class and a reference to "this", so the
   * input handler can call hook methods from the driver.  Such
   * methods are the common factored out code from other
   * implementations of timer queues.
   */
  Reactor_Timer_Queue_Test_Driver (void);

  /// Default destructor
  virtual ~Reactor_Timer_Queue_Test_Driver (void);

  /// Prints the menu of options.
  virtual int display_menu (void);

  /**
   * Sets the timer queue that the REACTOR will use; registers the
   * stdin input handler with the REACTOR and sets the <Command>s that
   * the <Timer_Queue_Test_Driver> will execute().
   */
  virtual int init (void);

  /// Main entry point to the test driver implementation.
  virtual int run_test (void);

private:
  /// This is the stdin handler.
  Input_Handler thandler_;
};

/**
 * @class Reactor_Timer_Handler
 *
 * @brief Target of the reactive timeout operation.
 */
class Reactor_Timer_Handler : public ACE_Event_Handler
{
public:
  /// Hook method that is called by the reactor when a timer expires.
  /// It prints the timer ID and the time it expired.
  virtual int handle_timeout (const ACE_Time_Value &tv,
                              const void *);

  /// Sets the timer id for this handler <tid_> to <tid>
  void set_timer_id (long tid);

private:
  /// timer ID.
  long tid_;
};

#endif /* _REACTOR_TIMER_QUEUE_TEST_H_ */