summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/orbsvcs/PortableGroup/PortableGroup_Acceptor_Registry.cpp
blob: 6d8ceba2c3b0bf9253ed2e369b4211af94c3fb42 (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
// This may look like C, but it's really -*- C++ -*-
//
// $Id$

#include "PortableGroup_Acceptor_Registry.h"
#include "tao/orb_core.h"
#include "tao/Profile.h"
#include "tao/GIOP_Message_State.h"
#include "tao/debug.h"
#include "tao/Endpoint.h"
#include "tao/Thread_Lane_Resources.h"
#include "tao/Leader_Follower.h"

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

TAO_PortableGroup_Acceptor_Registry::TAO_PortableGroup_Acceptor_Registry (void)
{
}

TAO_PortableGroup_Acceptor_Registry::~TAO_PortableGroup_Acceptor_Registry (void)
{
  // Free the memory for the endpoints.
  Entry *entry;
  Acceptor_Registry_Iterator iter (this->registry_);

  while (iter.next (entry))
    {
      delete entry->endpoint;
      delete entry->acceptor;
      iter.advance ();
    }
}


void
TAO_PortableGroup_Acceptor_Registry::open (const TAO_Profile* profile,
                                           TAO_ORB_Core &orb_core
                                           TAO_ENV_ARG_DECL)
{
  Entry *entry;

  if (this->find (profile, entry) == 1)
    {
      // Found it.  Increment the reference count.
      ++entry->cnt;
    }
  else
    {
      // Not found.  Open a new acceptor.

      // Now get the list of available protocol factories.
      TAO_ProtocolFactorySetItor end =
        orb_core.protocol_factories ()->end ();

      int found = 0;
      // If usable protocol (factory) is found then this will be
      // set equal to 1.

      for (TAO_ProtocolFactorySetItor factory =
             orb_core.protocol_factories ()->begin ();
           factory != end;
           ++factory)
        {
          if ((*factory)->factory ()->tag () == profile->tag ())
            {
              this->open_i (profile,
                            orb_core,
                            factory
                            TAO_ENV_ARG_PARAMETER);
              ACE_CHECK;

              found = 1;  // A usable protocol was found.
            }
          else
            continue;
        }
    }
}

#define MAX_ADDR_LENGTH   (32)

void
TAO_PortableGroup_Acceptor_Registry::open_i (const TAO_Profile* profile,
                                             TAO_ORB_Core &orb_core,
                                             TAO_ProtocolFactorySetItor &factory
                                             TAO_ENV_ARG_DECL)
{
  TAO_Acceptor *acceptor = (*factory)->factory ()->make_acceptor ();

  if (acceptor != 0)
    {
      // Extract the desired endpoint/protocol version if one
      // exists.
      const TAO_GIOP_Message_Version &version = profile->version ();
      char buffer [MAX_ADDR_LENGTH];

      // Removed the constness of profile.  We're not changing
      // anything, but need to call a nonconst function.
      TAO_Profile* nc_profile = ACE_const_cast (TAO_Profile *, profile);
      nc_profile->endpoint ()->addr_to_string (buffer, MAX_ADDR_LENGTH);

      if (acceptor->open (&orb_core,
                          orb_core.lane_resources ().leader_follower ().reactor(),
                          version.major,
                          version.minor,
                          buffer,
                          0) == -1)
        {
          delete acceptor;

          if (TAO_debug_level > 0)
            ACE_ERROR ((LM_ERROR,
                        ACE_TEXT ("TAO (%P|%t) ")
                        ACE_TEXT ("unable to open acceptor ")
                        ACE_TEXT ("for <%s>%p\n"),
                        buffer,
                        ""));

          ACE_THROW (CORBA::BAD_PARAM (
              CORBA_SystemException::_tao_minor_code (
                TAO_ACCEPTOR_REGISTRY_OPEN_LOCATION_CODE,
                EINVAL),
              CORBA::COMPLETED_NO));
        }

      // Add acceptor to list.
      Entry tmp_entry;
      tmp_entry.acceptor = acceptor;
      tmp_entry.endpoint = nc_profile->endpoint ()->duplicate ();
      tmp_entry.cnt = 1;

      if (this->registry_.enqueue_tail (tmp_entry) == -1)
        {
          delete acceptor;

          if (TAO_debug_level > 0)
            ACE_ERROR ((LM_ERROR,
                        ACE_TEXT ("TAO (%P|%t) ")
                        ACE_TEXT ("unable to add acceptor to registry")
                        ACE_TEXT ("for <%s>%p\n"),
                        buffer,
                        ""));

          ACE_THROW (CORBA::BAD_PARAM (
              CORBA_SystemException::_tao_minor_code (
                TAO_ACCEPTOR_REGISTRY_OPEN_LOCATION_CODE,
                EINVAL),
              CORBA::COMPLETED_NO));
        }
    }
  else
    {
      if (TAO_debug_level > 0)
        ACE_ERROR ((LM_ERROR,
                    ACE_TEXT ("TAO (%P|%t) ")
                    ACE_TEXT ("unable to create acceptor ")
                    ));

      ACE_THROW (CORBA::BAD_PARAM (
          CORBA_SystemException::_tao_minor_code (
            TAO_ACCEPTOR_REGISTRY_OPEN_LOCATION_CODE,
            EINVAL),
          CORBA::COMPLETED_NO));
    }
}

int
TAO_PortableGroup_Acceptor_Registry::find (const TAO_Profile* profile,
                                           Entry *&entry)
{
  Acceptor_Registry_Iterator iter (this->registry_);

  while (iter.next (entry))
    {
      // Since the endpoint routine is nonconst, need to
      // cast away the constness even though we're not
      // changing anything.
      TAO_Profile *nc_profile = ACE_const_cast (TAO_Profile *,profile);
      if (entry->endpoint->is_equivalent (nc_profile->endpoint ()))
        return 1;

      iter.advance ();
   }

   return 0;
}