summaryrefslogtreecommitdiff
path: root/TAO/examples/POA/DSI/Database_i.cpp
blob: a8c998cfa1f727e0b0a695fabd24963bb4d70d0b (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
// $Id$

#include "Database_i.h"

ACE_RCSID(DSI, Database_i, "$Id$")

DatabaseImpl::Simpler_Malloc::Simpler_Malloc (void)
  : MALLOC (ACE_DEFAULT_BACKING_STORE)
{
}

DatabaseImpl::Simpler_Malloc::~Simpler_Malloc (void)
{
  this->remove ();
}

DatabaseImpl::Entry::Entry (CORBA::ORB_ptr orb,
                            PortableServer::POA_ptr poa,
                            CORBA::Environment &ACE_TRY_ENV)
  : orb_ (CORBA::ORB::_duplicate (orb)),
    poa_ (PortableServer::POA::_duplicate (poa))
{
  // Get the POA Current object reference
  CORBA::Object_var obj = this->orb_->resolve_initial_references ("POACurrent",
                                                                  ACE_TRY_ENV);
  ACE_CHECK;

  // Narrow the object reference to a POA Current reference
  this->poa_current_ = PortableServer::Current::_narrow (obj.in (),
                                                         ACE_TRY_ENV);
  ACE_CHECK;
}

DatabaseImpl::Entry::~Entry (void)
{
}

void
DatabaseImpl::Entry::invoke (CORBA::ServerRequest_ptr request,
                             CORBA::Environment &ACE_TRY_ENV)
{
  // The servant determines the key associated with thex database
  // entry represented by self
  PortableServer::ObjectId_var oid =
    this->poa_current_->get_object_id (ACE_TRY_ENV);
  ACE_CHECK;

  // Now convert the id into a string
  CORBA::String_var key =
    PortableServer::ObjectId_to_string (oid.in ());

  // Get the operation name for this request
  const char *operation = request->operation ();

  if (ACE_OS::strcmp (operation, "_is_a") == 0)
    {
      this->is_a (request, ACE_TRY_ENV);
      ACE_CHECK;
    }
  else
    {
      ACE_THROW (CORBA::NO_IMPLEMENT ());
    }
}

void
DatabaseImpl::Entry::is_a (CORBA::ServerRequest_ptr request,
                           CORBA::Environment &ACE_TRY_ENV)
{
  CORBA::NVList_ptr list;
  this->orb_->create_list (0, list);

  CORBA::Any any_1 (CORBA::_tc_string);

  list->add_value ("value",
                   any_1,
                   CORBA::ARG_IN,
                   ACE_TRY_ENV);
  ACE_CHECK;

  request->arguments (list,
                      ACE_TRY_ENV);
  ACE_CHECK;

  CORBA_NamedValue_ptr nv = list->item (0, ACE_TRY_ENV);
  ACE_CHECK;

  CORBA::Any_ptr ap = nv->value ();
  char *value;
  *ap >>= value;

  const char *object_id = CORBA::_tc_Object->id (ACE_TRY_ENV);
  ACE_CHECK;

  CORBA::Boolean result;
  if (!ACE_OS::strcmp (value, "IDL:Database/Employee:1.0") ||
      !ACE_OS::strcmp (value, "IDL:Database/Entry:1.0") ||
      !ACE_OS::strcmp (value, object_id))
    result = 1;
  else
    result = 0;

  CORBA::Any result_any;
  CORBA::Any::from_boolean from_boolean (result);
  result_any <<= from_boolean;

  request->set_result (result_any, ACE_TRY_ENV);
  ACE_CHECK;
}

CORBA::RepositoryId
DatabaseImpl::Entry::_primary_interface (const PortableServer::ObjectId &/*oid*/,
                                         PortableServer::POA_ptr,
                                         CORBA::Environment &)
{
  return 0;
}

PortableServer::POA_ptr
DatabaseImpl::Entry::_default_POA (CORBA::Environment &)
{
  return PortableServer::POA::_duplicate (this->poa_.in ());
}

DatabaseImpl::Agent::Agent (CORBA::ORB_ptr orb,
                            PortableServer::POA_ptr poa,
                            CORBA::Environment &ACE_TRY_ENV)
  : orb_ (CORBA::ORB::_duplicate (orb)),
    poa_ (PortableServer::POA::_duplicate (poa)),
    common_servant_ (orb,
                     poa,
                     ACE_TRY_ENV)
{
  ACE_CHECK;

  this->poa_->set_servant (&this->common_servant_,
                           ACE_TRY_ENV);
  ACE_CHECK;
}

DatabaseImpl::Agent::~Agent (void)
{
}

Database::Entry_ptr
DatabaseImpl::Agent::create_entry (const char *key,
                                   const char *entry_type,
                                   const Database::NVPairSequence &initial_attributes,
                                   CORBA::Environment &ACE_TRY_ENV)
{
  // Create a new entry in the database:
  if (ACE_OS::strcmp (entry_type, "Employee") != 0 ||
      initial_attributes.length () != 2)
    {
      ACE_THROW_RETURN (Database::Unknown_Type (),
                        Database::Entry::_nil ());
    }

  char *name = 0;
  CORBA::Long id = 0;

  const Database::NamedValue &first = initial_attributes[0];
  const Database::NamedValue &second = initial_attributes[1];
  if (ACE_OS::strcmp (first.name.in (), "name") != 0 ||
      ACE_OS::strcmp (second.name.in (), "id") != 0)
    {
      ACE_THROW_RETURN (Database::Unknown_Type (),
                        Database::Entry::_nil ());
    }

  first.value >>= name;
  second.value >>= id;

  Employee *new_employee = new Employee (name, id);
  DATABASE::instance ()->bind (key, new_employee);

  ACE_DEBUG ((LM_DEBUG,
              "New employee created with name = %s and id = %d\n",
              name,
              id));

  // creates a reference to the CORBA object used to encapsulate
  // access to the new entry in the database.  There is an interface
  // for each entry type:
  PortableServer::ObjectId_var obj_id = PortableServer::string_to_ObjectId (key);
  CORBA::String_var repository_id = DatabaseImpl::entry_type_to_repository_id ("Entry");

  CORBA::Object_var obj = this->poa_->create_reference_with_id (obj_id.in (),
                                                                repository_id.in (),
                                                                ACE_TRY_ENV);
  ACE_CHECK_RETURN (Database::Entry::_nil ()) ;

  Database::Entry_var entry = Database::Entry::_narrow (obj.in (),
                                                        ACE_TRY_ENV);
  ACE_CHECK_RETURN (Database::Entry::_nil ());

  return entry._retn ();
}

Database::Entry_ptr
DatabaseImpl::Agent::find_entry (const char *key,
                                 const char *entry_type,
                                 CORBA::Environment &ACE_TRY_ENV)
{
  if (ACE_OS::strcmp (entry_type, "Employee") != 0)
    {
      ACE_THROW_RETURN (Database::Unknown_Type (),
                        Database::Entry::_nil ());
    }

  void *temp;
  Database::Entry_var entry;
  if (DATABASE::instance ()->find (key, temp) == 0)
    {
      Employee *employee = (Employee *) temp;

      ACE_DEBUG ((LM_DEBUG,
                  "Employee with key = %s found: name = %s and id = %d\n",
                  key,
                  employee->name (),
                  employee->id ()));

      // creates a reference to the CORBA object used to encapsulate
      // access to the new entry in the database.  There is an interface
      // for each entry type:
      PortableServer::ObjectId_var obj_id = PortableServer::string_to_ObjectId (key);
      CORBA::String_var repository_id = DatabaseImpl::entry_type_to_repository_id ("Entry");
      CORBA::Object_var obj = this->poa_->create_reference_with_id (obj_id.in (),
                                                                    repository_id.in (),
                                                                    ACE_TRY_ENV);
      ACE_CHECK_RETURN (Database::Entry::_nil ());

      entry = Database::Entry::_narrow (obj.in (),
                                        ACE_TRY_ENV);
      ACE_CHECK_RETURN (Database::Entry::_nil ());
    }
  else
    {
      ACE_DEBUG ((LM_DEBUG,
                  "Employee with key = %s not found\n",
                  key));

      ACE_THROW_RETURN (Database::Unknown_Key (),
                        Database::Entry::_nil ());
    }

  return entry._retn ();
}

void
DatabaseImpl::Agent::destroy_entry (const char *key,
                                    const char *entry_type,
                                    CORBA::Environment &ACE_TRY_ENV)
{
  if (ACE_OS::strcmp (entry_type, "Employee") != 0)
    {
      ACE_THROW (Database::Unknown_Type ());
    }

  void *temp;
  if (DATABASE::instance ()->unbind (key, temp) == 0)
    {
      Employee *employee = (Employee *) temp;

      ACE_DEBUG ((LM_DEBUG,
                  "Employee with key = %s will be removed from the database: "
                  "name = %s and id = %d \n",
                  key,
                  employee->name (),
                  employee->id ()));

      delete employee;
    }
  else
    {
      ACE_DEBUG ((LM_DEBUG,
                  "Employee with key = %s not found\n",
                  key));

      ACE_THROW (Database::Unknown_Key ());
    }
}

void
DatabaseImpl::Agent::shutdown (CORBA::Environment &)
{
  this->orb_->shutdown ();
}

PortableServer::POA_ptr
DatabaseImpl::Agent::_default_POA (CORBA::Environment &)
{
  return PortableServer::POA::_duplicate (this->poa_.in ());
}

char *
DatabaseImpl::entry_type_to_repository_id (const char *entry_type)
{
  static const char *prefix = "IDL:Database/";
  static int prefix_length = ACE_OS::strlen (prefix);
  static const char *suffix = ":1.0";
  static int suffix_length = ACE_OS::strlen (prefix);

  int len =
    prefix_length +
    ACE_OS::strlen (entry_type) +
    suffix_length +
    1;

  char *result = CORBA::string_alloc (len);
  ACE_OS::sprintf (result,
                   "%s%s%s",
                   prefix,
                   entry_type,
                   suffix);
  return result;
}

DatabaseImpl::Employee::Employee (const char* name,
                                  CORBA::Long id)
  : id_ (id),
    name_ (0)
{
  this->name (name);
}

DatabaseImpl::Employee::~Employee (void)
{
  DATABASE::instance ()->free (this->name_);
}

const char *
DatabaseImpl::Employee::name (void) const
{
  return this->name_;
}

void
DatabaseImpl::Employee::name (const char* name)
{
  DATABASE::instance ()->free (this->name_);
  this->name_ = (char *) DATABASE::instance ()->malloc (ACE_OS::strlen (name) + 1);
  ACE_OS::strcpy (this->name_, name);
}

CORBA::Long
DatabaseImpl::Employee::id (void) const
{
  return this->id_;
}

void
DatabaseImpl::Employee::id (CORBA::Long id)
{
  this->id_ = id;
}

void *
DatabaseImpl::Employee::operator new (size_t size)
{
  return DATABASE::instance ()->malloc (size);
}

void
DatabaseImpl::Employee::operator delete (void *pointer)
{
  DATABASE::instance ()->free (pointer);
}

#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
template class ACE_Malloc<ACE_MMAP_MEMORY_POOL, ACE_Null_Mutex>;
template class ACE_Malloc_Iterator<ACE_MMAP_MEMORY_POOL, ACE_Null_Mutex>;
template class ACE_Singleton<DatabaseImpl::Simpler_Malloc, ACE_Null_Mutex>;
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
#pragma instantiate ACE_Malloc<ACE_MMAP_MEMORY_POOL, ACE_Null_Mutex>
#pragma instantiate ACE_Malloc_Iterator<ACE_MMAP_MEMORY_POOL, ACE_Null_Mutex>
#pragma instantiate ACE_Singleton<DatabaseImpl::Simpler_Malloc, ACE_Null_Mutex>
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */