Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / ACE / examples / Bounded_Packet_Relay / BPR_Drivers_T.cpp
blobf4daee108504e1397211fdee6ca081ee64ac8966
2 //=============================================================================
3 /**
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"
24 // Constructor.
26 template <class TQ>
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),
36 shutdown_cmd_ (0),
37 packet_count_ (1000),
38 arrival_period_ (10000),
39 send_period_ (10000),
40 duration_limit_ (20000000),
41 logging_level_ (0)
45 // Destructor.
47 template <class TQ>
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_;
59 delete shutdown_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)
67 int option;
69 if (::sscanf (buf, "%d", &option) <= 0)
70 // If there was an error reading the option simply try on the next
71 // line.
72 return 0;
74 switch (option)
76 case 1: // set packet count
78 u_long 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.
85 return 0;
86 if (packet_count_cmd_->execute ((void *) &count) == -1)
87 ACE_ERROR_RETURN ((LM_ERROR,
88 "%t %p\n",
89 "set packet count failed"),
90 -1);
91 break;
93 case 2: // Set the arrival period.
95 u_long usec;
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.
102 return 0;
103 if (arrival_period_cmd_->execute ((void *) &usec) == -1)
104 ACE_ERROR_RETURN ((LM_ERROR,
105 "%t %p\n",
106 "set arrival period failed"),
107 -1);
108 break;
110 case 3: // Set transmit period.
112 u_long usec;
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.
119 return 0;
120 if (transmit_period_cmd_->execute ((void *) &usec) == -1)
121 ACE_ERROR_RETURN ((LM_ERROR,
122 "%t %p\n",
123 "set transmit period failed"),
124 -1);
125 break;
127 case 4: // Set duration limit.
129 u_long usec;
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.
136 return 0;
137 if (duration_limit_cmd_->execute ((void *) &usec) == -1)
138 ACE_ERROR_RETURN ((LM_ERROR,
139 "%t %p\n",
140 "\nSet duration limit failed."),
141 -1);
142 break;
144 case 5: // Set logging level.
146 int 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.
155 return 0;
158 if (logging_level_cmd_->execute ((void *) &level) == -1)
159 ACE_ERROR_RETURN ((LM_ERROR,
160 "%t %p\n",
161 "set logging level failed"),
162 -1);
163 break;
165 case 6: // Run one transmission.
166 return run_transmission_cmd_->execute (0);
167 /* NOTREACHED */
168 case 7: // Cancel current transmission.
169 return cancel_transmission_cmd_->execute (0);
170 /* NOTREACHED */
171 case 8: // Report statistics.
172 return report_stats_cmd_->execute (0);
173 /* NOTREACHED */
174 case 9: // Shut down the driver.
175 return shutdown_cmd_->execute (0);
176 /* NOTREACHED */
177 default:
178 // Display an error message.
179 ACE_ERROR_RETURN ((LM_ERROR, "invalid input %s\n", buf), 0);
180 ACE_NOTREACHED (break);
181 /* NOTREACHED */
182 } /* ENDSWITCH */
183 return 0;
186 // Runs the test.
188 template <class TQ> int
189 Bounded_Packet_Relay_Driver<TQ>::run ()
191 this->init ();
193 // Process all the incoming events.
195 for (;;)
196 if (this->get_next_request () == -1)
197 return -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 ()
207 char buf[BUFSIZ];
209 this->display_menu ();
211 // Reads input from the user.
212 if (this->read_input (buf, sizeof buf) <= 0)
213 return -1;
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)
244 packet_count_ = 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 ()
268 return 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)
276 send_period_ = 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)
307 logging_level_ = ll;
309 #endif /* _BPR_DRIVER_T_CPP_ */