Changes to attempt to silence bcc64x
[ACE_TAO.git] / TAO / examples / Event_Comm / Consumer_Input_Handler.cpp
blobd20b3ce6fa120219f08e2fc77e619b2871aeff04
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)
10 // No-Op.
13 int
14 Consumer_Input_Handler::consumer_initiated_shutdown ()
16 return this->consumer_initiated_shutdown_;
19 void
20 Consumer_Input_Handler::consumer_initiated_shutdown (int c)
22 this->consumer_initiated_shutdown_ = c;
25 int
26 Consumer_Input_Handler::close ()
28 ACE_DEBUG ((LM_DEBUG,
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!
41 try
43 // Gracefully shutdown the Receiver by removing it from the
44 // Notifier's internal map.
46 if (notifier != 0)
47 notifier->unsubscribe (receiver,
48 "");
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)
60 ACE_ERROR ((LM_ERROR,
61 "%p\n",
62 "remove_stdin_handler"));
64 return 0;
67 int Consumer_Input_Handler::initialize (Consumer_Handler *ch)
69 receiver_handler_ = ch;
71 if (ACE_Event_Handler::register_stdin_handler
72 (this,
73 TAO_ORB_Core_instance ()->reactor (),
74 TAO_ORB_Core_instance ()->thr_mgr ()) == -1)
75 ACE_ERROR_RETURN ((LM_ERROR,
76 "%p\n",
77 "register_stdin_handler"),
78 -1);
79 return 0;
82 int
83 Consumer_Input_Handler::handle_input (ACE_HANDLE h)
85 char buf[BUFSIZ];
87 // Read up to BUFSIZ worth of data from ACE_HANDLE h.
88 ssize_t n = ACE_OS::read (h, buf, sizeof buf - 1);
90 if (n > 0)
92 // Null terminate the buffer, replacing the '\n' with '\0'.
93 if (buf[n - 1] == '\n')
94 buf[n - 1] = '\0';
95 else
96 buf[n] = '\0';
97 ACE_DEBUG ((LM_DEBUG,
98 "notifying for event %s\n",
99 buf));
101 else
103 // If nothing is read, do nothing.
104 return 0;
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 ();
120 else
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");
136 /* NOTREACHED */
137 return 0;