summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/orbsvcs/PortableGroup/PG_Object_Group_Storable.cpp
blob: 266859dfd9fa43378895a051cd063d08ba39979b (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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
#include "orbsvcs/PortableGroup/PG_Object_Group_Storable.h"
#include "orbsvcs/Log_Macros.h"

#include "tao/Storable_File_Guard.h"
#include "tao/Storable_Factory.h"
#include "tao/CDR.h"

TAO_BEGIN_VERSIONED_NAMESPACE_DECL

namespace
{
  template <typename T>
  void read_cdr (TAO::Storable_Base & stream, T & corba_data)
  {
    int size;
    stream >> size;

    char *tmp = 0;
    ACE_NEW_THROW_EX (tmp, char [size], CORBA::NO_MEMORY ());
    ACE_Auto_Basic_Array_Ptr<char> buf (tmp);
    stream.read (size, buf.get ());

    TAO_InputCDR cdr (buf.get (), size);
    cdr >> corba_data;
    if (!cdr.good_bit ())
      {
        stream.clear ();
        if (TAO_debug_level > 0)
          {
            ORBSVCS_DEBUG ((LM_DEBUG,
                            ACE_TEXT ("(%P|%t) read_cdr:IO error \n")));
          }
        throw CORBA::INTERNAL ();
      }
  }
}

namespace TAO
{

  class Object_Group_File_Guard : public TAO::Storable_File_Guard
  {
  public:

    Object_Group_File_Guard ( TAO::PG_Object_Group_Storable & object_group,
                              Method_Type method_type);

    ~Object_Group_File_Guard ();

    virtual void set_object_last_changed (const time_t & time);

    virtual time_t get_object_last_changed ();

    /// Check if the guarded object is current with the last
    /// update which could have been performed independently of
    /// the owner of this object.
    virtual bool object_obsolete ();

    /// Mark the object as current with respect to the
    /// file to which it was persisted.
    virtual void mark_object_current ();

    virtual int load_from_stream ();

    virtual bool is_loaded_from_stream ();

    virtual TAO::Storable_Base * create_stream (const char * mode);

  private:

    TAO::PG_Object_Group_Storable & object_group_;
  };

}

TAO::Object_Group_File_Guard::Object_Group_File_Guard (
  TAO::PG_Object_Group_Storable & object_group,
  Method_Type method_type)
  : TAO::Storable_File_Guard(true)
  , object_group_(object_group)
{
  if (object_group_.lock_.acquire() == -1)
    {
      if (TAO_debug_level > 0)
        {
          ORBSVCS_DEBUG ((LM_DEBUG,
                          ACE_TEXT ("(%P|%t) Object_Group_File_Guard:acquire ")
                          ACE_TEXT ("failed\n")));
        }
      throw CORBA::INTERNAL ();
    }

  try
    {
      this->init (method_type);
    }
  catch (const TAO::Storable_Exception &se)
    {
      if (TAO_debug_level > 0)
        {
          ORBSVCS_DEBUG ((LM_DEBUG,
                          ACE_TEXT ("(%P|%t) Object_Group_File_Guard::ctor caught ")
                          ACE_TEXT ("Storable Exception, file = %C\n"),
                          se.get_file_name().c_str()));
        }
      if (object_group_.lock_.release() == -1 && TAO_debug_level > 0)
        {
          ORBSVCS_DEBUG ((LM_DEBUG,
                          ACE_TEXT ("(%P|%t) Object_Group_File_Guard::ctor %p\n"),
                          ACE_TEXT ("lock.release")));
        }
      throw CORBA::INTERNAL ();
    }
  catch (const CORBA::NO_MEMORY &)
    {
      if (TAO_debug_level > 0)
        {
          ORBSVCS_DEBUG ((LM_DEBUG,
                          ACE_TEXT ("(%P|%t) Object_Group_File_Guard::ctor caught ")
                          ACE_TEXT ("CORBA::NO_MEMORY Exception\n")));
        }
      if (object_group_.lock_.release() == -1 && TAO_debug_level > 0)
        {
          ORBSVCS_DEBUG ((LM_DEBUG,
                          ACE_TEXT ("(%P|%t) Object_Group_File_Guard::ctor %p\n"),
                          ACE_TEXT ("lock.release")));
        }
      throw CORBA::INTERNAL ();
    }
  catch (...)
    {
      if (TAO_debug_level > 0)
        {
          ORBSVCS_DEBUG ((LM_DEBUG,
                          ACE_TEXT ("(%P|%t) Object_Group_File_Guard::ctor caught ")
                          ACE_TEXT ("Unknown Exception\n")));
        }
      if (object_group_.lock_.release() == -1 && TAO_debug_level > 0)
        {
          ORBSVCS_DEBUG ((LM_DEBUG,
                          ACE_TEXT ("(%P|%t) Object_Group_File_Guard::ctor %p\n"),
                          ACE_TEXT ("lock.release")));
        }
      throw CORBA::INTERNAL ();
    }
}

TAO::Object_Group_File_Guard::~Object_Group_File_Guard ()
{
  try
    {
      this->release ();
      // Notify if persistent store was updated.
      if (object_group_.write_occurred_)
        object_group_.state_written ();

    }
  catch (const TAO::Storable_Exception &se)
    {
      if (TAO_debug_level > 0)
        {
          ORBSVCS_DEBUG ((LM_DEBUG,
                          ACE_TEXT ("(%P|%t) Object_Group_File_Guard::dtor caught ")
                          ACE_TEXT ("Storable Exception, file = %C\n"),
                          se.get_file_name().c_str()));
        }
    }
  if (object_group_.lock_.release() == -1 && TAO_debug_level > 0)
    {
      ORBSVCS_DEBUG ((LM_DEBUG,
                      ACE_TEXT ("(%P|%t) Object_Group_File_Guard::dtor %p\n"),
                      ACE_TEXT ("lock.release")));
    }
}

void
TAO::Object_Group_File_Guard::set_object_last_changed (const time_t & time)
{
  object_group_.last_changed_ = time;
}

time_t
TAO::Object_Group_File_Guard::get_object_last_changed ()
{
  return object_group_.last_changed_;
}

bool
TAO::Object_Group_File_Guard::object_obsolete ()
{
  return object_group_.is_obsolete (fl_->last_changed ());
}

void
TAO::Object_Group_File_Guard::mark_object_current ()
{
  object_group_.stale (false);
  TAO::Storable_File_Guard::mark_object_current ();
}

int
TAO::Object_Group_File_Guard::load_from_stream ()
{
  object_group_.read (this->peer ());
  object_group_.loaded_from_stream_ = true;
  return this->peer ().good ();
}

bool
TAO::Object_Group_File_Guard::is_loaded_from_stream ()
{
  return object_group_.loaded_from_stream_;
}

TAO::Storable_Base *
TAO::Object_Group_File_Guard::create_stream (const  char * mode)
{
  return object_group_.create_stream (mode);
}

// Make shortcut to get to Method_Type enums
typedef TAO::Storable_File_Guard SFG;

TAO::PG_Object_Group_Storable::PG_Object_Group_Storable (
  CORBA::ORB_ptr orb,
  PortableGroup::FactoryRegistry_ptr factory_registry,
  TAO::PG_Object_Group_Manipulator & manipulator,
  CORBA::Object_ptr empty_group,
  const PortableGroup::TagGroupTaggedComponent & tagged_component,
  const char * type_id,
  const PortableGroup::Criteria & the_criteria,
  const TAO::PG_Property_Set_var & type_properties,
  TAO::Storable_Factory & storable_factory)
  : PG_Object_Group(orb,
                    factory_registry,
                    manipulator,
                    empty_group,
                    tagged_component,
                    type_id,
                    the_criteria,
                    type_properties)
  , group_previously_stored_(false)
  , group_id_previously_stored_(0)
  , storable_factory_ (storable_factory)
  , last_changed_ (0)
  , loaded_from_stream_ (false)
  , destroyed_ (false)
  , write_occurred_ (false)
{
  // Create a temporary stream simply to check if a readable
  // version already exists.
  bool stream_exists = false;
  {
    ACE_Auto_Ptr<TAO::Storable_Base> stream (
      this->create_stream ("r"));

    if (stream->exists ())
      stream_exists = true;
  }

  if (stream_exists)
    {
      Object_Group_File_Guard fg (*this, SFG::CREATE_WITH_FILE);
    }
  else
    {
      Object_Group_File_Guard fg (*this, SFG::CREATE_WITHOUT_FILE);
      this->write (fg.peer ());
    }
}

TAO::PG_Object_Group_Storable::PG_Object_Group_Storable (
  PortableGroup::ObjectGroupId group_id,
  CORBA::ORB_ptr orb,
  PortableGroup::FactoryRegistry_ptr factory_registry,
  TAO::PG_Object_Group_Manipulator & manipulator,
  TAO::Storable_Factory & storable_factory)
  : PG_Object_Group(orb,
                    factory_registry,
                    manipulator)
  , group_previously_stored_(true)
  , group_id_previously_stored_(group_id)
  , storable_factory_ (storable_factory)
  , last_changed_ (0)
  , loaded_from_stream_ (false)
  , destroyed_ (false)
  , write_occurred_ (false)
{
  // Create a temporary stream to simply to check if a readable
  // version already exists.
  bool stream_exists = false;
  {
    ACE_Auto_Ptr<TAO::Storable_Base> stream (
      this->create_stream ("r"));

    if (stream->exists ())
      stream_exists = true;
  }

  if (stream_exists)
    {
      Object_Group_File_Guard fg (*this, SFG::ACCESSOR);
    }
  else
    {
      throw CORBA::INTERNAL ();
    }
}

TAO::PG_Object_Group_Storable::~PG_Object_Group_Storable (void)
{
  if (destroyed_)
    {
      ACE_Auto_Ptr<TAO::Storable_Base> stream (
        this->create_stream ("r"));

      if (stream->exists ())
        {
          stream->remove ();
        }

    }

}

void
TAO::PG_Object_Group_Storable::set_destroyed (bool destroyed)
{
  this->destroyed_ = destroyed;
}

const PortableGroup::Location &
TAO::PG_Object_Group_Storable::get_primary_location (void)
{
  Object_Group_File_Guard fg (*this, SFG::ACCESSOR);
  return TAO::PG_Object_Group::get_primary_location ();
}


void
TAO::PG_Object_Group_Storable::add_member (
  const PortableGroup::Location & the_location,
  CORBA::Object_ptr member)
{
  Object_Group_File_Guard fg (*this, SFG::MUTATOR);
  PG_Object_Group::add_member (the_location, member);
  this->write (fg.peer ());
}

int
TAO::PG_Object_Group_Storable::set_primary_member (
  TAO_IOP::TAO_IOR_Property * prop,
  const PortableGroup::Location & the_location)
{
  Object_Group_File_Guard fg (*this, SFG::MUTATOR);
  int primary_member = PG_Object_Group::set_primary_member (prop, the_location);
  this->write (fg.peer ());
  return primary_member;
}

void
TAO::PG_Object_Group_Storable::remove_member (
  const PortableGroup::Location & the_location)
{
  Object_Group_File_Guard fg (*this, SFG::MUTATOR);
  PG_Object_Group::remove_member (the_location);
  this->write (fg.peer ());
}


PortableGroup::Locations *
TAO::PG_Object_Group_Storable::locations_of_members (void)
{
  Object_Group_File_Guard fg (*this, SFG::ACCESSOR);
  return PG_Object_Group::locations_of_members ();
}

void
TAO::PG_Object_Group_Storable::create_member (
  const PortableGroup::Location & the_location,
  const char * type_id,
  const PortableGroup::Criteria & the_criteria)
{
  Object_Group_File_Guard fg (*this, SFG::MUTATOR);
  PG_Object_Group::create_member (the_location,
                                  type_id,
                                  the_criteria);
  this->write (fg.peer ());
}

void
TAO::PG_Object_Group_Storable::set_name (const char* group_name)
{
  Object_Group_File_Guard fg (*this, SFG::MUTATOR);
  PG_Object_Group::set_name (group_name);
  this->write (fg.peer ());
}

const char*
TAO::PG_Object_Group_Storable::get_name (void)
{
  Object_Group_File_Guard fg (*this, SFG::ACCESSOR);
  return PG_Object_Group::get_name ();
}

void
TAO::PG_Object_Group_Storable::initial_populate (void)
{
  Object_Group_File_Guard fg (*this, SFG::MUTATOR);
  PG_Object_Group::initial_populate ();
  this->write (fg.peer ());
}

void
TAO::PG_Object_Group_Storable::minimum_populate (void)
{
  Object_Group_File_Guard fg (*this, SFG::MUTATOR);
  PG_Object_Group::minimum_populate ();
  this->write (fg.peer ());
}

int
TAO::PG_Object_Group_Storable::has_member_at (
  const PortableGroup::Location & location)
{
  Object_Group_File_Guard fg (*this, SFG::ACCESSOR);
  return PG_Object_Group::has_member_at (location);
}

void
TAO::PG_Object_Group_Storable::distribute (int value)
{
  Object_Group_File_Guard fg (*this, SFG::MUTATOR);
  PG_Object_Group::distribute (value);
  this->write (fg.peer ());
}

CORBA::Object_ptr
TAO::PG_Object_Group_Storable::get_member_reference (
  const PortableGroup::Location & the_location)
{
  Object_Group_File_Guard fg (*this, SFG::ACCESSOR);
  return PG_Object_Group::get_member_reference (the_location);
}

PortableGroup::ObjectGroupId
TAO::PG_Object_Group_Storable::get_object_group_id () const
{
  if (this->group_previously_stored_)
    return this->group_id_previously_stored_;

  return PG_Object_Group::get_object_group_id ();
}

TAO::Storable_Base *
TAO::PG_Object_Group_Storable::create_stream (const char * mode)
{
  char file_name[BUFSIZ];
  int id =  static_cast<int> (this->get_object_group_id ());
  ACE_OS::sprintf (file_name, "ObjectGroup_%d", id);
  return this->storable_factory_.create_stream (file_name, mode);
}

void
TAO::PG_Object_Group_Storable::read (TAO::Storable_Base & stream)
{
  stream.rewind ();

  ACE_CString group_name;
  stream >> group_name;

  PG_Object_Group::set_name(group_name.c_str());

  stream >> this->distribute_;

  stream >> this->role_;

  ///// primary_location_ /////
  read_cdr (stream, this->primary_location_);

  ///// reference_ /////
  ACE_CString reference_ior;
  stream >> reference_ior;
  this->reference_ = this->orb_->string_to_object (reference_ior.c_str ());

  ///// tagged_component_ /////
  read_cdr (stream, this->tagged_component_);

  ///// type_id_ /////
  // special note: A memory leak appears when the type_id_ is read into directly.
  // reading into a temporary string and handing that to the type_id_ does not leak.
  CORBA::String_var tmp;
  read_cdr(stream, tmp);
  this->type_id_ = tmp._retn();

  ///// properties_ /////
  PortableGroup::Criteria properties;
  read_cdr (stream, properties);
  PG_Object_Group::set_properties_dynamically (properties);

  ///// members_ /////
  int num_members;
  stream >> num_members;

  if (num_members == 0)
    this->empty_ = 1;
  else
    this->empty_ = 0;

  this->clear_members_map ();

  for (int i = 0; i < num_members; ++i)
    {
      ///// location used as members_ key /////
      PortableGroup::Location the_location;
      read_cdr (stream, the_location);

      ///// member /////
      ACE_CString member_ior;
      stream >> member_ior;
      CORBA::Object_var member =
        this->orb_->string_to_object (member_ior.c_str ());
      if (CORBA::is_nil (member.in ()))
        {
          if (TAO_debug_level > 0)
            {
              ORBSVCS_DEBUG ((LM_DEBUG,
                              ACE_TEXT ("(%P|%t) PG_Object_Group_Storable::")
                              ACE_TEXT ("string_to_object failed\n")));
            }
          throw CORBA::INV_OBJREF ();
        }

      ///// location /////
      PortableGroup::Location location;
      read_cdr (stream, location);

      ///// factory /////
      ACE_CString factory_ior;
      stream >> factory_ior;
      CORBA::Object_var obj =
        this->orb_->string_to_object (factory_ior.c_str ());
      PortableGroup::GenericFactory_var factory =
        PortableGroup::GenericFactory::_narrow (obj.in());

      ///// factory_id (typedef of CORBA::Any) /////
      PortableGroup::GenericFactory::FactoryCreationId factory_id;
      read_cdr (stream, factory_id);

      ///// is_primary /////
      int is_primary;
      stream >> is_primary;

      MemberInfo * info = 0;
      ACE_NEW_THROW_EX (info, MemberInfo(member.in (),
                                         the_location,
                                         factory.in (),
                                         factory_id),
                        CORBA::NO_MEMORY());

      info->is_primary_ = is_primary;

      if (this->members_.bind (the_location, info) != 0)
        {
          throw CORBA::NO_MEMORY();
        }
    }
}

void
TAO::PG_Object_Group_Storable::write (TAO::Storable_Base & stream)
{
  stream.rewind ();

  ACE_CString group_name = PG_Object_Group::get_name ();
  stream << group_name;
  stream << this->distribute_;
  stream << this->role_;

  TAO_OutputCDR primary_location_cdr;
  primary_location_cdr << PG_Object_Group::get_primary_location ();
  stream << primary_location_cdr;

  CORBA::String_var reference_ior =
    this->orb_->object_to_string (this->reference_.in ());
  stream << reference_ior.in ();

  TAO_OutputCDR tagged_component_cdr;
  tagged_component_cdr << this->tagged_component_;
  stream << tagged_component_cdr;

  TAO_OutputCDR type_id_cdr;
  PortableGroup::TypeId_var type_id = PG_Object_Group::get_type_id ();
  type_id_cdr << type_id;
  stream << type_id_cdr;

  TAO_OutputCDR properties_cdr;
  PortableGroup::Criteria properties;
  this->properties_.export_properties (properties);
  properties_cdr << properties;
  stream << properties_cdr;

  ///// members_ /////
  int num_members = this->members_.current_size  ();
  stream << num_members;
  for (MemberMap_Iterator it = this->members_.begin ();
       it != this->members_.end ();
       ++it)
    {
      PortableGroup::Location the_location = it->key ();
      TAO_OutputCDR the_location_cdr;
      the_location_cdr << the_location;
      stream << the_location_cdr;

      MemberInfo * member = it->item ();
      CORBA::String_var member_ior =
        this->orb_->object_to_string (member->member_.in ());
      stream << member_ior.in ();

      TAO_OutputCDR location_cdr;
      location_cdr << member->location_;
      stream << location_cdr;

      CORBA::String_var factory_ior =
        this->orb_->object_to_string (member->factory_.in ());
      stream << factory_ior.in ();

      TAO_OutputCDR factory_id_cdr;
      factory_id_cdr << member->factory_id_;
      stream << factory_id_cdr;

      stream << (int)member->is_primary_;
    }
  stream.flush ();
  this->write_occurred_ = true;
}

void
TAO::PG_Object_Group_Storable::stale (bool is_stale)
{
  ACE_UNUSED_ARG (is_stale);
  // Default implementation is no-op
}

bool
TAO::PG_Object_Group_Storable::stale ()
{
  // Default is to return false
  return false;
}

void
TAO::PG_Object_Group_Storable::state_written (void)
{
  // No-op. Overridden by derived class.
}

bool
TAO::PG_Object_Group_Storable::is_obsolete (time_t stored_time)
{
  return (!this->loaded_from_stream_) ||
    stored_time > this->last_changed_;
}

TAO_END_VERSIONED_NAMESPACE_DECL