1 //=============================================================================
3 * @file Consumer_Input_Handler.cpp
5 * Implementation of the Consumer_Input_Handler class.
7 * @author Kirthika Parameswaran <kirthika@cs.wustl.edu>
9 //=============================================================================
12 #include "Consumer_Input_Handler.h"
13 #include "ace/Read_Buffer.h"
14 #include "ace/OS_NS_unistd.h"
15 #include "ace/OS_NS_ctype.h"
17 Consumer_Input_Handler::Consumer_Input_Handler (Consumer_Handler
*consumer_handler
)
19 consumer_handler_
= consumer_handler
;
24 Consumer_Input_Handler::handle_input (ACE_HANDLE
)
28 // The string could read contains \n\0 hence using ACE_OS::read
29 // which returns the no of bytes read and hence i can manipulate
30 // and remove the devil from the picture i.e '\n' ! ;)
32 ssize_t strlen
= ACE_OS::read (ACE_STDIN
,
35 if (buf
[strlen
-1] == '\n')
36 buf
[strlen
-1] = '\0';
38 switch (ACE_OS::ace_tolower (buf
[0]))
40 case Consumer_Input_Handler::REGISTER
:
42 this->register_consumer ();
45 case Consumer_Input_Handler::UNREGISTER
:
47 this->unregister_consumer ();
50 case Consumer_Input_Handler::EXIT
:
52 this->quit_consumer_process ();
61 Consumer_Input_Handler::register_consumer ()
64 // Get the stockname the consumer is interested in.
65 static char stockname
[BUFSIZ
];
70 ssize_t strlen
= ACE_OS::read (ACE_STDIN
,
72 sizeof stockname
- 1);
74 // Taking care of platforms where an carriage return is padded with newline.
75 if (stockname
[strlen
-2] == '\n' || stockname
[strlen
-2] == '\r')
76 stockname
[strlen
-2] = '\0';
78 if (stockname
[strlen
-1] == '\n' || stockname
[strlen
-1] == '\r')
79 stockname
[strlen
-1] = '\0';
82 this->consumer_handler_
->stock_name_
= stockname
;
84 // Get the threshold value.
85 char needed_stock_value
[BUFSIZ
];
87 "Threshold Stock value?"));
89 strlen
= ACE_OS::read (ACE_STDIN
,
91 sizeof needed_stock_value
);
93 if (needed_stock_value
[strlen
-1] == '\n')
94 needed_stock_value
[strlen
-1] = '\0';
96 this->consumer_handler_
->threshold_value_
=
97 ACE_OS::atoi (needed_stock_value
);
103 // Register with the server.
104 this->consumer_handler_
->server_
->register_callback (this->consumer_handler_
->stock_name_
.c_str (),
105 this->consumer_handler_
->threshold_value_
,
106 this->consumer_handler_
->consumer_var_
.in ());
108 // Note the registration.
109 consumer_handler_
->registered_
= 1;
110 consumer_handler_
->unregistered_
= 0;
112 // @@ Up to this point..
114 ACE_DEBUG ((LM_DEBUG
,
115 "registeration done!\n"));
117 catch (const CORBA::Exception
& ex
)
119 ex
._tao_print_exception (
120 "Consumer_Input_Handler::register_consumer()\n");
130 Consumer_Input_Handler::unregister_consumer ()
132 // Only if the consumer is registered can the
133 // unregistration take place.
135 if (consumer_handler_
->registered_
== 1)
137 this->consumer_handler_
->server_
->unregister_callback (this->consumer_handler_
->consumer_var_
.in());
139 ACE_DEBUG ((LM_DEBUG
,
140 " Consumer Unregistered \n"));
141 consumer_handler_
->unregistered_
= 1;
142 consumer_handler_
->registered_
= 0;
145 ACE_DEBUG ((LM_DEBUG
,
146 " Invalid Operation: Consumer not Registered\n"));
153 Consumer_Input_Handler::quit_consumer_process ()
155 // Only if the consumer is registered and wants to shut
156 // down, its necessary to unregister and then shutdown.
161 if (consumer_handler_
->unregistered_
!= 1 && consumer_handler_
->registered_
== 1)
163 // If the notifier has exited and the consumer tries to call
164 // the unregister_callback method tehn an exception will be
165 // raised. Hence check for this case using.
166 this->consumer_handler_
->server_
->unregister_callback (this->consumer_handler_
->consumer_var_
.in ());
168 ACE_DEBUG ((LM_DEBUG
,
169 " Consumer Unregistered \n"));
170 consumer_handler_
->unregistered_
= 0;
171 consumer_handler_
->registered_
= 0;
173 this->consumer_handler_
->consumer_servant_
->shutdown ();
175 catch (const CORBA::Exception
& ex
)
177 // There would be an exception only if there is a communication
178 // failure between the notifier and consumer. On catching the
179 // exception proclaim the problem and do a graceful exit.
180 ex
._tao_print_exception ("Communication failed!\n");
184 this->consumer_handler_
->consumer_servant_
->shutdown ();
186 catch (const CORBA::Exception
&)
196 Consumer_Input_Handler::~Consumer_Input_Handler (void)