summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/orbsvcs/Naming/Storable_Naming_Context_ReaderWriter.cpp
blob: 91abe2ed650f0808136e241ca38fe5c8a50bf6e6 (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
// -*- C++ -*-

//=============================================================================
/**
 * @file  Storable_Naming_Context_ReaderWriter.cpp
 *
 * $Id$
 *
 * @author Marina Spivak <marina@cs.wustl.edu>
 * @author Byron Harris <harrisb@ociweb.com>
 */
//=============================================================================

#include "orbsvcs/Naming/Storable_Naming_Context_ReaderWriter.h"
#include "orbsvcs/Naming/Storable_Naming_Context.h"
#include "orbsvcs/Naming/Storable.h"

#include "tao/Storable_Base.h"

TAO_BEGIN_VERSIONED_NAMESPACE_DECL

TAO_Storable_Naming_Context_ReaderWriter::
TAO_Storable_Naming_Context_ReaderWriter (TAO::Storable_Base & stream)
  : stream_ (stream)
{
}

void
TAO_Storable_Naming_Context_ReaderWriter::write (
  TAO_Storable_Naming_Context & context)
{
  TAO_NS_Persistence_Header header;

  header.size (static_cast<unsigned int> (
    context.storable_context_->current_size ()));

  header.destroyed (context.destroyed_);

  this->write_header (header);

  if (0u == header.size ())
    return;

  ACE_Hash_Map_Iterator<TAO_Storable_ExtId,TAO_Storable_IntId, ACE_Null_Mutex>
    it = context.storable_context_->map ().begin ();
  ACE_Hash_Map_Iterator<TAO_Storable_ExtId,TAO_Storable_IntId,ACE_Null_Mutex>
    itend = context.storable_context_->map ().end ();

  ACE_Hash_Map_Entry<TAO_Storable_ExtId,TAO_Storable_IntId> ent = *it;

  while (!(it == itend))
  {
    TAO_NS_Persistence_Record record;

    ACE_CString name;
    CosNaming::BindingType bt = (*it).int_id_.type_;
    if (bt ==  CosNaming::ncontext)
      {
        CORBA::Object_var
          obj = context.orb_->string_to_object ((*it).int_id_.ref_.in ());
        if (obj->_is_collocated ())
          {
            // This is a local (i.e. non federated context) we therefore
            // store only the ObjectID (persistence filename) for the object.

            // The driving force behind storing ObjectIDs rather than IORs for
            // local contexts is to provide for a redundant naming service.
            // That is, a naming service that runs simultaneously on multiple
            // machines sharing a file system. It allows multiple redundant
            // copies to be started and stopped independently.
            // The original target platform was Tru64 Clusters where there was
            // a cluster address. In that scenario, clients may get different
            // servers on each request, hence the requirement to keep
            // synchronized to the disk. It also works on non-cluster system
            // where the client picks one of the redundant servers and uses it,
            // while other systems can pick different servers. (However in this
            // scenario, if a server fails and a client must pick a new server,
            // that client may not use any saved context IORs, instead starting
            // from the root to resolve names. So this latter mode is not quite
            // transparent to clients.) [Rich Seibel (seibel_r) of ociweb.com]

            PortableServer::ObjectId_var
              oid = context.poa_->reference_to_id (obj.in ());
            CORBA::String_var
              nm = PortableServer::ObjectId_to_string (oid.in ());
            const char
              *newname = nm.in ();
            name.set (newname); // The local ObjectID (persistance filename)
            record.type (TAO_NS_Persistence_Record::LOCAL_NCONTEXT);
          }
        else
          {
            // Since this is a foreign (federated) context, we can not store
            // the objectID (because it isn't in our storage), if we did, when
            // we restore, we would end up either not finding a permanent
            // record (and thus ending up incorrectly assuming the context was
            // destroyed) or loading another context altogether (just because
            // the contexts shares its objectID filename which is very likely).
            // [Simon Massey  (sma) of prismtech.com]

            name.set ((*it).int_id_.ref_.in ()); // The federated context IOR
            record.type (TAO_NS_Persistence_Record::REMOTE_NCONTEXT);
          }
      }
    else // if (bt == CosNaming::nobject) // shouldn't be any other, can there?
      {
        name.set ((*it).int_id_.ref_.in ()); // The non-context object IOR
        record.type (TAO_NS_Persistence_Record::OBJREF);
      }
    record.ref (name);

    const char *myid = (*it).ext_id_.id ();
    ACE_CString id (myid);
    record.id (id);

    const char *mykind = (*it).ext_id_.kind ();
    ACE_CString kind (mykind);
    record.kind (kind);

    write_record (record);
    it.advance ();
  }

  context.write_occurred_ = 1;
}

int
TAO_Storable_Naming_Context_ReaderWriter::read (
  TAO_Storable_Naming_Context & context)
{
  // assume file already open for reading
  TAO_Storable_Bindings_Map *bindings_map;

  // create the new bindings map
  ACE_NEW_THROW_EX (bindings_map,
                    TAO_Storable_Bindings_Map (context.hash_table_size_,
                                               context.orb_.in ()),
                    CORBA::NO_MEMORY ());

  // get the data for this bindings map from the file

  TAO_NS_Persistence_Header header;
  TAO_NS_Persistence_Record record;

  // we are only using the size from this header
  this->read_header (header);
  if (!stream_.good ())
    {
      stream_.clear ();
      throw CORBA::INTERNAL ();
    }

  // reset the destroyed flag
  context.destroyed_ = header.destroyed ();

  // read in the data for the map
  for (unsigned int i= 0u; i<header.size (); ++i)
    {
      this->read_record (record);
      if (!stream_.good ())
        {
          stream_.clear ();
          throw CORBA::INTERNAL ();
        }

      if (TAO_NS_Persistence_Record::LOCAL_NCONTEXT == record.type ())
        {
          PortableServer::ObjectId_var
            id = PortableServer::string_to_ObjectId (record.ref ().c_str ());
          const char
            *intf = context.interface_->_interface_repository_id ();
          CORBA::Object_var
            objref = context.poa_->create_reference_with_id (id.in (), intf);
          bindings_map->bind ( record.id ().c_str (),
                               record.kind ().c_str (),
                               objref.in (),
                               CosNaming::ncontext );
        }
      else
        {
          CORBA::Object_var
            objref = context.orb_->string_to_object (record.ref ().c_str ());
          bindings_map->bind ( record.id ().c_str (),
                               record.kind ().c_str (),
                               objref.in (),
                               ((TAO_NS_Persistence_Record::REMOTE_NCONTEXT
                                == record.type ())
                                ? CosNaming::ncontext    // REMOTE_NCONTEXT
                                : CosNaming::nobject )); // OBJREF
        }
    }
  context.storable_context_ = bindings_map;
  context.context_ = context.storable_context_;
  return 0;
}

void
TAO_Storable_Naming_Context_ReaderWriter::write_header (
  const TAO_NS_Persistence_Header & header)
{
  stream_.rewind ();
  stream_ << header.size ();
  stream_ << header.destroyed ();
  stream_.flush ();
}
void
TAO_Storable_Naming_Context_ReaderWriter::read_header (
  TAO_NS_Persistence_Header & header)
{
  unsigned int size;
  int destroyed;

  stream_.rewind ();

  stream_ >> size;
  header.size (size);

  stream_ >> destroyed;
  header.destroyed (destroyed);
}

void
TAO_Storable_Naming_Context_ReaderWriter::write_record (const TAO_NS_Persistence_Record & record)
{
  TAO_NS_Persistence_Record::Record_Type type = record.type ();
  stream_ << type;

  stream_ << record.id ();
  stream_ << record.kind ();
  stream_ << record.ref ();

  stream_.flush ();
}

void
TAO_Storable_Naming_Context_ReaderWriter::read_record (TAO_NS_Persistence_Record & record)
{
  int temp_type_in;
  stream_ >> temp_type_in;
  TAO_NS_Persistence_Record::Record_Type type =
    (TAO_NS_Persistence_Record::Record_Type) temp_type_in;
  record.type (type);

  ACE_CString record_id;
  stream_ >> record_id;
  record.id (record_id);

  ACE_CString record_kind;
  stream_ >> record_kind;
  record.kind (record_kind);

  ACE_CString record_ref;
  stream_ >> record_ref;
  record.ref (record_ref);
}

void
TAO_Storable_Naming_Context_ReaderWriter::write_global (const TAO_NS_Persistence_Global & global)
{
  stream_.rewind ();
  stream_ << global.counter ();
  stream_.flush ();
}

void
TAO_Storable_Naming_Context_ReaderWriter::read_global (TAO_NS_Persistence_Global & global)
{
  unsigned int counter = 0;

  stream_.rewind ();
  stream_ >> counter;
  global.counter (counter);

}

TAO_END_VERSIONED_NAMESPACE_DECL