1 //=============================================================================
3 * @file Consumer_Input_Handler.cpp
5 * Implementation of the Consumer_Input_Handler class.
7 * @author Kirthika Parameswaran <kirthika@cs.wustl.edu>
9 //=============================================================================
11 #include "Consumer_Input_Handler.h"
12 #include "ace/Read_Buffer.h"
13 #include "ace/OS_NS_unistd.h"
14 #include "ace/OS_NS_ctype.h"
16 Consumer_Input_Handler::Consumer_Input_Handler (Consumer_Handler
*consumer_handler
)
18 consumer_handler_
= consumer_handler
;
22 Consumer_Input_Handler::handle_input (ACE_HANDLE
)
26 // The string could read contains \n\0 hence using ACE_OS::read
27 // which returns the no of bytes read and hence i can manipulate
28 // and remove the devil from the picture i.e '\n' ! ;)
29 ssize_t strlen
= ACE_OS::read (ACE_STDIN
, buf
, sizeof buf
);
30 if (buf
[strlen
-1] == '\n')
31 buf
[strlen
-1] = '\0';
33 switch (ACE_OS::ace_tolower (buf
[0]))
35 case Consumer_Input_Handler::REGISTER
:
37 this->register_consumer ();
40 case Consumer_Input_Handler::UNREGISTER
:
42 this->unregister_consumer ();
45 case Consumer_Input_Handler::EXIT
:
47 this->quit_consumer_process ();
55 Consumer_Input_Handler::register_consumer ()
57 // Get the stockname the consumer is interested in.
58 static char stockname
[BUFSIZ
];
60 ACE_DEBUG ((LM_DEBUG
, "Stockname?"));
62 ssize_t strlen
= ACE_OS::read (ACE_STDIN
, stockname
, sizeof stockname
- 1);
64 // Taking care of platforms where an carriage return is padded with newline.
65 if (stockname
[strlen
-2] == '\n' || stockname
[strlen
-2] == '\r')
66 stockname
[strlen
-2] = '\0';
68 if (stockname
[strlen
-1] == '\n' || stockname
[strlen
-1] == '\r')
69 stockname
[strlen
-1] = '\0';
71 this->consumer_handler_
->stock_name_
= stockname
;
73 // Get the threshold value.
74 char needed_stock_value
[BUFSIZ
];
76 "Threshold Stock value?"));
78 strlen
= ACE_OS::read (ACE_STDIN
,
80 sizeof needed_stock_value
);
82 if (needed_stock_value
[strlen
-1] == '\n')
83 needed_stock_value
[strlen
-1] = '\0';
85 this->consumer_handler_
->threshold_value_
=
86 ACE_OS::atoi (needed_stock_value
);
91 // Register with the server.
92 this->consumer_handler_
->server_
->register_callback (this->consumer_handler_
->stock_name_
.c_str (),
93 this->consumer_handler_
->threshold_value_
,
94 this->consumer_handler_
->consumer_var_
.in ());
96 // Note the registration.
97 consumer_handler_
->registered_
= 1;
98 consumer_handler_
->unregistered_
= 0;
100 // @@ Up to this point..
101 ACE_DEBUG ((LM_DEBUG
, "Registration done!\n"));
103 catch (const CORBA::Exception
& ex
)
105 ex
._tao_print_exception (
106 "Consumer_Input_Handler::register_consumer()\n");
114 Consumer_Input_Handler::unregister_consumer ()
116 // Only if the consumer is registered can the
117 // unregistration take place.
118 if (consumer_handler_
->registered_
== 1)
120 this->consumer_handler_
->server_
->unregister_callback (this->consumer_handler_
->consumer_var_
.in());
122 ACE_DEBUG ((LM_DEBUG
,
123 " Consumer Unregistered \n"));
124 consumer_handler_
->unregistered_
= 1;
125 consumer_handler_
->registered_
= 0;
128 ACE_DEBUG ((LM_DEBUG
,
129 " Invalid Operation: Consumer not Registered\n"));
135 Consumer_Input_Handler::quit_consumer_process ()
137 // Only if the consumer is registered and wants to shut
138 // down, its necessary to unregister and then shutdown.
143 if (consumer_handler_
->unregistered_
!= 1 && consumer_handler_
->registered_
== 1)
145 // If the notifier has exited and the consumer tries to call
146 // the unregister_callback method tehn an exception will be
147 // raised. Hence check for this case using.
148 this->consumer_handler_
->server_
->unregister_callback (this->consumer_handler_
->consumer_var_
.in ());
150 ACE_DEBUG ((LM_DEBUG
,
151 " Consumer Unregistered \n"));
152 consumer_handler_
->unregistered_
= 0;
153 consumer_handler_
->registered_
= 0;
155 this->consumer_handler_
->consumer_servant_
->shutdown ();
157 catch (const CORBA::Exception
& ex
)
159 // There would be an exception only if there is a communication
160 // failure between the notifier and consumer. On catching the
161 // exception proclaim the problem and do a graceful exit.
162 ex
._tao_print_exception ("Communication failed!\n");
166 this->consumer_handler_
->consumer_servant_
->shutdown ();
168 catch (const CORBA::Exception
&)