2 //=============================================================================
4 * @file BPR_Drivers_T.cpp
6 * This code builds an abstraction to factor out common code for
7 * the different implementations of the Timer_Queue.
9 * @author Chris Gill <cdgill@cs.wustl.edu> and Douglas C. Schmidt <d.schmidt@vanderbilt.edu> Based on the Timer Queue Test example written by Carlos O'Ryan <coryan@cs.wustl.edu> and Douglas C. Schmidt <d.schmidt@vanderbilt.edu> and Sergio Flores-Gaitan <sergio@cs.wustl.edu>
11 //=============================================================================
14 #ifndef _BPR_DRIVER_T_CPP_
15 #define _BPR_DRIVER_T_CPP_
17 // #include BPR_Drivers.h instead of BPR_Drivers_T.h
18 // to avoid problems with circular includes
19 #include "BPR_Drivers.h"
20 #include "ace/OS_NS_string.h"
21 #include "ace/OS_NS_unistd.h"
27 Bounded_Packet_Relay_Driver
<TQ
>::Bounded_Packet_Relay_Driver ()
28 : packet_count_cmd_ (0),
29 arrival_period_cmd_ (0),
30 transmit_period_cmd_ (0),
31 duration_limit_cmd_ (0),
32 logging_level_cmd_ (0),
33 run_transmission_cmd_ (0),
34 cancel_transmission_cmd_ (0),
35 report_stats_cmd_ (0),
38 arrival_period_ (10000),
40 duration_limit_ (20000000),
48 Bounded_Packet_Relay_Driver
<TQ
>::~Bounded_Packet_Relay_Driver ()
50 // delete all instantiated command objects
51 delete packet_count_cmd_
;
52 delete arrival_period_cmd_
;
53 delete transmit_period_cmd_
;
54 delete duration_limit_cmd_
;
55 delete logging_level_cmd_
;
56 delete run_transmission_cmd_
;
57 delete cancel_transmission_cmd_
;
58 delete report_stats_cmd_
;
62 // Parse the input and execute the corresponding command.
64 template <class TQ
> int
65 Bounded_Packet_Relay_Driver
<TQ
>::parse_commands (const char *buf
)
69 if (::sscanf (buf
, "%d", &option
) <= 0)
70 // If there was an error reading the option simply try on the next
76 case 1: // set packet count
80 // We just reread the option, this simplies parsing (since
81 // sscanf can do it for us).
82 if (::sscanf (buf
, "%d %lu", &option
, &count
) < 2)
83 // If there was not enough information on the line, ignore
84 // option and try the next line.
86 if (packet_count_cmd_
->execute ((void *) &count
) == -1)
87 ACE_ERROR_RETURN ((LM_ERROR
,
89 "set packet count failed"),
93 case 2: // Set the arrival period.
97 // We just reread the option, this simplies parsing (since
98 // sscanf can do it for us).
99 if (::sscanf (buf
, "%d %lu", &option
, &usec
) < 2)
100 // If there was not enough information on the line, ignore
101 // option and try the next line.
103 if (arrival_period_cmd_
->execute ((void *) &usec
) == -1)
104 ACE_ERROR_RETURN ((LM_ERROR
,
106 "set arrival period failed"),
110 case 3: // Set transmit period.
114 // We just reread the option, this simplies parsing (since
115 // sscanf can do it for us).
116 if (::sscanf (buf
, "%d %lu", &option
, &usec
) < 2)
117 // If there was not enough information on the line, ignore
118 // option and try the next line.
120 if (transmit_period_cmd_
->execute ((void *) &usec
) == -1)
121 ACE_ERROR_RETURN ((LM_ERROR
,
123 "set transmit period failed"),
127 case 4: // Set duration limit.
131 // We just reread the option, this simplies parsing (since
132 // sscanf can do it for us).
133 if (::sscanf (buf
, "%d %lu", &option
, &usec
) < 2)
134 // If there was not enough information on the line, ignore
135 // option and try the next line.
137 if (duration_limit_cmd_
->execute ((void *) &usec
) == -1)
138 ACE_ERROR_RETURN ((LM_ERROR
,
140 "\nSet duration limit failed."),
144 case 5: // Set logging level.
148 // We just reread the option, this simplies parsing (since
149 // sscanf can do it for us).
150 if ((::sscanf (buf
, "%d %d", &option
, &level
) < 2) ||
151 (level
< 0) || (level
> 7))
153 // If there was not enough information on the line, or the
154 // passed value was invalid, ignore and try again.
158 if (logging_level_cmd_
->execute ((void *) &level
) == -1)
159 ACE_ERROR_RETURN ((LM_ERROR
,
161 "set logging level failed"),
165 case 6: // Run one transmission.
166 return run_transmission_cmd_
->execute (0);
168 case 7: // Cancel current transmission.
169 return cancel_transmission_cmd_
->execute (0);
171 case 8: // Report statistics.
172 return report_stats_cmd_
->execute (0);
174 case 9: // Shut down the driver.
175 return shutdown_cmd_
->execute (0);
178 // Display an error message.
179 ACE_ERROR_RETURN ((LM_ERROR
, "invalid input %s\n", buf
), 0);
180 ACE_NOTREACHED (break);
188 template <class TQ
> int
189 Bounded_Packet_Relay_Driver
<TQ
>::run ()
193 // Process all the incoming events.
196 if (this->get_next_request () == -1)
199 ACE_NOTREACHED (return 0);
202 // Gets the next request from the user input.
204 template <class TQ
> int
205 Bounded_Packet_Relay_Driver
<TQ
>::get_next_request ()
209 this->display_menu ();
211 // Reads input from the user.
212 if (this->read_input (buf
, sizeof buf
) <= 0)
215 // Parse and run the command.
216 return this->parse_commands (buf
);
219 // Reads input from the user from ACE_STDIN into the buffer specified.
221 template <class TQ
> ssize_t
222 Bounded_Packet_Relay_Driver
<TQ
>::read_input (char *buf
, size_t bufsiz
)
224 ACE_OS::memset (buf
, 0, bufsiz
);
226 // Wait for user to type commands. This call is automatically
227 // restarted when SIGINT or SIGALRM signals occur.
228 return ACE_OS::read (ACE_STDIN
, buf
, bufsiz
);
231 // Get count of packets to send in a transmission.
233 template <class TQ
> u_long
234 Bounded_Packet_Relay_Driver
<TQ
>::packet_count ()
236 return packet_count_
;
239 // Set count of packets to send in a transmission.
241 template <class TQ
> void
242 Bounded_Packet_Relay_Driver
<TQ
>::packet_count (u_long pc
)
247 // Get rate at which input packets are to arrive.
249 template <class TQ
> u_long
250 Bounded_Packet_Relay_Driver
<TQ
>::arrival_period ()
252 return arrival_period_
;
255 // Set rate at which input packets are to arrive.
257 template <class TQ
> void
258 Bounded_Packet_Relay_Driver
<TQ
>::arrival_period (u_long ap
)
260 arrival_period_
= ap
;
263 // Get rate at which packets are to be relayed (usec).
265 template <class TQ
> u_long
266 Bounded_Packet_Relay_Driver
<TQ
>::send_period ()
271 // Set rate at which packets are to be relayed (usec).
273 template <class TQ
> void
274 Bounded_Packet_Relay_Driver
<TQ
>::send_period (u_long sp
)
279 // Get limit on the duration of the transmission (usec).
281 template <class TQ
> u_long
282 Bounded_Packet_Relay_Driver
<TQ
>::duration_limit ()
284 return duration_limit_
;
287 // Set limit on the duration of the transmission (usec).
289 template <class TQ
> void
290 Bounded_Packet_Relay_Driver
<TQ
>::duration_limit (u_long dl
)
292 duration_limit_
= dl
;
294 // Get logging level.
296 template <class TQ
> int
297 Bounded_Packet_Relay_Driver
<TQ
>::logging_level ()
299 return logging_level_
;
302 // Set logging level.
304 template <class TQ
> void
305 Bounded_Packet_Relay_Driver
<TQ
>::logging_level (int ll
)
309 #endif /* _BPR_DRIVER_T_CPP_ */