Merge pull request #1551 from DOCGroup/plm_jira_333
[ACE_TAO.git] / TAO / examples / Event_Comm / Supplier_Input_Handler.cpp
blobcef8fb40b2caba2a46f242ef6c38e28b1af81aa8
1 #include "Event_Comm_i.h"
2 #include "Notifier_Handler.h"
3 #include "Supplier_Input_Handler.h"
4 #include "tao/ORB_Core.h"
6 Supplier_Input_Handler::Supplier_Input_Handler ()
7 : notifier_ (0)
9 // No-Op.
12 Supplier_Input_Handler::~Supplier_Input_Handler (void)
14 ACE_DEBUG ((LM_DEBUG,
15 "closing down Supplier_Input_Handler::~Supplier_Input_Handler\n"));
18 int
19 Supplier_Input_Handler::close (void)
21 ACE_DEBUG ((LM_DEBUG,
22 "closing down Supplier::Supplier_Input_Handler\n"));
24 // Make sure to cleanup the STDIN handler.
25 if (ACE_Event_Handler::remove_stdin_handler
27 TAO_ORB_Core_instance ()->reactor (),
28 TAO_ORB_Core_instance ()->thr_mgr ()) == -1)
29 ACE_ERROR ((LM_ERROR,
30 "%p\n",
31 "remove_stdin_handler"));
32 return 0;
35 int
36 Supplier_Input_Handler::initialize (Notifier_Handler *notifier)
38 notifier_ = notifier;
39 // Register our <Input_Handler> to handle STDIN events, which will
40 // trigger the <handle_input> method to process these events.
42 if (ACE_Event_Handler::register_stdin_handler
43 (this,
44 TAO_ORB_Core_instance ()->reactor (),
45 TAO_ORB_Core_instance ()->thr_mgr ()) == -1)
46 ACE_ERROR_RETURN ((LM_ERROR,
47 "%p\n",
48 "register_stdin_handler"),
49 -1);
52 return 0;
55 // Frame input events and notify <Consumers>.
57 int
58 Supplier_Input_Handler::handle_input (ACE_HANDLE)
60 char buf[BUFSIZ];
62 // Read up to BUFSIZ worth of data from ACE_HANDLE h.
64 if (ACE_OS::fgets (buf,
65 sizeof buf - 1,
66 stdin) == 0)
68 ACE_DEBUG ((LM_DEBUG,
69 "shutting down Supplier_Input_Handler\n"));
70 return 0;
72 else
74 size_t n = ACE_OS::strlen (buf);
76 // Null terminate the buffer, replacing the '\n' with '\0'.
77 if (buf[n - 1] == '\n')
78 buf[n - 1] = '\0';
79 else
80 buf[n] = '\0';
81 ACE_DEBUG ((LM_DEBUG,
82 "notifying for event %s\n",
83 buf));
88 if (ACE_OS::strncmp (buf, "quit", 4) == 0)
89 // Tell the main event loop to shutdown.
90 this->notifier_->shutdown ();
91 else
93 Event_Comm::Notifier *notifier = this->notifier_->notifier ();
94 ACE_ASSERT (notifier != 0);
96 // Use the notifier to notify Consumers.
97 try
99 Event_Comm::Event event;
101 // Pass the buf over in the tag field.
102 event.tag_ = ACE_OS::strdup (buf);
104 // This is where the "any" value goes or the object
105 // reference... event.value_ = ...
107 // Forward <Event> to all <Consumers>.
108 notifier->push (event);
110 catch (const CORBA::Exception& ex)
112 ex._tao_print_exception ("Unexpected Error\n");
115 return 0;