diff options
author | cdgill <cdgill@ae88bc3d-4319-0410-8dbf-d08b4c9d3795> | 1998-09-21 00:26:03 +0000 |
---|---|---|
committer | cdgill <cdgill@ae88bc3d-4319-0410-8dbf-d08b4c9d3795> | 1998-09-21 00:26:03 +0000 |
commit | f1eaa8b6747e96452cc2cce8661f65835321a829 (patch) | |
tree | 52d4c7c9e72f53c1bd6ca71fab2029e89afecc21 /examples/Bounded_Packet_Relay | |
parent | 852b1099e4e144a03a6e3930a683097961ee50b8 (diff) | |
download | ATCD-f1eaa8b6747e96452cc2cce8661f65835321a829.tar.gz |
third checkin for bounded packet relay example
Diffstat (limited to 'examples/Bounded_Packet_Relay')
-rw-r--r-- | examples/Bounded_Packet_Relay/BPR_Drivers.cpp | 510 | ||||
-rw-r--r-- | examples/Bounded_Packet_Relay/BPR_Drivers.h | 33 | ||||
-rw-r--r-- | examples/Bounded_Packet_Relay/README | 6 | ||||
-rw-r--r-- | examples/Bounded_Packet_Relay/Thread_Bounded_Packet_Relay.cpp | 372 | ||||
-rw-r--r-- | examples/Bounded_Packet_Relay/Thread_Bounded_Packet_Relay.h | 25 | ||||
-rw-r--r-- | examples/Bounded_Packet_Relay/bpr_thread.cpp | 8 |
6 files changed, 483 insertions, 471 deletions
diff --git a/examples/Bounded_Packet_Relay/BPR_Drivers.cpp b/examples/Bounded_Packet_Relay/BPR_Drivers.cpp index 16b05b72e9e..3252b477964 100644 --- a/examples/Bounded_Packet_Relay/BPR_Drivers.cpp +++ b/examples/Bounded_Packet_Relay/BPR_Drivers.cpp @@ -11,7 +11,7 @@ // This code builds an abstraction to factor out common code for // the different implementations of the Timer_Queue. // -// = AUTHOR +// = AUTHORS // Chris Gill <cdgill@cs.wustl.edu> and // Douglas C. Schmidt <schmidt@cs.wustl.edu> // @@ -33,6 +33,36 @@ ACE_RCSID(Bounded_Packet_Relay, BPR_Driver, "$Id$") // Constructor. +BPR_Handler_Base (Bounded_Packet_Relay<ACE_Thread_Mutex> &relay, + Thread_Timer_Queue &queue) + : relay_ (relay), + queue (queue) +{ +} + +// Destructor. + +~BPR_Handler_Base (void) +{ +} + +// Helper method: clears all timers. + +int +clear_all_timers (void) +{ + // loop through the timers in the queue, cancelling each one + for (ACE_Timer_Node_T <ACE_Event_Handler *> *node; + (node = queue_->timer_queue ().get_first ()) != 0; + ) + queue_->cancel (node->get_timer_id (), 0); + + return 0; +} + + +// Constructor. + Input_Device_Wrapper_Base::Input_Device_Wrapper_Base (ACE_Thread_Manager *input_task_mgr) : ACE_Task_Base (input_task_mgr), send_input_msg_cmd_ (0), @@ -85,10 +115,10 @@ int Input_Device_Wrapper_Base::request_stop (void) { if (is_active_) - { - is_active_ = 0; - return 1; - } + { + is_active_ = 0; + return 1; + } return 0; } @@ -110,46 +140,46 @@ Input_Device_Wrapper_Base::svc (void) // While we're still marked active, and there are packets to send. is_active_ && count != 0; ) - { - // make sure there is a send command object - if (send_input_msg_cmd_ == 0) - { - is_active_ = 0; - ACE_ERROR_RETURN ((LM_ERROR, "%t %p\n", - "send message command object not instantiated"), - -1); - } - - // create an input message to send - message = create_input_message (); - if (message == 0) - { - is_active_ = 0; - ACE_ERROR_RETURN ((LM_ERROR, "%t %p\n", - "Failed to create input message object"), - -1); - } - - // send the input message - if (send_input_msg_cmd_->execute ((void *) message) < 0) { - is_active_ = 0; - delete message; - ACE_ERROR_RETURN ((LM_ERROR, "%t %p\n", - "Failed executing send message command object"), - -1); + // make sure there is a send command object + if (send_input_msg_cmd_ == 0) + { + is_active_ = 0; + ACE_ERROR_RETURN ((LM_ERROR, "%t %p\n", + "send message command object not instantiated"), + -1); + } + + // create an input message to send + message = create_input_message (); + if (message == 0) + { + is_active_ = 0; + ACE_ERROR_RETURN ((LM_ERROR, "%t %p\n", + "Failed to create input message object"), + -1); + } + + // send the input message + if (send_input_msg_cmd_->execute ((void *) message) < 0) + { + is_active_ = 0; + delete message; + ACE_ERROR_RETURN ((LM_ERROR, "%t %p\n", + "Failed executing send message command object"), + -1); + } + + // If all went well, decrement count of messages to send, + // and run the reactor event loop unti we get a timeout + // or something happens in a registered upcall. + if (count > 0) + --count; + + timeout = ACE_Time_Value (0, period_); + reactor_.run_event_loop (timeout); } - // If all went well, decrement count of messages to send, - // and run the reactor event loop unti we get a timeout - // or something happens in a registered upcall. - if (count > 0) - --count; - - timeout = ACE_Time_Value (0, period_); - reactor_.run_event_loop (timeout); - } - is_active_ = 0; return 0; @@ -250,33 +280,33 @@ Bounded_Packet_Relay<SYNCH>::start_transmission (u_long packet_count, // Initialize the output device. if (output_wrapper_->modify_device_settings ((void *) &logging level) < 0) - { - status_ = ERROR; - transmission_end_ = ACE_OS::gettimeofday (); - ACE_ERROR_RETURN ((LM_ERROR, "%t %p\n", - "failed to initialize output device object"), - -1); - } + { + status_ = ERROR; + transmission_end_ = ACE_OS::gettimeofday (); + ACE_ERROR_RETURN ((LM_ERROR, "%t %p\n", + "failed to initialize output device object"), + -1); + } // Initialize the input device. if (input_wrapper_->set_input_period (u_long input_period) < 0) - { - status_ = ERROR; - transmission_end_ = ACE_OS::gettimeofday (); - ACE_ERROR_RETURN ((LM_ERROR, "%t %p\n", - "failed to initialize input device object"), - -1); - } + { + status_ = ERROR; + transmission_end_ = ACE_OS::gettimeofday (); + ACE_ERROR_RETURN ((LM_ERROR, "%t %p\n", + "failed to initialize input device object"), + -1); + } // Activate the input device and save a handle to the new thread. if (input_wrapper_->activate () < 0) - { - status_ = ERROR; - transmission_end_ = ACE_OS::gettimeofday (); - ACE_ERROR_RETURN ((LM_ERROR, "%t %p\n", - "failed to activate input device object"), - -1); - } + { + status_ = ERROR; + transmission_end_ = ACE_OS::gettimeofday (); + ACE_ERROR_RETURN ((LM_ERROR, "%t %p\n", + "failed to activate input device object"), + -1); + } // If all went well, return success. return 0; @@ -297,23 +327,23 @@ Bounded_Packet_Relay<SYNCH>::end_transmission (Transmission_Status status) // Ask the the input thread to stop. if (input_wrapper_->request_stop () < 0) - { - status_ = ERROR; - transmission_end_ = ACE_OS::gettimeofday (); - ACE_ERROR_RETURN ((LM_ERROR, "%t %p\n", - "failed asking input device thread to stop"), - -1); - } + { + status_ = ERROR; + transmission_end_ = ACE_OS::gettimeofday (); + ACE_ERROR_RETURN ((LM_ERROR, "%t %p\n", + "failed asking input device thread to stop"), + -1); + } // Wait for input thread to stop. if (input_task_mgr_->wait_task (input_wrapper_) < 0) - { - status_ = ERROR; - transmission_end_ = ACE_OS::gettimeofday (); - ACE_ERROR_RETURN ((LM_ERROR, "%t %p\n", - "failed waiting for input device thread to stop"), - -1); - } + { + status_ = ERROR; + transmission_end_ = ACE_OS::gettimeofday (); + ACE_ERROR_RETURN ((LM_ERROR, "%t %p\n", + "failed waiting for input device thread to stop"), + -1); + } // If all went well, set passed status, stamp end time, return success. status_ = status; @@ -336,64 +366,52 @@ Bounded_Packet_Relay<SYNCH>::report_statistics (void) const char *status_msg; switch (status_) - { - case UN_INITIALIZED: - status_msg = "Uninitialized"; - break; - case STARTED: - // NOT REACHED: user should never see this ;-) - status_msg = "In progress"; - break; - case COMPLETED: - status_msg = "Completed with all packets sent"; - break; - case TIMED_OUT: - status_msg = "Terminated by transmission duration timer"; - break; - case CANCELLED: - status_msg = "Cancelled by external control"; - break; - case ERROR: - status_msg = "Error was detected"; - break; - default: - status_msg = "Unknown"; - break; - } + { + case UN_INITIALIZED: + status_msg = "Uninitialized"; + break; + case STARTED: + // NOT REACHED: user should never see this ;-) + status_msg = "In progress"; + break; + case COMPLETED: + status_msg = "Completed with all packets sent"; + break; + case TIMED_OUT: + status_msg = "Terminated by transmission duration timer"; + break; + case CANCELLED: + status_msg = "Cancelled by external control"; + break; + case ERROR: + status_msg = "Error was detected"; + break; + default: + status_msg = "Unknown"; + break; + } // Calculate duration of trasmission. ACE_Time_Value duration (transmission_end_); duration -= transmission_start_; // Report transmission statistics. - // @@ Chris, please don't use ACE_OS::fprintf(), use ACE_DEBUG or ACE_ERROR instead... - if (ACE_OS::fprintf (ACE_STDOUT, - "\n\nStatisics for transmission %lu:\n\n" - "Transmission status: %s\n" - "Start time: %ld (sec) %ld usec\n" - "End time: %ld (sec) %ld usec\n" - "Duration: %ld (sec) %ld usec\n" - "Packets relayed: %lu\n\n", - transmission_number_, status_msg, - transmission_start_.sec (), - transmission_start_.usec (), - transmission_end_.sec (), - transmission_end_.usec (), - duration.sec (), - duration.usec (), - packets_sent_) < 0) - ACE_ERROR_RETURN ((LM_ERROR, "%t %p\n", - "Bounded_Packet_Relay<SYNCH>::report_statistics" - "ACE_OS::fprintf failed"), - -1); + ACE_DEBUG ((LM_DEBUG, + "\n\nStatisics for transmission %lu:\n\n" + "Transmission status: %s\n" + "Start time: %d (sec) %d (usec)\n" + "End time: %d (sec) %d (usec)\n" + "Duration: %d (sec) %d (usec)\n" + "Packets relayed: %u\n\n", + transmission_number_, status_msg, + transmission_start_.sec (), + transmission_start_.usec (), + transmission_end_.sec (), + transmission_end_.usec (), + duration.sec (), + duration.usec (), + packets_sent_)); - // @@ Chris, if you use ACE_DEBUG or ACE_ERROR you won't need to use - // fflush here.... - if (ACE_OS::fflush (ACE_STDOUT) < 0) - ACE_ERROR_RETURN ((LM_ERROR, "%t %p\n", - "Bounded_Packet_Relay<SYNCH>::report_statistics" - "ACE_OS::fflush failed"), - -1); return 0; } @@ -423,121 +441,121 @@ Bounded_Packet_Relay_Driver<TQ, RECEIVER, ACTION>::parse_commands (const char *b return 0; switch (option) - { - case 1: // set packet count - { - u_long count; - - // We just reread the option, this simplies parsing - // (since sscanf can do it for us). - if (::sscanf (buf, "%d %lu", &option, &count) < 2) - // If there was not enough information on the line, - // ignore option and try the next line. - return 0; - - if (packet_count_cmd_->execute ((void *) &count) == -1) - ACE_ERROR_RETURN ((LM_ERROR, - "%t %p\n", - "set packet count failed"), - -1); - break; - } - - case 2: // Set the arrival period. - { - u_long usec; - - // We just reread the option, this simplies parsing - // (since sscanf can do it for us). - if (::sscanf (buf, "%d %lu", &option, &usec) < 2) - // If there was not enough information on the line, - // ignore option and try the next line. - return 0; - - if (arrival_period_cmd_->execute ((void *) &usec) == -1) - ACE_ERROR_RETURN ((LM_ERROR, - "%t %p\n", - "set arrival period failed"), - -1); - break; - } - - case 3: // Set transmit period. - { - u_long usec; - - // We just reread the option, this simplies parsing - // (since sscanf can do it for us). - if (::sscanf (buf, "%d %lu", &option, &usec) < 2) - // If there was not enough information on the line, - // ignore option and try the next line. - return 0; - - if (transmit_period_cmd_->execute ((void *) &usec) == -1) - ACE_ERROR_RETURN ((LM_ERROR, - "%t %p\n", - "set transmit period failed"), - -1); - break; - } - - case 4: // Set duration limit. { - u_long usec; - - // We just reread the option, this simplies parsing - // (since sscanf can do it for us). - if (::sscanf (buf, "%d %lu", &option, &usec) < 2) - // If there was not enough information on the line, - // ignore option and try the next line. - return 0; - - if (duration_limit_cmd_->execute ((void *) &usec) == -1) - ACE_ERROR_RETURN ((LM_ERROR, - "%t %p\n", - "set duration limit failed"), - -1); - break; - } - - case 5: // Set logging level. - { - u_long level; - - // We just reread the option, this simplies parsing - // (since sscanf can do it for us). - if (::sscanf (buf, "%d %lu", &option, &level) < 2) - // If there was not enough information on the line, - // ignore option and try the next line. - return 0; - - if (logging_level_cmd_->execute ((void *) &level) == -1) - ACE_ERROR_RETURN ((LM_ERROR, - "%t %p\n", - "set logging level failed"), - -1); - break; - } - - case 6: // Run one transmission. - return run_transmission_cmd_->execute (0); - /* NOTREACHED */ - - case 7: // Report statistics. - return report_stats_cmd_->execute (0); - /* NOTREACHED */ - - case 8: // Shut down the driver. - return shutdown_cmd_->execute (0); - /* NOTREACHED */ - - default: - // Display an error message. - ACE_ERROR_RETURN ((LM_ERROR, "invalid input %s\n", buf), 0); - ACE_NOTREACHED (break); - /* NOTREACHED */ - - } /* ENDSWITCH */ + case 1: // set packet count + { + u_long count; + + // We just reread the option, this simplies parsing + // (since sscanf can do it for us). + if (::sscanf (buf, "%d %lu", &option, &count) < 2) + // If there was not enough information on the line, + // ignore option and try the next line. + return 0; + + if (packet_count_cmd_->execute ((void *) &count) == -1) + ACE_ERROR_RETURN ((LM_ERROR, + "%t %p\n", + "set packet count failed"), + -1); + break; + } + + case 2: // Set the arrival period. + { + u_long usec; + + // We just reread the option, this simplies parsing + // (since sscanf can do it for us). + if (::sscanf (buf, "%d %lu", &option, &usec) < 2) + // If there was not enough information on the line, + // ignore option and try the next line. + return 0; + + if (arrival_period_cmd_->execute ((void *) &usec) == -1) + ACE_ERROR_RETURN ((LM_ERROR, + "%t %p\n", + "set arrival period failed"), + -1); + break; + } + + case 3: // Set transmit period. + { + u_long usec; + + // We just reread the option, this simplies parsing + // (since sscanf can do it for us). + if (::sscanf (buf, "%d %lu", &option, &usec) < 2) + // If there was not enough information on the line, + // ignore option and try the next line. + return 0; + + if (transmit_period_cmd_->execute ((void *) &usec) == -1) + ACE_ERROR_RETURN ((LM_ERROR, + "%t %p\n", + "set transmit period failed"), + -1); + break; + } + + case 4: // Set duration limit. + { + u_long usec; + + // We just reread the option, this simplies parsing + // (since sscanf can do it for us). + if (::sscanf (buf, "%d %lu", &option, &usec) < 2) + // If there was not enough information on the line, + // ignore option and try the next line. + return 0; + + if (duration_limit_cmd_->execute ((void *) &usec) == -1) + ACE_ERROR_RETURN ((LM_ERROR, + "%t %p\n", + "set duration limit failed"), + -1); + break; + } + + case 5: // Set logging level. + { + u_long level; + + // We just reread the option, this simplies parsing + // (since sscanf can do it for us). + if (::sscanf (buf, "%d %lu", &option, &level) < 2) + // If there was not enough information on the line, + // ignore option and try the next line. + return 0; + + if (logging_level_cmd_->execute ((void *) &level) == -1) + ACE_ERROR_RETURN ((LM_ERROR, + "%t %p\n", + "set logging level failed"), + -1); + break; + } + + case 6: // Run one transmission. + return run_transmission_cmd_->execute (0); + /* NOTREACHED */ + + case 7: // Report statistics. + return report_stats_cmd_->execute (0); + /* NOTREACHED */ + + case 8: // Shut down the driver. + return shutdown_cmd_->execute (0); + /* NOTREACHED */ + + default: + // Display an error message. + ACE_ERROR_RETURN ((LM_ERROR, "invalid input %s\n", buf), 0); + ACE_NOTREACHED (break); + /* NOTREACHED */ + + } /* ENDSWITCH */ return 0; } @@ -565,13 +583,13 @@ Bounded_Packet_Relay_Driver<TQ, RECEIVER, ACTION>::get_next_request (void) this->display_menu (); - // @@ Chris, please don't use ACE_OS::fprintf, use ACE_DEBUG here... - ACE_OS::fprintf (ACE_STDERR, "Please enter your choice: "); - ACE_OS::fflush (ACE_STDERR); + ACE_DEBUG ((LM_DEBUG, "Please enter your choice: ")); // Reads input from the user. if (this->read_input (buf, sizeof buf) <= 0) - return -1; + { + return -1; + } // Parse and run the command. return this->parse_commands (buf); diff --git a/examples/Bounded_Packet_Relay/BPR_Drivers.h b/examples/Bounded_Packet_Relay/BPR_Drivers.h index 39171726507..82eb3ead629 100644 --- a/examples/Bounded_Packet_Relay/BPR_Drivers.h +++ b/examples/Bounded_Packet_Relay/BPR_Drivers.h @@ -46,6 +46,39 @@ public: // Invokes the method <action_> from the object <receiver_>. }; +class BPR_Handler_Base : public ACE_Event_Handler +{ + // = TITLE + // Base event handler class for bounded packet relay example. + // + // = DESCRIPTION + // The <handle_timeout> hook method calls the relay's send + // method and decrements its count of messages to send. + // If there are still messages to send, it re-registers itself + // with the timer queue. Otherwise it calls the relay's end + // transmission method, clears the timer queue, and then deletes "this". +public: + BPR_Handler_Base (Bounded_Packet_Relay<ACE_Thread_Mutex> &relay, + Thread_Timer_Queue &queue); + // Constructor. + + ~BPR_Handler_Base (void); + // Destructor. + + virtual int clear_all_timers (void); + // Helper method: clears all timers. + +protected: + + Bounded_Packet_Relay<ACE_Thread_Mutex> &relay_; + // Stores a reference to the relay object on which to invoke + // the appropritate calls when the timer expires. + + Thread_Timer_Queue &queue_; + // Store a reference to the timer queue, in which to re-register + // the send timer and handler if there are still sends to perform. +}; + class Input_Device_Wrapper_Base : public ACE_Task_Base { // = TITLE diff --git a/examples/Bounded_Packet_Relay/README b/examples/Bounded_Packet_Relay/README index c067ffc215f..46fdbde6af2 100644 --- a/examples/Bounded_Packet_Relay/README +++ b/examples/Bounded_Packet_Relay/README @@ -19,11 +19,7 @@ the program, using selections from an interactive text-based menu. In addition, the example program can be run in batch mode, with the appropriate commands piped to the program in place of its standard -input stream. Commands sent in batch mode must be separated by new -lines. Menu prompts and error messages are directed to the standard -error stream. The results of the transmission run and statistics -reporting commands are directed to the standard output stream, so the -results can be isolated and directed to a file. +input stream. Transmission parameters are intialized to default values. Transmission parameter values persist until/unless they are diff --git a/examples/Bounded_Packet_Relay/Thread_Bounded_Packet_Relay.cpp b/examples/Bounded_Packet_Relay/Thread_Bounded_Packet_Relay.cpp index e6804bc161f..ea4804828ae 100644 --- a/examples/Bounded_Packet_Relay/Thread_Bounded_Packet_Relay.cpp +++ b/examples/Bounded_Packet_Relay/Thread_Bounded_Packet_Relay.cpp @@ -28,6 +28,8 @@ #include "ace/Timer_Queue_Adapters.h" #include "Thread_Bounded_Packet_Relay.h" +typedef Thread_Bounded_Packet_Relay_Driver::COMMAND CMD; + ACE_RCSID(Bounded_Packet_Relay, Thread_Bounded_Packet_Relay, "$Id$") // Constructor. @@ -116,15 +118,15 @@ int Text_Output_Driver_Wrapper::write_output_message (void *message) { if (message) - { - if (logging_) { + if (logging_) + { // CDG - TBD - print (also fix other printfs to use ACE_DEBUG, ACE_ERROR) // ACE_DEBUG (); - } + } - delete ACE_static_cast (ACE_Message_Block *, message); - } + delete ACE_static_cast (ACE_Message_Block *, message); + } ACE_ERROR_RETURN ((LM_ERROR, "Text_Output_Driver_Wrapper::" @@ -141,20 +143,16 @@ Text_Output_Driver_Wrapper::write_output_message (void *message) int Text_Output_Driver_Wrapper::modify_device_settings (void *logging) { - // @@ Chris, your indentation throughout this file is not consistent - // with ACE coding guidelines. Please make sure you use the - // indentation format that GNU emacs provide. If you have any - // questions, please let me know. if (logging) - { - logging_ = *ACE_static_cast (int *, logging); - } + { + logging_ = *ACE_static_cast (int *, logging); + } else - { - ACE_ERROR_RETURN ((LM_ERROR, - "null logging level pointer"), - -1); - } + { + ACE_ERROR_RETURN ((LM_ERROR, + "null logging level pointer"), + -1); + } } // Constructor. @@ -193,10 +191,10 @@ int User_Input_Task::set_packet_count (void *argument) { if (argument) - { - packet_count_ = *ACE_static_cast (int *, argument); - return 0; - } + { + packet_count_ = *ACE_static_cast (int *, argument); + return 0; + } ACE_ERROR_RETURN ((LM_ERROR, "User_Input_Task::set_packet_count: null argument"), @@ -210,10 +208,10 @@ int User_Input_Task::set_arrival_period (void *argument) { if (argument) - { - arrival_period_ = *ACE_static_cast (int *, argument); - return 0; - } + { + arrival_period_ = *ACE_static_cast (int *, argument); + return 0; + } ACE_ERROR_RETURN ((LM_ERROR, "User_Input_Task::set_arrival_period: null argument"), @@ -227,10 +225,10 @@ int User_Input_Task::set_send_period (void *argument) { if (argument) - { - send_period_ = *ACE_static_cast (int *, argument); - return 0; - } + { + send_period_ = *ACE_static_cast (int *, argument); + return 0; + } ACE_ERROR_RETURN ((LM_ERROR, "User_Input_Task::set_send_period: null argument"), @@ -243,10 +241,10 @@ int User_Input_Task::set_duration_limit (void *argument) { if (argument) - { - duration_limit_ = *ACE_static_cast (int *, argument); - return 0; - } + { + duration_limit_ = *ACE_static_cast (int *, argument); + return 0; + } ACE_ERROR_RETURN ((LM_ERROR, "User_Input_Task::set_duration_limit: null argument"), @@ -260,10 +258,10 @@ int User_Input_Task::set_logging_level (void *argument) { if (argument) - { - logging_level_ = *ACE_static_cast (int *, argument); - return 0; - } + { + logging_level_ = *ACE_static_cast (int *, argument); + return 0; + } ACE_ERROR_RETURN ((LM_ERROR, "User_Input_Task::set_logging_level: null argument"), @@ -279,72 +277,72 @@ User_Input_Task::run_transmission (void *argument) ACE_UNUSED_ARG (argument); if (relay_) - { - switch (relay_->start_transmission (packet_count_, - arrival_period_, - logging_level_)) { - case 1: - // @@ Chris, please don't use ACE_OS::fprintf() for things - // like this, use ACE_DEBUG or ACE_ERROR instead. - ACE_OS::fprintf (ACE_STDERR, - "\nRun transmission: " - "transmission already in progress\n"); - return 0; - /* NOT REACHED */ - case 0: - { - ACE_Time_Value now = ACE_OS::gettimeofday (); - ACE_Time_Value send_every (0, send_period_); - ACE_Time_Value send_at (send_every + now); - - Send_Handler *send_handler; - - ACE_NEW_RETURN (send_handler, - Send_Handler (packet_count_, - send_every, - *relay_, - *queue_), - -1); - - if (queue_->schedule (send_handler, 0, send_at) < 0) + switch (relay_->start_transmission (packet_count_, + arrival_period_, + logging_level_)) { - ACE_ERROR_RETURN ((LM_ERROR, - "User_Input_Task::run_transmission: " - "failed to schedule send handler"), - -1); - } - - if (duration_limit_) - { - ACE_Time_Value terminate_at (0, duration_limit_); - terminate_at += now; - - Termination_Handler *termination_handler; - - ACE_NEW_RETURN (termination_handler, - Termination_Handler (*relay_, - *queue_), - -1); - - if (queue_->schedule (termination_handler, 0, terminate_at) < 0) - { - ACE_ERROR_RETURN ((LM_ERROR, - "User_Input_Task::run_transmission: " - "failed to schedule termination handler"), + case 1: + ACE_DEBUG ((LM_DEBUG, + "\nRun transmission: " + "transmission already in progress\n")); + return 0; + /* NOT REACHED */ + case 0: + { + ACE_Time_Value now = ACE_OS::gettimeofday (); + ACE_Time_Value send_every (0, send_period_); + ACE_Time_Value send_at (send_every + now); + + Send_Handler *send_handler; + + ACE_NEW_RETURN (send_handler, + Send_Handler (packet_count_, + send_every, + *relay_, + *queue_), -1); - } - } - return 0; - } - /* not reached */ - - default: - return -1; - /* not reached */ - } - } + if (queue_->schedule (send_handler, 0, send_at) < 0) + { + ACE_ERROR_RETURN ((LM_ERROR, + "User_Input_Task::run_transmission: " + "failed to schedule send handler"), + -1); + } + + if (duration_limit_) + { + ACE_Time_Value terminate_at (0, duration_limit_); + terminate_at += now; + + Termination_Handler *termination_handler; + + ACE_NEW_RETURN (termination_handler, + Termination_Handler (*relay_, + *queue_), + -1); + + if (queue_->schedule (termination_handler, + 0, terminate_at) < 0) + { + ACE_ERROR_RETURN ((LM_ERROR, + "User_Input_Task::run_transmission: " + "failed to schedule termination " + "handler"), + -1); + } + } + + return 0; + } + /* not reached */ + + default: + return -1; + /* not reached */ + } + } ACE_ERROR_RETURN ((LM_ERROR, "User_Input_Task::run_transmission: " @@ -361,46 +359,41 @@ User_Input_Task::end_transmission (void *argument) ACE_UNUSED_ARG (argument); if (relay_) - { - Bounded_Packet_Relay::Transmission_Status *status; + { + Bounded_Packet_Relay::Transmission_Status *status; - status = - ACE_static_cast (Bounded_Packet_Relay::Transmission_Status *, - argument); + status = + ACE_static_cast (Bounded_Packet_Relay::Transmission_Status *, + argument); - if (status) - { - switch (relay_->end_transmission (*status) - { - case 1: - ACE_OS::fprintf (ACE_STDERR, - "\nEnd transmission: " - "no transmission in progress\n"); - return 0; - /* not reached */ - - case 0: - // Cancel any remaining timers. - - for (ACE_Timer_Node_T <ACE_Event_Handler *> *node; - (node = queue_->timer_queue ().get_first ()) != 0; - ) - queue->cancel (node->get_timer_id (), 0); - - return 0; - /* not reached */ + if (status) + { + switch (relay_->end_transmission (*status) + { + case 1: + ACE_DEBUG ((LM_DEBUG, + "\nEnd transmission: " + "no transmission in progress\n")); + return 0; + /* not reached */ + + case 0: + // Cancel any remaining timers. + this->clear_all_timers (); + return 0; + /* not reached */ - default: - return -1; - /* not reached */ + default: + return -1; + /* not reached */ + } } - } - ACE_ERROR_RETURN ((LM_ERROR, + ACE_ERROR_RETURN ((LM_ERROR, "User_Input_Task::end_transmission: " "null argument"), -1); - } + } ACE_ERROR_RETURN ((LM_ERROR, "User_Input_Task::end_transmission: " @@ -418,24 +411,24 @@ User_Input_Task::report_stats (void *argument) ACE_UNUSED_ARG (argument); if (relay_) - { - switch (relay_->report_statistics ()) { - case 1: - ACE_OS::fprintf (ACE_STDERR, - "\nRun transmission: " - "\transmission already in progress\n"); - /* fall through to next case */ - - case 0: - return 0; - /* not reached */ - - default: - return -1; - /* not reached */ + switch (relay_->report_statistics ()) + { + case 1: + ACE_DEBUG ((LM_DEBUG, + "\nRun transmission: " + "\transmission already in progress\n")); + /* fall through to next case */ + + case 0: + return 0; + /* not reached */ + + default: + return -1; + /* not reached */ + } } - } ACE_ERROR_RETURN ((LM_ERROR, "User_Input_Task::report_stats: " @@ -471,10 +464,9 @@ Send_Handler::Send_Handler (u_long send_count, const ACE_Time_Value &duration, Bounded_Packet_Relay<ACE_Thread_Mutex> &relay, Thread_Timer_Queue &queue) - : send_count_ (send_count), + : BPR_Handler_Base (relay, queue), + send_count_ (send_count), duration_ (duration), - relay_ (relay), - queue_ (queue) { } @@ -492,43 +484,35 @@ Send_Handler::handle_timeout (const ACE_Time_Value ¤t_time, { switch (relay_->send_input ()) { - case 0: - // Decrement count of packets to relay. - --send_count_; - /* Fall through to next case. */ - case 1: - if (send_count_ > 0) - { - // Re-register the handler for a new timeout. - if (queue_->schedule (this, 0, - duration_ + ACE_OS::gettimeofday ()) < 0) - ACE_ERROR_RETURN ((LM_ERROR, - "Send_Handler::handle_timeout: " - "failed to reschedule send handler"), - -1); - return 0; - } - else - { - // @@ Chris, I think you repeat this code a number of times. - // Can you please abstract it out into a helper method and - // call it, rather than writing it redundantly? - - // All packets are sent, time to cancel any other - // timers, end the transmission, and go away. - for (ACE_Timer_Node_T <ACE_Event_Handler *> *node; - (node = queue_->timer_queue ().get_first ()) != 0; - ) - queue->cancel (node->get_timer_id (), 0); - - relay_->end_transmission (Bounded_Packet_Relay::COMPLETED); - delete this; - return 0; - } + case 0: + // Decrement count of packets to relay. + --send_count_; + /* Fall through to next case. */ + case 1: + if (send_count_ > 0) + { + // Re-register the handler for a new timeout. + if (queue_->schedule (this, 0, + duration_ + ACE_OS::gettimeofday ()) < 0) + ACE_ERROR_RETURN ((LM_ERROR, + "Send_Handler::handle_timeout: " + "failed to reschedule send handler"), + -1); + return 0; + } + else + { + // All packets are sent, time to cancel any other + // timers, end the transmission, and go away. + this->clear_all_timers (); + relay_->end_transmission (Bounded_Packet_Relay::COMPLETED); + delete this; + return 0; + } + /* NOT REACHED */ - /* NOT REACHED */ - default: - return -1; + default: + return -1; } } @@ -545,8 +529,7 @@ Send_Handler::cancelled (void) Termination_Handler::Termination_Handler (Bounded_Packet_Relay<ACE_Thread_Mutex> &relay, Thread_Timer_Queue &queue) - : relay_ (relay), - queue_ (queue) + : BPR_Handler_Base (relay, queue) { } @@ -564,11 +547,7 @@ Termination_Handler::handle_timeout (const ACE_Time_Value ¤t_time, { // Transmission timed out, so cancel any other // timers, end the transmission, and go away. - for (ACE_Timer_Node_T <ACE_Event_Handler *> *node; - (node = queue_->timer_queue ().get_first ()) != 0; - ) - queue->cancel (node->get_timer_id (), 0); - + this->clear_all_timers (); relay_->end_transmission (Bounded_Packet_Relay::TIMED_OUT); delete this; return 0; @@ -620,8 +599,7 @@ Thread_Bounded_Packet_Relay_Driver::display_menu (void) " 8 - report statistics from the most recent transmission\n" " 9 - quit the program\n"; - // @@ Chris, don't use ACE_OS::printf... - ACE_OS::fprintf (ACE_STDERR, "%s", menu); + ACE_DEBUG ((LM_DEBUG, "%s", menu)); return 0; } @@ -631,10 +609,6 @@ Thread_Bounded_Packet_Relay_Driver::display_menu (void) int Thread_Bounded_Packet_Relay_Driver::init (void); { - // @@ Chris, I suspect that putting a typedef here may break some - // compilers. Can you please move it to outside of the method? - typedef Command<Input_Task, Input_Task::ACTION> CMD; - // Initialize the <Command> objects with their corresponding // methods from <User_Input_Task>. ACE_NEW_RETURN (packet_count_cmd_, diff --git a/examples/Bounded_Packet_Relay/Thread_Bounded_Packet_Relay.h b/examples/Bounded_Packet_Relay/Thread_Bounded_Packet_Relay.h index c1ecfbdd154..b94bbbca282 100644 --- a/examples/Bounded_Packet_Relay/Thread_Bounded_Packet_Relay.h +++ b/examples/Bounded_Packet_Relay/Thread_Bounded_Packet_Relay.h @@ -236,7 +236,7 @@ private: }; -class Send_Handler : public ACE_Event_Handler +class Send_Handler : public BPR_Handler_Base { // = TITLE // Event handler for message send timeout events. @@ -265,6 +265,7 @@ public: // Cancellation hook. private: + u_long send_count_; // Count of the number of messages to send from the // relay object to the output device object. @@ -272,17 +273,9 @@ private: ACE_Time_Value duration_; // Stores the expected duration until expiration, and is used to // re-register the handler if there are still sends to perform. - - Bounded_Packet_Relay<ACE_Thread_Mutex> &relay_; - // Stores a reference to the relay object on which to invoke - // the appropritate calls when the timer expires. - - Thread_Timer_Queue &queue_; - // Store a reference to the timer queue, in which to re-register - // the send timer and handler if there are still sends to perform. }; -class Termination_Handler : public ACE_Event_Handler +class Termination_Handler : public BPR_Handler_Base { // = TITLE // Event handler for end transmission timeout events. @@ -305,14 +298,6 @@ public: virtual int cancelled (void); // Cancellation hook. -private: - Bounded_Packet_Relay<ACE_Thread_Mutex> &relay_; - // Stores a reference to the relay object on which to invoke - // the end transmission call when the timer expires. - - Thread_Timer_Queue &queue_; - // Stores a reference to the timer queue, which we'll clear of all - // timers when this one expires. }; class Thread_Bounded_Packet_Relay_Driver : public Bounded_Packet_Relay_Driver <Thread_Timer_Queue> @@ -328,6 +313,10 @@ class Thread_Bounded_Packet_Relay_Driver : public Bounded_Packet_Relay_Driver <T // thread implementation of the timer queue. public: + // = Trait for commands issued from this driver + + typedef Command<Input_Task, Input_Task::ACTION> COMMAND; + // = Initialization and termination methods. Thread_Bounded_Packet_Relay_Driver (void); diff --git a/examples/Bounded_Packet_Relay/bpr_thread.cpp b/examples/Bounded_Packet_Relay/bpr_thread.cpp index 9f145d81c7d..60b199945f9 100644 --- a/examples/Bounded_Packet_Relay/bpr_thread.cpp +++ b/examples/Bounded_Packet_Relay/bpr_thread.cpp @@ -12,11 +12,13 @@ // Exercises drivers for a bounded packet relay, based on threaded timer queues. // // = AUTHORS -// Chris Gill <cdgill@cs.wustl.edu> +// Chris Gill <cdgill@cs.wustl.edu> and +// Douglas C. Schmidt <schmidt@cs.wustl.edu> // -// Based on the Timer_Queue_Test example written by: +// Based on the Timer Queue Test example written by // -// Douglas Schmidt <schmidt@cs.wustl.edu> && +// Carlos O'Ryan <coryan@cs.wustl.edu> and +// Douglas C. Schmidt <schmidt@cs.wustl.edu> and // Sergio Flores-Gaitan <sergio@cs.wustl.edu> // // ============================================================================ |