1 #include "Consumer_Input_Handler.h"
2 #include "Consumer_Handler.h"
4 #include "tao/ORB_Core.h"
6 Consumer_Input_Handler::Consumer_Input_Handler (void)
7 : receiver_handler_ (0),
8 consumer_initiated_shutdown_ (0)
13 Consumer_Input_Handler::~Consumer_Input_Handler (void)
19 Consumer_Input_Handler::consumer_initiated_shutdown (void)
21 return this->consumer_initiated_shutdown_
;
25 Consumer_Input_Handler::consumer_initiated_shutdown (int c
)
27 this->consumer_initiated_shutdown_
= c
;
31 Consumer_Input_Handler::close (void)
34 "closing down Consumer::Input_Handler\n"));
36 Event_Comm::Consumer
*receiver
=
37 this->receiver_handler_
->receiver ();
38 Event_Comm::Notifier
*notifier
=
39 this->receiver_handler_
->notifier ();
41 if (this->consumer_initiated_shutdown ())
43 // Only try to unsubscribe if the Consumer initiated the
44 // shutdown. Otherwise, the Notifier initiated it and it has
45 // probably gone away by now!
48 // Gracefully shutdown the Receiver by removing it from the
49 // Notifier's internal map.
52 notifier
->unsubscribe (receiver
,
55 catch (const CORBA::Exception
& ex
)
57 ex
._tao_print_exception ("Consumer_Input_Handler::handle_close\n");
61 // Make sure to cleanup the STDIN handler.
62 if (ACE_Event_Handler::remove_stdin_handler
63 (TAO_ORB_Core_instance ()->reactor (),
64 TAO_ORB_Core_instance ()->thr_mgr ()) == -1)
67 "remove_stdin_handler"));
72 int Consumer_Input_Handler::initialize (Consumer_Handler
*ch
)
74 receiver_handler_
= ch
;
76 if (ACE_Event_Handler::register_stdin_handler
78 TAO_ORB_Core_instance ()->reactor (),
79 TAO_ORB_Core_instance ()->thr_mgr ()) == -1)
80 ACE_ERROR_RETURN ((LM_ERROR
,
82 "register_stdin_handler"),
88 Consumer_Input_Handler::handle_input (ACE_HANDLE h
)
92 // Read up to BUFSIZ worth of data from ACE_HANDLE h.
93 ssize_t n
= ACE_OS::read (h
, buf
, sizeof buf
- 1);
97 // Null terminate the buffer, replacing the '\n' with '\0'.
98 if (buf
[n
- 1] == '\n')
102 ACE_DEBUG ((LM_DEBUG
,
103 "notifying for event %s\n",
108 // If nothing is read, do nothing.
112 Event_Comm::Notifier
*notifier
=
113 this->receiver_handler_
->notifier ();
115 ACE_ASSERT (notifier
!= 0);
117 if (ACE_OS::strncmp (buf
, "quit", 4) == 0)
119 // Consumer wants to shutdown.
120 this->consumer_initiated_shutdown (1);
122 // Tell the main event loop to shutdown.
123 this->receiver_handler_
->shutdown ();
129 Event_Comm::Event event
;
131 event
.tag_
= ACE_OS::strdup (buf
);
133 notifier
->push (event
);
135 catch (const CORBA::Exception
& ex
)
137 ex
._tao_print_exception ("Unexpected exception\n");