summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/orbsvcs/Notify/Notify_Event.cpp
blob: 71d9096c54ea6034f77b0fa9b6d081685db60d38 (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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
// $Id$
#include "Notify_Event.h"

#include "tao/debug.h"

#if ! defined (__ACE_INLINE__)
#include "Notify_Event.i"
#endif /* __ACE_INLINE__ */

ACE_RCSID(Notify, Notify_Event, "$Id$")

// @@ Pradeep: David is going to give you a hard time from having a
// static object
TAO_Notify_EventType
TAO_Notify_EventType::special_event_type_ ("*", "%ALL");

// @@ You can probably get away returning this stuff by value, and
// creating it on the fly, assuming there aren't too many calls to
// this.
TAO_Notify_EventType&
TAO_Notify_EventType::special_event_type (void)
{
  return special_event_type_;
}

TAO_Notify_EventType::TAO_Notify_EventType (void)
{
  // No-Op.
}

TAO_Notify_EventType::TAO_Notify_EventType (const char* domain_name,
                                            const char* type_name)
{
  this->event_type_.type_name = type_name;
  this->event_type_.domain_name = domain_name;
  this->recompute_hash ();
}

TAO_Notify_EventType::TAO_Notify_EventType (const CosNotification::EventType& event_type)
{
  this->event_type_.type_name = event_type.type_name.in ();
  this->event_type_.domain_name = event_type.domain_name.in ();
  this->recompute_hash ();
}

TAO_Notify_EventType::~TAO_Notify_EventType ()
{
}

u_long
TAO_Notify_EventType::hash (void) const
{
  // @@ Pradeep: this is an excellent candidate for an inline
  // function. Get in the habit of creating .h, .cpp and .i files,
  // even if initially the .i file is empty, that way it is easier to
  // do this stuff.
  return this->hash_value_;
}

void
TAO_Notify_EventType::recompute_hash (void)
{
  // @@ Pradeep: this code is bound to crash someday if the strings
  // are too long....  See if the hash_pjw() function can be modified
  // to take accumulate multiple strings, as in:
  //   hash = ACE::hash_pjw_accummulate (0, str1);
  //   hash = ACE::hash_pjw_accummulate (hash, str2);
  //
  // @@ Or use grow the buffer when needed, or just add the two hash
  // values or something, but fix this code!
  //
  char buffer[BUFSIZ];
  ACE_OS::strcpy (buffer, this->event_type_.domain_name.in ());
  ACE_OS::strcat (buffer, this->event_type_.type_name.in ());

  this->hash_value_ =  ACE::hash_pjw (buffer);
}

void
TAO_Notify_EventType::operator=(const CosNotification::EventType& rhs)
{
  this->event_type_.type_name = rhs.type_name;
  this->event_type_.domain_name = rhs.domain_name;
  this->recompute_hash ();
}

int
TAO_Notify_EventType::operator==(const TAO_Notify_EventType& rhs) const
{
  if (this->hash () != rhs.hash ())
    return 0;
  else // compare the strings
    return (ACE_OS::strcmp (this->event_type_.type_name, rhs.event_type_.type_name) == 0  &&
            ACE_OS::strcmp (this->event_type_.domain_name, rhs.event_type_.domain_name) == 0
            );
}

CORBA::Boolean
TAO_Notify_EventType::is_special (void) const
{
  if ((event_type_.domain_name == 0 ||
       ACE_OS::strcmp (event_type_.domain_name, "") == 0 ||
       ACE_OS::strcmp (event_type_.domain_name, "*") == 0) &&
      (event_type_.type_name == 0 ||
       ACE_OS::strcmp (event_type_.domain_name, "") == 0 ||
       ACE_OS::strcmp (event_type_.type_name, "*") == 0 ||
       ACE_OS::strcmp (event_type_.type_name, "%ALL") == 0))
    return 1;
  else
    return 0;
}

const CosNotification::EventType&
TAO_Notify_EventType::get_native (void) const
{
  return event_type_;
}

// ****************************************************************

TAO_Notify_Event::TAO_Notify_Event (void)
  :lock_ (0),
   refcount_ (1),
   event_reliability_ (CosNotification::BestEffort),
   priority_ (CosNotification::DefaultPriority),
   //   start_time_ (0),
   // stop_time_ (0),
   timeout_ (0)
{
  ACE_NEW (lock_, ACE_Lock_Adapter<TAO_SYNCH_MUTEX> ());
}

TAO_Notify_Event::~TAO_Notify_Event ()
{
  delete this->lock_;
  this->lock_ = 0;
  if (TAO_debug_level > 0)
    ACE_DEBUG ((LM_DEBUG, "in ~TAO_Notify_Event %X\n", this));
}


void
TAO_Notify_Event::_incr_refcnt (void)
{
  ACE_GUARD (ACE_Lock, ace_mon, *this->lock_);
  this->refcount_++;

  if (TAO_debug_level > 0)
    ACE_DEBUG ((LM_DEBUG, "in TAO_Notify_Event %X incr %d\n", this, refcount_));
}

void
TAO_Notify_Event::_decr_refcnt (void)
{
  int delete_me = 0;

  {
    ACE_GUARD (ACE_Lock, ace_mon, *this->lock_);
    this->refcount_--;

    if (TAO_debug_level > 0)
      ACE_DEBUG ((LM_DEBUG, "in TAO_Notify_Event %X decr %d\n", this, refcount_));

    if (this->refcount_ == 0)
      delete_me = 1;
  }

  if (delete_me == 1)
    delete this;
}

// ****************************************************************

// = Any Event Type.

TAO_Notify_Any::TAO_Notify_Any (CORBA::Any * data)
  :data_ (data),
   is_owner_(1)
{
}

TAO_Notify_Any::TAO_Notify_Any (const CORBA::Any * data)
  :data_ ((CORBA::Any*)data),
   is_owner_(0)
{
}

TAO_Notify_Any::~TAO_Notify_Any ()
{
  if (this->is_owner_)
    delete this->data_;
}

TAO_Notify_Event*
TAO_Notify_Any::clone (void)
{
  TAO_Notify_Any* clone;

  if (this->is_owner_)
    {
      // @@ Are you sure this is the right way to clone?  You are
      // stealing the data from the original class...
      ACE_NEW_RETURN (clone, TAO_Notify_Any ((CORBA::Any const *)this->data_),
                      0);
      this->is_owner_ = 0;
    }
  else
    {
      CORBA::Any * data_copy;
      ACE_NEW_RETURN (data_copy, CORBA::Any (*this->data_), 0);
      ACE_NEW_RETURN (clone, TAO_Notify_Any (data_copy), 0);

      // Later: cleanup data_copy if this new fails.
    }

  return clone;
}

void
TAO_Notify_Any::operator=(const TAO_Notify_Any& notify_any)
{
  if (TAO_debug_level > 0)
    ACE_DEBUG ((LM_DEBUG, "In TAO_Notify_Any::operator=\n"));

  if (this->is_owner_)
    delete data_;

  ACE_NEW (this->data_,
           CORBA::Any (*notify_any.data_));

  this->is_owner_ = 1;
}

CORBA::Boolean
TAO_Notify_Any::is_special_event_type (void) const
{
  return 1;
}

const TAO_Notify_EventType&
TAO_Notify_Any::event_type (void) const
{
  return TAO_Notify_EventType::special_event_type ();
}

CORBA::Boolean
TAO_Notify_Any::do_match (CosNotifyFilter::Filter_ptr filter,
                          CORBA::Environment &ACE_TRY_ENV) const
{
  return filter->match (*this->data_, ACE_TRY_ENV);
}

void
TAO_Notify_Any::do_push (CosEventComm::PushConsumer_ptr consumer,
                         CORBA::Environment &ACE_TRY_ENV) const
{
  consumer->push (*this->data_, ACE_TRY_ENV);
}

void
TAO_Notify_Any::do_push (CosNotifyComm::StructuredPushConsumer_ptr consumer,
                         CORBA::Environment &ACE_TRY_ENV) const
{
  // translation pg. 28
  CosNotification::StructuredEvent event;
  event.remainder_of_body <<= *this->data_;
  event.header.fixed_header.event_type.type_name = CORBA::string_dup ("%ANY");
  event.header.fixed_header.event_type.domain_name = CORBA::string_dup ("");

  consumer->push_structured_event (event, ACE_TRY_ENV);
}

// ****************************************************************

// = TAO_Notify_StructuredEvent

// @@ Pradeep: many of the same comments that i made for
// TAO_Notify_Any apply here too.

TAO_Notify_StructuredEvent::TAO_Notify_StructuredEvent (CosNotification::StructuredEvent * notification)
  :data_ (notification),
   event_type_ (notification->header.fixed_header.event_type),
   is_owner_ (1)
{

  this->init_QoS ();
}

TAO_Notify_StructuredEvent::TAO_Notify_StructuredEvent (const CosNotification::StructuredEvent * notification)
  :data_ ((CosNotification::StructuredEvent*)notification),
   event_type_ (notification->header.fixed_header.event_type),
   is_owner_ (0)
{
}

TAO_Notify_StructuredEvent::~TAO_Notify_StructuredEvent ()
{
  if (this->is_owner_)
    delete this->data_;
}

void
TAO_Notify_StructuredEvent::init_QoS (void)
{
  CosNotification::PropertySeq& qos = data_->header.variable_header;

  for (CORBA::ULong index = 0; index < qos.length (); ++index)
    {
      ACE_CString property_name(qos[index].name);

      if (property_name.compare (CosNotification::Priority) == 0)
        {
          qos[index].value >>= this->priority_;
        }
      else if (property_name.compare (CosNotification::StartTime))
        {
          // qos[index].value >>= this->start_time_;
        }
      else if (property_name.compare (CosNotification::StopTime))
        {
          // qos[index].value >>= this->stop_time_;
        }
      else if (property_name.compare (CosNotification::Timeout))
        {
          qos[index].value >>= this->timeout_;
        }
    }
}

TAO_Notify_Event*
TAO_Notify_StructuredEvent::clone (void)
{
  TAO_Notify_StructuredEvent* clone;

  if (this->is_owner_)
    {
      ACE_NEW_RETURN (clone, TAO_Notify_StructuredEvent ((CosNotification::StructuredEvent const *) this->data_), 0);
      this->is_owner_ = 0;
    }
  else
    {
      CosNotification::StructuredEvent *data_copy;
      ACE_NEW_RETURN (data_copy, CosNotification::StructuredEvent (*this->data_),
                      0);
      ACE_NEW_RETURN (clone, TAO_Notify_StructuredEvent (data_copy), 0);
      // Later: cleanup *data_copy if this new fails.
      clone->is_owner_ = 1;
    }

  return clone;
}

void
TAO_Notify_StructuredEvent::operator=(const TAO_Notify_StructuredEvent& structured_event)
{
  if (TAO_debug_level > 0)
    ACE_DEBUG ((LM_DEBUG, "In TAO_Notify_StructuredEvent::operator=\n"));

  if (this->is_owner_)
    delete this->data_;

  ACE_NEW (this->data_,
           CosNotification::StructuredEvent (*structured_event.data_));

  this->is_owner_ = 1;
  this->event_type_ = structured_event.data_->header.fixed_header.event_type;
}

CORBA::Boolean
TAO_Notify_StructuredEvent::is_special_event_type (void) const
{
  return this->event_type_.is_special ();
}

const TAO_Notify_EventType&
TAO_Notify_StructuredEvent::event_type (void) const
{
  return this->event_type_;
}

CORBA::Boolean
TAO_Notify_StructuredEvent::do_match (CosNotifyFilter::Filter_ptr filter,
                                      CORBA::Environment &ACE_TRY_ENV) const
{
  return filter->match_structured (*this->data_, ACE_TRY_ENV);
}

void
TAO_Notify_StructuredEvent::do_push (CosEventComm::PushConsumer_ptr consumer,
                                     CORBA::Environment &ACE_TRY_ENV) const
{
  // translation pg. 28
  CORBA::Any any;
  any <<= *this->data_;
  // is the typecode set by this operation or do we need to set it explicity.

  consumer->push (any, ACE_TRY_ENV);
}

void
TAO_Notify_StructuredEvent::do_push (CosNotifyComm::StructuredPushConsumer_ptr consumer, CORBA::Environment &ACE_TRY_ENV) const
{
  consumer->push_structured_event (*this->data_, ACE_TRY_ENV);
}