Merge pull request #1551 from DOCGroup/plm_jira_333
[ACE_TAO.git] / TAO / examples / Event_Comm / Consumer_Input_Handler.cpp
blob4a6a49ee4a25bdb219d22bdf045a514f87b58173
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)
10 // No-Op.
13 Consumer_Input_Handler::~Consumer_Input_Handler (void)
15 // No-Op.
18 int
19 Consumer_Input_Handler::consumer_initiated_shutdown (void)
21 return this->consumer_initiated_shutdown_;
24 void
25 Consumer_Input_Handler::consumer_initiated_shutdown (int c)
27 this->consumer_initiated_shutdown_ = c;
30 int
31 Consumer_Input_Handler::close (void)
33 ACE_DEBUG ((LM_DEBUG,
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!
46 try
48 // Gracefully shutdown the Receiver by removing it from the
49 // Notifier's internal map.
51 if (notifier != 0)
52 notifier->unsubscribe (receiver,
53 "");
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)
65 ACE_ERROR ((LM_ERROR,
66 "%p\n",
67 "remove_stdin_handler"));
69 return 0;
72 int Consumer_Input_Handler::initialize (Consumer_Handler *ch)
74 receiver_handler_ = ch;
76 if (ACE_Event_Handler::register_stdin_handler
77 (this,
78 TAO_ORB_Core_instance ()->reactor (),
79 TAO_ORB_Core_instance ()->thr_mgr ()) == -1)
80 ACE_ERROR_RETURN ((LM_ERROR,
81 "%p\n",
82 "register_stdin_handler"),
83 -1);
84 return 0;
87 int
88 Consumer_Input_Handler::handle_input (ACE_HANDLE h)
90 char buf[BUFSIZ];
92 // Read up to BUFSIZ worth of data from ACE_HANDLE h.
93 ssize_t n = ACE_OS::read (h, buf, sizeof buf - 1);
95 if (n > 0)
97 // Null terminate the buffer, replacing the '\n' with '\0'.
98 if (buf[n - 1] == '\n')
99 buf[n - 1] = '\0';
100 else
101 buf[n] = '\0';
102 ACE_DEBUG ((LM_DEBUG,
103 "notifying for event %s\n",
104 buf));
106 else
108 // If nothing is read, do nothing.
109 return 0;
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 ();
125 else
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");
141 /* NOTREACHED */
142 return 0;