2 //=============================================================================
6 * This code builds an abstraction to factor out common code for
7 * the different implementations of the Timer_Queue.
9 * @author Douglas Schmidt <d.schmidt@vanderbilt.edu> && Sergio Flores-Gaitan <sergio@cs.wustl.edu>
11 //=============================================================================
14 #if !defined (_DRIVER_CPP_)
17 #include "ace/OS_NS_stdio.h"
18 #include "ace/OS_NS_string.h"
19 #include "ace/OS_NS_unistd.h"
24 template <class RECEIVER
, class ACTION
>
25 Command
<RECEIVER
, ACTION
>::Command (RECEIVER
&recvr
,
33 template <class RECEIVER
, class ACTION
>
34 Command
<RECEIVER
, ACTION
>::~Command ()
38 // invokes an operation.
40 template <class RECEIVER
, class ACTION
> int
41 Command
<RECEIVER
, ACTION
>::execute (void *arg
)
43 return (receiver_
.*action_
) (arg
);
46 // gets the next request from the user input.
48 template <class TQ
, class RECEIVER
, class ACTION
>
49 Timer_Queue_Test_Driver
<TQ
, RECEIVER
, ACTION
>::~Timer_Queue_Test_Driver ()
53 template <class TQ
, class RECEIVER
, class ACTION
> int
54 Timer_Queue_Test_Driver
<TQ
, RECEIVER
, ACTION
>::get_next_request ()
58 this->display_menu ();
60 ACE_OS::printf ("please enter your choice: ");
61 ACE_OS::fflush (stdout
);
63 // reads input from the user
64 if (this->read_input (buf
, sizeof buf
) <= 0)
67 // Parse and run the command.
68 return this->parse_commands (buf
);
73 template <class TQ
, class RECEIVER
, class ACTION
> int
74 Timer_Queue_Test_Driver
<TQ
, RECEIVER
, ACTION
>::run_test ()
79 if (this->get_next_request () == -1)
82 ACE_NOTREACHED (return 0);
85 // Reads input from the user from ACE_STDIN into the buffer specified.
87 template <class TQ
, class RECEIVER
, class ACTION
> ssize_t
88 Timer_Queue_Test_Driver
<TQ
, RECEIVER
, ACTION
>::read_input (char *buf
, size_t bufsiz
)
90 ACE_OS::memset (buf
, 0, bufsiz
);
92 // Wait for user to type commands. This call is automatically
93 // restarted when SIGINT or SIGALRM signals occur.
94 return ACE_OS::read (ACE_STDIN
, buf
, bufsiz
);
97 // Parse the input and executes the corresponding operation
99 template <class TQ
, class RECEIVER
, class ACTION
> int
100 Timer_Queue_Test_Driver
<TQ
, RECEIVER
, ACTION
>::parse_commands (const char *buf
)
104 if (::sscanf (buf
, "%d", &option
) <= 0)
105 // If there was an error reading the option simply try on the next line.
110 case 1: // Schedule a new timer.
113 // We just reread the option, this simplies parsing (since
114 // sscanf can do it for us.)
115 if (::sscanf (buf
, "%d %lu", &option
, &useconds
) < 2)
118 if (schedule_cmd_
->execute ((void *) &useconds
) == -1)
119 ACE_ERROR_RETURN ((LM_ERROR
, "%t %p\n", "new timer failed"), -1);
121 break; // Cancel an existing timer.
126 // We just reread the option, this simplies parsing (since
127 // sscanf can do it for us.)
128 if (::sscanf (buf
, "%d %lu", &option
, &id
) < 2)
131 if (cancel_cmd_
->execute ((void *) &id
) == -1)
132 ACE_DEBUG ((LM_DEBUG
, "Timer #%d is not valid\n", id
));
137 case 3: // Dump the existing timers.
138 return list_cmd_
->execute (0);
141 case 4: // Exit the program.
142 return shutdown_cmd_
->execute (0);
146 // Display an error message.
147 ACE_ERROR_RETURN ((LM_ERROR
, "invalid input %s\n", buf
), 0);
148 ACE_NOTREACHED (break);
154 #endif /* _DRIVER_CPP_ */