summaryrefslogtreecommitdiff
path: root/examples/Timer_Queue/Thread_Timer_Queue_Test.h
blob: cc1280a011fd0627724e6f5387cb27e17ce30550 (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
163
164
165
166
/* -*- C++ -*- */

// $Id$

// ============================================================================
//
// = LIBRARY
//    examples
// 
// = FILENAME
//    Thread_Timer_Queue_Test.h
//
// = DESCRIPTION
//   // @@ Please comment me
//
// = AUTHORS
//    Carlos O'Ryan <coryan@cs.wustl.edu> and 
//    Sergio Flores-Gaitan <sergio@cs.wustl.edu>
// 
// ============================================================================

#if !defined (_THREAD_TIMER_QUEUE_TEST_H_)
#define _THREAD_TIMER_QUEUE_TEST_H_

#include "ace/Task.h"
#include "ace/Timer_Heap_T.h"
#include "ace/Timer_Queue_Adapters.h"
#include "Driver.h"

// These typedefs ensure that we use the minimal amount of locking
// necessary.
typedef ACE_Event_Handler_Handle_Timeout_Upcall<ACE_Null_Mutex> 
	Upcall;
typedef ACE_Timer_Heap_T<ACE_Event_Handler *,
	                 ACE_Event_Handler_Handle_Timeout_Upcall<ACE_Null_Mutex>,
			 ACE_Null_Mutex>
	Timer_Heap;
typedef ACE_Timer_Heap_Iterator_T<ACE_Event_Handler *,
                                  ACE_Event_Handler_Handle_Timeout_Upcall<ACE_Null_Mutex>,
				  ACE_Null_Mutex> 
        Timer_Heap_Iterator;
typedef ACE_Thread_Timer_Queue_Adapter<Timer_Heap>
        Thread_Timer_Queue;

class Thread_Timer_Queue_Test_Driver;

class Input_Task : public ACE_Task<ACE_SYNCH>
  // = TITLE
  //   Read user actions on the Timer_Queue from stdin.
  //
  // = DESCRIPTION
  //   This class reads user input from stdin; those commands permit the
  //   control of a Timer_Queue, which is dispatched by another thread.
{
public:
  Input_Task (Thread_Timer_Queue *queue,
	      Thread_Timer_Queue_Test_Driver &timer_queue_driver);

  virtual int svc (void);
  // The method run on the new thread.

  // = Some helper methods.

  int add_timer (void *);
  // Add a new timer to expire in <seconds> more.

  int cancel_timer (void *);
  // Cancel timer <id>.

  int list_timer (void *);
  // List the current scheduled timers.

  int shutdown_timer (void *);
  // Shutdown task.

  void dump (void);
  // Dump the state of the timer queue.

private:
  Thread_Timer_Queue *queue_;
  // The timer queue implementation.

  const int usecs_;
  // How many micro seconds are in a second.

  Timer_Queue_Test_Driver<Thread_Timer_Queue, Input_Task> &driver_;
};

class Thread_Timer_Queue_Test_Driver : public Timer_Queue_Test_Driver <Thread_Timer_Queue, Input_Task>
// = TITLE
//
// = DESCRIPTION
{
public:
  // @@ Please don't put the definitions of methods in their declarations.
  Thread_Timer_Queue_Test_Driver (void)
    : input_task (&timer_queue_, *this)
    {}

  virtual int display_menu (void);
  virtual int init (void);
  virtual int run_test (void);

private:
  Input_Task input_task;
};

class Handler : public ACE_Event_Handler 
  // = TITLE
  //    This class implements a simple Event_Handler,
  //
  // = DESCRIPTION
  //     The <handle_timeout> hook method justs printouts the current
  //     time, delete this and prints the delay on the twhen it is
  //     expired. 
{
public:
  Handler (const ACE_Time_Value &expiration_time);
  ~Handler (void);

  void set_id (int id);
  // Store an "id" for the Handler, which is only use to print better
  // messages.

  virtual int handle_timeout (const ACE_Time_Value &current_time,
			      const void *arg);
  // Call back hook.
  virtual int cancelled (void);

private:
  ACE_Time_Value expires_;
  // Store the expected time of expiration, it is used to print a nice
  // message saying how much delay was at the actual expiration time.

  int id_;
  // Store an "id" for the Handler, which is only use to print better
  // messages.
};

#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
template class ACE_Thread_Timer_Queue_Adapter<Timer_Heap>;
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
#pragma instantiate ACE_Thread_Timer_Queue_Adapter<Timer_Heap>
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */

#if !defined(ACE_MT_SAFE)

// These templates will specialized in liACE.* if the platforms does
// not define ACE_MT_SAFE.

#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)

template class ACE_Event_Handler_Handle_Timeout_Upcall<ACE_Null_Mutex>;
template class ACE_Timer_Heap_T<ACE_Event_Handler *, ACE_Event_Handler_Handle_Timeout_Upcall<ACE_Null_Mutex>, ACE_Null_Mutex>;
template class ACE_Timer_Heap_Iterator_T<ACE_Event_Handler *, ACE_Event_Handler_Handle_Timeout_Upcall<ACE_Null_Mutex>, ACE_Null_Mutex>;

#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)

#pragma instantiate ACE_Event_Handler_Handle_Timeout_Upcall<ACE_Null_Mutex>
#pragma instantiate ACE_Timer_Heap_T<ACE_Event_Handler *, ACE_Event_Handler_Handle_Timeout_Upcall<ACE_Null_Mutex>, ACE_Null_Mutex>
#pragma instantiate ACE_Timer_Heap_Iterator_T<ACE_Event_Handler *, ACE_Event_Handler_Handle_Timeout_Upcall<ACE_Null_Mutex>, ACE_Null_Mutex>

#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */
#endif /* ACE_MT_SAFE */

#endif /* _THREAD_TIMER_QUEUE_TEST_H_ */