diff options
author | pradeep <pradeep@ae88bc3d-4319-0410-8dbf-d08b4c9d3795> | 1998-10-13 01:30:21 +0000 |
---|---|---|
committer | pradeep <pradeep@ae88bc3d-4319-0410-8dbf-d08b4c9d3795> | 1998-10-13 01:30:21 +0000 |
commit | 72722af0e8f27cd61ef704cff9c11a01cac7fe62 (patch) | |
tree | 49b87c84d06236cc8488c955f24b1b5b1ec27dbd | |
parent | 4d9d8b0e02cd620cb8b405188534bf6ca2fce309 (diff) | |
download | ATCD-72722af0e8f27cd61ef704cff9c11a01cac7fe62.tar.gz |
*** empty log message ***
-rw-r--r-- | TAO/examples/Event_Comm/Consumer_Handler.cpp | 182 | ||||
-rw-r--r-- | TAO/examples/Event_Comm/Consumer_Handler.h | 51 | ||||
-rw-r--r-- | TAO/examples/Event_Comm/Consumer_Input_Handler.cpp | 41 | ||||
-rw-r--r-- | TAO/examples/Event_Comm/Consumer_Input_Handler.h | 15 | ||||
-rw-r--r-- | TAO/examples/Event_Comm/Event_Comm.idl | 19 | ||||
-rw-r--r-- | TAO/examples/Event_Comm/Event_Comm_i.cpp | 190 | ||||
-rw-r--r-- | TAO/examples/Event_Comm/Event_Comm_i.h | 47 | ||||
-rw-r--r-- | TAO/examples/Event_Comm/Makefile | 8 | ||||
-rw-r--r-- | TAO/examples/Event_Comm/Notifier_Handler.cpp | 91 | ||||
-rw-r--r-- | TAO/examples/Event_Comm/Notifier_Handler.h | 38 | ||||
-rw-r--r-- | TAO/examples/Event_Comm/Notifier_Server.cpp | 137 | ||||
-rw-r--r-- | TAO/examples/Event_Comm/Notifier_Server.h | 72 | ||||
-rw-r--r-- | TAO/examples/Event_Comm/Supplier_Input_Handler.cpp | 42 | ||||
-rw-r--r-- | TAO/examples/Event_Comm/Supplier_Input_Handler.h | 15 | ||||
-rw-r--r-- | TAO/examples/Event_Comm/consumer.cpp | 96 | ||||
-rw-r--r-- | TAO/examples/Event_Comm/notifier.cpp | 80 | ||||
-rw-r--r-- | TAO/examples/Event_Comm/supplier.cpp | 74 |
17 files changed, 789 insertions, 409 deletions
diff --git a/TAO/examples/Event_Comm/Consumer_Handler.cpp b/TAO/examples/Event_Comm/Consumer_Handler.cpp index d6c6fa84702..6eb901d5bb3 100644 --- a/TAO/examples/Event_Comm/Consumer_Handler.cpp +++ b/TAO/examples/Event_Comm/Consumer_Handler.cpp @@ -4,102 +4,138 @@ ACE_RCSID(Consumer, Consumer_Handler, "$Id$") +Consumer_Handler::Consumer_Handler (void) +: notifier_ (0), + receiver_ (0) +{ + // No-Op. +} + +// Destroy a Receiver target object. + +Consumer_Handler::~Consumer_Handler (void) +{ + // No-Op. +} + int Consumer_Handler::handle_close (ACE_HANDLE, ACE_Reactor_Mask) { -// ACE_ST_CORBA_Handler::remove_service (Event_Comm_Consumer_IMPL); - if (this->receiver_ != 0) { ACE_DEBUG ((LM_DEBUG, "closing down Consumer_Handler\n")); - /*CORBA_HANDLER::instance ()->deactivate_service (Event_Comm_Consumer_IMPL, - this->receiver_->_marker ()); - */ - CORBA::release (this->receiver_); + + CORBA::release (this->receiver_.in()); this->receiver_ = 0; - CORBA::release (this->notifier_); + CORBA::release (this->notifier_.in()); this->notifier_ = 0; - // *Must* be allocated dynamically in order to delete this! - delete this; } return 0; } -Consumer_Handler::Consumer_Handler (int argc, char *argv[]) - : notifier_ (0), - receiver_ (0) +int +Consumer_Handler::init (int argc, char *argv[]) { - // @@ Orbix specific things. figure out TAO equivalents - /* - const char *server_name = ""; // @@ fill this in. - // Event_Comm_Consumer_IMPL; - // @@ what is this?? - char buf[BUFSIZ]; - char *receiver_marker = buf; - char *filtering_criteria; - char *host; - char *notifier_marker; - char *service_location = argv[0]; - - // First see if we have any environment variables. + char *filtering_criteria = ""; + + // First see if we have any environment variables. filtering_criteria = ACE_OS::getenv ("FILTERING_CRITERIA"); - host = ACE_OS::getenv ("HOST"); - notifier_marker = ACE_OS::getenv ("NOTIFIER_MARKER"); - // Then override these variables with command-line arguments. + // Then override these variables with command-line arguments. filtering_criteria = argc > 1 ? argv[1] : ""; - host = argc > 2 ? argv[2] : "tango.cs"; - notifier_marker = argc > 3 ? argv[3] : "notifier:" ; - // Event_Comm_Notifier_IR; @@ what is this? - // CORBA::Orbix.setDiagnostics (0); - - utsname name; + TAO_TRY + { + // Retrieve the ORB. + this->orb_ = CORBA::ORB_init (argc, + argv, + 0, + TAO_TRY_ENV); - // Make the marker name be the "/hostname/processid" - ACE_OS::uname (&name); - sprintf (buf, "/%s/%d", name.nodename, ACE_OS::getpid ()); + TAO_CHECK_ENV; - CORBA_HANDLER::instance ()->activate_service (Event_Comm_Consumer_IMPL, - receiver_marker, - service_location); + this->receiver_ = + this->receiver_i_._this (TAO_TRY_ENV); - // Create the receiver object. - ACE_NEW (this->receiver_, - TIE_Event_Comm_Consumer (Consumer_i) - (new Consumer_i)); + TAO_CHECK_ENV; - this->receiver_->_marker (receiver_marker); - ACE_ASSERT (this->receiver_); + if (get_notifier() == -1) + ACE_ERROR_RETURN ((LM_ERROR, + " (%P|%t) Unable to get the notifier " + "the TAO_Naming_Client. \n"), + -1); - TRY - { - // Get a binding to the notifier. - this->notifier_ = Event_Comm::Notifier::_bind (notifier_marker, host, IT_X); - - if (this->notifier_ != CORBA::OBJECT_NIL) - // Subscribe ourselves with the notifier's broker. - this->notifier_->subscribe (this->receiver_, - filtering_criteria, IT_X); + // Subscribe ourselves with the notifier's broker. + this->notifier_->subscribe (this->receiver_, + filtering_criteria, + TAO_TRY_ENV); } - CATCHANY + TAO_CATCHANY + { + TAO_TRY_ENV.print_exception ("Consumer_Handler::init\n"); + return -1; + } + TAO_ENDTRY; +} + +int +Consumer_Handler::get_notifier(void) +{ + TAO_TRY { - cerr << "Unexpected exception " << IT_X << endl; - ACE_OS::exit (1); + // Initialization of the naming service. + if (naming_services_client_.init (orb_.in ()) != 0) + ACE_ERROR_RETURN ((LM_ERROR, + " (%P|%t) Unable to initialize " + "the TAO_Naming_Client. \n"), + -1); + + CosNaming::Name notifier_ref_name (1); + notifier_ref_name.length (1); + notifier_ref_name[0].id = CORBA::string_dup (NOTIFIER_BIND_NAME); + + CORBA::Object_var notifier_obj = + this->naming_services_client_->resolve (notifier_ref_name, + TAO_TRY_ENV); + TAO_CHECK_ENV; + + // The CORBA::Object_var object is downcast to Notifier_var using + // the <_narrow> method. + this->notifier_ = + Event_Comm::Notifier::_narrow (notifier_obj.in (), + TAO_TRY_ENV); + TAO_CHECK_ENV; } - ENDTRY; - // Print out context. - - receiver_marker = (char *) this->receiver_->_marker (); - CORBA::BOA::activationMode mode = CORBA::Orbix.myActivationMode (); - ACE_DEBUG ((LM_DEBUG, - "starting up a %spersistent server in mode %d with marker name %s\n", - mode == CORBA::BOA::persistentActivationMode ? "" : "non-", - mode, - receiver_marker)); -*/ + TAO_CATCHANY + { + TAO_TRY_ENV.print_exception ("Consumer_Handler::get_notifier\n"); + return -1; + } + TAO_ENDTRY; + + return 0; +} + +void +Consumer_Handler:: close (void) +{ + this->orb_->shutdown(); +} + +int +Consumer_Handler::run (void) +{ + // Run the ORB. + this->orb_->run (); + return 0; +} + +ACE_Reactor* +Consumer_Handler::reactor(void) +{ + return TAO_ORB_Core_instance ()->reactor (); } Event_Comm::Consumer * @@ -113,11 +149,3 @@ Consumer_Handler::notifier (void) { return this->notifier_; } - -// Destroy a Receiver target object. - -Consumer_Handler::~Consumer_Handler (void) -{ - this->handle_close (-1, - ACE_Event_Handler::ALL_EVENTS_MASK); -} diff --git a/TAO/examples/Event_Comm/Consumer_Handler.h b/TAO/examples/Event_Comm/Consumer_Handler.h index 352c8231bf1..021da94cd51 100644 --- a/TAO/examples/Event_Comm/Consumer_Handler.h +++ b/TAO/examples/Event_Comm/Consumer_Handler.h @@ -21,38 +21,67 @@ #if !defined (_CONSUMER_HANDLER_H) #define _CONSUMER_HANDLER_H -#include "ace/CORBA_Handler.h" +//#include "ace/CORBA_Handler.h" #include "Event_Comm_i.h" +#include "orbsvcs/Naming/Naming_Utils.h" +#include "orbsvcs/CosNamingC.h" class Consumer_Handler { // = TITLE - // Subclass of Corba_Handler that sets up the Notification - // Receiver handler for use with the ACE_Reactor. + // The Consumer_Handler class. // // = DESCRIPTION - // Note that this class doesn't inherit from ACE_ST_CORBA_Handler - // (unlike the Supplier's Notifier_Handler class). Instead, it - // uses an alternative interface that can be called directly. + // Starts up the ORB, registers the <Event_Comm::Consumer> servant with the orb + // and gets a reference to a <Notifier> from the Naming Service. + // + public: - Consumer_Handler (int argc, char *argv[]); + // = Initialization and termination methods. + Consumer_Handler (void); + ~Consumer_Handler (void); + + int init (int argc, char *argv[]); + // Initializes the ORB, + // gets the Notifier reference from the Naming Service, + // and starts the servant for the Consumer object. + + int run (void); + // runs the ORB. + + void close (void); + // shutdown the orb. + // = Accessors Event_Comm::Consumer *receiver (void); Event_Comm::Notifier *notifier (void); + ACE_Reactor *reactor (void); + virtual int handle_close (ACE_HANDLE = ACE_INVALID_HANDLE, ACE_Reactor_Mask = ACE_Event_Handler::NULL_MASK); // Close down the handler. private: - ~Consumer_Handler (void); - // Ensure dynamic allocation. + int get_notifier(void); + + Consumer_i receiver_i_; + // The <Consumer> implementation. - Event_Comm::Consumer *receiver_; + Event_Comm::Consumer_var receiver_; // Pointer to an IDL <Consumer> proxy object. - Event_Comm::Notifier *notifier_; + Event_Comm::Notifier_var notifier_; // Pointer to an IDL <Notifier> proxy object. + + TAO_Naming_Client naming_services_client_; + // An instance of the name client used for resolving the factory + // objects. + + CORBA::ORB_var orb_; + // Remember our orb. }; +#define NOTIFIER_BIND_NAME "Notifier" + #endif /* _CONSUMER_HANDLER_H */ diff --git a/TAO/examples/Event_Comm/Consumer_Input_Handler.cpp b/TAO/examples/Event_Comm/Consumer_Input_Handler.cpp index 308a0bb9261..886d83e24f3 100644 --- a/TAO/examples/Event_Comm/Consumer_Input_Handler.cpp +++ b/TAO/examples/Event_Comm/Consumer_Input_Handler.cpp @@ -5,11 +5,17 @@ ACE_RCSID(Consumer, Consumer_Input_Handler, "$Id$") +Consumer_Input_Handler::Consumer_Input_Handler (void) + : receiver_handler_ (0), + handle_ (0), + consumer_initiated_shutdown_ (0) +{ + // No-Op. +} + Consumer_Input_Handler::~Consumer_Input_Handler (void) { - ACE_DEBUG ((LM_DEBUG, - "closing down Input_Handler::~Input_Handler\n")); - this->handle_close (); + // No-Op. } int @@ -48,8 +54,8 @@ Consumer_Input_Handler::handle_close (ACE_HANDLE, ACE_Reactor_Mask) { // Gracefully shutdown the Receiver by removing it from the // Notifier's internal map. - - notifier->unsubscribe (receiver, "", TAO_TRY_ENV); //IT_X); + if (notifier != 0) + notifier->unsubscribe (receiver, "", TAO_TRY_ENV); TAO_CHECK_ENV; } TAO_CATCHANY @@ -61,29 +67,28 @@ Consumer_Input_Handler::handle_close (ACE_HANDLE, ACE_Reactor_Mask) // Don't execute a callback here otherwise we'll recurse // indefinitely! - if (ACE_Reactor::instance ()->remove_handler + if (this->receiver_handler_->reactor()->remove_handler (this, ACE_Event_Handler::READ_MASK | ACE_Event_Handler::DONT_CALL) == -1) ACE_ERROR ((LM_ERROR, "%p\n", "remove_handler")); - // *Must* be allocated dyanmically! - ::operator delete (this); return 0; } -Consumer_Input_Handler::Consumer_Input_Handler (Consumer_Handler *ch, +int Consumer_Input_Handler::initialize (Consumer_Handler *ch, ACE_HANDLE handle) - : receiver_handler_ (ch), - handle_ (handle), - consumer_initiated_shutdown_ (0) { - if (ACE_Reactor::instance ()->register_handler + receiver_handler_ = ch; + handle_ = handle; + + if (this->receiver_handler_->reactor()->register_handler (this, ACE_Event_Handler::READ_MASK) == -1) - ACE_ERROR ((LM_ERROR, - "Consumer_Input_Handler::Input_Handler\n")); + ACE_ERROR_RETURN ((LM_ERROR, + "Consumer_Input_Handler::Input_Handler\n"), -1); + return 0; } int @@ -123,10 +128,8 @@ Consumer_Input_Handler::handle_input (ACE_HANDLE h) // Consumer wants to shutdown. this->consumer_initiated_shutdown (1); - - // Tell the main event loop to shutdown. - ACE_Reactor::end_event_loop(); + this->receiver_handler_->close (); } else { @@ -136,7 +139,7 @@ Consumer_Input_Handler::handle_input (ACE_HANDLE h) event.tag_ = ACE_OS::strdup (buf); - notifier->push (event, TAO_TRY_ENV); //IT_X); + notifier->push (event, TAO_TRY_ENV); TAO_CHECK_ENV; } TAO_CATCHANY diff --git a/TAO/examples/Event_Comm/Consumer_Input_Handler.h b/TAO/examples/Event_Comm/Consumer_Input_Handler.h index e0fcd60eca5..1872f2aedec 100644 --- a/TAO/examples/Event_Comm/Consumer_Input_Handler.h +++ b/TAO/examples/Event_Comm/Consumer_Input_Handler.h @@ -28,10 +28,16 @@ class Consumer_Input_Handler : public ACE_Service_Object // Handles input events generated from a keyboard. // // = DESCRIPTION - // This subclass <ACE_Service_Object> receives "unsubscribes" - // from the <Notifier> when input is received from the keyboard. + // This class holds a <Consumer_Handler> object. It accepts input from the user + // and passes events to the <Notifier>. It also initiates shutdown. + // public: - Consumer_Input_Handler (Consumer_Handler *, + // Initialization and termination methods. + Consumer_Input_Handler(void); + + ~Consumer_Input_Handler (void); + + int initialize (Consumer_Handler *, ACE_HANDLE h = 0); virtual int handle_input (ACE_HANDLE); @@ -48,9 +54,6 @@ public: // Indicate that the Consumer initiated the shutdown. private: - ~Consumer_Input_Handler (void); - // Ensure dynamic allocation. - virtual ACE_HANDLE get_handle (void) const; ACE_HANDLE handle_; diff --git a/TAO/examples/Event_Comm/Event_Comm.idl b/TAO/examples/Event_Comm/Event_Comm.idl index 8c2018bbba2..09908daa751 100644 --- a/TAO/examples/Event_Comm/Event_Comm.idl +++ b/TAO/examples/Event_Comm/Event_Comm.idl @@ -47,19 +47,16 @@ module Event_Comm }; interface Consumer + { // = TITLE // Defines the interface for a <Consumer> of events. - // Note that all operations are <oneway> to avoid blocking. - { + void push (in Event event); // Inform the <Consumer> that <event> has occurred. void disconnect (in string reason); // Disconnect the <Consumer> from the <Notifier>, // giving it the <reason>. - - string marker (); - // @@ what does this do?? why was it missing? }; interface Notifier @@ -67,6 +64,18 @@ module Event_Comm // = TITLE // Defines the interface for a <Notifier> of events. + exception CannotSubscribe + { + string reason_; + }; + // This exception in thrown when a <subscribe> fails. + + exception CannotUnsubscribe + { + string reason_; + }; + // This exception in thrown when a <unsubscribe> fails. + // = The following operations are intended for Suppliers. void disconnect (in string reason); diff --git a/TAO/examples/Event_Comm/Event_Comm_i.cpp b/TAO/examples/Event_Comm/Event_Comm_i.cpp index 593ed187a94..7d90713f4ee 100644 --- a/TAO/examples/Event_Comm/Event_Comm_i.cpp +++ b/TAO/examples/Event_Comm/Event_Comm_i.cpp @@ -10,8 +10,8 @@ class Consumer_Entry // Keeps track of context information associated with // a <Event_Comm::Consumer> entry. public: - Consumer_Entry (Event_Comm::Consumer *notification_receiver, - const char *filtering_criteria); + Consumer_Entry (Event_Comm::Consumer *consumer, + const char *filtering_criteria); ~Consumer_Entry (void); // = Set/get filtering criteria. @@ -20,8 +20,8 @@ public: const char *criteria (void); // = Set/get Event_Comm::Consumer object reference. - Event_Comm::Consumer *receiver (void); - void receiver (Event_Comm::Consumer *); + Event_Comm::Consumer *consumer (void); + void consumer (Event_Comm::Consumer *); // = Set/get the compiled regular expression buffer. const char *regexp (void); @@ -35,7 +35,7 @@ private: // Compiled representation of the regular expression (see // regexpr(3g)). - Event_Comm::Consumer *receiver_; + Event_Comm::Consumer_ptr consumer_; // Object reference for the Event_Comm::Consumer. }; @@ -57,15 +57,15 @@ Consumer_Entry::criteria (void) // = Set/get Event_Comm::Consumer object reference. Event_Comm::Consumer * -Consumer_Entry::receiver (void) +Consumer_Entry::consumer (void) { - return this->receiver_; + return this->consumer_; } void -Consumer_Entry::receiver (Event_Comm::Consumer *receiver) +Consumer_Entry::consumer (Event_Comm::Consumer *consumer) { - this->receiver_ = receiver; + this->consumer_ = consumer; } const char * @@ -77,13 +77,13 @@ Consumer_Entry::regexp (void) void Consumer_Entry::regexp (char *regexp) { - ACE_OS::free (ACE_MALLOC_T (this->compiled_regexp_)); + ACE_OS::free ((void *) this->compiled_regexp_); this->compiled_regexp_ = regexp; } -Consumer_Entry::Consumer_Entry (Event_Comm::Consumer *receiver, +Consumer_Entry::Consumer_Entry (Event_Comm::Consumer *consumer, const char *filtering_criteria) - : receiver_ (receiver), + : consumer_ (consumer), filtering_criteria_ (0), compiled_regexp_ (0) { @@ -108,15 +108,15 @@ Consumer_Entry::Consumer_Entry (Event_Comm::Consumer *receiver, // Increment the reference count since we are keeping a copy of // this... - this->receiver_->_duplicate (this->receiver_); + this->consumer_ = Event_Comm::Consumer::_duplicate (this->consumer_); } Consumer_Entry::~Consumer_Entry (void) { - ACE_OS::free ((void*)this->filtering_criteria_); - ACE_OS::free ((void*)this->compiled_regexp_); + ACE_OS::free ((void *) this->filtering_criteria_); + ACE_OS::free ((void *) this->compiled_regexp_); // Decrement the object reference count. - CORBA::release (this->receiver_); + CORBA::release (this->consumer_); } Notifier_i::Notifier_i (size_t size) @@ -124,49 +124,55 @@ Notifier_i::Notifier_i (size_t size) { } -// Add a new receiver to the table, being careful to check for -// duplicate entries. A receiver is considered a duplicate under the +// Add a new consumer to the table, being careful to check for +// duplicate entries. A consumer is considered a duplicate under the // following circumstances: -// 1. It has the same marker name and the same filtering criteria -// 2. It has the same marker name and its filtering criteria is "" (the wild card). +// +// 1. It has the same object reference and the same filtering +// criteria. +// 2. It has the same object reference and its filtering criteria is +// "" (the wild card). void -Notifier_i::subscribe (Event_Comm::Consumer_ptr receiver_ref, +Notifier_i::subscribe (Event_Comm::Consumer_ptr consumer_ref, const char *filtering_criteria, CORBA::Environment &TAO_TRY_ENV) { ACE_DEBUG ((LM_DEBUG, - "in Notifier_i::subscribe for %s with filtering criteria \"%s\"\n", - receiver_ref->marker (), + "in Notifier_i::subscribe for %x with filtering criteria \"%s\"\n", + consumer_ref, filtering_criteria)); - ACE_SString key (receiver_ref->marker ()); + MAP_ITERATOR mi (this->map_); - // Try to locate an entry using its marker name (which should be - // unique across the system). If we don't find the entry, or if the + // Try to locate an entry checking if the object references are equivalent . + // If we don't find the entry, or if the // filtering criteria is different that is good news since we - // currently don't allow duplicates... In particular, if @@ Should + // currently don't allow duplicates... @@ Should // duplicates be allowed? for (MAP_ENTRY *me = 0; mi.next (me) != 0; mi.advance ()) { Consumer_Entry *nr_entry = me->int_id_; + // The <_is_equivalent> function checks if objects + // are the same. + // NOTE: this call might not behave well on other + // ORBs since <_is_equivalent> isn't guaranteed to differentiate + // object references. + // Check for a duplicate entry. - if (key == me->ext_id_ - && (ACE_OS::strcmp (filtering_criteria, "") == 0 - || ACE_OS::strcmp (filtering_criteria, nr_entry->criteria ()) == 0)) + if (consumer_ref->_is_equivalent (me->ext_id_) //(nr_entry->consumer()) + && (ACE_OS::strcmp (filtering_criteria, + "") == 0 + || ACE_OS::strcmp (filtering_criteria, + nr_entry->criteria ()) == 0)) { - // Inform the caller that the - // Event_Comm::Consumer * is already being - // used. - - errno = EADDRINUSE; - ACE_ERROR ((LM_ERROR, - "duplicate entry for receiver %s with criteria \"%s\"", - receiver_ref->marker (), - filtering_criteria)); - // Raise exception here??? + // Inform the caller that the <Event_Comm::Consumer> * is + // already being used. + + TAO_TRY_ENV.exception (new Event_Comm::Notifier::CannotSubscribe + ("Duplicate consumer and filtering criteria found.\n")); return; } } @@ -175,70 +181,73 @@ Notifier_i::subscribe (Event_Comm::Consumer_ptr receiver_ref, // new entry! Consumer_Entry *nr_entry; ACE_NEW (nr_entry, - Consumer_Entry (receiver_ref, + Consumer_Entry (consumer_ref, filtering_criteria)); // Try to add new <Consumer_Entry> to the map. - /*else*/ if (this->map_.bind (key, nr_entry) == -1) + if (this->map_.bind (nr_entry->consumer(), nr_entry) == -1) { // Prevent memory leaks. delete nr_entry; - // Raise exception here... - ACE_ERROR ((LM_ERROR, - "%p\n", - "bind failed")); + TAO_TRY_ENV.exception (new Event_Comm::Notifier::CannotSubscribe + ("Failed to add Consumer to internal map\n")); } } -// Remove a receiver from the table. +// Remove a consumer from the table. void -Notifier_i::unsubscribe (Event_Comm::Consumer_ptr receiver_ref, +Notifier_i::unsubscribe (Event_Comm::Consumer_ptr consumer_ref, const char *filtering_criteria, CORBA::Environment &TAO_TRY_ENV) { ACE_DEBUG ((LM_DEBUG, - "in Notifier_i::unsubscribe for %s\n", - receiver_ref->marker ())); + "in Notifier_i::unsubscribe for %x\n", + consumer_ref)); + Consumer_Entry *nr_entry = 0; - ACE_SString key; MAP_ITERATOR mi (this->map_); int found = 0; - // Don't make a copy since we are deleting... - key.rep ((char *) receiver_ref->marker ()); - // Locate <Consumer_Entry> and free up resources. @@ // Note, we don't properly handle deallocation of KEYS! - for (MAP_ENTRY *me = 0; mi.next (me) != 0; mi.advance ()) + for (MAP_ENTRY *me = 0; + mi.next (me) != 0; + mi.advance ()) { - if (key == me->ext_id_ + Consumer_Entry *nr_entry = me->int_id_; + + // The <_is_equivalent> function checks if objects + // are the same. + // NOTE: this call might not behave well on other + // ORBs since <_is_equivalent> isn't guaranteed to differentiate + // object references. + + // look for a match .. + if (consumer_ref->_is_equivalent (me->ext_id_) && (ACE_OS::strcmp (filtering_criteria, "") == 0 || ACE_OS::strcmp (filtering_criteria, nr_entry->criteria ()) == 0)) { ACE_DEBUG ((LM_DEBUG, - "removed entry %s with criteria \"%s\"\n", - receiver_ref->marker (), + "removed entry %x with criteria \"%s\"\n", + consumer_ref, filtering_criteria)); found = 1; // @@ This is a hack, we need a better approach! - if (this->map_.unbind (key, nr_entry) == -1) - ACE_ERROR ((LM_ERROR, - "unbind failed for %s\n", - receiver_ref->marker ())); + if (this->map_.unbind (me->ext_id_, nr_entry) == -1) + TAO_TRY_ENV.exception (new Event_Comm::Notifier::CannotUnsubscribe + ("Internal map unbind failed.")); else delete nr_entry; } } if (found == 0) - ACE_ERROR ((LM_ERROR, - "entry %s with criteria \"%s\" not found\n", - receiver_ref->marker (), - filtering_criteria)); + TAO_TRY_ENV.exception (new Event_Comm::Notifier::CannotUnsubscribe + ("The Consumer and filtering criteria were not found.")); } -// Disconnect all the receivers, giving them the <reason>. +// Disconnect all the consumers, giving them the <reason>. void Notifier_i::disconnect (const char *reason, @@ -250,18 +259,18 @@ Notifier_i::disconnect (const char *reason, MAP_ITERATOR mi (this->map_); int count = 0; - // Notify all the receivers, taking into account the filtering criteria. + // Notify all the consumers, taking into account the filtering criteria. for (MAP_ENTRY *me = 0; mi.next (me) != 0; mi.advance ()) { - Event_Comm::Consumer *receiver_ref = me->int_id_->receiver (); - ACE_ASSERT (receiver_ref->marker () != 0); + Event_Comm::Consumer_ptr consumer_ref = me->ext_id_; //int_id_->consumer (); + ACE_ASSERT (consumer_ref != 0); ACE_DEBUG ((LM_DEBUG, - "disconnecting client %s\n", - receiver_ref->marker ())); + "disconnecting client %x\n", + consumer_ref)); TAO_TRY { - receiver_ref->disconnect (reason, TAO_TRY_ENV); + consumer_ref->disconnect (reason, TAO_TRY_ENV); } TAO_CATCHANY { @@ -279,14 +288,14 @@ Notifier_i::disconnect (const char *reason, this->map_.close (); if (count == 1) ACE_DEBUG ((LM_DEBUG, - "there was 1 receiver\n")); + "there was 1 consumer\n")); else ACE_DEBUG ((LM_DEBUG, - "there were %d receivers\n", + "there were %d consumers\n", count)); } -// Notify all receivers whose filtering criteria match the event. +// Notify all consumers whose filtering criteria match the event. void Notifier_i::push (const Event_Comm::Event &event, @@ -294,34 +303,35 @@ Notifier_i::push (const Event_Comm::Event &event, { ACE_DEBUG ((LM_DEBUG, "in Notifier_i::send_notification = %s\n", - event.tag_)); + (const char *)event.tag_)); MAP_ITERATOR mi (this->map_); int count = 0; - // Notify all the receivers. + // Notify all the consumers. + // @@ Later on we need to consider the filtering_criteria! for (MAP_ENTRY *me = 0; mi.next (me) != 0; mi.advance ()) { - Event_Comm::Consumer *receiver_ref = me->int_id_->receiver (); - ACE_ASSERT (receiver_ref->marker () != 0); - const char *regexp = me->int_id_->regexp (); + Event_Comm::Consumer_ptr consumer_ref = me->int_id_->consumer (); + ACE_ASSERT (consumer_ref != 0); + ACE_CString regexp (me->int_id_->regexp ()); const char *criteria = me->int_id_->criteria (); - ACE_ASSERT (regexp); + ACE_ASSERT (regexp.fast_rep()); ACE_ASSERT (criteria); // Do a regular expression comparison to determine matching. if (ACE_OS::strcmp ("", criteria) == 0 // Everything matches the wildcard. - // || ACE_OS::strcmp (event.tag_, regexp) == 0) - || ACE_OS::step (event.tag_, regexp) != 0) + || ACE_OS::strcmp (event.tag_, regexp.fast_rep()) == 0 + || ACE_OS::step (event.tag_, regexp.rep()) != 0) { ACE_DEBUG ((LM_DEBUG, - "string %s matched regexp \"%s\" for client %s\n", - event.tag_, me->int_id_->criteria (), - receiver_ref->marker ())); + "string %s matched regexp \"%s\" for client %x\n", + (const char *)event.tag_, me->int_id_->criteria (), + consumer_ref)); TAO_TRY { - receiver_ref->push (event, TAO_TRY_ENV); + consumer_ref->push (event, TAO_TRY_ENV); TAO_CHECK_ENV; } TAO_CATCHANY @@ -336,10 +346,10 @@ Notifier_i::push (const Event_Comm::Event &event, if (count == 1) ACE_DEBUG ((LM_DEBUG, - "there was 1 receiver\n")); + "there was 1 consumer\n")); else ACE_DEBUG ((LM_DEBUG, - "there were %d receivers\n", + "there were %d consumers\n", count)); } diff --git a/TAO/examples/Event_Comm/Event_Comm_i.h b/TAO/examples/Event_Comm/Event_Comm_i.h index 3ccf5191d88..f5f37f15dfb 100644 --- a/TAO/examples/Event_Comm/Event_Comm_i.h +++ b/TAO/examples/Event_Comm/Event_Comm_i.h @@ -26,7 +26,7 @@ #include "ace/SString.h" #include "Event_CommS.h" -class Consumer_i : public POA_Event_Comm :: Consumer +class Consumer_i : public POA_Event_Comm::Consumer { // = TITLE // Defines the implementation class for event <Consumers>. @@ -38,47 +38,45 @@ public: ~Consumer_i (void); // Destructor. - virtual void push ( - const Event_Comm::Event & event, - CORBA::Environment &TAO_TRY_ENV); + virtual void push (const Event_Comm::Event & event, + CORBA::Environment &TAO_TRY_ENV); // Pass the <event> to the <Consumer>. - virtual void disconnect ( - const char * reason, - CORBA::Environment &TAO_TRY_ENV); - // Disconnect the <Consumer> from the <Notifier>, - // giving it the <reason>. + virtual void disconnect (const char * reason, + CORBA::Environment &TAO_TRY_ENV); + // Disconnect the <Consumer> from the <Notifier>, giving it the + // <reason>. }; // Forward reference. class Consumer_Entry; -class Notifier_i : public POA_Event_Comm :: Notifier +class Notifier_i : public POA_Event_Comm::Notifier { // = TITLE // Defines the implementation class for event <Notifiers>. public: enum { - DEFAULT_SIZE = 1024 // Default max number of Event_Comm::Consumers. + DEFAULT_SIZE = 1024 + // Default max number of Event_Comm::Consumers. }; Notifier_i (size_t size_hint = Notifier_i::DEFAULT_SIZE); // Initialize a Notifier_i object with the specified size hint. virtual void disconnect (const char *reason, - CORBA::Environment &TAO_TRY_ENV); + CORBA::Environment &TAO_TRY_ENV); // Disconnect all the receivers, giving them the <reason>. virtual void push (const Event_Comm::Event &event, - CORBA::Environment &TAO_TRY_ENV); - // Send the <event> to all the consumers who have subscribed - // and who match the filtering criteria. - - virtual void subscribe ( - Event_Comm::Consumer_ptr Consumer, - const char * filtering_criteria, - CORBA::Environment &TAO_TRY_ENV); + CORBA::Environment &TAO_TRY_ENV); + // Send the <event> to all the consumers who have subscribed and who + // match the filtering criteria. + + virtual void subscribe (Event_Comm::Consumer_ptr Consumer, + const char * filtering_criteria, + CORBA::Environment &TAO_TRY_ENV); // Subscribe the <Consumer> to receive events that match // <filtering_criteria> applied by the <Notifier>. @@ -88,16 +86,15 @@ public: // Unsubscribe the <Consumer>. private: - typedef ACE_Map_Manager <ACE_SString, Consumer_Entry *, ACE_Null_Mutex> + typedef ACE_Map_Manager <Event_Comm::Consumer_ptr, Consumer_Entry *, ACE_Null_Mutex> MAP_MANAGER; - typedef ACE_Map_Iterator <ACE_SString, Consumer_Entry *, ACE_Null_Mutex> + typedef ACE_Map_Iterator <Event_Comm::Consumer_ptr, Consumer_Entry *, ACE_Null_Mutex> MAP_ITERATOR; - typedef ACE_Map_Entry <ACE_SString, Consumer_Entry *> + typedef ACE_Map_Entry <Event_Comm::Consumer_ptr, Consumer_Entry *> MAP_ENTRY; MAP_MANAGER map_; - // Table that maps a <Event_Comm::Consumer *> to a <Consumer_Entry - // *>. + // Table that maps a <Event_Comm::Consumer *> to a <Consumer_Entry *>. }; #endif /* _EVENT_COMM_I_H */ diff --git a/TAO/examples/Event_Comm/Makefile b/TAO/examples/Event_Comm/Makefile index 6f5a4ffc51d..226e8f6f90e 100644 --- a/TAO/examples/Event_Comm/Makefile +++ b/TAO/examples/Event_Comm/Makefile @@ -20,7 +20,8 @@ PROG_SRCS = Consumer_Handler.cpp \ Supplier_Input_Handler.cpp \ consumer.cpp \ notifier.cpp \ - supplier.cpp + supplier.cpp \ + Notifier_Server.cpp IDL_SRC = Event_CommS.cpp Event_CommC.cpp @@ -40,7 +41,10 @@ SUPPLIER_OBJS = Event_CommC.o \ Notifier_Handler.o \ supplier.o -NOTIFIER_OBJS = Notifier_Handler.o \ +NOTIFIER_OBJS = Event_CommC.o \ + Event_CommS.o \ + Notifier_Server.o \ + Event_Comm_i.o \ notifier.o BIN = supplier \ diff --git a/TAO/examples/Event_Comm/Notifier_Handler.cpp b/TAO/examples/Event_Comm/Notifier_Handler.cpp index a3abcf5a6f2..57093beac9e 100644 --- a/TAO/examples/Event_Comm/Notifier_Handler.cpp +++ b/TAO/examples/Event_Comm/Notifier_Handler.cpp @@ -4,8 +4,20 @@ ACE_RCSID(Supplier, Notifier_Handler, "$Id$") +Notifier_Handler::Notifier_Handler (void) +{ + // No-Op. +} + +// Destroy a Notifier target object. + +Notifier_Handler::~Notifier_Handler (void) +{ + this->handle_close (); +} + int -Notifier_Handler::handle_close (ACE_HANDLE, ACE_Reactor_Mask) +Notifier_Handler::handle_close (void) { if (this->notifier_ != 0) { @@ -13,8 +25,6 @@ Notifier_Handler::handle_close (ACE_HANDLE, ACE_Reactor_Mask) "closing down Notifier_Handler\n")); CORBA::release (this->notifier_); this->notifier_ = 0; - // *Must* be allocated dyanmically! - ::operator delete ((void *) this); } return 0; @@ -36,29 +46,62 @@ Notifier_Handler::notifier (Event_Comm::Notifier *notifier) } } -Notifier_Handler::Notifier_Handler() -{ - // get the notifier from the naming service. -} -// Create and initialize a Notifier target object. -Notifier_Handler::Notifier_Handler (const char *service_location, - const char *marker, - int putit) -{ - // Create a notifier object using the implementation class - // Notifier_i. - /*ACE_NEW (this->notifier_, - TIE_Event_Comm_Notifier (Notifier_i) (new Notifier_i, - marker)); - */ - //@@ declare and orb and a notifir_var in the .h - // init the orb, register the object with the orb and the naming service -} -// Destroy a Notifier target object. +// Init function. -Notifier_Handler::~Notifier_Handler (void) +int +Notifier_Handler::init (int argc, char *argv[]) { - this->handle_close (); + TAO_TRY + { + // Retrieve the ORB. + this->orb_ = CORBA::ORB_init (argc, + argv, + 0, + TAO_TRY_ENV); + TAO_CHECK_ENV; + + // Initialization of the naming service. + if (this->naming_client_.init (orb_.in ()) != 0) + ACE_ERROR_RETURN ((LM_ERROR, + "(%P|%t) Unable to initialize " + "the TAO_Naming_Client. \n"), + -1); + + ACE_DEBUG ((LM_DEBUG, + "after naming_client init \n")); + + CosNaming::Name notifier_ref_name (1); + notifier_ref_name.length (1); + notifier_ref_name[0].id = + CORBA::string_dup (NOTIFIER_BIND_NAME); + + ACE_DEBUG ((LM_DEBUG, + "after using CosNaming::Name\n")); + + CORBA::Object_var notifier_obj = + this->naming_client_->resolve (notifier_ref_name, + TAO_TRY_ENV); + TAO_CHECK_ENV; + + + ACE_DEBUG ((LM_DEBUG, + "after naming_client resolve \n")); + // The CORBA::Object_var object is downcast to Echo_var using + // the <_narrow> method. + this->notifier_ = + Event_Comm::Notifier::_narrow (notifier_obj.in (), + TAO_TRY_ENV); + ACE_DEBUG ((LM_DEBUG, + "after notifier\n")); + TAO_CHECK_ENV; + + } + TAO_CATCHANY + { + TAO_TRY_ENV.print_exception ("Notifier_Handler::init\n"); + return -1; + } + TAO_ENDTRY; } diff --git a/TAO/examples/Event_Comm/Notifier_Handler.h b/TAO/examples/Event_Comm/Notifier_Handler.h index e4b0ddd0b9a..7e2c7ad7952 100644 --- a/TAO/examples/Event_Comm/Notifier_Handler.h +++ b/TAO/examples/Event_Comm/Notifier_Handler.h @@ -19,34 +19,44 @@ #include "ace/CORBA_Handler.h" #include "Event_Comm_i.h" +#include "orbsvcs/Naming/Naming_Utils.h" +#include "orbsvcs/CosNamingC.h" class Notifier_Handler - // = TITLE - // Integrate CORBA with the ACE ACE_Reactor. { + // = TITLE + // Holds the <Event_Comm::Notifier> object which is obtained from the naming service. public: - Notifier_Handler (); - // This constructor gets the notifier object from the Naming Service. + // = Initialization and termination methods. + Notifier_Handler (void); + // This constructor gets the notifier object from the Naming + // Service. - Notifier_Handler (const char *service_location, - const char *marker = "notifier", - int putit = 1); // Default marker name. - // This constructor starts up the ORB etc and - // registers the notifier object with the Naming Sxervice. + ~Notifier_Handler (void); + // Destructor. + int init (int argc, char *argv[]); + // Initialize the client communication endpoint with server. + + // = Accessors. Event_Comm::Notifier *notifier (void); void notifier (Event_Comm::Notifier *); - virtual int handle_close (ACE_HANDLE = ACE_INVALID_HANDLE, - ACE_Reactor_Mask = ACE_Event_Handler::NULL_MASK); + virtual int handle_close (void); // Close down the handler. private: - ~Notifier_Handler (void); - // Ensure dynamic allocation. - Event_Comm::Notifier *notifier_; // Pointer to an <Event_Comm::Notifier> object. + + TAO_Naming_Client naming_client_; + // An instance of the name client used for resolving the factory + // objects. + + CORBA::ORB_var orb_; + // Remember our orb. }; +#define NOTIFIER_BIND_NAME "Notifier" + #endif /* _NOTIFIER_HANDLER_H */ diff --git a/TAO/examples/Event_Comm/Notifier_Server.cpp b/TAO/examples/Event_Comm/Notifier_Server.cpp new file mode 100644 index 00000000000..a5269e6c3a6 --- /dev/null +++ b/TAO/examples/Event_Comm/Notifier_Server.cpp @@ -0,0 +1,137 @@ +#include "Notifier_Server.h" + +ACE_RCSID(Echo, Server_i, "$Id$") + +// Constructor. +Notifier_Server::Notifier_Server (void) +{ + // no-op. +} + +// Destructor. +Notifier_Server::~Notifier_Server (void) +{ + // no-op. +} + +int +Notifier_Server::close (void) +{ + TAO_TRY + { + // Name the object. + CosNaming::Name notifier_obj_name (1); + notifier_obj_name.length (1); + notifier_obj_name[0].id = CORBA::string_dup (NOTIFIER_BIND_NAME); + + this->naming_server_->unbind (notifier_obj_name, + TAO_TRY_ENV); + TAO_CHECK_ENV; + + // Instruct the ORB to shutdown. + this->orb_manager_.orb ()->shutdown (); + } + TAO_CATCHANY + { + return -1; + } + TAO_ENDTRY; + + return 0; +} + +// The naming service is initialized and the naming context as well as +// the object name is bound to the naming server. + +int +Notifier_Server::init_naming_service (CORBA::Environment &TAO_TRY_ENV) +{ + TAO_TRY + { + CORBA::ORB_var orb = this->orb_manager_.orb (); + + PortableServer::POA_var child_poa + = this->orb_manager_.child_poa (); + + TAO_debug_level = 1; + + if (this->naming_server_.init (orb.in (), + child_poa.in ()) == -1) + ACE_ERROR_RETURN ((LM_ERROR, + "Failed to initialize TAO_Naming_Server\n"), + -1); + + // Register the object implementation with the POA. + Notifier_var notifier_obj = + this->servant_._this (TAO_TRY_ENV); + TAO_CHECK_ENV; + + // Name the object. + CosNaming::Name notifier_obj_name (1); + notifier_obj_name.length (1); + notifier_obj_name[0].id = + CORBA::string_dup (NOTIFIER_BIND_NAME); + TAO_CHECK_ENV; + + // Now, attach the object name to the context. + this->naming_server_->bind (notifier_obj_name, + notifier_obj.in (), + TAO_TRY_ENV); + TAO_CHECK_ENV; + } + TAO_CATCHANY + { + TAO_TRY_ENV.print_exception ("Notifier_Server::init_naming_service\n"); + return -1; + } + TAO_ENDTRY; + + return 0; +} + + +// Initialize the server. +int +Notifier_Server::init (int argc, + char *argv[], + CORBA::Environment &TAO_TRY_ENV) +{ + // Call the init of <TAO_ORB_Manager> to initialize the ORB and + // create the child poa under the root POA. + if (this->orb_manager_.init_child_poa (argc, + argv, + "child_poa", + TAO_TRY_ENV) == -1) + ACE_ERROR_RETURN ((LM_ERROR, + "%p\n", + "init_child_poa"), + -1); + TAO_CHECK_ENV_RETURN (TAO_TRY_ENV, + -1); + + CORBA::ORB_var orb = + this->orb_manager_.orb (); + + // Activate the servant in the POA. + CORBA::String_var str = + this->orb_manager_.activate_under_child_poa (NOTIFIER_BIND_NAME, + &this->servant_, + TAO_TRY_ENV); + return this->init_naming_service (TAO_TRY_ENV); +} + +int +Notifier_Server::run (CORBA::Environment &TAO_TRY_ENV) +{ + // Run the main event loop for the ORB. + if (this->orb_manager_.run (TAO_TRY_ENV) == -1) + ACE_ERROR_RETURN ((LM_ERROR, + "Notifier_Server::run"), + -1); + return 0; +} + +ACE_Reactor* Notifier_Server::reactor(void) +{ + return TAO_ORB_Core_instance ()->reactor (); +} diff --git a/TAO/examples/Event_Comm/Notifier_Server.h b/TAO/examples/Event_Comm/Notifier_Server.h new file mode 100644 index 00000000000..8f96c6565c3 --- /dev/null +++ b/TAO/examples/Event_Comm/Notifier_Server.h @@ -0,0 +1,72 @@ +/* -*- C++ -*- */ +// $Id$ + +// ============================================================================ +// +// = LIBRARY +// EventComm +// +// = FILENAME +// Notifier_Server.h +// +// = AUTHOR +// Pradeep Gore <pradeep@cs.wustl.edu> +// +// ============================================================================ + +#if !defined (_NOTIFIER_SERVER_H) +#define _NOTIFIER_SERVER_H + +#include "Event_Comm_i.h" +#include "tao/TAO.h" +#include "orbsvcs/CosNamingS.h" +#include "orbsvcs/Naming/Naming_Utils.h" + +class Notifier_Server +{ + // = TITLE + // The Notifier Server that accepts connections from Consumers + // and Suppliers. + +public: + // = Initialization and termination methods. + Notifier_Server (void); + // Constructor. + + ~Notifier_Server (void); + // Destructor. + + int init (int argc, char *argv[], CORBA::Environment &env); + // Initialize the Server state. + + int run (CORBA::Environment &env); + // Run the orb. + + int close (void); + // Close the naming service. + + ACE_Reactor* reactor(void); + // Reactor accessor. + +private: + int init_naming_service (CORBA::Environment &env); + // Initialises the name server and registers the Notifier server + // object name with the name server. + + TAO_ORB_Manager orb_manager_; + // The ORB manager. + + Notifier_i servant_; + // Servant for the Notifier interface. + + CosNaming::NamingContext_var naming_context_; + // Naming context for the naming service. + + TAO_Naming_Server naming_server_; + // An instance of the name server, wherein the naming context + // containing the factory of objects will be registered. +}; + +#define NOTIFIER_BIND_NAME "Notifier" + +#endif /* _NOTIFIER_SERVER_H */ diff --git a/TAO/examples/Event_Comm/Supplier_Input_Handler.cpp b/TAO/examples/Event_Comm/Supplier_Input_Handler.cpp index fba76cd857c..b5ccb29265a 100644 --- a/TAO/examples/Event_Comm/Supplier_Input_Handler.cpp +++ b/TAO/examples/Event_Comm/Supplier_Input_Handler.cpp @@ -6,6 +6,19 @@ ACE_RCSID(Supplier, Supplier_Input_Handler, "$Id$") +Supplier_Input_Handler::Supplier_Input_Handler () + : notifier_ (0), + handle_ (0) +{ + // No-Op. +} + +Supplier_Input_Handler::~Supplier_Input_Handler (void) +{ + ACE_DEBUG ((LM_DEBUG, + "closing down Supplier_Input_Handler::~Supplier_Input_Handler\n")); +} + int Supplier_Input_Handler::handle_close (ACE_HANDLE, ACE_Reactor_Mask) { @@ -38,38 +51,31 @@ Supplier_Input_Handler::handle_close (ACE_HANDLE, ACE_Reactor_Mask) "%p\n", "remove_handler")); - // *Must* be allocated dyanmically! - ::operator delete ((void *) this); return 0; } -Supplier_Input_Handler::Supplier_Input_Handler (Notifier_Handler *notifier, - ACE_HANDLE handle) // Use stdin by default. - : notifier_ (notifier), - handle_ (handle) +int Supplier_Input_Handler::initialize (Notifier_Handler *notifier, + ACE_HANDLE handle) // Use stdin by default. { + notifier_ = notifier; + handle_ = handle; + // Register ourselves with the ACE_Reactor so that input events // cause our handle_input() method to be dispatched automatically. if (ACE_Reactor::instance ()->register_handler (this, ACE_Event_Handler::READ_MASK) == -1) - ACE_ERROR ((LM_ERROR, + ACE_ERROR_RETURN ((LM_ERROR, "%p\n", - "register_handler")); + "register_handler"), -1); this->fp_ = ACE_OS::fdopen (handle, "r"); if (this->fp_ == 0) - ACE_ERROR ((LM_ERROR, - "%p\n", - "fdopen")); -} - -Supplier_Input_Handler::~Supplier_Input_Handler (void) -{ - ACE_DEBUG ((LM_DEBUG, - "closing down Supplier_Input_Handler::~Supplier_Input_Handler\n")); - this->handle_close (); + ACE_ERROR_RETURN ((LM_ERROR, + "%p\n", + "fdopen"), -1); + return 0; } ACE_HANDLE diff --git a/TAO/examples/Event_Comm/Supplier_Input_Handler.h b/TAO/examples/Event_Comm/Supplier_Input_Handler.h index ded79677c6b..041e3797a68 100644 --- a/TAO/examples/Event_Comm/Supplier_Input_Handler.h +++ b/TAO/examples/Event_Comm/Supplier_Input_Handler.h @@ -36,9 +36,14 @@ class Supplier_Input_Handler : public ACE_Service_Object // be more selective and only send to those Consumers // whose filtering criteria matches! public: - Supplier_Input_Handler (Notifier_Handler *, - ACE_HANDLE = ACE_STDIN); - // Use stdin by default. + Supplier_Input_Handler (); + // Constructor. + + ~Supplier_Input_Handler (void); + // Destructor. + + int initialize (Notifier_Handler *, ACE_HANDLE = ACE_STDIN); + // Initialization, uses stdin by default. virtual int handle_input (ACE_HANDLE); // Frame input events and notify <Consumers>. @@ -59,10 +64,6 @@ protected: FILE *fp_; // Pointer to an input ACE_FILE. - -private: - ~Supplier_Input_Handler (void); - // Ensure dynamic allocation. }; #endif /* SUPPLIER_INPUT_HANDLER_H */ diff --git a/TAO/examples/Event_Comm/consumer.cpp b/TAO/examples/Event_Comm/consumer.cpp index 2056af45f4a..e0145302aa8 100644 --- a/TAO/examples/Event_Comm/consumer.cpp +++ b/TAO/examples/Event_Comm/consumer.cpp @@ -8,32 +8,44 @@ ACE_RCSID(Consumer, consumer, "$Id$") class Consumer : public ACE_Event_Handler { // = TITLE - // Consumer driver for the Publish/Subscribe example. + // Consumer driver for the Publish/Subscribe example. + // = DESCRIPTION + // The Consumer holds the <Consumer_Input_Handler> and <Cosumer_Handler> objects. + public: - Consumer (int argc, char *argv[]); + // = Initialization and Termination methods. + Consumer (); ~Consumer (void); + int initialize (int argc, char *argv[]); + // initialization method. + void run (void); // Execute the consumer; private: virtual int handle_signal (int signum, siginfo_t *, ucontext_t *); + // signal handler method. - virtual int handle_close (ACE_HANDLE, ACE_Reactor_Mask); - - Consumer_Input_Handler *ih_; + Consumer_Input_Handler ih_; // Handler for keyboard input. - Consumer_Handler *ch_; + Consumer_Handler ch_; // Handler for CORBA Consumer. }; -int -Consumer::handle_close (ACE_HANDLE, ACE_Reactor_Mask) +Consumer::Consumer (void) + : ih_ (), + ch_ () { - ACE_DEBUG ((LM_DEBUG, - "closing down Consumer\n")); - return 0; + // No-Op. +} + +Consumer::~Consumer (void) +{ + // Allow the handlers to clean up. + this->ih_.handle_close(); + this->ch_.handle_close(); } int @@ -44,55 +56,53 @@ Consumer::handle_signal (int signum, siginfo_t *, ucontext_t *) signum)); // Indicate that the consumer initiated the shutdown. - this->ih_->consumer_initiated_shutdown (1); + this->ih_.consumer_initiated_shutdown (1); - // Shut down the event loop. - ACE_Reactor::end_event_loop(); + // Shut down the ORB + ch_.close(); return 0; } -// Run the event loop until someone calls -// calls ACE_Reactor::end_event_loop(). - void Consumer::run (void) { - if (ACE_Reactor::run_event_loop () == -1) - ACE_ERROR ((LM_ERROR, - "%p\n", - "run_reactor_event_loop")); + // run the <Consumer_Handler>'s ORB + ch_.run(); } -Consumer::Consumer (int argc, char *argv[]) - : ih_ (0), - ch_ (0) +int Consumer::initialize (int argc, char *argv[]) { - ACE_DEBUG ((LM_DEBUG, - "no config file, using static binding\n")); - // The constructor registers the handlers... - ACE_NEW (this->ch_, - Consumer_Handler (argc, argv)); - ACE_NEW (this->ih_, - Consumer_Input_Handler (this->ch_)); - - if (ACE_Reactor::instance ()->register_handler (SIGINT, this) == -1) - ACE_ERROR ((LM_ERROR, - "%p\n", - "register_handler")); + // initialize the <Consumer_Handler>. + if (this->ch_.init (argc, argv) == -1) + ACE_ERROR_RETURN ((LM_ERROR, + "%p\n", + "Consumer_Handler failed to initialize\n"), -1); + + // initialize the <Consumer_Input_Handler>. + if (this->ih_.initialize (&this->ch_) == -1) + ACE_ERROR_RETURN ((LM_ERROR, + "%p\n", + "Consumer_Input_Handler failed to initialize\n"), -1); + + if (this->ch_.reactor()->register_handler (SIGINT, this) == -1) + ACE_ERROR_RETURN ((LM_ERROR, + "%p\n", + "register_handler"), -1); + return 0; } -Consumer::~Consumer (void) -{ - // Free up the handlers if they were statically bound. - this->ih_->handle_close (); - this->ch_->handle_close (); -} + int main (int argc, char *argv[]) { // Initialize the supplier and consumer object references. - Consumer consumer (argc, argv); + Consumer consumer; + + if (consumer.initialize (argc, argv) == -1) + ACE_ERROR_RETURN ((LM_ERROR, + "%p\n", + "Consumer init failed\n"), 1); // Loop forever handling events. consumer.run (); diff --git a/TAO/examples/Event_Comm/notifier.cpp b/TAO/examples/Event_Comm/notifier.cpp index a676f546e58..d1fbf4c81e0 100644 --- a/TAO/examples/Event_Comm/notifier.cpp +++ b/TAO/examples/Event_Comm/notifier.cpp @@ -1,6 +1,6 @@ // $Id$ -#include "Notifier_Handler.h" +#include "Notifier_Server.h" ACE_RCSID(Notifier, notifier, "$Id$") @@ -10,69 +10,69 @@ class Notifier : public ACE_Event_Handler // Notifier driver for the TAO Publish/Subscribe example. // // = DESCRIPTION - // The executable file generated from this code should be - // registered (under the name 'logger'). + // The driver class for the <Event_Comm::Notifier> object. public: + // = Initialization and termination methods. Notifier (int argc, char *argv[]); + // Constructor. + ~Notifier (void); + // Destructor. void run (void); // Execute the notifier. private: - virtual int handle_signal (int signum, siginfo_t *, ucontext_t *); - - virtual int handle_close (ACE_HANDLE, ACE_Reactor_Mask); + virtual int handle_signal (int signum, + siginfo_t *, + ucontext_t *); + // Handle signals that shut us down. - Notifier_Handler *nh_; - // Handler for CORBA Notifier. - - // Notifier_Input_Handler *ih_; + Notifier_Server ns_; + // The notifier server. }; int -Notifier::handle_close (ACE_HANDLE, ACE_Reactor_Mask) -{ - ACE_DEBUG ((LM_DEBUG, - "closing down Notifier\n")); - return 0; -} - -int Notifier::handle_signal (int signum, siginfo_t *, ucontext_t *) { ACE_DEBUG ((LM_DEBUG, - "%S\n", + "got signal in handle_signal %S\n", signum)); - ACE_Reactor::end_event_loop (); + + // tell the <Notifier_Server> to shut down the ORB. + ns_.close (); return 0; } void Notifier::run (void) - { - if (ACE_Reactor::run_event_loop () == -1) - ACE_ERROR ((LM_ERROR, - "%p\n", - "run_reactor_event_loop")); + TAO_TRY + { + ns_.run (TAO_TRY_ENV); + TAO_CHECK_ENV; + } + TAO_CATCHANY + { + } + TAO_ENDTRY; } Notifier::Notifier (int argc, char *argv[]) - : //ih_ (0), - nh_ (0) { - ACE_DEBUG ((LM_DEBUG, - "no config file, using static binding\n")); - - // Pass in program exec name to use a service_location! - ACE_NEW (this->nh_, - Notifier_Handler (argv[0], - "notifier")); - //ACE_NEW (this->ih_, - // Notifier_Input_Handler (this->nh_)); - - if (ACE_Reactor::instance ()->register_handler (SIGINT, this) == -1) + TAO_TRY + { + ns_.init (argc, argv, TAO_TRY_ENV); + } + TAO_CATCHANY + { + TAO_TRY_ENV.print_exception ("Notifier_Server.init failed\n "); + } + TAO_ENDTRY; + + // Register with the ORB's Reactor to receive a signal to shut us + // down. + if (ns_.reactor ()->register_handler (SIGINT, this) == -1) ACE_ERROR ((LM_ERROR, "%p\n", "register_handler")); @@ -80,8 +80,8 @@ Notifier::Notifier (int argc, char *argv[]) Notifier::~Notifier (void) { - // Free up the handlers if they were statically bound. - this->nh_->handle_close (); + // cleanup. + this->ns_.close (); } int diff --git a/TAO/examples/Event_Comm/supplier.cpp b/TAO/examples/Event_Comm/supplier.cpp index 7cc760626c5..a5d2f7cbb8b 100644 --- a/TAO/examples/Event_Comm/supplier.cpp +++ b/TAO/examples/Event_Comm/supplier.cpp @@ -11,11 +11,18 @@ class Supplier : public ACE_Event_Handler // Supplier driver for the TAO Publish/Subscribe example. // // = DESCRIPTION - // The executable file generated from this code should be - // registered (under the name 'logger'). + // This class starts up the <Supplier_Input_Handler> and <Notifier_Handler> + // objects. public: - Supplier (int argc, char *argv[]); + // Initialization and Termination methods. + Supplier (void); + // Constructor. + ~Supplier (void); + // Destructor. + + int init (int argc, char *argv[]); + // Initialization method. returns 0 on success, -1 on error. void run (void); // Execute the supplier. @@ -23,21 +30,24 @@ public: private: virtual int handle_signal (int signum, siginfo_t *, ucontext_t *); - virtual int handle_close (ACE_HANDLE, ACE_Reactor_Mask); - - Supplier_Input_Handler *ih_; + Supplier_Input_Handler ih_; // Handler for keyboard input. - Notifier_Handler *nh_; + Notifier_Handler nh_; // The notifier handler. }; -int -Supplier::handle_close (ACE_HANDLE, ACE_Reactor_Mask) +Supplier::Supplier (void) + :nh_ (), + ih_ () + { - ACE_DEBUG ((LM_DEBUG, - "closing down Supplier\n")); - return 0; + // No-Op. +} + +Supplier::~Supplier (void) +{ + // No-Op. } int @@ -46,6 +56,10 @@ Supplier::handle_signal (int signum, siginfo_t *, ucontext_t *) ACE_DEBUG ((LM_DEBUG, "%S\n", signum)); + + this->ih_.handle_close (); + this->nh_.handle_close (); + ACE_Reactor::end_event_loop (); return 0; } @@ -59,33 +73,37 @@ Supplier::run (void) "run_reactor_event_loop")); } -Supplier::Supplier (int argc, char *argv[]) - : ih_ (0) +int +Supplier::init (int argc, char *argv[]) { - ACE_DEBUG ((LM_DEBUG, - "no config file, using static binding\n")); + if (this->nh_.init (argc, argv) == -1) + ACE_ERROR_RETURN ((LM_ERROR, + "%p\n", + "Notifier_Handler did not init\n"), -1); - ACE_NEW (this->nh_, Notifier_Handler()); - ACE_NEW (this->ih_, - Supplier_Input_Handler (this->nh_)); + if (this->ih_.initialize (&nh_) == -1) + ACE_ERROR_RETURN ((LM_ERROR, + "%p\n", + "Supplier Input handler did not init\n"), -1); if (ACE_Reactor::instance ()->register_handler (SIGINT, this) == -1) - ACE_ERROR ((LM_ERROR, - "%p\n", - "register_handler")); + ACE_ERROR_RETURN ((LM_ERROR, + "%p\n", + "register_handler"), -1); } -Supplier::~Supplier (void) -{ - // Free up the handlers if they were statically bound. - this->ih_->handle_close (); -} + int main (int argc, char *argv[]) { // Initialize server daemon. - Supplier supplier (argc, argv); + Supplier supplier; + + if (supplier.init (argc, argv) == -1) + ACE_ERROR_RETURN ((LM_ERROR, + "%p\n", + "supplier init failed"), 1); // Loop forever handling events. supplier.run (); |