1 #include "Consumer_Input_Handler.h"
2 #include "Consumer_Handler.h"
4 #include "tao/ORB_Core.h"
6 Consumer_Input_Handler::Consumer_Input_Handler ()
7 : receiver_handler_ (0),
8 consumer_initiated_shutdown_ (0)
14 Consumer_Input_Handler::consumer_initiated_shutdown ()
16 return this->consumer_initiated_shutdown_
;
20 Consumer_Input_Handler::consumer_initiated_shutdown (int c
)
22 this->consumer_initiated_shutdown_
= c
;
26 Consumer_Input_Handler::close ()
29 "closing down Consumer::Input_Handler\n"));
31 Event_Comm::Consumer
*receiver
=
32 this->receiver_handler_
->receiver ();
33 Event_Comm::Notifier
*notifier
=
34 this->receiver_handler_
->notifier ();
36 if (this->consumer_initiated_shutdown ())
38 // Only try to unsubscribe if the Consumer initiated the
39 // shutdown. Otherwise, the Notifier initiated it and it has
40 // probably gone away by now!
43 // Gracefully shutdown the Receiver by removing it from the
44 // Notifier's internal map.
47 notifier
->unsubscribe (receiver
,
50 catch (const CORBA::Exception
& ex
)
52 ex
._tao_print_exception ("Consumer_Input_Handler::handle_close\n");
56 // Make sure to cleanup the STDIN handler.
57 if (ACE_Event_Handler::remove_stdin_handler
58 (TAO_ORB_Core_instance ()->reactor (),
59 TAO_ORB_Core_instance ()->thr_mgr ()) == -1)
62 "remove_stdin_handler"));
67 int Consumer_Input_Handler::initialize (Consumer_Handler
*ch
)
69 receiver_handler_
= ch
;
71 if (ACE_Event_Handler::register_stdin_handler
73 TAO_ORB_Core_instance ()->reactor (),
74 TAO_ORB_Core_instance ()->thr_mgr ()) == -1)
75 ACE_ERROR_RETURN ((LM_ERROR
,
77 "register_stdin_handler"),
83 Consumer_Input_Handler::handle_input (ACE_HANDLE h
)
87 // Read up to BUFSIZ worth of data from ACE_HANDLE h.
88 ssize_t n
= ACE_OS::read (h
, buf
, sizeof buf
- 1);
92 // Null terminate the buffer, replacing the '\n' with '\0'.
93 if (buf
[n
- 1] == '\n')
98 "notifying for event %s\n",
103 // If nothing is read, do nothing.
107 Event_Comm::Notifier
*notifier
=
108 this->receiver_handler_
->notifier ();
110 ACE_ASSERT (notifier
!= 0);
112 if (ACE_OS::strncmp (buf
, "quit", 4) == 0)
114 // Consumer wants to shutdown.
115 this->consumer_initiated_shutdown (1);
117 // Tell the main event loop to shutdown.
118 this->receiver_handler_
->shutdown ();
124 Event_Comm::Event event
;
126 event
.tag_
= ACE_OS::strdup (buf
);
128 notifier
->push (event
);
130 catch (const CORBA::Exception
& ex
)
132 ex
._tao_print_exception ("Unexpected exception\n");