summaryrefslogtreecommitdiff
path: root/TAO/examples/Callback_Quoter/Notifier_i.cpp
blob: 5103fbeb3b78f7ea6f8e6973078b5826f989274e (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// $Id$

// ===========================================================
//
// = LIBRARY
//    TAO/examples/Callback_Quoter
//
// = FILENAME
//    Notifier_i.cpp
//
// = DESCRIPTION
//   Implementation of the Notifier_i class. This class is the servant
//   object for the callback quoter server.
//
// = AUTHOR
//    Kirthika Parameswaran <kirthika@cs.wustl.edu>
//
// ===========================================================

#include "Notifier_i.h"

Notifier_i::Notifier_i (void)
{
  // No-op
}

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

// Register a distributed callback handler that is invoked when the
// given stock reaches the desired threshold value.

void
Notifier_i::register_callback (const char *stock_name,
				     CORBA::Long threshold_value,
				     Callback_Quoter::Consumer_ptr consumer_handler,
				     CORBA::Environment &TAO_TRY_ENV)
{
  // Store the client information.
  Consumer_Data consumer_data;

  // Necessary to make another copy of the consumer_handler using
  // <_duplicate> so that we dont lose the consumer object reference
  // after the method invocation is done.
  consumer_data.consumer_ =
    Callback_Quoter::Consumer::_duplicate (consumer_handler);
  consumer_data.desired_value_= threshold_value;

  CONSUMERS *consumers = 0;

  // @@ Please add a comment explaining what you're doing ;-)
  if (this->consumer_map_.find (stock_name, consumers) == 0)
    {
      // @@ Always make sure to check the return values of all
      // calls...
      consumers->insert (consumer_data);

      ACE_DEBUG ((LM_DEBUG,
		  "inserted map entry: stockname %s threshold %d",
		  stock_name,
		  threshold_value));
    }
  else
    {
      // @@ Make sure to use the ACE_NEW_THROW macro, which works with
      // CORBA exceptions...
      consumers = new CONSUMERS;

      // @@ Always make sure to check the return values of all
      // calls...
      consumers->insert (consumer_data);

      // @@ Always make sure to check the return values of all
      // calls...
      this->consumer_map_.bind (stock_name, consumers);

      ACE_DEBUG ((LM_DEBUG,
		  "new map entry: stockname %s threshold %d",
		  stock_name,
		  threshold_value));
    }

}

// Obtain a pointer to the orb.

void
Notifier_i::orb (CORBA::ORB_ptr orb)
{
  this->orb_ = orb;
}

// Remove the client handler.

void
Notifier_i::unregister_callback (Callback_Quoter::Consumer_ptr consumer,
				       CORBA::Environment &TAO_TRY_ENV)
{
  // @@ Make sure to add a comment here.

  for (CONSUMER_MAP::ITERATOR iter = this->consumer_map_.begin ();
       iter!= this->consumer_map_.end ();
       iter ++)
    {
      // The *iter is nothing but the stockname + unbounded set of
      // consumers+threshold values, i.e a ACE_Hash_Map_Entry.

       Consumer_Data consumer_to_remove;
       // @@ I don't think you need to do this duplicate(), but make
       // sure to run purify to double-check this.
       consumer_to_remove.consumer_ = Callback_Quoter::Consumer::_duplicate (consumer);

       // int_id is a member of the ACE_Hash_Map_Entry.  The remove
       // method will do a find internally using operator == which
       // will check only the consumer pointers.  If match found it
       // will be removed from the set.

       // @@ Make sure to check the return value.
        (*iter).int_id_->remove (consumer_to_remove);

        ACE_DEBUG ((LM_DEBUG,
		    "unregister_callback:consumer removed\n"));
    }
}

// Gets the market status and sends the information to the consumer
// who is interested in it.

void
Notifier_i::market_status (const char *stock_name,
				 CORBA::Long stock_value,
				 CORBA::Environment &TAO_TRY_ENV)
{
  ACE_DEBUG ((LM_DEBUG,
	      "Notifier_i:: The stockname is %s with price %d\n",
	      stock_name,
              stock_value));

  CONSUMERS *consumers;

  if (this->consumer_map_.find (stock_name, consumers) == 0)
    {
      // Go through the list of <Consumer_Data> to find which registered
      // client wants to be notified.

      for (CONSUMERS::ITERATOR iter = consumers->begin ();
           iter != consumers->end ();
           iter++)
        {
          // Check whether the stockname is equal before proceeding
          // further.
          if (stock_value >= (*iter).desired_value_)
            {
              Callback_Quoter::Info interested_consumer_data;

	      // @@ Please check this when you run purify.
              interested_consumer_data.stock_name =
		CORBA::string_dup (stock_name);
              interested_consumer_data.value =
		stock_value;

	      ACE_DEBUG ((LM_DEBUG,
			  "pushing information to consumer\n"));

	      // @@ Please add a comment.
              (*iter).consumer_->push (interested_consumer_data);

	    }
        }
    }
  else
    // @@ Please add a user defined exception called something like
    // NOT_FOUND.
    ACE_DEBUG ((LM_DEBUG,
		"Consumer not found having stockname \"%s\" with threshold value %d!\n",
		stock_name,
		stock_value));
}

void
Notifier_i::shutdown (CORBA::Environment &)
{
  if (this->consumer_map_.current_size () > 0)
    {
        this->consumer_map_.close ();

    }

      /* for (CONSUMER_MAP::ITERATOR iter = this->consumer_map_.begin ();
	   iter!= this->consumer_map_.end ();
	   iter ++)
	{
	  (*iter).int_id_->reset ();
	  size_t no= this->consumer_map_.unbind ((*iter).ext_id_,
			             (*iter).int_id_);


          if (no == -1)
	    {
	    ACE_ERROR ((LM_ERROR,
			"error! ACE_Hash_Map_Manager: unbind ()\n" );

    		}



          this->consumer_map_.close ();

        	}
        }

        this->consumer_map_.close ();

	}*/

     ACE_DEBUG ((LM_DEBUG,
		 "The Callback Quoter server is shutting down"));

     // Instruct the ORB to shutdown.
     this->orb_->shutdown ();

}

int
Notifier_i::Consumer_Data::operator== (const Consumer_Data &rhs)
{
  // The <_is_equivalent> function checks if the _var and _ptr objects
  // are the same.  NOTE: this call might not behave well on other
  // ORBs since <_is_equivalent> isn't guaranteed to differentiate
  // object references.

  return this->consumer_->_is_equivalent (rhs.consumer_.in ());
}

/*CONSUMER_MAP*
Notifier_i::get_consumer_map_ptr ()
{
return (&consumer_map_);
}
*/


#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)

template class ACE_Node<Notifier_i::Consumer_Data>;
template class ACE_Unbounded_Set<Notifier_i::Consumer_Data>;
template class ACE_Unbounded_Set_Iterator<Notifier_i::Consumer_Data>;

template class ACE_Hash_Map_Entry<ACE_CString,ACE_Unbounded_Set<Notifier_i::Consumer_Data>*>;
template class ACE_Hash_Map_Manager<ACE_CString,ACE_Unbounded_Set<Notifier_i::Consumer_Data>*,ACE_Null_Mutex>;
template class ACE_Hash_Map_Iterator_Base<ACE_CString,ACE_Unbounded_Set<Notifier_i::Consumer_Data>*,ACE_Null_Mutex>;
template class ACE_Hash_Map_Iterator<ACE_CString,ACE_Unbounded_Set<Notifier_i::Consumer_Data>*,ACE_Null_Mutex>;
template class ACE_Hash_Map_Reverse_Iterator<ACE_CString,ACE_Unbounded_Set<Notifier_i::Consumer_Data>*,ACE_Null_Mutex>;

#elif defined(ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)

#pragma instantiate ACE_Node<Notifier_i::Consumer_Data>
#pragma instantiate ACE_Unbounded_Set<Notifier_i::Consumer_Data>
#pragma instantiate ACE_Unbounded_Set_Iterator<Notifier_i::Consumer_Data>

#pragma instantiate ACE_Hash_Map_Entry<ACE_CString,ACE_Unbounded_Set<Notifier_i::Consumer_Data>*>
#pragma instantiate ACE_Hash_Map_Manager<ACE_CString,ACE_Unbounded_Set<Notifier_i::Consumer_Data>*,ACE_Null_Mutex>
#pragma instantiate ACE_Hash_Map_Iterator_Base<ACE_CString,ACE_Unbounded_Set<Notifier_i::Consumer_Data>*,ACE_Null_Mutex>
#pragma instantiate ACE_Hash_Map_Iterator<ACE_CString,ACE_Unbounded_Set<Notifier_i::Consumer_Data>*,ACE_Null_Mutex>
#pragma instantiate ACE_Hash_Map_Reverse_Iterator<ACE_CString,ACE_Unbounded_Set<Notifier_i::Consumer_Data>*,ACE_Null_Mutex>

#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */