summaryrefslogtreecommitdiff
path: root/TAO/examples/OBV/Typed_Events/Event_Types_impl.cpp
blob: 101e58d964d4cb765c68ad414da0c2796ff4e575 (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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
// $Id$

#include "Event_Types_impl.h"

// Implementation of the valuetype member functions.

Event_impl::Event_impl ()
  // initializers (': foo ()') don't work for OBV state members
  // since we should only access the state through modifier functions
{
  // Put a timestamp on event's birth.
  ACE_Time_Value now (ACE_OS::gettimeofday ());
  this->time_ (now.sec ());
}

Event_impl::~Event_impl ()
{
}

void
Event_impl::do_print (void)
{
  ACE_DEBUG((LM_DEBUG, "(time %d origin %d)  ",
             (CORBA::ULong) this->time_(), (CORBA::ULong) this->origin_id_() ));
}


/* Not defined, see header
Event_factory::~Event_factory ()
{
}

TAO_OBV_CREATE_RETURN_TYPE (Event)
Event_factory::create_for_unmarshal ()
{
  return new Event_impl;
}
*/

// Temperature implementation  ===================================

Temperature_impl::Temperature_impl ()
{
}

Temperature_impl::Temperature_impl (CORBA::Float temp)
{
  this->temperature_ (temp);
}

Temperature_impl::~Temperature_impl ()
{
}

void
Temperature_impl::do_print (void)
{
  Event_impl::do_print ();
  // The timestamp

  ACE_DEBUG((LM_DEBUG, "Temperature is %f\n", this->temperature_() ));
}

Temperature_factory::~Temperature_factory ()
{
}

TAO_OBV_CREATE_RETURN_TYPE (Temperature)
Temperature_factory::create_for_unmarshal ()
{
  return new Temperature_impl;
}


// Position implementation  ======================================

Position_impl::Position_impl ()
{
}

Position_impl::Position_impl (Point &p)
{
  this->xyz (p);
  // Does a copy of the p array
}

Position_impl::~Position_impl ()
{
}

void
Position_impl::do_print (void)
{
  Event_impl::do_print ();
  // The timestamp

  ACE_DEBUG((LM_DEBUG, "Position is (%f, %f, %f)\n",
             this->x(), this->y(), this->z() ));
}

CORBA::Float Position_impl::x ()  { return this->xyz()[0]; }
void Position_impl::x (CORBA::Float x)   { this->xyz()[0] = x; }
CORBA::Float Position_impl::y ()  { return this->xyz()[1]; }
void Position_impl::y (CORBA::Float y)   { this->xyz()[1] = y; }
CORBA::Float Position_impl::z ()  { return this->xyz()[2]; }
void Position_impl::z (CORBA::Float z)   { this->xyz()[2] = z; }


Position_factory::~Position_factory ()
{
}

TAO_OBV_CREATE_RETURN_TYPE (Position)
Position_factory::create_for_unmarshal ()
{
  return new Position_impl;
}


// Log_Msg implementation  ===================================

Log_Msg_impl::Log_Msg_impl ()
{
}

Log_Msg_impl::Log_Msg_impl (CORBA::Short u, const char *m)
{
  this->urgency (u);
  this->message (CORBA::string_dup (m));
}

Log_Msg_impl::~Log_Msg_impl ()
{
}

void
Log_Msg_impl::do_print (void)
{
  Event_impl::do_print ();
  // The timestamp

  if (this->urgency () > 0)
    {
      ACE_DEBUG((LM_DEBUG, "**** %s ****\n", this->message () ));
    }
  else
    {
      ACE_DEBUG((LM_DEBUG, "%s\n", this->message () ));
    }
}

Log_Msg_factory::~Log_Msg_factory ()
{
}

TAO_OBV_CREATE_RETURN_TYPE (Log_Msg)
Log_Msg_factory::create_for_unmarshal ()
{
  return new Log_Msg_impl;
}



// Event_List_Link implementation  ===================================

Event_List_Link_impl::Event_List_Link_impl ()
{
  this->my_event (0);
  this->next (0);
}

Event_List_Link_impl::Event_List_Link_impl (Event* e)
{
  this->my_event (e);
  // Note that the modifier increases the reference counter of e.

  this->next (0);
}

Event_List_Link_impl::~Event_List_Link_impl ()
{
  // Destructor does nothing explicit, because my_event and next are
  // _var types, which destroy (decrement the reference counter of)
  // the held event resp. the next links.
}

Event*
Event_List_Link_impl::get_event (void)
{
  return this->my_event ();
}

Event_List_Link*
Event_List_Link_impl::get_next_link (void)
{
  return this->next ();
}

void
Event_List_Link_impl::attach_next_link (Event_List_Link *n)
{
  this->next (n);
}

Event_List_Link_factory::~Event_List_Link_factory ()
{
}

TAO_OBV_CREATE_RETURN_TYPE (Event_List_Link)
Event_List_Link_factory::create_for_unmarshal ()
{
  return new Event_List_Link_impl;
}


// Event_List implementation  ===================================

Event_List_impl::Event_List_impl ()
{
  this->first_link (0);
  last_link_cache_ = 0;
}

Event_List_impl::~Event_List_impl ()
{
  // Destructor does nothing explicit, because the _var types do care.
}

void
Event_List_impl::store_event (Event* e)
{
  // This operation should perform atomically and should
  // guard against the access to the list from another thread.
  // But this is omitted in this example.

   Event_List_Link_var new_link (new Event_List_Link_impl (e));

   // We need a new link to store the reference to the event e.
   // But if we'd had assigned the newly created instance directly through
   // a modifier function to a valuetype member, it would never be released.
   // From pointer to _var the reference count of the pointed to never
   // increased, but the modifier does.


  if (last_link_cache_ == 0)
    {
      // Search the end.
      for (Event_List_Link *i = this->first_link ();
           i != 0;
           i = i->get_next_link ())
        {
          last_link_cache_ = i;
        }
    }


  // If still null then the list is empty.
  if (last_link_cache_ == 0)
    {
      this->first_link (new_link);
    }
  else
    {
      last_link_cache_->attach_next_link (new_link);
      last_link_cache_ = new_link;
    }
}

Event_List_factory::~Event_List_factory ()
{
}

TAO_OBV_CREATE_RETURN_TYPE (Event_List)
Event_List_factory::create_for_unmarshal ()
{
  return new Event_List_impl;
}


Event_List_Iterator::Event_List_Iterator (Event_List *list)
{
  this->init (list);
}

Event_List_Iterator::Event_List_Iterator ()
{
  // current_ is a _var and set itself to null.
}

Event_List_Iterator::~Event_List_Iterator ()
{
  // nothing
}


void
Event_List_Iterator::init (Event_List *list)
{
  Event_List_Link *tmp = list->first_link ();
  CORBA::add_ref (tmp);
  current_ = tmp;
}


CORBA::Boolean
Event_List_Iterator::next (Event_var &next)
{
  if (this->current_.in ())
    {
      Event *e = current_->get_event ();
      CORBA::add_ref (e);
      next = e;
      return 1;
    }
  else
    {
      return 0;
    }
}

Event *
Event_List_Iterator::next ()
{
  if (this->current_.in ())
    {
      return current_->get_event ();
    }
  return 0;
}

void
Event_List_Iterator::advance ()
{
  if (this->current_.in ())
    {
      Event_List_Link *tmp = current_->get_next_link ();
      CORBA::add_ref (tmp);
      current_ = tmp;
    }
}



// Checkpoint server side --------------------------------------------


// Criterion classes implementation ----------------------------------



Temperature_Criterion_impl::Temperature_Criterion_impl ()
{
}

Temperature_Criterion_impl::
Temperature_Criterion_impl (CORBA::ULong origin_id, CORBA::Float temp)
{
  this->origin_id_ (origin_id);
  Temperature_var tmp (new Temperature_impl (temp));
  this->meltingpoint (tmp.in ());
}

Temperature_Criterion_impl::~Temperature_Criterion_impl ()
{
}


CORBA::Boolean
Temperature_Criterion_impl::is_critical (Event* e)
{
  // Downcast to a temperature.
  Temperature* t = Temperature::_downcast (e);
  // Is Event really a Temperature ?
  if (t)
    {
      // Now return the comparison with the meltingpoint.
      return
        (t->temperature_ () > this->meltingpoint ()->temperature_ ()) ?
        1 : 0;
    }
  return 0;
}


void
Temperature_Criterion_impl::do_print (void)
{
  ACE_DEBUG((LM_DEBUG, "Alarm boundary for events with origin id %d is\n",
             this->origin_id_ () ));
  this->meltingpoint ()->do_print();
}


Temperature_Criterion_factory::~Temperature_Criterion_factory ()
{
}

TAO_OBV_CREATE_RETURN_TYPE (Temperature_Criterion)
Temperature_Criterion_factory::create_for_unmarshal ()
{
  return new Temperature_Criterion_impl;
}



Position_Criterion_impl::Position_Criterion_impl ()
{
}

Position_Criterion_impl::Position_Criterion_impl (CORBA::ULong origin_id,
                                                  Position *lb,
                                                  Position *tr)
{
  this->origin_id_ (origin_id);
  this->leftbottom(lb);
  this->topright(tr);
}

Position_Criterion_impl::~Position_Criterion_impl ()
{
}


CORBA::Boolean
Position_Criterion_impl::is_critical (Event* e)
{
  Position* p = Position::_downcast (e);
  // Is Event really a Position ?
  if (p)
    {
      // If the position of p is out of the box return true.
      return (
              (p->x () > this->leftbottom ()->x ()) &&
              (p->y () > this->leftbottom ()->y ()) &&
              (p->z () > this->leftbottom ()->z ()) &&
              (p->x () < this->topright ()->x ()) &&
              (p->y () < this->topright ()->y ()) &&
              (p->z () < this->topright ()->z ())
              ) ? 0 : 1;
    }
  return 0;
}


void
Position_Criterion_impl::do_print (void)
{
  ACE_DEBUG((LM_DEBUG,
             "Alarm boundary for events with origin id %d is the box\n",
             this->origin_id_ () ));
  this->leftbottom ()->do_print();
  this->topright ()->do_print();
}

Position_Criterion_factory::~Position_Criterion_factory ()
{
}

TAO_OBV_CREATE_RETURN_TYPE (Position_Criterion)
Position_Criterion_factory::create_for_unmarshal ()
{
  return new Position_Criterion_impl;
}



Log_Msg_Criterion_impl::Log_Msg_Criterion_impl ()
{
}


Log_Msg_Criterion_impl::~Log_Msg_Criterion_impl ()
{
}


CORBA::Boolean
Log_Msg_Criterion_impl::is_critical (Event* e)
{
  Log_Msg* lm = Log_Msg::_downcast (e);

  // Is Event really a Log_Msg ?
  if (lm)
    {
      return (lm->urgency () ? 1 : 0);
    }
  return 0;
}


void
Log_Msg_Criterion_impl::do_print (void)
{
  ACE_DEBUG((LM_DEBUG,
             "All log messages with urgency greater zero are registered.\n" ));
}

Log_Msg_Criterion_factory::~Log_Msg_Criterion_factory ()
{
}

TAO_OBV_CREATE_RETURN_TYPE (Log_Msg_Criterion)
Log_Msg_Criterion_factory::create_for_unmarshal ()
{
  return new Log_Msg_Criterion_impl;
}



Criterion_List_impl::Criterion_List_impl ()
{
  this->my_list (0);
  // We don't want create our list member here, because this constructor
  // is called before unmarshalling too. We cant't distinguish that.
  // If we transmit this type eventually. This is not done in this example.
  // (That is no weakness of OBV since the instance should be created and
  // initialized through the factory create (),
  // which is not yet implemented %!)
}

Criterion_List_impl::~Criterion_List_impl ()
{
}

void
Criterion_List_impl::store_criterion (Criterion *c)
{
  if (!my_list ())
    {
      Event_List_var ev(new Event_List_impl);
      my_list (ev);
    }

  Event *e = Event::_downcast (c);
  my_list ()->store_event (e);
}

CORBA::Boolean
Criterion_List_impl::is_critical (Event *e)
{
  // Try all criterions. Walking the list is efficient enough for
  // demonstration.

  for (Criterion_List_Iterator i (this); i.next (); i.advance ())
    {
      Criterion *c = i.next ();

      // Let e_c point to the Event part of the Criterion.
      // We know that c has an Event part.
      Event *e_c = Event::_downcast (c);


      // A criterion is applied if the origin id is matching the event.
      // A null id flags that it should be applied to all events.

      if (e->origin_id_() == e_c->origin_id_ () ||
          e_c->origin_id_ () == 0)
        {
          if (c->is_critical (e))
            return 1;
        }
    }
  return 0;
}


Criterion_List_Iterator::Criterion_List_Iterator (Criterion_List *list)
{
  this->init (list);
}

Criterion_List_Iterator::~Criterion_List_Iterator ()
{
  // nothing
}


void
Criterion_List_Iterator::init (Criterion_List *list)
{
  iterator_.init (list->my_list());
}


Criterion *
Criterion_List_Iterator::next ()
{
  return (Criterion::_downcast (iterator_.next ()));
}

void
Criterion_List_Iterator::advance ()
{
  (void) iterator_.advance ();
}