summaryrefslogtreecommitdiff
path: root/TAO/examples/Callback_Quoter/Consumer_Input_Handler.cpp
blob: 770fbfb89b15fe548b1efd5be2ea8385e42ad273 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// $Id$
// ===========================================================
//
//
// = LIBRARY
//    TAO/examples/Callback_Quoter
//
// = FILENAME
//    Consumer_Input_Handler.cpp
//
// = DESCRIPTION
//    Implementation of the Consumer_Input_Handler class.
//
// = AUTHOR
//    Kirthika Parameswaran <kirthika@cs.wustl.edu>
//
// ===========================================================

#include "Consumer_Input_Handler.h"
#include "ace/Read_Buffer.h"
#include "ace/OS.h"

Consumer_Input_Handler::Consumer_Input_Handler (Consumer_Handler *consumer_handler)
  {
    consumer_handler_ = consumer_handler;

  }

int
Consumer_Input_Handler::handle_input (ACE_HANDLE)
{
  char buf[BUFSIZ];

  // The string could read contains \n\0 hence using ACE_OS::read
  // which returns the no of bytes read and hence i can manipulate
  // and remove the devil from the picture i.e '\n' ! ;)

  ssize_t strlen = ACE_OS::read (ACE_STDIN,
                                 buf,
                                 sizeof buf);
  if (buf[strlen -1] == '\n')
    buf[strlen -1] = '\0';

  switch (tolower (buf[0]))
    {
    case Consumer_Input_Handler::REGISTER:
      {
        this->register_consumer ();
        break;
      }
    case Consumer_Input_Handler::UNREGISTER:
      {
        this->unregister_consumer ();
        break;
      }
    case Consumer_Input_Handler::EXIT:
      {
        this->quit_consumer_process ();
        break;
      }
    }
  return 0;
}


int
Consumer_Input_Handler::register_consumer ()
{

  // Get the stockname the consumer is interested in.
  static char stockname[BUFSIZ];

  ACE_DEBUG ((LM_DEBUG,
              "Stockname?"));

  ssize_t strlen = ACE_OS::read (ACE_STDIN,
                                 stockname,
                                 sizeof stockname - 1);

  // Taking care of platforms where an carriage return is padded with newline.
  if (stockname[strlen -2] == '\n' || stockname[strlen -2] == '\r')
    stockname[strlen -2] = '\0';
  else
    if (stockname[strlen -1] == '\n' || stockname[strlen -1] == '\r')
      stockname[strlen -1] = '\0';


  this->consumer_handler_->stock_name_ = stockname;

  // Get the threshold value.
  char needed_stock_value[BUFSIZ];
  ACE_DEBUG ((LM_DEBUG,
              "Threshold Stock value?"));

  strlen = ACE_OS::read (ACE_STDIN,
                         needed_stock_value,
                         sizeof needed_stock_value);

  if (needed_stock_value[strlen -1] == '\n')
    needed_stock_value[strlen -1] = '\0';

  this->consumer_handler_->threshold_value_ =
    ACE_OS::atoi (needed_stock_value);

  ACE_DECLARE_NEW_CORBA_ENV;

  ACE_TRY
    {
     
      // Register with the server.
      this->consumer_handler_->server_->register_callback (this->consumer_handler_->stock_name_,
                                                           this->consumer_handler_->threshold_value_,
                                                           this->consumer_handler_->consumer_var_.in (),
                                                           ACE_TRY_ENV);
      ACE_TRY_CHECK;

      // Note the registration.
      consumer_handler_->registered_ = 1;
      consumer_handler_->unregistered_ = 0;

      // @@ Up to this point..

      ACE_DEBUG ((LM_DEBUG,
                  "registeration done!\n"));
    }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,"Consumer_Input_Handler::register_consumer()\n");
      return -1;
    }
  ACE_ENDTRY;
  ACE_CHECK_RETURN (-1);

return 0;
}



int
Consumer_Input_Handler::unregister_consumer ()
{
  // Only if the consumer is registered can the
  // unregistration take place.

  if (consumer_handler_->registered_ == 1)
    {
      this->consumer_handler_->server_->unregister_callback (this->consumer_handler_->consumer_var_.in());

      ACE_DEBUG ((LM_DEBUG,
                  " Consumer Unregistered \n "));
      consumer_handler_->unregistered_ = 1;
      consumer_handler_->registered_ = 0;
    }
  else
    ACE_DEBUG ((LM_DEBUG,
                " Invalid Operation: Consumer not Registered\n"));


  return 0;
}

int
Consumer_Input_Handler::quit_consumer_process ()
{
  // Only if the consumer is registered and wants to shut
  // down, its necessary to unregister and then shutdown.

  ACE_DECLARE_NEW_CORBA_ENV;

  ACE_TRY
    {
      if (consumer_handler_->unregistered_ != 1 && consumer_handler_->registered_ == 1)
	{
          // If the notifier has exited and the consumer tries to call
          // the unregister_callback method tehn an execption will be
          // raised. Hence check for this case using ACE_TRY_ENV.
	  this->consumer_handler_->server_->unregister_callback (this->consumer_handler_->consumer_var_.in (),
                                                                 ACE_TRY_ENV);
	  ACE_TRY_CHECK;

	  ACE_DEBUG ((LM_DEBUG,
		      " Consumer Unregistered \n "));
	  consumer_handler_->unregistered_ = 0;
          consumer_handler_->registered_ = 0;
	}
      this->consumer_handler_->consumer_servant_->shutdown (ACE_TRY_ENV);
      ACE_TRY_CHECK;
    }
  ACE_CATCHANY
    {
      this->consumer_handler_->consumer_servant_->shutdown (ACE_TRY_ENV);

      // There would be an exception only if there is a communication
      // failure between the notifier and consumer. On catching the
      // exception proclaim the problem and do a graceful exit.
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
                           "Communication failed!\n");
      return -1;
    }
  ACE_ENDTRY;
  ACE_CHECK_RETURN (-1);

return 0;
}

Consumer_Input_Handler::~Consumer_Input_Handler (void)
{
  // No-op
}