Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / examples / Timer_Queue / Driver.cpp
blob4663ffaa95fcdc35265ec33ba766b2fccd61ab30
2 //=============================================================================
3 /**
4 * @file Driver.cpp
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_)
15 #define _DRIVER_CPP_
17 #include "ace/OS_NS_stdio.h"
18 #include "ace/OS_NS_string.h"
19 #include "ace/OS_NS_unistd.h"
20 #include "Driver.h"
21 #include <memory>
23 // constructor
24 template <class RECEIVER, class ACTION>
25 Command<RECEIVER, ACTION>::Command (RECEIVER &recvr,
26 ACTION action)
27 : receiver_ (recvr),
28 action_ (action)
32 // destructor
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 ()
56 char buf[BUFSIZ];
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)
65 return -1;
67 // Parse and run the command.
68 return this->parse_commands (buf);
71 // Runs the test.
73 template <class TQ, class RECEIVER, class ACTION> int
74 Timer_Queue_Test_Driver<TQ, RECEIVER, ACTION>::run_test ()
76 this->init ();
78 for (;;)
79 if (this->get_next_request () == -1)
80 return -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)
102 int option;
104 if (::sscanf (buf, "%d", &option) <= 0)
105 // If there was an error reading the option simply try on the next line.
106 return 0;
108 switch (option)
110 case 1: // Schedule a new timer.
112 u_long useconds;
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)
116 return 0;
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.
122 /* NOTREACHED */
123 case 2:
125 u_long id;
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)
129 return 0;
131 if (cancel_cmd_->execute ((void *) &id) == -1)
132 ACE_DEBUG ((LM_DEBUG, "Timer #%d is not valid\n", id));
134 break;
135 /* NOTREACHED */
137 case 3: // Dump the existing timers.
138 return list_cmd_->execute (0);
139 /* NOTREACHED */
141 case 4: // Exit the program.
142 return shutdown_cmd_->execute (0);
143 /* NOTREACHED */
145 default:
146 // Display an error message.
147 ACE_ERROR_RETURN ((LM_ERROR, "invalid input %s\n", buf), 0);
148 ACE_NOTREACHED (break);
149 /* NOTREACHED */
151 return 0;
154 #endif /* _DRIVER_CPP_ */