summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/examples/CosEC/Factory/CosEventChannelFactory_i.cpp
blob: 06855792215d26f06286adfa13a0268dac482390 (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
// -*- C++ -*-
// $Id$

#include "CosEventChannelFactory_i.h"
#include "FactoryCosEventChannel_i.h"
#include "ace/Auto_Ptr.h"
#include "tao/POA.h"

TAO_CosEventChannelFactory_i::TAO_CosEventChannelFactory_i (void)
  :poa_ (PortableServer::POA::_nil ()),
   naming_ (CosNaming::NamingContext::_nil ())
{
  // No-Op.
}

TAO_CosEventChannelFactory_i::~TAO_CosEventChannelFactory_i (void)
{
  ACE_DEBUG ((LM_DEBUG,
              "in TAO_CosEventChannelFactory_i dtor"));
  // No-Op.
}

int
TAO_CosEventChannelFactory_i::init
(PortableServer::POA_ptr poa,
 CosNaming::NamingContext_ptr naming,
 CORBA_Environment &TAO_IN_ENV
 )
{
  // Check if we have a parent poa.
  if (poa == PortableServer::POA::_nil ())
    return -1;

  // Create a UNIQUE_ID policy
  PortableServer::IdUniquenessPolicy_var idpolicy =
    poa->create_id_uniqueness_policy (PortableServer::MULTIPLE_ID, //UNIQUE_ID,
                                      ACE_TRY_ENV);
  ACE_CHECK_RETURN (-1);
  // @@ Pradeep: Why did you end up using the MULTIPLE_ID policy? And
  //    why doesn't it match with the comment?

  // Create a PolicyList
  CORBA::PolicyList policy_list;
  policy_list.length (1);
  policy_list [0] =
    PortableServer::IdUniquenessPolicy::_duplicate (idpolicy);
  // @@ Pradeep: maybe a little comment explaining why you don't need
  //    to override any POA policies would be a good idea.

  // Create the child POA.
  this->poa_ = poa->create_POA ("CosEC_ChildPOA",
                                PortableServer::POAManager::_nil (),
                                policy_list,
                                ACE_TRY_ENV);

  ACE_CHECK_RETURN (-1);

  idpolicy->destroy (ACE_TRY_ENV);
  ACE_CHECK_RETURN (-1);

  this->poa_ = poa;

  // @@ I want to use the child poa but the factoryec::create fails
  //    with that.
  //    So use the root poa for the time being. ;(
  // @@ Pradeep: Maybe it fails because you didn't specify the USER_ID
  //    policy too?
  return 0;
}

CosEventChannelAdmin::EventChannel_ptr
TAO_CosEventChannelFactory_i::create
(
 const char * channel_id,
 CORBA::Boolean store_in_naming_service,
 CORBA::Environment &ACE_TRY_ENV
 )
{
  ACE_ASSERT (this->poa_ != PortableServer::POA::_nil ());

  ACE_TRY
    {
      PortableServer::ObjectId_var oid =
        TAO_POA::string_to_ObjectId (channel_id);

      FactoryCosEventChannel_i *_ec;

      ACE_NEW_THROW_EX (_ec,
                        FactoryCosEventChannel_i,
                        CORBA::NO_MEMORY ());
      // @@ Pradeep: you may need to pass more arguments to that
      //    exception.
      ACE_TRY_CHECK;

      auto_ptr <FactoryCosEventChannel_i> ec (_ec);

      // @@ Pradeep: you may want to store the _ec variable directly
      //    in the auto_ptr, that way you don't leak memory if the
      //    constructor raises an exception...

      // @@ Pradeep: use the auto_ptr to manipulate the ec, that way
      //    you don't need to change the code if the initialization of
      //    _ec and ec changes.
      if (_ec->init (this->poa_,
                     ACE_TRY_ENV) == -1)
        return 0;
      // @@ Pradeep: you have a point here! the POA that the EC needs
      //    to activate its own objects maybe different from the POA
      //    where you activate the EC itself, this is because the
      //    policies required in each case are different....

      ACE_TRY_CHECK;

      ACE_CString str_channel_id (channel_id);

      if (_ec->activate (str_channel_id,
                         ACE_TRY_ENV) == -1)
        return 0;

      this->poa_->activate_object_with_id (oid,
                                           _ec,
                                           ACE_TRY_ENV);
      ACE_TRY_CHECK;

      CORBA::Object_var obj =
        this->poa_->id_to_reference (oid,
                                     ACE_TRY_ENV);
      ACE_TRY_CHECK;

      // Give the ownership to the POA.
      _ec->_remove_ref (ACE_TRY_ENV);
      ACE_TRY_CHECK;
      // @@ Pradeep: is this OK? Does the reference count start at 1
      //     then?

      if (store_in_naming_service &&
          this->naming_ != CosNaming::NamingContext::_nil ())
        {
          CosNaming::Name name (1);
          name.length (1);
          name[0].id = CORBA::string_dup (channel_id);
          this->naming_->bind (name,
                               obj.in (),
                               ACE_TRY_ENV);
          ACE_TRY_CHECK;
        }

      ec.release (); // release the ownership from the auto_ptr.

      return CosEventChannelAdmin::EventChannel::_narrow (obj.in ());
    }
  ACE_CATCH (PortableServer::POA::ServantAlreadyActive, sa_ex)
    {
      // @@ Pradeep: you shouldn't return 0, but
      //    CosEventChannelAdmin::EventChannel::_nil () use a
      //    temporary variable or a macro if it is too much pain.
      ACE_THROW_RETURN (CosEventChannelFactory::DuplicateChannel,
                        0);
    }
  ACE_CATCH (PortableServer::POA::ObjectAlreadyActive, oaa_ex)
    {
      ACE_THROW_RETURN (CosEventChannelFactory::DuplicateChannel,
                        0);
    }
  ACE_CATCH (PortableServer::POA::WrongPolicy, wp_ex)
    {
      ACE_THROW_RETURN (CORBA::UNKNOWN (),
                        0);
    }
  ACE_CATCH (PortableServer::POA::ObjectNotActive, ona_ex)
    {
      ACE_THROW_RETURN (CosEventChannelFactory::BindFailed,
                        0);
    }
  ACE_CATCH (CosNaming::NamingContext::NotFound, nf_ex)
    {
      ACE_THROW_RETURN (CosEventChannelFactory::BindFailed,
                        0);
    }
  ACE_CATCH (CosNaming::NamingContext::CannotProceed, cp_ex)
    {
      ACE_THROW_RETURN (CosEventChannelFactory::BindFailed,
                        0);
    }
  ACE_CATCH (CosNaming::NamingContext::InvalidName, in_ex)
    {
      ACE_THROW_RETURN (CosEventChannelFactory::BindFailed,
                        0);
    }
  ACE_CATCH (CosNaming::NamingContext::AlreadyBound, ab)
    {
      ACE_THROW_RETURN (CosEventChannelFactory::BindFailed,
                        0);
    }
  ACE_ENDTRY;
  ACE_CHECK_RETURN (0);

  ACE_NOTREACHED (return 0;)
}

void
TAO_CosEventChannelFactory_i::destroy
(
 const char * channel_id,
 CORBA::Boolean unbind_from_naming_service,
 CORBA::Environment &ACE_TRY_ENV
 )
{
  ACE_ASSERT (this->poa_ != PortableServer::POA::_nil ());

  ACE_TRY
    {
      // Get hold of the objectid first.
      PortableServer::ObjectId_var oid =
        TAO_POA::string_to_ObjectId (channel_id);

      CORBA::Object_var obj =
        this->poa_->id_to_reference (oid,
                                     ACE_TRY_ENV);
      ACE_CHECK;
      // Remove from the naming service.
      if (unbind_from_naming_service &&
          this->naming_ != CosNaming::NamingContext::_nil ())
        {
          CosNaming::Name name (1);
          name.length (1);
          name[0].id = CORBA::string_dup (channel_id);
          this->naming_->unbind (name,
                                 ACE_TRY_ENV);
          ACE_CHECK;
        }
      // deactivate from the poa.
      this->poa_->deactivate_object (oid,
                                     ACE_TRY_ENV);

      CosEventChannelAdmin::EventChannel_var fact_ec =
        CosEventChannelAdmin::EventChannel::_narrow (obj.in (),
                                                     ACE_TRY_ENV);
      ACE_CHECK;

      // @@ Pradeep: in a completely complaint ORB this will fail: you
      //    are invoking an operation after the servant was
      //    deactivated.  It is even possible that the reference count
      //    went to 0 already and the program crashes.
      //    We need to either invoke this before or use
      //    id_to_servant() then a downcast and then operate on the
      //    servant directly.
      fact_ec->destroy (ACE_TRY_ENV);
      ACE_CHECK;
    }
  ACE_CATCHANY
    {
      ACE_THROW (CosEventChannelFactory::NoSuchChannel);
    }
  ACE_ENDTRY;
  ACE_CHECK;
}

CosEventChannelAdmin::EventChannel_ptr
TAO_CosEventChannelFactory_i::find
(
 const char * channel_id,
 CORBA::Environment &ACE_TRY_ENV
 )
{
  ACE_ASSERT (this->poa_ != PortableServer::POA::_nil ());

  ACE_TRY
    {
      PortableServer::ObjectId_var oid =
        TAO_POA::string_to_ObjectId (channel_id);

      CORBA::Object_var obj =
        this->poa_->id_to_reference (oid,
                                     ACE_TRY_ENV);
      ACE_TRY_CHECK;
      return CosEventChannelAdmin::EventChannel::_narrow (obj.in ());
    }
  ACE_CATCHANY
    {
      // @@ Pradeep: same comment about 0 vs. _nil()
      ACE_THROW_RETURN (CosEventChannelFactory::NoSuchChannel,
                        0);
    }
  ACE_ENDTRY;
  ACE_CHECK_RETURN (0);

  ACE_NOTREACHED (return 0;)
}

char*
TAO_CosEventChannelFactory_i::find_channel_id
(
 CosEventChannelAdmin::EventChannel_ptr channel,
 CORBA::Environment &ACE_TRY_ENV
 )
{
  ACE_ASSERT (this->poa_ != PortableServer::POA::_nil ());

  ACE_TRY
    {
      PortableServer::ObjectId_var oid =
        this->poa_->reference_to_id (channel,
                                     ACE_TRY_ENV);
      ACE_TRY_CHECK;

      return TAO_POA::ObjectId_to_string (oid);
    }
  ACE_CATCHANY
    {
      ACE_THROW_RETURN (CosEventChannelFactory::NoSuchChannel,
                        0);
    }
  ACE_ENDTRY;
  ACE_CHECK_RETURN (0);

  ACE_NOTREACHED (return 0;)
}

int
main (int argc, char *argv [])
{
  // @@ Pradeep: can we put the main function on a separate file? That
  //    will make it easier for the users to integrate the class in
  //    their system.
  //    Also: can we give the users some command line options to
  //    control the name to use in the naming service?
  const char* Factory_Name = "CosEC_Factory";
  // The name of the factory registered with the naming service.

  TAO_CosEventChannelFactory_i *factory_servant_;
  // The factory servant.

  PortableServer::POA_var root_poa_;
  // Reference to the root poa.

  CORBA::ORB_var orb_;
  // The ORB that we use.

  CosEventChannelFactory::ChannelFactory_var factory_;
  // The corba object after activation.

  TAO_Naming_Client naming_client_;
  // Use a naming client.

  // Ref counted servants are on the heap..
  ACE_NEW_RETURN (factory_servant_,
                  TAO_CosEventChannelFactory_i (),
                  1);

  ACE_DECLARE_NEW_CORBA_ENV;
  ACE_TRY
    {
      orb_ = CORBA::ORB_init (argc,
                              argv,
                              "",
                              ACE_TRY_ENV);
      ACE_TRY_CHECK;

      CORBA::Object_var poa_object  =
        orb_->resolve_initial_references("RootPOA",
                                         ACE_TRY_ENV);
      ACE_TRY_CHECK;

      if (CORBA::is_nil (poa_object.in ()))
        ACE_ERROR_RETURN ((LM_ERROR,
                           " (%P|%t) Unable to initialize the POA.\n"),
                          -1);

      root_poa_ =
        PortableServer::POA::_narrow (poa_object.in (),
                                      ACE_TRY_ENV);
      ACE_TRY_CHECK;

      PortableServer::POAManager_var poa_manager =
        root_poa_->the_POAManager (ACE_TRY_ENV);

      ACE_TRY_CHECK;

      poa_manager->activate (ACE_TRY_ENV);

      ACE_TRY_CHECK;

      // Initialization of the naming service.
      if (naming_client_.init (orb_.in ()) != 0)
        ACE_ERROR_RETURN ((LM_ERROR,
                           "(%P|%t) Unable to initialize "
                           "the TAO_Naming_Client. \n"),
                          1);

      if (factory_servant_->init (root_poa_.in (),
                                  naming_client_.get_context (),
                                 ACE_TRY_ENV) != 0)
        ACE_ERROR_RETURN ((LM_ERROR,
                           "(%P|%t) Unable to initialize "
                           "the factory. \n"),
                          1);

      // activate the factory in the root poa.
      factory_ = factory_servant_->_this (ACE_TRY_ENV);
      ACE_TRY_CHECK;

      // Give the ownership to the POA.
      factory_servant_->_remove_ref (ACE_TRY_ENV);

      ACE_TRY_CHECK;
      CORBA::String_var
        str = orb_->object_to_string (factory_.in (),
                                      ACE_TRY_ENV);
      ACE_TRY_CHECK;

      ACE_DEBUG ((LM_DEBUG,
                  "CosEvent_Service: The Cos Event Channel Factory IOR is <%s>\n",
                  str.in ()));

      CosNaming::Name name (1);
      name.length (1);
      name[0].id = CORBA::string_dup (Factory_Name);
      naming_client_->rebind (name,
                              factory_.in (),
                              ACE_TRY_ENV);
      ACE_TRY_CHECK;

      ACE_DEBUG ((LM_DEBUG,
                  "Registered with the naming service as %s\n", Factory_Name));

      orb_->run ();

      return 0;
    }
  ACE_CATCH (CORBA::UserException, ue)
    {
      // @@ Pradeep: there is a macro for this: ACE_PRINT_EXCEPTION.
      //    print_exception() is a TAO extension, so we shouldn't rely
      //    on it.
      ue.print_exception ("cosecfactory: ");
      return -1;
    }
  ACE_CATCH (CORBA::SystemException, se)
    {
      se.print_exception ("cosecfactory: ");
      return -1;
    }
  ACE_ENDTRY;
  ACE_CHECK_RETURN (-1);

  ACE_NOTREACHED (return 0;)
  // @@ Pradeep: was this intentional or is it just a misplaced
  //    semi-colon?
}