summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/orbsvcs/PortableGroup/PG_Group_List_Store.cpp
blob: 9bfdebcffd05d77e5993be1f2816c74aeab140a4 (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
// -*- C++ -*-

//=============================================================================
/**
 * @file  PG_Group_List_Store.cpp
 *
 * $Id$
 *
 * @author Byron Harris <harrisb@ociweb.com>
 */
//=============================================================================


#include "orbsvcs/PortableGroup/PG_Group_List_Store.h"

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

#include <algorithm>

TAO_BEGIN_VERSIONED_NAMESPACE_DECL

namespace TAO
{
  class PG_Group_List_Store_File_Guard : public TAO::Storable_File_Guard
  {
  public:
    PG_Group_List_Store_File_Guard ( PG_Group_List_Store & list_store,
                                     const char * mode);

    ~PG_Group_List_Store_File_Guard ();

    virtual void set_object_last_changed (const time_t & time);

    virtual time_t get_object_last_changed ();

    virtual bool object_obsolete ();

    virtual void mark_object_current ();

    virtual void load_from_stream ();

    virtual bool is_loaded_from_stream ();

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

private:

    PG_Group_List_Store & list_store_;
  };
}

TAO::PG_Group_List_Store_File_Guard::PG_Group_List_Store_File_Guard (
  PG_Group_List_Store & list_store, const char * mode)
  : TAO::Storable_File_Guard(true),
  list_store_(list_store)
{
  this->init(mode);
}

TAO::PG_Group_List_Store_File_Guard::~PG_Group_List_Store_File_Guard ()
{
  this->release ();
}

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

time_t
TAO::PG_Group_List_Store_File_Guard::get_object_last_changed ()
{
  return list_store_.last_changed_;
}

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

void
TAO::PG_Group_List_Store_File_Guard::mark_object_current ()
{
  // Reset the stale flag
  list_store_.stale(false);
  // Set the last update time to the file last update time
  this->set_object_last_changed (fl_->last_changed ());
}

void
TAO::PG_Group_List_Store_File_Guard::load_from_stream ()
{
  list_store_.read (this->peer ());
  list_store_.loaded_from_stream_ = true;
  this->peer ().rewind ();
}

bool
TAO::PG_Group_List_Store_File_Guard::is_loaded_from_stream ()
{
  return list_store_.loaded_from_stream_;
}

TAO::Storable_Base *
TAO::PG_Group_List_Store_File_Guard::create_stream (const char * mode)
{
  return list_store_.create_stream (mode);
}

typedef TAO::PG_Group_List_Store_File_Guard File_Guard;

TAO::PG_Group_List_Store::PG_Group_List_Store (
  Storable_Factory & storable_factory)
  : next_group_id_ (0)
  , storable_factory_ (storable_factory)
  , loaded_from_stream_ (false)
  , last_changed_ (0)
  , stale_ (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 (ACE_TEXT_ALWAYS_CHAR ("r")));

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

  if (stream_exists)
    {
      File_Guard fg(*this, ACE_TEXT_ALWAYS_CHAR ("r"));
    }
  else
    {
      File_Guard fg(*this, ACE_TEXT_ALWAYS_CHAR ("wc"));
      this->write (fg.peer ());
    }
}

TAO::PG_Group_List_Store::~PG_Group_List_Store ()
{
}

PortableGroup::ObjectGroupId
TAO::PG_Group_List_Store::get_next_group_id ()
{
  File_Guard fg(*this, "rw");
  PortableGroup::ObjectGroupId next_id = this->next_group_id_;
  ++this->next_group_id_;
  this->write (fg.peer ());
  return next_id;
}

int
TAO::PG_Group_List_Store::add (PortableGroup::ObjectGroupId id)
{
  File_Guard fg(*this, "rw");
  Group_Id_Const_Iterator it = std::find (this->group_ids_.begin (),
                                          this->group_ids_.end (),
                                          id);
  if (it != this->group_ids_.end())
    return -1;
  this->group_ids_.insert (id);
  this->write (fg.peer ());
  return 0;
}

int
TAO::PG_Group_List_Store::remove (PortableGroup::ObjectGroupId id)
{
  File_Guard fg(*this, "rw");
  Group_Id_Iterator it = std::find (this->group_ids_.begin (),
                                    this->group_ids_.end (),
                                    id);
  if (it == this->group_ids_.end ())
    return -1;
  this->group_ids_.erase (it);
  this->write (fg.peer ());
  return 0;
}

TAO::PG_Group_List_Store::Group_Ids &
TAO::PG_Group_List_Store::get_group_ids ()
{
  File_Guard fg(*this, ACE_TEXT_ALWAYS_CHAR ("r"));
  return group_ids_;
}

void
TAO::PG_Group_List_Store::read (TAO::Storable_Base & stream)
{
  group_ids_.clear ();

  stream.rewind ();
  if (!stream.good ())
    throw CORBA::INTERNAL ();

  unsigned int next_group_id;
  stream >> next_group_id;
  this->next_group_id_ = next_group_id;
  if (!stream.good ())
    throw CORBA::INTERNAL ();

  int size;
  stream >> size;
  if (!stream.good ())
    throw CORBA::INTERNAL ();

  // TODO: Look at adding streaming of unsigned long long
  // PortableGroup::ObjectGroupId group_id;
  int group_id;
  for (int i = 0; i < size; ++i)
    {
      stream >> group_id;
      if (!stream.good ())
        throw CORBA::INTERNAL ();
      group_ids_.insert (group_id);
    }
}

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

  unsigned int next_group_id = static_cast<unsigned int> (this->next_group_id_);
  stream << next_group_id;

  int size = group_ids_.size ();
  stream << size;
  for (Group_Id_Const_Iterator it = group_ids_.begin ();
                               it != group_ids_.end (); ++it)
    {
      int group_id = static_cast<int> (*it);
      stream << group_id;
    }

  stream.flush ();
}

TAO::Storable_Base *
TAO::PG_Group_List_Store::create_stream (const char * mode)
{
  return this->storable_factory_.create_stream (
    ACE_TEXT_ALWAYS_CHAR ("ObjectGroup_global"), mode);
}

bool
TAO::PG_Group_List_Store::list_obsolete ()
{
  // TODO: Upate if obsolete flag is set based on CORBA call.
  ACE_Auto_Ptr<TAO::Storable_Base> stream (this->create_stream ("r"));
  if (!stream->exists ())
    throw CORBA::INTERNAL ();
  stream->open ();
  return this->is_obsolete (stream->last_changed ());
}

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

void
TAO::PG_Group_List_Store::stale (bool is_stale)
{
  this->stale_ = is_stale;
}

bool
TAO::PG_Group_List_Store::stale ()
{
  return this->stale_;
}

TAO_END_VERSIONED_NAMESPACE_DECL