diff options
author | sergio <sergio@ae88bc3d-4319-0410-8dbf-d08b4c9d3795> | 1997-10-20 02:13:26 +0000 |
---|---|---|
committer | sergio <sergio@ae88bc3d-4319-0410-8dbf-d08b4c9d3795> | 1997-10-20 02:13:26 +0000 |
commit | e99f4cf6541db2347f3d2e854c318d0119de82c1 (patch) | |
tree | c4161b05afc5a3faede3746e190f7566133f6295 /examples/Timer_Queue | |
parent | 1f737ed0433844e395d154890191e0fb75b24ae0 (diff) | |
download | ATCD-e99f4cf6541db2347f3d2e854c318d0119de82c1.tar.gz |
Added a driver class to factor out common code from other timer queue
implementations.
Diffstat (limited to 'examples/Timer_Queue')
-rw-r--r-- | examples/Timer_Queue/Async_Timer_Queue_Test.cpp | 44 | ||||
-rw-r--r-- | examples/Timer_Queue/Async_Timer_Queue_Test.h | 23 | ||||
-rw-r--r-- | examples/Timer_Queue/Driver.cpp | 33 | ||||
-rw-r--r-- | examples/Timer_Queue/Driver.h | 53 | ||||
-rw-r--r-- | examples/Timer_Queue/Makefile | 41 | ||||
-rw-r--r-- | examples/Timer_Queue/Reactor_Timer_Queue_Test.cpp | 25 | ||||
-rw-r--r-- | examples/Timer_Queue/Reactor_Timer_Queue_Test.h | 44 | ||||
-rw-r--r-- | examples/Timer_Queue/Thread_Timer_Queue_Test.cpp | 26 | ||||
-rw-r--r-- | examples/Timer_Queue/Thread_Timer_Queue_Test.h | 16 |
9 files changed, 227 insertions, 78 deletions
diff --git a/examples/Timer_Queue/Async_Timer_Queue_Test.cpp b/examples/Timer_Queue/Async_Timer_Queue_Test.cpp index 2d09a63128b..2367316da24 100644 --- a/examples/Timer_Queue/Async_Timer_Queue_Test.cpp +++ b/examples/Timer_Queue/Async_Timer_Queue_Test.cpp @@ -1,5 +1,22 @@ // $Id$ +// ============================================================================ +// +// = LIBRARY +// examples +// +// = FILENAME +// Async_Timer_Queue_Test.cpp +// +// = DESCRIPTION +// This test exercises the <ACE_Asynch_Timer_Queue_Adapter> +// using an <ACE_Timer_Heap>. +// +// = AUTHORS +// Douglas C. Schmidt and +// Sergio Flores-Gaitan +// ============================================================================ + #include "ace/Signal.h" #include "ace/Timer_Heap.h" #include "ace/Timer_Queue_Adapters.h" @@ -7,7 +24,7 @@ #include "Async_Timer_Queue_Test.h" int -Timer_Handler::handle_timeout (const ACE_Time_Value &tv, +Async_Timer_Handler::handle_timeout (const ACE_Time_Value &tv, const void *arg) { // Print some information here (note that this is not strictly @@ -80,7 +97,7 @@ Async_Timer_Queue::schedule (u_int microsecs) // Create a new Event_Handler for our timer. ACE_Event_Handler *eh; - ACE_NEW (eh, Timer_Handler); + ACE_NEW (eh, Async_Timer_Handler); // Schedule the timer to run in the future. long tid = this->tq_.schedule @@ -109,6 +126,8 @@ Async_Timer_Queue::cancel (long timer_id) delete (ACE_Event_Handler *) act; } +// Schedule timer hook method. This method is called from the driver. + int Async_Timer_Queue::schedule_timer (void *argument) { @@ -120,6 +139,8 @@ Async_Timer_Queue::schedule_timer (void *argument) return 0; } +// Cancel timer hook method. Is called from the driver class. + int Async_Timer_Queue::cancel_timer (void *argument) { @@ -131,6 +152,9 @@ Async_Timer_Queue::cancel_timer (void *argument) return 0; } +// Dummy list timer hook method. The listing of timers is done from +// a signal handler using SIGINT, not from the driver. + int Async_Timer_Queue::list_timer (void *argument) { @@ -138,6 +162,9 @@ Async_Timer_Queue::list_timer (void *argument) ACE_ERROR_RETURN ((LM_ERROR, "invalid input\n"), 0); } +// Dummy shutdown timer hook method. The shutdown of the timer queue +// is done with a signal handler using SIGQUIT, not from the driver. + int Async_Timer_Queue::shutdown_timer (void *argument) { @@ -191,10 +218,13 @@ register_signal_handlers (void) ACE_UNUSED_ARG (sigint); } +// constructor + Async_Timer_Queue_Test_Driver::Async_Timer_Queue_Test_Driver (void) - { - timer_queue_ = Async_Timer_Queue::instance (); - } +{ +} + +// displays the menu of options. int Async_Timer_Queue_Test_Driver::display_menu (void) @@ -212,10 +242,12 @@ Async_Timer_Queue_Test_Driver::display_menu (void) return 0; } +// Initializes the test driver. + int Async_Timer_Queue_Test_Driver::init (void) { - // initialize commands with their corresponding input_task methods. + // initialize <Command> objects with their corresponding <Input_Task> methods. ACE_NEW_RETURN (schedule_cmd_, Command<Async_Timer_Queue> (*Async_Timer_Queue::instance (), Async_Timer_Queue::instance ()->schedule_timer), diff --git a/examples/Timer_Queue/Async_Timer_Queue_Test.h b/examples/Timer_Queue/Async_Timer_Queue_Test.h index c09c45d4f4a..1705d8d3bf5 100644 --- a/examples/Timer_Queue/Async_Timer_Queue_Test.h +++ b/examples/Timer_Queue/Async_Timer_Queue_Test.h @@ -15,8 +15,8 @@ // using an <ACE_Timer_Heap>. // // = AUTHORS -// Douglas C. Schmidt -// +// Douglas C. Schmidt and +// Sergio Flores-Gaitan // ============================================================================ #if !defined (_ASYNC_TIMER_QUEUE_TEST_H_) @@ -28,7 +28,7 @@ #include "Driver.h" -class Timer_Handler : public ACE_Event_Handler +class Async_Timer_Handler : public ACE_Event_Handler // = TITLE // Target of the asynchronous timeout operation. { @@ -61,9 +61,16 @@ public: // Dump the contents of the queue. int schedule_timer (void *argument); + // hook method to schedule a timer. Called from <Timer_Queue_Test_Driver> + int cancel_timer (void *argument); + // hook method to cancel a timer. Called from <Timer_Queue_Test_Driver> + int list_timer (void *argument); + // hook method to list timers. Called from <Timer_Queue_Test_Driver> + int shutdown_timer (void *argument); + // hook method to exit the timer queue. Called from <Timer_Queue_Test_Driver> private: Async_Timer_Queue (ACE_Sig_Set *); @@ -78,13 +85,23 @@ private: class Async_Timer_Queue_Test_Driver : public Timer_Queue_Test_Driver <Async_Timer_Queue *, Async_Timer_Queue> // = TITLE +// Async_Timer_Queue_Test_Driver // // = DESCRIPTION +// This class implements a test driver for the <Async_Timer_Queue>. Implements +// a display_menu() method that prints the options for a user. and init() which +// initializes the driver. The rest of the common functionality is in the parent +// class <Timer_Queue_Test_Driver>. { public: Async_Timer_Queue_Test_Driver (void); + // Print menu of options. + virtual int display_menu (void); + + // Initializes the driver's internal variables inherited from the parent + virtual int init (void); }; diff --git a/examples/Timer_Queue/Driver.cpp b/examples/Timer_Queue/Driver.cpp index 2d828caf579..b6d719c7bf4 100644 --- a/examples/Timer_Queue/Driver.cpp +++ b/examples/Timer_Queue/Driver.cpp @@ -18,23 +18,46 @@ #include "ace/Auto_Ptr.h" #include "Driver.h" +// constructor + +template <class Receiver> +Command<Receiver>::Command (Receiver &recvr, + Action action) + : receiver_(recvr), + action_(action) +{} + +// invokes an operation. + +template <class Receiver> int +Command<Receiver>::execute (void *arg) +{ + return (receiver_.*action_) (arg); +} + + +// gets the next request from the user input. + template <class TQ, class Receiver> int Timer_Queue_Test_Driver<TQ, Receiver>::get_next_request (void) { - this->display_menu (); - char buf[BUFSIZ]; + this->display_menu (); + ACE_OS::printf ("please enter your choice: "); ACE_OS::fflush (stdout); + // reads input from the user if (this->read_input (buf, sizeof buf) <= 0) return -1; - // Run the command. + // Parse and run the command. return this->parse_commands (buf); } +// Runs the test. + template <class TQ, class Receiver> int Timer_Queue_Test_Driver<TQ, Receiver>::run_test (void) { @@ -47,6 +70,8 @@ Timer_Queue_Test_Driver<TQ, Receiver>::run_test (void) return 0; } +// Reads input from the user from ACE_STDIN into the buffer specified. + template <class TQ, class Receiver> ssize_t Timer_Queue_Test_Driver<TQ, Receiver>::read_input (char *buf, size_t bufsiz) { @@ -57,6 +82,8 @@ Timer_Queue_Test_Driver<TQ, Receiver>::read_input (char *buf, size_t bufsiz) return ACE_OS::read (ACE_STDIN, buf, bufsiz); } +// Parse the input and executes the corresponding operation + template <class TQ, class Receiver> int Timer_Queue_Test_Driver<TQ, Receiver>::parse_commands (const char *buf) { diff --git a/examples/Timer_Queue/Driver.h b/examples/Timer_Queue/Driver.h index 2e669a8b12b..b6e9d25a196 100644 --- a/examples/Timer_Queue/Driver.h +++ b/examples/Timer_Queue/Driver.h @@ -11,7 +11,8 @@ // Driver.h // // = DESCRIPTION -// // @@ Please comment me +// This code builds an abstraction to factor out common code for +// the different implementations of the Timer_Queue. // // = AUTHORS // Sergio Flores-Gaitan <sergio@cs.wustl.edu> @@ -40,50 +41,76 @@ typedef ACE_Thread_Timer_Queue_Adapter<Timer_Heap> template <class Receiver> class Command +// = TITLE +// Command +// +// = DESCRIPTION +// This class declares an interface to execute operations, binding a Receiver +// object with an Action. The Receiver knows how to implement the operation. +// A class can invoke operations without knowing anything about it, or how it +// was implemented.ue_Test_Driver>. { public: typedef int (Receiver::*Action) (void *); - Command (Receiver &recvr, Action action) - : receiver_(recvr), - action_(action) - {} + Command (Receiver &recvr, Action action); - virtual int execute (void *arg) - { - return (receiver_.*action_) (arg); - } + virtual int execute (void *arg); private: Receiver &receiver_; + // object where the method resides. + Action action_; + // method that is going to be invoked. }; template <class TQ, class Receiver> class Timer_Queue_Test_Driver +// = TITLE +// Timer_Queue_Test_Driver +// +// = DESCRIPTION +// This class implements a test driver for timer queues. +// This is the place where the common code to test the +// different implementations of the timer queue resides. +// This class has the logic for the parse_commands() method, +// the run_test(), read_input() and the get_next_request(). +// Subclasses can override these methods if there is some +// logic that is specific to that implementation. { public: virtual int parse_commands (const char *buf); virtual int run_test (void); + virtual int get_next_request (void); + + virtual ssize_t read_input (char *buf, size_t bufsiz); + // = Template Methods. virtual int display_menu (void)=0; virtual int init (void)=0; - virtual int get_next_request (void); - - virtual ssize_t read_input (char *buf, size_t bufsiz); - protected: TQ timer_queue_; + // timer queue + + // Set of <Command>s to be executed. Command<Receiver> *schedule_cmd_; + // schedule timer command + Command<Receiver> *cancel_cmd_; + // cancel timer command. + Command<Receiver> *list_cmd_; + // list timers command. + Command<Receiver> *shutdown_cmd_; + // shutdown the driver. }; diff --git a/examples/Timer_Queue/Makefile b/examples/Timer_Queue/Makefile index 58c6b5fc69f..f29de0447ed 100644 --- a/examples/Timer_Queue/Makefile +++ b/examples/Timer_Queue/Makefile @@ -6,25 +6,28 @@ # Local macros #---------------------------------------------------------------------------- -INFO = README - -BIN = main_async -FILES = Async_Timer_Queue_Test -LSRC = $(addsuffix .cpp,$(FILES)) -LOBJ = $(addsuffix .o,$(FILES)) -BUILD = $(VBIN) - -BIN = main_reactor -FILES = Reactor_Timer_Queue_Test -LSRC = $(addsuffix .cpp,$(FILES)) -LOBJ = $(addsuffix .o,$(FILES)) -BUILD = $(VBIN) - -BIN = main_thread -FILES = Thread_Timer_Queue_Test -LSRC = $(addsuffix .cpp,$(FILES)) -LOBJ = $(addsuffix .o,$(FILES)) -BUILD = $(VBIN) +INFO = README + +LIB = libTQTD.a +SHLIB = libTQTD.$(SOEXT) + +BIN = main_async \ + main_reactor \ + main_thread + +SRC = $(addsuffix .cpp,$(BIN)) +OBJ = $(SRC:%.cpp=$(VDIR)%.o) + +LSRC = Async_Timer_Queue_Test.cpp \ + Reactor_Timer_Queue_Test.cpp \ + Thread_Timer_Queue_Test.cpp \ + Driver.cpp + +LDLIBS = -lTQTD + +VLDLIBS = $(LDLIBS:%=%$(VAR)) + +BUILD = $(VLIB) $(VSHLIB) $(SHLIBA) $(VBIN) #---------------------------------------------------------------------------- # Include macros and targets diff --git a/examples/Timer_Queue/Reactor_Timer_Queue_Test.cpp b/examples/Timer_Queue/Reactor_Timer_Queue_Test.cpp index 02895f9944c..0e5819cfd74 100644 --- a/examples/Timer_Queue/Reactor_Timer_Queue_Test.cpp +++ b/examples/Timer_Queue/Reactor_Timer_Queue_Test.cpp @@ -11,7 +11,8 @@ // This example tests the timer queue mechanism of ACE_Reactor. // // = AUTHOR -// Nanbor Wang <nw1@cs.wustl.edu> +// Nanbor Wang <nw1@cs.wustl.edu> and +// Sergio Flores-Gaitan <sergio@cs.wustl.edu> // // ============================================================================ @@ -26,17 +27,19 @@ static const int NO_OF_IO_HANDLERS = 5; #define REACTOR ACE_Reactor::instance () -Timer_Handler::Timer_Handler (void) +// constructor + +Reactor_Timer_Handler::Reactor_Timer_Handler (void) {} void -Timer_Handler::set_timer_id (long tid) +Reactor_Timer_Handler::set_timer_id (long tid) { this->tid_ = tid; } int -Timer_Handler::handle_timeout (const ACE_Time_Value &tv, +Reactor_Timer_Handler::handle_timeout (const ACE_Time_Value &tv, const void *) { ACE_Time_Value txv = ACE_OS::gettimeofday (); @@ -64,10 +67,10 @@ int Input_Handler::schedule_timer (void *argument) { int delay = *(int *) argument; - Timer_Handler *th; + Reactor_Timer_Handler *th; long tid; - th = new Timer_Handler; + th = new Reactor_Timer_Handler; if (th != 0) { tid = this->reactor ()->schedule_timer (th, @@ -130,9 +133,9 @@ Input_Handler::handle_input (ACE_HANDLE) } Reactor_Timer_Queue_Test_Driver::Reactor_Timer_Queue_Test_Driver (void) - : thandler (&timer_queue_, *this) - { - } + : thandler (&timer_queue_, *this) +{ +} int Reactor_Timer_Queue_Test_Driver::display_menu (void) @@ -153,7 +156,7 @@ Reactor_Timer_Queue_Test_Driver::display_menu (void) int Reactor_Timer_Queue_Test_Driver::init (void) { - // initialize commands with their corresponding input_task methods. + // initialize <Command>s with their corresponding <Input_Handler> methods. ACE_NEW_RETURN (schedule_cmd_, Command<Input_Handler> (thandler, thandler.schedule_timer), @@ -182,6 +185,8 @@ Reactor_Timer_Queue_Test_Driver::init (void) this->display_menu (); } +// run test was overrun due to the reactive way of handling input. + int Reactor_Timer_Queue_Test_Driver::run_test (void) { diff --git a/examples/Timer_Queue/Reactor_Timer_Queue_Test.h b/examples/Timer_Queue/Reactor_Timer_Queue_Test.h index a3960c169c9..7447fe3255c 100644 --- a/examples/Timer_Queue/Reactor_Timer_Queue_Test.h +++ b/examples/Timer_Queue/Reactor_Timer_Queue_Test.h @@ -8,12 +8,14 @@ // examples // // = FILENAME -// Async_Timer_Queue_Test.h +// Reactor_Timer_Queue_Test.h // // = DESCRIPTION -// // @@ Please comment me +// This code is an implementation of a test driver for a reactor based +// timer queue. // // = AUTHORS +// Nanbor Wang <nw1@cs.wustl.edu> and // Sergio Flores-Gaitan <sergio@cs.wustl.edu> // // ============================================================================ @@ -27,9 +29,15 @@ class Reactor_Timer_Queue_Test_Driver; class Input_Handler : public ACE_Event_Handler -// @@ Please add comments. +// = TITLE +// Input_Handler +// +// = DESCRIPTION +// This class handles the reading of user input from stdin. Also +// has the logic to handle the commands that are to be invoked in response +// to the user input. { -public: + public: Input_Handler (ACE_Timer_Queue *tq, Reactor_Timer_Queue_Test_Driver &timer_queue_driver); int handle_input (ACE_HANDLE); @@ -39,42 +47,44 @@ public: int list_timer (void *argument); int shutdown_timer (void *argument); -private: + private: ACE_Timer_Queue *tq_; // Keep a pointer to the timer queue we are using so we can // traverse the queue. - + int done_; // Flag used to close down program. Reactor_Timer_Queue_Test_Driver &driver_; + // Test driver. }; class Reactor_Timer_Queue_Test_Driver : public Timer_Queue_Test_Driver <ACE_Timer_Heap, Input_Handler> -// @@ Please add comments. +// = TITLE +// Reactor_Timer_Queue_Test_Driver +// +// = DESCRIPTION +// This class implements the logic to test the reactor implementation of +// timer queue, using an <ACE_Timer_Heap>. { -public: + public: Reactor_Timer_Queue_Test_Driver (void); - + virtual int display_menu (void); virtual int init (void); virtual int run_test (void); private: - // pick a timer queue implementation - // which happens to be the one that ACE is using. - // ACE_Timer_Heap private_queue; - // This is the stdin handler. Input_Handler thandler; }; -class Timer_Handler : public ACE_Event_Handler -// @@ Please add comments. +class Reactor_Timer_Handler : public ACE_Event_Handler +// = TITLE +// Target of the reactive timeout operation. { public: - // Timer_Handler (Reactor_Queue_Test_Driver &driver); - Timer_Handler (void); + Reactor_Timer_Handler (void); virtual int handle_timeout (const ACE_Time_Value &tv, const void *); diff --git a/examples/Timer_Queue/Thread_Timer_Queue_Test.cpp b/examples/Timer_Queue/Thread_Timer_Queue_Test.cpp index 149ae7ad1a3..c3c277af147 100644 --- a/examples/Timer_Queue/Thread_Timer_Queue_Test.cpp +++ b/examples/Timer_Queue/Thread_Timer_Queue_Test.cpp @@ -78,18 +78,25 @@ Input_Task::Input_Task (Thread_Timer_Queue *queue, Thread_Timer_Queue_Test_Drive { } +// Svc method is called from the thread library to read input from the user. + int Input_Task::svc (void) { for (;;) + // call bacck to the driver's implementation on how to read and parse input. if (this->driver_.get_next_request () == -1) break; + // we are done. this->queue_->deactivate (); ACE_DEBUG ((LM_DEBUG, "terminating input thread\n")); return 0; } +// schedule a new timer. This method will be called from inside the +// <Timer_Queue_Test_Driver> class. (see Command pattern) + int Input_Task::add_timer (void *argument) { @@ -115,12 +122,19 @@ Input_Task::add_timer (void *argument) return 0; } +// Cancel a timer. This method will be called from inside the +// <Timer_Queue_Test_Driver> class. (see Command pattern) + int Input_Task::cancel_timer (void *argument) { return this->queue_->cancel (*(int *)argument); } +// lists the timers in the queue. Ignores the argument. This +// method will be called from inside the <Timer_Queue_Test_Driver> +// class. (see Command pattern) + int Input_Task::list_timer (void *argument) { @@ -128,6 +142,9 @@ Input_Task::list_timer (void *argument) return 0; } +// Shutdown the timer queue. Return -1 indicates to the +// <Timer_Queue_Test_Driver> class that we are done. + int Input_Task::shutdown_timer (void *argument) { @@ -149,6 +166,12 @@ Input_Task::dump (void) ACE_DEBUG ((LM_DEBUG, "end dumping timer queue\n")); } +// constructor + +Thread_Timer_Queue_Test_Driver::Thread_Timer_Queue_Test_Driver (void) + : input_task (&timer_queue_, *this) + {} + int Thread_Timer_Queue_Test_Driver::run_test (void) { @@ -175,7 +198,8 @@ int Thread_Timer_Queue_Test_Driver::init (void) { - // initialize commands with their corresponding input_task methods. + // initialize the <Command> objects with their corresponding + // methods from <Input_Task> ACE_NEW_RETURN (schedule_cmd_, Command<Input_Task> (input_task, input_task.add_timer), diff --git a/examples/Timer_Queue/Thread_Timer_Queue_Test.h b/examples/Timer_Queue/Thread_Timer_Queue_Test.h index cc1280a011f..c4f8178c022 100644 --- a/examples/Timer_Queue/Thread_Timer_Queue_Test.h +++ b/examples/Timer_Queue/Thread_Timer_Queue_Test.h @@ -11,7 +11,8 @@ // Thread_Timer_Queue_Test.h // // = DESCRIPTION -// // @@ Please comment me +// This code exercises the <ACE_Thread_Timer_Queue_Adapter> using +// an <ACE_Timer_Heap_T> // // = AUTHORS // Carlos O'Ryan <coryan@cs.wustl.edu> and @@ -42,6 +43,7 @@ typedef ACE_Timer_Heap_Iterator_T<ACE_Event_Handler *, typedef ACE_Thread_Timer_Queue_Adapter<Timer_Heap> Thread_Timer_Queue; +// Forward declaration class Thread_Timer_Queue_Test_Driver; class Input_Task : public ACE_Task<ACE_SYNCH> @@ -84,18 +86,20 @@ private: // How many micro seconds are in a second. Timer_Queue_Test_Driver<Thread_Timer_Queue, Input_Task> &driver_; + // The thread timer queue test driver }; class Thread_Timer_Queue_Test_Driver : public Timer_Queue_Test_Driver <Thread_Timer_Queue, Input_Task> // = TITLE -// +// Thread_Timer_Queue_Test_Driver // = DESCRIPTION +// This class implements a simple test driver for the <Thread_Timer_Queue>. +// The <display_menu> hook method is called from the base class to print +// a menu specific to the thread implementation of the timer queue. +// { public: - // @@ Please don't put the definitions of methods in their declarations. - Thread_Timer_Queue_Test_Driver (void) - : input_task (&timer_queue_, *this) - {} + Thread_Timer_Queue_Test_Driver (void); virtual int display_menu (void); virtual int init (void); |