summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/orbsvcs/LoadBalancer_i.cpp
blob: af98cb7e372a9869c25717e4f4356f2cdf3d225e (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
// -*- C++ -*-

// $Id$

#include "LoadBalancer_i.h"
#include "ReplicaProxy.h"

ACE_RCSID(orbsvcs, LoadBalancer, "$Id$")

#if !defined (__ACE_INLINE__)
#include "LoadBalancer_i.i"
#endif /* __ACE_INLINE__ */

TAO_LB_LoadBalancer::TAO_LB_LoadBalancer (
     const char * interface_id,
     TAO_LB_LoadBalancing_Strategy *strategy,
     PortableServer::POA_ptr root_poa)
  : locator_ (this),
    strategy_ (strategy),
    poa_ ()
{
  (void) this->init (interface_id, root_poa);
}

TAO_LB_LoadBalancer::~TAO_LB_LoadBalancer (void)
{
  // Nothing else
}

LoadBalancing::ReplicaProxy_ptr
TAO_LB_LoadBalancer::connect (LoadBalancing::ReplicaControl_ptr control,
                              CORBA::Object_ptr replica
                              TAO_ENV_ARG_DECL)
  ACE_THROW_SPEC ((LoadBalancing::ReplicaProxy::NilControl,
                   LoadBalancing::ReplicaProxy::NilReplica,
                   CORBA::SystemException))
{
  TAO_LB_ReplicaProxy *proxy = 0;
  ACE_NEW_THROW_EX (proxy,
                    TAO_LB_ReplicaProxy,
                    CORBA::NO_MEMORY (
                      CORBA_SystemException::_tao_minor_code (
                        TAO_DEFAULT_MINOR_CODE,
                        ENOMEM),
                      CORBA::COMPLETED_NO));
  ACE_CHECK_RETURN (LoadBalancing::ReplicaProxy::_nil ());

  PortableServer::ServantBase_var proxy_servant = proxy;

  proxy->connect (this, control, replica TAO_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (LoadBalancing::ReplicaProxy::_nil ());

  if (this->strategy_->insert (proxy) == -1)
    {
      ACE_THROW_RETURN (CORBA::INTERNAL (),
                        LoadBalancing::ReplicaProxy::_nil ());
    }

  return proxy->_this (TAO_ENV_SINGLE_ARG_PARAMETER);
}

CORBA::Object_ptr
TAO_LB_LoadBalancer::group_identity (TAO_ENV_SINGLE_ARG_DECL_NOT_USED)
  ACE_THROW_SPEC ((CORBA::SystemException))
{
  return CORBA::Object::_duplicate (this->group_identity_.in ());
}

void
TAO_LB_LoadBalancer::load_changed (TAO_LB_ReplicaProxy *proxy
                                   TAO_ENV_ARG_DECL)
{
  this->strategy_->load_changed (proxy TAO_ENV_ARG_PARAMETER);
}

int
TAO_LB_LoadBalancer::init (const char * repository_id,
                           PortableServer::POA_ptr root_poa)
{
  ACE_TRY_NEW_ENV
    {
      // Create the appropriate RequestProcessingPolicy
      // (USE_SERVANT_MANAGER) and ServantRetentionPolicy (NON_RETAIN)
      // for a ServantLocator.
      PortableServer::RequestProcessingPolicy_var request =
        root_poa->create_request_processing_policy (
          PortableServer::USE_SERVANT_MANAGER
          TAO_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      PortableServer::ServantRetentionPolicy_var retention =
        root_poa->create_servant_retention_policy (
          PortableServer::NON_RETAIN
          TAO_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      // Create the PolicyList.
      CORBA::PolicyList policy_list;
      policy_list.length (2);
      policy_list[0] =
        PortableServer::RequestProcessingPolicy::_duplicate (
          request.in ());
      policy_list[1] =
        PortableServer::ServantRetentionPolicy::_duplicate (
           retention. in ());

      // Create the child POA with the ServantManager (ReplicaLocator)
      // above policies.
      PortableServer::POAManager_var poa_manager =
        root_poa->the_POAManager (TAO_ENV_SINGLE_ARG_PARAMETER);
      ACE_TRY_CHECK;
      this->poa_ = root_poa->create_POA ("TAO_LB_ReplicaLocator_POA",
                                         poa_manager.in (),
                                         policy_list
                                         TAO_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      // Activate the child POA.
      poa_manager->activate (TAO_ENV_SINGLE_ARG_PARAMETER);
      ACE_TRY_CHECK;

      request->destroy (TAO_ENV_SINGLE_ARG_PARAMETER);
      ACE_TRY_CHECK;

      retention->destroy (TAO_ENV_SINGLE_ARG_PARAMETER);
      ACE_TRY_CHECK;

      // Now set the ReplicaLocator as the child POA's Servant
      // Manager.
      this->poa_->set_servant_manager (&this->locator_
                                       TAO_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      // @@ What ObjectId should be used?
      PortableServer::ObjectId_var oid =
        PortableServer::string_to_ObjectId ("TAO_LB_ObjectGroup");

      this->group_identity_ =
        this->poa_->create_reference_with_id (oid.in (),
                                              repository_id
                                              TAO_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;
    }
  ACE_CATCHANY
    {
      // @@ Should we do anything here?

      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
                           "(%P|%t) Load Balancer initialization:");

      return -1;
    }
  ACE_ENDTRY;

  return 0;
}