summaryrefslogtreecommitdiff
path: root/TAO/tao/IIOP_Profile.cpp
blob: d3a9ec97b063f12cbecebc0591e628dd827be7f0 (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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
// This may look like C, but it's really -*- C++ -*-
// $Id$

// ============================================================================
//
// = LIBRARY
//
// = FILENAME
//
// = DESCRIPTION
//
// = AUTHOR
//
// ============================================================================

#include "tao/IIOP_Profile.h"
#include "tao/GIOP.h"
#include "tao/CDR.h"
#include "tao/Environment.h"
#include "tao/ORB.h"
#include "tao/MProfile.h"
#include "tao/ORB_Core.h"

ACE_RCSID(tao, IIOP_Profile, "$Id$")

static const char *prefix_ = "iiop:";

TAO_IIOP_Profile::TAO_IIOP_Profile (const ACE_INET_Addr& addr,
                                    const char *object_key)
  : host_ (0),
    port_ (0),
    tag_ (TAO_IOP_TAG_INTERNET_IOP),
    body_ (),
    version_ (DEF_IIOP_MAJOR, DEF_IIOP_MINOR),
    object_key_ (),
    object_addr_ (addr),
    hint_ (0),
    // what about refcount_lock_ (),
    refcount_ (1),
    forward_to_ (0)
{
  this->set(addr);
  int l = ACE_OS::strlen (object_key);
  this->object_key_.length (l);

  for (int i = 0; i < l; ++i)
    this->object_key_[i] = object_key[i];

  this->create_body ();
}

TAO_IIOP_Profile::TAO_IIOP_Profile (const ACE_INET_Addr& addr,
                                    const TAO_ObjectKey& object_key)
  : host_ (0),
    port_ (0),
    tag_ (TAO_IOP_TAG_INTERNET_IOP),
    body_ (),
    version_ (DEF_IIOP_MAJOR, DEF_IIOP_MINOR),
    object_key_ (object_key),
    object_addr_ (addr),
    hint_ (0),
    // what about refcount_lock_ (),
    refcount_ (1),
    forward_to_ (0)
{
  this->set(addr);
  this->create_body ();
}

TAO_IIOP_Profile::TAO_IIOP_Profile (const ACE_INET_Addr& addr,
                                    const Version& version,
                                    const char *object_key)
  : host_ (0),
    port_ (0),
    tag_ (TAO_IOP_TAG_INTERNET_IOP),
    body_ (),
    version_ (version),
    object_key_ (),
    object_addr_ (addr),
    hint_ (0),
    // what about refcount_lock_ (),
    refcount_ (1),
    forward_to_ (0)
{
  this->set(addr);
  int l = ACE_OS::strlen (object_key);
  this->object_key_.length (l);

  for (int i = 0; i < l; ++i)
    this->object_key_[i] = object_key[i];

  this->create_body ();
}

TAO_IIOP_Profile::TAO_IIOP_Profile (const ACE_INET_Addr& addr,
                                    const Version& version,
                                    const TAO_ObjectKey& object_key)
  : host_ (0),
    port_ (0),
    tag_ (TAO_IOP_TAG_INTERNET_IOP),
    body_ (),
    version_ (version),
    object_key_ (object_key),
    object_addr_ (addr),
    hint_ (0),
    // what about refcount_lock_ (),
    refcount_ (1),
    forward_to_ (0)
{
  this->set(addr);
  this->create_body ();
}

TAO_IIOP_Profile::TAO_IIOP_Profile (const char* host,
                                    CORBA::UShort port,
                                    const TAO_ObjectKey& object_key)
  : host_ (0),
    port_ (port),
    tag_ (TAO_IOP_TAG_INTERNET_IOP),
    body_ (),
    version_ (DEF_IIOP_MAJOR, DEF_IIOP_MINOR),
    object_key_ (object_key),
    object_addr_ (port, host),
    hint_ (0),
    // what about refcount_lock_ (),
    refcount_ (1),
    forward_to_ (0)
{

  if (host)
  {
    ACE_NEW (this->host_,
             char[ACE_OS::strlen (host) + 1]);
    ACE_OS::strcpy (this->host_, host);
  }

  this->create_body ();
}

TAO_IIOP_Profile::TAO_IIOP_Profile (const char* host,
                                    CORBA::UShort port,
                                    const TAO_ObjectKey& object_key,
				    const ACE_INET_Addr& addr)
  : host_ (0),
    port_ (port),
    tag_ (TAO_IOP_TAG_INTERNET_IOP),
    body_ (),
    version_ (DEF_IIOP_MAJOR, DEF_IIOP_MINOR),
    object_key_ (object_key),
    object_addr_ (addr),
    hint_ (0),
    // what about refcount_lock_ (),
    refcount_ (1),
    forward_to_ (0)
{

  if (host)
  {
    ACE_NEW (this->host_,
             char[ACE_OS::strlen (host) + 1]);
    ACE_OS::strcpy (this->host_, host);
  }

  this->create_body ();
}

TAO_IIOP_Profile::TAO_IIOP_Profile (const char* host,
                                    CORBA::UShort port,
                                    const Version& version,
                                    const TAO_ObjectKey& object_key)
  : host_ (0),
    port_ (port),
    tag_ (TAO_IOP_TAG_INTERNET_IOP),
    body_ (),
    version_ (DEF_IIOP_MAJOR, DEF_IIOP_MINOR),
    object_key_ (object_key),
    object_addr_ (port, host),
    hint_ (0),
    // what about refcount_lock_ (),
    refcount_ (1),
    forward_to_ (0)
{
  ACE_UNUSED_ARG (version);

  ACE_NEW (this->host_,
           char[ACE_OS::strlen (host) + 1]);
  ACE_OS::strcpy (this->host_, host);

  this->create_body ();
}

TAO_IIOP_Profile::TAO_IIOP_Profile (const TAO_IIOP_Profile *pfile)
  : host_(0),
    port_(pfile->port_),
    tag_(pfile->tag_),
    body_(pfile->body_),
    version_(pfile->version_),
    object_key_(pfile->object_key_),
    object_addr_(pfile->object_addr_),
    hint_(0),
    // what about refcount_lock_ (),
    refcount_ (1),
    forward_to_ (0)
{

  ACE_NEW (this->host_,
           char[ACE_OS::strlen (pfile->host_) + 1]);
  ACE_OS::strcpy (this->host_, pfile->host_);
  hint_ = pfile->hint_;

}

TAO_IIOP_Profile::TAO_IIOP_Profile (const TAO_IIOP_Profile &pfile)
  : host_(0),
    port_(pfile.port_),
    tag_(pfile.tag_),
    body_(pfile.body_),
    version_(pfile.version_),
    object_key_(pfile.object_key_),
    object_addr_(pfile.object_addr_),
    hint_(0),
    // what about refcount_lock_ (),
    refcount_ (1),
    forward_to_ (0)
{

  ACE_NEW (this->host_,
           char[ACE_OS::strlen (pfile.host_) + 1]);
  ACE_OS::strcpy (this->host_, pfile.host_);
  hint_ = pfile.hint_;

}

TAO_IIOP_Profile::TAO_IIOP_Profile (const Version &version)
  : host_ (0),
    port_ (0),
    tag_ (TAO_IOP_TAG_INTERNET_IOP),
    body_ (),
    version_ (version),
    object_key_ (),
    object_addr_ (),
    hint_ (0),
    // what about refcount_lock_ (),
    refcount_ (1),
    forward_to_ (0)
{
}

TAO_IIOP_Profile::TAO_IIOP_Profile (const char *string, CORBA::Environment &env)
  : host_ (0),
    port_ (0),
    tag_ (TAO_IOP_TAG_INTERNET_IOP),
    body_ (),
    version_ (DEF_IIOP_MAJOR, DEF_IIOP_MINOR),
    object_key_ (),
    object_addr_ (),
    hint_ (0),
    // what about refcount_lock_ (),
    refcount_ (1),
    forward_to_ (0)
{
  parse_string (string, env);
}

TAO_IIOP_Profile::TAO_IIOP_Profile (void)
  : host_ (0),
    port_ (0),
    tag_ (TAO_IOP_TAG_INTERNET_IOP),
    body_ (),
    version_ (DEF_IIOP_MAJOR, DEF_IIOP_MINOR),
    object_key_ (),
    object_addr_ (),
    hint_ (0),
    // what about refcount_lock_ (),
    refcount_ (1),
    forward_to_ (0)
{
}

int
TAO_IIOP_Profile::set (const ACE_INET_Addr& addr)
{
  char temphost[MAXHOSTNAMELEN + 1];
  const char *temphost2 = 0;

  this->port_ = addr.get_port_number();

  if (TAO_ORB_Core_instance ()->orb_params ()->use_dotted_decimal_addresses ())
  {
    temphost2 = addr.get_host_addr ();
    if (temphost2 == 0)
      return -1;
  }
  else
  {
    if (addr.get_host_name (temphost, sizeof temphost) != 0)
      return -1;

    temphost2 = temphost;
  }

  ACE_NEW_RETURN (this->host_,
                  char[ACE_OS::strlen (temphost2) + 1],
                  -1);
  ACE_OS::strcpy (this->host_, temphost2);

  return 0;

}

TAO_IIOP_Profile:: ~TAO_IIOP_Profile ()
{
  assert (this->refcount_ == 0);

  delete [] this->host_;
  this->host_ = 0;

  if (forward_to_)
  {
    delete forward_to_;
  }

}

CORBA::ULong
TAO_IIOP_Profile::tag (void)
{
  return this->tag_;
}

TAO_Transport *
TAO_IIOP_Profile::transport (void)
{
  // do I need to do a dynamic cast here?
  if (hint_) {
    return hint_->transport ();
  }
  return 0;
}

const TAO_ObjectKey &
TAO_IIOP_Profile::object_key (void) const
{
  return this->object_key_;
}

TAO_ObjectKey &
TAO_IIOP_Profile::object_key (TAO_ObjectKey& objkey)
{
  this->object_key_ = objkey;
  return this->object_key_;
}

TAO_ObjectKey *
TAO_IIOP_Profile::_key (CORBA::Environment &)
{
  return new TAO_ObjectKey (this->object_key_);
}

// return codes:
// -1 -> error
//  0 -> can't understand this version
//  1 -> success.
int
TAO_IIOP_Profile::parse (TAO_InputCDR& cdr,
                         CORBA::Boolean &continue_decoding,
                         CORBA::Environment &env)
{
  CORBA::ULong encap_len = cdr.length ();

  // Read and verify major, minor versions, ignoring IIOP
  // profiles whose versions we don't understand.
  //
  // XXX this doesn't actually go back and skip the whole
  // encapsulation...
  if (!(cdr.read_octet (this->version_.major)
      && this->version_.major == TAO_IIOP_Profile::DEF_IIOP_MAJOR
      && cdr.read_octet (this->version_.minor)
      && this->version_.minor <= TAO_IIOP_Profile::DEF_IIOP_MINOR))
  {
    ACE_DEBUG ((LM_DEBUG, "detected new v%d.%d IIOP profile",
                          this->version_.major,
                          this->version_.minor));
    return 0;
  }

  if (this->host_) {
    delete [] this->host_;
    this->host_ = 0;
  }

  // Get host and port
  if (cdr.decode (CORBA::_tc_string,
                  &this->host_,
                  0,
                  env) != CORBA::TypeCode::TRAVERSE_CONTINUE
      || !cdr.read_ushort (this->port_))
  {
    env.exception (new CORBA::MARSHAL (CORBA::COMPLETED_MAYBE));
    ACE_DEBUG ((LM_DEBUG, "error decoding IIOP host/port"));
    return -1;
  }

  this->object_addr_.set (this->port_, this->host_);

  // ... and object key.

  // @@ This is a hack. This code was moved from encode.cpp
  //    but it is not clear to me what is going on.  So I have
  //    passed a reference to continue_decoding into this method
  //    continue_decoding is used in STUB_Object::decode ()
  continue_decoding = cdr.decode (TC_opaque,
                                  &this->object_key_,
                                  0,
                                  env) == CORBA::TypeCode::TRAVERSE_CONTINUE;

  if (cdr.length () != 0)
  {
    env.exception (new CORBA::MARSHAL (CORBA::COMPLETED_MAYBE));
    ACE_DEBUG ((LM_DEBUG,
                "%d bytes out of %d left after IIOP profile data\n",
                cdr.length (), encap_len));
    return -1;
  }
  return 1;
}

int
TAO_IIOP_Profile::parse_string (const char *string,
                                CORBA::Environment &env)
{

  if (!string || !*string)
    return 0;

  // Remove the "N.N//" prefix, and verify the version's one
  // that we accept

  if (isdigit (string [0])
      && isdigit (string [2])
      && string [1] == '.'
      && string [3] == '/'
      && string [4] == '/')
    {
      // @@ This may fail for non-ascii character sets [but take that
      // with a grain of salt]
      this->version_.set_version ((char) (string [0] - '0'),
                                  (char) (string [2] - '0'));
      string += 5;
    }
  else
    {
      // @@ AFAIK the right kind of exception to raise here is
      // CORBA_MARSHAL: CORBA_DATA_CONVERSION is reserved for failure
      // to translate *values* (such as character set mismatches,
      // fixed<> types, floats, etc.)
      env.exception (new CORBA_DATA_CONVERSION (CORBA::COMPLETED_NO));
    }

  if (this->version_.major != TAO_IIOP_Profile::DEF_IIOP_MAJOR
   || this->version_.minor  > TAO_IIOP_Profile::DEF_IIOP_MINOR)
    {
      // @@ Same thing here....
      env.exception (new CORBA_DATA_CONVERSION (CORBA::COMPLETED_NO));
      return -1;
    }

  // Pull off the "hostname:port/" part of the objref
  // Copy the string because we are going to modify it...
  CORBA::String_var copy = CORBA::string_dup (string);

  char *start = copy.inout ();
  char *cp = ACE_OS::strchr (start, ':');

  if (cp == 0)
  {
    env.exception (new CORBA_DATA_CONVERSION (CORBA::COMPLETED_NO));
    return -1;
  }

  if (this->host_)
  {
    // @@ You are setting this->host_ using CORBA::string_alloc() a
    // couple of lines below, you should then use CORBA::string_free()
    // to release it!  In general use a single form of memory
    // allocation for a field/variable to avoid new/free() and
    // malloc/delete() mismatches.
    // Ohh, and if you are going to use CORBA::string_alloc() &
    // friends you may consider using CORBA::String_var to manage
    // the memory automatically (though there may be forces that
    // suggest otherwise).
    delete [] this->host_;
    this->host_ = 0;
  }

  this->host_ = CORBA::string_alloc (1 + cp - start);
  for (cp = this->host_; *start != ':'; *cp++ = *start++)
    continue;

  *cp = 0; start++; // increment past :

  cp = ACE_OS::strchr (start, '/');

  if (cp == 0)
  {
    env.exception (new CORBA_DATA_CONVERSION (CORBA::COMPLETED_NO));
    CORBA::string_free (this->host_);
    return -1;
  }

  this->port_ = (CORBA::UShort) ACE_OS::atoi (start);

  this->object_addr_.set (this->port_, this->host_);

  start = ++cp;  // increment past the /

  TAO_POA::decode_string_to_sequence (this->object_key_, start);

  return 1;
}

//  TAO_InputCDR cdr (body.get_buffer (), body.length (), body[0]);
//
// @@
//  CORBA::Boolean byte_order;
//  cdr >> CORBA::Any::to_boolean (byte_order);
//  cdr >> this->version_.major;
//  cdr >> this->version_.minor;
//  cdr >> this->host_ ();
//  cdr >> this->port_;
//  cdr >> this->object_key_;
//  this->object_addr_.set(this->port_, this->host_);
// }

const TAO_opaque&
TAO_IIOP_Profile::body (void) const
{
  return this->body_;
}

void
TAO_IIOP_Profile::create_body (void)
{
  TAO_OutputCDR cdr;
}
// @@
//   cdr << TAO_ENCAP_BYTE_ORDER;
//   cdr << this->version_.major;
//   cdr << this->version_.minor;
//   cdr << this->host_;
//   cdr << this->port_;
//   cdr << this->object_key_;
//
//   // Create a copy by making a temporary TAO_opaque object.
//   // @@ TODO the CDR stream may contain several fragments, need to
//   // merge them...
//   this->body_ = TAO_opaque (cdr.length (), // length
//                             cdr.length (), // maximum
//                             cdr.buffer (), // buffer
//                             0);            // not own
// }

CORBA::Boolean
TAO_IIOP_Profile::is_equivalent (TAO_Profile *other_profile,
                                 CORBA::Environment &env)
{
  env.clear ();

  if (other_profile->tag () != TAO_IOP_TAG_INTERNET_IOP)
    return 0;

  TAO_IIOP_Profile *op =
                   ACE_dynamic_cast (TAO_IIOP_Profile *, other_profile);

  ACE_ASSERT (op->object_key_.length () < UINT_MAX);

  return (  (this->port_       == op->port_)
         && (this->object_key_ == op->object_key_)
         && (ACE_OS::strcmp (this->host_, op->host_) == 0)
         && (this->version_    == op->version_));
}

CORBA::ULong
TAO_IIOP_Profile::hash (CORBA::ULong max,
                        CORBA::Environment &env)
{
  CORBA::ULong hashval;

  env.clear ();

  // Just grab a bunch of convenient bytes and hash them; could do
  // more (hostname, full key, exponential hashing) but no real need
  // to do so except if performance requires a more costly hash.

  hashval = this->object_key_.length () * this->port_;
  hashval += this->version_.minor;

  if (this->object_key_.length () >= 4)
    {
      hashval += this->object_key_ [1];
      hashval += this->object_key_ [3];
    }

  return hashval % max;
}

ACE_Addr&
TAO_IIOP_Profile::object_addr (const ACE_Addr *addr)
{
  const ACE_INET_Addr *inet_addr =
    ACE_dynamic_cast (const ACE_INET_Addr *, addr);

  if (inet_addr != 0)
    this->object_addr_ = *inet_addr;
  else if (this->host_)
    this->object_addr_.set (this->port_, this->host_);
  return this->object_addr_;
}

ACE_Addr&
TAO_IIOP_Profile::object_addr (void)
{
  return this->object_addr_;
}

char *
TAO_IIOP_Profile::addr_to_string(void)
{
  static char s[MAXHOSTNAMELEN + MAXHOSTNAMELEN];
  ACE_OS::sprintf (s, "%s:%d",
                   this->host_, port_);
  return s;
}

const char *
TAO_IIOP_Profile::host (void)
{
  return this->host_;
}

const char *
TAO_IIOP_Profile::host (const char *h)
{
  if (this->host_)
  {
    delete [] this->host_;
    this->host_ = 0;
  }

  if (h)
  {
    ACE_NEW_RETURN (this->host_,
                    char[ACE_OS::strlen (h) + 1],
                    0);
    ACE_OS::strcpy (this->host_, h);
  }

  return this->host_;
}

CORBA::UShort
TAO_IIOP_Profile::port (void)
{
  return this->port_;
}

CORBA::UShort
TAO_IIOP_Profile::port (CORBA::UShort p)
{
  return this->port_ = p;
}

const Version *
TAO_IIOP_Profile::version (void)
{
  return &this->version_;
}

const Version *
TAO_IIOP_Profile::version (Version *v)
{
  this->version_ = *v;
  return &this->version_;
}

void
TAO_IIOP_Profile::reset_hint (void)
{
  if (this->hint_)
  {
    this->hint_->cleanup_hint ();
    this->hint_ = 0;
  }
}

TAO_Client_Connection_Handler *&
TAO_IIOP_Profile::hint(void)
{
  return this->hint_;
}

TAO_Profile *
TAO_IIOP_Profile::_nil (void)
{
  return (TAO_IIOP_Profile *)0;
}

TAO_IIOP_Profile &
TAO_IIOP_Profile::operator= (const TAO_IIOP_Profile &src)
{
  this->version_ = src.version_;

  this->object_key_ = src.object_key_;

  this->object_addr_.set (src.object_addr_);

  this->port_ = src.port_;

  if (this->host_)
  {
    delete [] this->host_;
    this->host_ = 0;
  }

  if (src.host_)
  {
    ACE_NEW_RETURN (this->host_,
                    char[ACE_OS::strlen (src.host_) + 1],
                    *this);
    ACE_OS::strcpy (this->host_, src.host_);
  }

  return *this;
}

// Memory managment

CORBA::ULong
TAO_IIOP_Profile::_incr_refcnt (void)
{
  // OK, think I got it.  When this object is created (guard) the
  // lock is automatically acquired (refcount_lock_).  Then when
  // we leave this method the destructir for guard is called which
  // releases the lock!
  ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, guard, this->refcount_lock_, 0);

  return this->refcount_++;
}

CORBA::ULong
TAO_IIOP_Profile::_decr_refcnt (void)
{
  {
    ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, mon, this->refcount_lock_, 0);
    this->refcount_--;
    if (this->refcount_ != 0)
      return this->refcount_;
  }

  // refcount is 0, so delete us!
  // delete will call our ~ destructor which in turn deletes stuff.
  delete this;
  return 0;
}


void
TAO_IIOP_Profile::forward_to (TAO_MProfile *mprofiles)
{
  // we assume ownership of the profile list!!
  if (forward_to_)
    delete this->forward_to_;

  this->forward_to_ = new TAO_MProfile (mprofiles);

}

TAO_MProfile *
TAO_IIOP_Profile::forward_to (void)
{
  return this->forward_to_;
}

TAO_MProfile *
TAO_IIOP_Profile::get_forward_to (void)
{
  return new TAO_MProfile (this->forward_to_);
}

CORBA::String
TAO_IIOP_Profile::to_string (CORBA::Environment &env)
{
  ACE_UNUSED_ARG (env);

  CORBA::String_var key;
  TAO_POA::encode_sequence_to_string (key.inout(),
                                      this->object_key ());

  u_int buflen = (ACE_OS::strlen (::prefix_) +
                  1 /* major # */ + 1 /* minor # */ +
                  2 /* double-slash separator */ +
                  ACE_OS::strlen (this->host_) +
                  1 /* colon separator */ +
                  5 /* port number */ +
                  1 /* slash separator */ +
                  ACE_OS::strlen (key) +
                  1 /* zero terminator */);

  CORBA::String buf = CORBA::string_alloc (buflen);

  static const char digits [] = "0123456789";

  ACE_OS::sprintf (buf,
                   "%s%c.%c//%s:%d/%s",
                   ::prefix_,
                   digits [this->version_.major],
                   digits [this->version_.minor],
                   this->host_,
                   this->port_,
                   key.in ());
  return buf;
}

const char *
TAO_IIOP_Profile::prefix ()
{
  return ::prefix_;
}

CORBA::TypeCode::traverse_status
TAO_IIOP_Profile::encode (TAO_OutputCDR *&stream,
                          CORBA::Environment &env)
{
  // UNSIGNED LONG, tag for this protocol profile;
  stream->write_ulong (TAO_IOP_TAG_INTERNET_IOP);

  // UNSIGNED LONG, number of succeeding bytes in the
  // encapsulation.  We don't actually need to make the
  // encapsulation, as nothing needs stronger alignment than
  // this longword; it guarantees the rest is aligned for us.
  u_int hostlen;

  hostlen = ACE_OS::strlen ((char *) this->host_);
  CORBA::ULong encap_len =
    1                              // byte order
    + 1                            // version major
    + 1                            // version minor
    + 1                            // pad byte
    + 4                            // sizeof (strlen)
    + hostlen + 1                  // strlen + null
    + (~hostlen & 01)              // optional pad byte
    + 2                            // port
    + ( hostlen & 02)              // optional pad short
    + 4                            // sizeof (key length)
    + this->object_key_.length (); // key length.
  stream->write_ulong (encap_len);

#if 0
  size_t current_len = stream->length ();
#endif

  // CHAR describing byte order, starting the encapsulation
  stream->write_octet (TAO_ENCAP_BYTE_ORDER);

  // IIOP::Version, two characters (version 1.0) padding
  stream->write_char (this->version_.major);
  stream->write_char (this->version_.minor);

  // STRING hostname from profile
  stream->encode (CORBA::_tc_string, &this->host_, 0, env);

  // UNSIGNED SHORT port number
  stream->write_ushort (this->port_);

  // OCTET SEQUENCE for object key
  stream->encode (TC_opaque, &this->object_key_, 0, env);

#if 0
      // This is good for debugging the computation of the key
  // length.
  size_t final_len = stream->length ();
  ACE_DEBUG ((LM_DEBUG, "ObjRef::encode: "
              "stored_len = %d, "
              "real_len = %d\n",
              encap_len,
              final_len - current_len));
#endif /* 0 */

  return CORBA::TypeCode::TRAVERSE_CONTINUE;
}