summaryrefslogtreecommitdiff
path: root/TAO/tao/poa.cpp
blob: 85ab195858011e668e59819953d599aee8c64d50 (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
// @(#) $Id$
//
// Copyright 1994-1995 by Sun Microsystems Inc.
// All Rights Reserved
//
// POA initialisation -- both anonymous and (for system bootstrapping)
// named POAs.
//
// XXX at this time, there's a strong linkage between this code and
// the modules knowing about IIOP.  In the future, a looser coupling
// between OA initialiszation and protocol components is desired.

#include "tao/corba.h"

// {A201E4C8-F258-11ce-9598-0000C07CA898}
DEFINE_GUID (IID_POA,
0xa201e4c8, 0xf258, 0x11ce, 0x95, 0x98, 0x0, 0x0, 0xc0, 0x7c, 0xa8, 0x98) ;

// CORBA_POA::init() is used in get_poa() and get_named_poa() in order
// to initialize the OA.  It was originally part of POA, and may no
// longer be useful.

CORBA::POA_ptr
CORBA_POA::init (CORBA::ORB_ptr parent,
                 ACE_INET_Addr &,
                 CORBA::Environment &env)
{
  env.clear ();
  TAO_ORB_Core *p = TAO_ORB_Core_instance ();

  if (p->root_poa ())
    {
      env.exception (new CORBA_INITIALIZE (CORBA::COMPLETED_NO));
      return 0;
    }

  CORBA::POA_ptr rp;
  ACE_NEW_RETURN (rp, CORBA_POA (parent, env), 0);
  p->root_poa (rp);

  return rp;
}

CORBA_POA::CORBA_POA (CORBA::ORB_ptr owning_orb,
                      CORBA::Environment &)
  : do_exit_ (CORBA::B_FALSE),
    orb_ (owning_orb),
    call_count_ (0),
    skeleton_ (0)
{
  TAO_ORB_Core* p = TAO_ORB_Core_instance ();
  TAO_Server_Strategy_Factory *f = p->server_factory ();

  this->objtable_ = f->create_object_table ();

  // @@ What is this doing here?  Why is it setting the root poa based
  // on whether objtable_ is non-zero?  (cjc)
  if (this->objtable_ != 0)
    p->root_poa (this);
}

CORBA_POA::~CORBA_POA (void)
{
}

// Create an objref

CORBA::Object_ptr
CORBA_POA::create (CORBA::OctetSeq &key,
                   CORBA::String type_id,
                   CORBA::Environment &env)
{
  CORBA::String id;
  IIOP_Object *data;

  if (type_id)
    id = CORBA::string_copy (type_id);
  else
    id = 0;



  data = new IIOP_Object (id,
                          IIOP::Profile (TAO_ORB_Core_instance ()->orb_params ()->addr (),
                                         key));
  if (data != 0)
    env.clear ();
  else
    {
      env.exception (new CORBA_NO_MEMORY (CORBA::COMPLETED_NO));
      return 0;
    }

  // Return the CORBA::Object_ptr interface to this objref.
  CORBA::Object_ptr new_obj;

  if (data->QueryInterface (IID_CORBA_Object,
			    (void **) &new_obj) != TAO_NOERROR)
    env.exception (new CORBA::INTERNAL (CORBA::COMPLETED_NO));

  data->Release ();
  return new_obj;
}

// Return the key fed into an object at creation time.

CORBA::OctetSeq *
CORBA_POA::get_key (CORBA::Object_ptr,
                    CORBA::Environment &env)
{
  // XXX implement me ! ... must have been created by this OA.
  env.exception (new CORBA_IMP_LIMIT (CORBA::COMPLETED_NO));
  return 0;
}

#if 0
// Used by method code to ask the OA to shut down.
void
CORBA_POA::please_shutdown (CORBA::Environment &env)
{
  ACE_MT (ACE_GUARD (ACE_SYNCH_MUTEX, poa_mon, lock_));

  env.clear ();
  do_exit_ = CORBA::B_TRUE;
}

// Used by non-method code to tell the OA to shut down.
void
CORBA_POA::clean_shutdown (CORBA::Environment &env)
{
  ACE_MT (ACE_GUARD (ACE_SYNCH_MUTEX, poa_mon, lock_));

  env.clear ();

  if (call_count_ != 0)
    {
      dmsg ("called clean_shutdown with requests outstanding");
      env.exception (new CORBA::BAD_INV_ORDER (CORBA::COMPLETED_NO));
      return;
    }

  // Here we need to tell all the endpoints to shut down...
}
#endif /* 0 */

// For POA -- POA operations for which we provide the vtable entry
void
CORBA_POA::register_dir (dsi_handler handler,
                         void *ctx,
                         CORBA::Environment &env)
{
  if (handler == 0)
    {
      env.exception (new CORBA::BAD_PARAM (CORBA::COMPLETED_NO));
      return;
    }

  skeleton_ = handler;
  context_ = ctx;

  env.clear ();
}

// A "Named POA" is used in bootstrapping some part of the ORB since
// it's name-to-address binding is managed by the OS.  Examples of
// such bindings are /etc/services (for TCP) and /etc/rpc (for ONC
// RPC) .  The name of a POA is only guaranteed to be unique within
// the domain of a single system, as a rule; two hosts would have
// distinct "king" POAs.
//
// For network endpoints, most such names are manually administered.
// Some other namespaces (AF_UNIX filesystem names for example) have a
// more formal underlying name service that can be dynamically updated
// while not compromising system security.
//
// The address family used by the POA is found from the ORB passed in.
//
// XXX the coupling could stand to be looser here, so this module did
// not know specifically about the Internet ORB !!

CORBA::POA_ptr
CORBA_POA::get_named_poa (CORBA::ORB_ptr orb,
                          CORBA::String name,
                          CORBA::Environment &env)
{
  env.clear ();

  // If the ORB is an Internet ORB, we know this must be a TCP OA.
  {
    IIOP_ORB *internet;

    if (orb->QueryInterface (IID_IIOP_ORB, (void **) &internet) == TAO_NOERROR)
      {
        CORBA::POA_ptr tcp_oa;

        internet->Release ();

        // POA initialization with name specified; it'll come from
        // /etc/services if it's not a port number.

        ACE_INET_Addr poa_name (name, (ACE_UINT32) INADDR_ANY);

        tcp_oa = CORBA::POA::init (orb, poa_name, env);

        if (env.exception () != 0)
          return 0;
        else
          return tcp_oa;		// derives from POA
      }
  }

  // We don't know how to deal with this kind of ORB.  Report error.

  env.exception (new CORBA::BAD_PARAM (CORBA::COMPLETED_NO) );
  return 0;
}

// An "Anonymous" POA is used more routinely.  The name used doesn't
// matter to anyone; it is only used to create object references with
// a short lifespan, namely that of the process acquiring this POA.

CORBA::POA_ptr
CORBA_POA::get_poa (CORBA::ORB_ptr orb,
		    CORBA::Environment &env)
{
  env.clear ();

  // If the ORB is an Internet ORB, we know this must be a TCP OA.
  {
    IIOP_ORB	*internet;

    if (orb->QueryInterface (IID_IIOP_ORB, (void **) &internet) == TAO_NOERROR)
      {
        CORBA::POA_ptr tcp_oa;

        internet->Release ();

        // POA initialization with null name means anonymous OA

        ACE_INET_Addr anonymous ((u_short) 0, (ACE_UINT32) INADDR_ANY);

        tcp_oa = CORBA::POA::init (orb, anonymous, env);

        if (env.exception () != 0)
          return 0;
        else
          return tcp_oa;		// derives from POA
      }
  }

  // We don't know how to deal with this kind of ORB.  Report error.

  env.exception (new CORBA::BAD_PARAM (CORBA::COMPLETED_NO) );
  return 0;
}

void CORBA_POA::dispatch (CORBA::OctetSeq &key,
                          CORBA::ServerRequest &req,
                          void *context,
                          CORBA::Environment &env)
{
  ACE_UNUSED_ARG(context);

  TAO_Skeleton skel;  // pointer to function pointer for the operation
  CORBA::Object_ptr obj;  // object that will be looked up based on the key
  CORBA::String  opname;

  // Get the skeleton

  // Find the object based on the key
  if (this->find (key, obj) != -1)
    {
#if 0 // XXXASG - testing a new way of handling requests to overcome the
      // casting problem arising out of virtual inheritance
      opname = req.op_name ();

      // Find the skeleton "glue" function based on the operation name
      if (obj->find (opname, skel) != -1)
        // Schedule the upcall.  This is the degenerate case of scheduling...
        // using a "do it now!" scheduler
        skel (req, obj, env);
      else
        {
          // Something really bad happened: the operation was not
          // found in the object, fortunately there is a standard
          // exception for that purpose.
          env.exception (new CORBA_BAD_OPERATION (CORBA::COMPLETED_NO));
          ACE_ERROR ((LM_ERROR,
                      "Cannot find operation <%s> in object\n",
                      opname));
        }
#else
      ACE_UNUSED_ARG (opname);
      ACE_UNUSED_ARG (skel);
#endif
      obj->dispatch (req, context, env);
    }
  else
    {
      env.exception (new CORBA::OBJECT_NOT_EXIST (CORBA::COMPLETED_NO));
      ACE_ERROR ((LM_ERROR, "Cannot find object\n"));

    }

  // @@ XXXASG -
  // We need to pass this skel and associated information to the
  // scheduler. How do we do it??
}

int
CORBA_POA::find (const CORBA::OctetSeq &key,
		 CORBA::Object_ptr &obj)
{
  return objtable_->find (key, obj);
}

int
CORBA_POA::bind (const CORBA::OctetSeq &key,
		 CORBA::Object_ptr obj)
{
  return objtable_->bind (key, obj);
}

void
CORBA_POA::handle_request (TAO_GIOP_RequestHeader hdr,
                           CDR &request_body,
                           CDR &response,
                           TAO_Dispatch_Context *some_info,
                           CORBA::Environment &env)
{
  ACE_UNUSED_ARG (some_info);

  IIOP_ServerRequest svr_req (&request_body, this->orb (), this);

  // Why are we copying this when we can just pass in a handle to the
  // hdr?
  svr_req.opname_ = hdr.operation;

  this->dispatch (hdr.object_key,
                  svr_req,
                  0, // this is IIOP residue
                  env);

  //  svr_req.release ();

  // If no reply is necessary (i.e., oneway), then return!
  if (hdr.response_expected == 0)
    {
      svr_req.release ();
      return;
    }

  // Otherwise check for correct parameter handling, and reply as
  // appropriate.
  //
  // NOTE: if "env" is set, it takes precedence over exceptions
  // reported using the mechanism of the ServerRequest.  Only system
  // exceptions are reported that way ...
  //
  // XXX Exception reporting is ambiguous; it can be cleaner than
  // this.  With both language-mapped and dynamic/explicit reporting
  // mechanisms, one of must be tested "first" ... so an exception
  // reported using the other mechanism could be "lost".  Perhaps only
  // the language mapped one should be used for system exceptions.

  TAO_GIOP::start_message (TAO_GIOP_Reply, response);
  TAO_GIOP_ServiceContextList resp_ctx;
  resp_ctx.length = 0;
  response.encode (&TC_ServiceContextList, &resp_ctx, 0, env);
  response.put_ulong (hdr.request_id);

  CORBA::TypeCode_ptr tc;
  const void *value;

  if (!svr_req.params_ && env.exception () == 0)
    {
      dmsg ("DSI user error, didn't supply params");
      env.exception (new CORBA::BAD_INV_ORDER (CORBA::COMPLETED_NO));
    }

  // Standard exceptions only.
  if (env.exception () != 0)
    {
      CORBA::Environment env2;
      CORBA::Exception *x = env.exception ();
      CORBA::TypeCode_ptr except_tc = x->type ();

      response.put_ulong (TAO_GIOP_SYSTEM_EXCEPTION);
      (void) response.encode (except_tc, x, 0, env2);
    }

  // Any exception at all.
  else if (svr_req.exception_)
    {
      CORBA::Exception *x;
      CORBA::TypeCode_ptr except_tc;

      x = (CORBA::Exception *) svr_req.exception_->value ();
      except_tc = svr_req.exception_->type ();

      // Finish the GIOP Reply header, then marshal the exception.
      //
      // XXX x->type () someday ...
      if (svr_req.ex_type_ == CORBA::SYSTEM_EXCEPTION)
        response.put_ulong (TAO_GIOP_SYSTEM_EXCEPTION);
      else
        response.put_ulong (TAO_GIOP_USER_EXCEPTION);

      (void) response.encode (except_tc, x, 0, env);
    }

  // Normal reply.
  else
    {
      // First finish the GIOP header ...
      response.put_ulong (TAO_GIOP_NO_EXCEPTION);

      // ... then send any return value ...
      if (svr_req.retval_)
        {
          tc = svr_req.retval_->type ();
          value = svr_req.retval_->value ();
          (void) response.encode (tc, value, 0, env);
        }

      // ... Followed by "inout" and "out" parameters, left to right
      for (u_int i = 0;
           i < svr_req.params_->count ();
           i++)
        {
          CORBA::NamedValue_ptr	nv = svr_req.params_->item (i, env);
          CORBA::Any_ptr any;

          if (!(nv->flags () & (CORBA::ARG_INOUT|CORBA::ARG_OUT)))
            continue;

          any = nv->value ();
          tc = any->type ();
          value = any->value ();
          (void) response.encode (tc, value, 0, env);
        }
    }
  svr_req.release ();
}

// IUnknown calls
ULONG
CORBA_POA::AddRef (void)
{
  ACE_MT (ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, poa_mon, com_lock_, 0));
  return ++refcount_;
}

ULONG
CORBA_POA::Release (void)
{
  {
    ACE_MT (ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, poa_mon, com_lock_, 0));

    if (--refcount_ != 0)
      return refcount_;
  }

  delete this;
  return 0;
}

TAO_HRESULT
CORBA_POA::QueryInterface (REFIID riid,
                           void **ppv)
{
  *ppv = 0;

  if (IID_POA == riid
      || IID_TAO_IUnknown == riid)
    *ppv = this;

  if (*ppv == 0)
    return ResultFromScode (TAO_E_NOINTERFACE);

 (void) AddRef ();
  return TAO_NOERROR;
}