⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lb_loadmanager.cpp

📁 这是广泛使用的通信开源项目,对于大容量,高并发的通讯要求完全能够胜任,他广泛可用于网络游戏医学图像网关的高qos要求.更详细的内容可阅读相应的材料
💻 CPP
📖 第 1 页 / 共 3 页
字号:

  if (invalid_criteria.length () != 0)
    ACE_THROW (PortableGroup::InvalidCriteria (invalid_criteria));

  if (found_factory == 0)
    ACE_THROW (PortableGroup::NoFactory ());
}
#endif  /* 0 */

void
TAO_LB_LoadManager::delete_object (
    const PortableGroup::GenericFactory::FactoryCreationId &
      factory_creation_id
    ACE_ENV_ARG_DECL)
  ACE_THROW_SPEC ((CORBA::SystemException,
                   PortableGroup::ObjectNotFound))
{
  this->generic_factory_.delete_object (factory_creation_id
                                        ACE_ENV_ARG_PARAMETER);
}

CORBA::Object_ptr
TAO_LB_LoadManager::next_member (const PortableServer::ObjectId & oid
                                 ACE_ENV_ARG_DECL)
{
  PortableGroup::ObjectGroup_var object_group =
    this->object_group_manager_.object_group (oid);

  if (CORBA::is_nil (object_group.in ()))
    ACE_THROW_RETURN (CORBA::OBJECT_NOT_EXIST (),
                      CORBA::Object::_nil ());

  PortableGroup::Properties_var properties =
    this->get_properties (object_group.in ()
                          ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (CORBA::Object::_nil ());

  // Prefer custom load balancing strategies over built-in ones.
  PortableGroup::Value value;
  CosLoadBalancing::Strategy_ptr strategy;

  if ((TAO_PG::get_property_value (this->built_in_balancing_strategy_name_,
                                   properties.in (),
                                   value)
       || TAO_PG::get_property_value (this->custom_balancing_strategy_name_,
                                      properties.in (),
                                      value))
       && (value >>= strategy)
       && !CORBA::is_nil (strategy))
    {
      return strategy->next_member (object_group.in (),
                                    this->lm_ref_.in ()
                                    ACE_ENV_ARG_PARAMETER);
    }

  ACE_THROW_RETURN (CORBA::OBJECT_NOT_EXIST (),
                    CORBA::Object::_nil ());
}

void
TAO_LB_LoadManager::init (ACE_Reactor * reactor,
                          CORBA::ORB_ptr orb,
                          PortableServer::POA_ptr root_poa
                          ACE_ENV_ARG_DECL)
{
  ACE_ASSERT (!CORBA::is_nil (orb));
  ACE_ASSERT (!CORBA::is_nil (root_poa));

  ACE_GUARD (TAO_SYNCH_MUTEX,
             guard,
             this->lock_);

  if (CORBA::is_nil (this->poa_.in ()))
    {
      // Create a new transient servant manager object in the child
      // POA.
      PortableServer::ServantManager_ptr tmp;
      ACE_NEW_THROW_EX (tmp,
                        TAO_LB_MemberLocator (this),
                        CORBA::NO_MEMORY (
                          CORBA::SystemException::_tao_minor_code (
                            TAO_DEFAULT_MINOR_CODE,
                            ENOMEM),
                          CORBA::COMPLETED_NO));
      ACE_CHECK;

      PortableServer::ServantManager_var member_locator = tmp;

      // 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
          ACE_ENV_ARG_PARAMETER);
      ACE_CHECK;

      PortableServer::ServantRetentionPolicy_var retention =
        root_poa->create_servant_retention_policy (
          PortableServer::NON_RETAIN
          ACE_ENV_ARG_PARAMETER);
      ACE_CHECK;

      // Create the PolicyList containing the policies necessary for
      // the POA to support ServantLocators.
      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 above ServantManager policies.
      // The ServantManager will be the MemberLocator.
      PortableServer::POAManager_var poa_manager =
        root_poa->the_POAManager (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_CHECK;

      // The child POA's name will consist of a string that includes
      // the current time in milliseconds in hexidecimal format (only
      // four bytes will be used).  This is an attempt to prevent
      // different load manager servants within the same ORB from
      // using the same POA.
      const ACE_Time_Value tv = ACE_OS::gettimeofday ();
      const CORBA::Long time =
        ACE_static_cast (CORBA::Long,
                         tv.msec ()); // Time in milliseconds.

      char poa_name[] = "TAO_LB_LoadManager_POA - 0xZZZZZZZZ";
      char * astr =
        poa_name
        + sizeof (poa_name)
        - 9 /* 8 + 1 */;

      // Overwrite the last 8 characters in the POA name with the
      // hexadecimal representation of the time in milliseconds.
      ACE_OS::sprintf (astr, "%x", time);

      this->poa_ = root_poa->create_POA (poa_name,
                                         poa_manager.in (),
                                         policy_list
                                         ACE_ENV_ARG_PARAMETER);
      ACE_CHECK;

      request->destroy (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_CHECK;

      retention->destroy (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_CHECK;

      // Now set the MemberLocator as the child POA's Servant
      // Manager.
      this->poa_->set_servant_manager (member_locator.in ()
                                       ACE_ENV_ARG_PARAMETER);
      ACE_CHECK;

      this->object_group_manager_.poa (this->poa_.in ());
      this->generic_factory_.poa (this->poa_.in ());

      // Activate the child POA.
      poa_manager->activate (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_CHECK;

      this->reactor_ = reactor;
      this->root_poa_ = PortableServer::POA::_duplicate (root_poa);
    }

  if (CORBA::is_nil (this->lm_ref_.in ()))
    {
      this->lm_ref_ = this->_this (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_CHECK;

      orb->register_initial_reference ("LoadManager",
                                       this->lm_ref_.in ()
                                       ACE_ENV_ARG_PARAMETER);
      ACE_CHECK;
    }

  if (CORBA::is_nil (this->load_alert_handler_.in ()))
    {
      TAO_LB_LoadAlert_Handler * handler;
      ACE_NEW_THROW_EX (handler,
                        TAO_LB_LoadAlert_Handler,
                        CORBA::NO_MEMORY (
                          CORBA::SystemException::_tao_minor_code (
                            TAO_DEFAULT_MINOR_CODE,
                            ENOMEM),
                          CORBA::COMPLETED_NO));
      ACE_CHECK;

      PortableServer::ServantBase_var safe_handler = handler;

      this->load_alert_handler_ =
        handler->_this (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_CHECK;
    }

  this->built_in_balancing_strategy_info_name_.length (1);
  this->built_in_balancing_strategy_info_name_[0].id =
    CORBA::string_dup ("org.omg.CosLoadBalancing.StrategyInfo");

  this->built_in_balancing_strategy_name_.length (1);
  this->built_in_balancing_strategy_name_[0].id =
    CORBA::string_dup ("org.omg.CosLoadBalancing.Strategy");

  this->custom_balancing_strategy_name_.length (1);
  this->custom_balancing_strategy_name_[0].id =
    CORBA::string_dup ("org.omg.CosLoadBalancing.CustomStrategy");
}

void
TAO_LB_LoadManager::preprocess_properties (PortableGroup::Properties & props
                                           ACE_ENV_ARG_DECL)
{
  // @@ This is slow.  Optimize this code.

  const CORBA::ULong len = props.length ();
  for (CORBA::ULong i = 0; i < len; ++i)
    {
      PortableGroup::Property & property = props[i];
      if (property.nam == this->custom_balancing_strategy_name_)
        {
          CosLoadBalancing::CustomStrategy_ptr strategy;
          if (!(property.val >>= strategy)
              || CORBA::is_nil (strategy))
            ACE_THROW (PortableGroup::InvalidProperty (property.nam,
                                                       property.val));
        }

      else if (property.nam == this->built_in_balancing_strategy_info_name_)
        {
          CosLoadBalancing::StrategyInfo * info;

          if (property.val >>= info)
            {
              // Convert the property from a "StrategyInfo" property
              // to a "Strategy" property.

              CosLoadBalancing::Strategy_var strategy =
                this->make_strategy (info
                                     ACE_ENV_ARG_PARAMETER);
              ACE_CHECK;

              if (!CORBA::is_nil (strategy.in ()))
                {
                  property.nam = this->built_in_balancing_strategy_name_;

                  property.val <<= strategy.in ();
                }
              else
                ACE_THROW (PortableGroup::InvalidProperty (property.nam,
                                                           property.val));
            }
          else
            ACE_THROW (PortableGroup::InvalidProperty (property.nam,
                                                       property.val));
        }
      else if (property.nam == this->built_in_balancing_strategy_name_)
        {
          // It is illegal to set the Strategy property externally.
          ACE_THROW (PortableGroup::InvalidProperty (property.nam,
                                                     property.val));
        }
    }
}

CosLoadBalancing::Strategy_ptr
TAO_LB_LoadManager::make_strategy (CosLoadBalancing::StrategyInfo * info
                                   ACE_ENV_ARG_DECL)
{
  /**
   * @todo We need a strategy factory.  This is just too messy.
   */

  if (ACE_OS::strcmp (info->name.in (), "RoundRobin") == 0)
    {
      {
        ACE_GUARD_RETURN (TAO_SYNCH_MUTEX,
                          monitor,
                          this->lock_,
                          CosLoadBalancing::Strategy::_nil ());

        if (CORBA::is_nil (this->round_robin_.in ()))
          {
            TAO_LB_RoundRobin * rr_servant;
            ACE_NEW_THROW_EX (rr_servant,
                              TAO_LB_RoundRobin (this->root_poa_.in ()),
                              CORBA::NO_MEMORY ());
            ACE_CHECK_RETURN (CosLoadBalancing::Strategy::_nil ());

            PortableServer::ServantBase_var s = rr_servant;

            this->round_robin_ =
              rr_servant->_this (ACE_ENV_SINGLE_ARG_PARAMETER);
            ACE_CHECK_RETURN (CosLoadBalancing::Strategy::_nil ());
          }
      }

      return CosLoadBalancing::Strategy::_duplicate (this->round_robin_.in ());
    }

  else if (ACE_OS::strcmp (info->name.in (), "Random") == 0)
    {
      {
        ACE_GUARD_RETURN (TAO_SYNCH_MUTEX,
                          monitor,
                          this->lock_,
                          CosLoadBalancing::Strategy::_nil ());

        if (CORBA::is_nil (this->random_.in ()))
          {
            TAO_LB_Random * rnd_servant;
            ACE_NEW_THROW_EX (rnd_servant,
                              TAO_LB_Random (this->root_poa_.in ()),
                              CORBA::NO_MEMORY ());
            ACE_CHECK_RETURN (CosLoadBalancing::Strategy::_nil ());

            PortableServer::ServantBase_var s = rnd_servant;

            this->random_ =
              rnd_servant->_this (ACE_ENV_SINGLE_ARG_PARAMETER);
            ACE_CHECK_RETURN (CosLoadBalancing::Strategy::_nil ());
          }
      }

      return CosLoadBalancing::Strategy::_duplicate (this->random_.in ());
    }

  else if (ACE_OS::strcmp (info->name.in (), "LeastLoaded") == 0)
    {
      // If no LeastLoaded properties have been set, just use the
      // default/cached LeastLoaded instance.  Otherwise create and
      // return a new LeastLoaded instance with the appropriate
      // properties.

      if (info->props.length () == 0)
        {
          {
            ACE_GUARD_RETURN (TAO_SYNCH_MUTEX,
                              monitor,
                              this->lock_,
                              CosLoadBalancing::Strategy::_nil ());

            if (CORBA::is_nil (this->least_loaded_.in ()))
              {
                TAO_LB_LeastLoaded * ll_servant;
                ACE_NEW_THROW_EX (ll_servant,
                                  TAO_LB_LeastLoaded (this->root_poa_.in ()),
                                  CORBA::NO_MEMORY ());
                ACE_CHECK_RETURN (CosLoadBalancing::Strategy::_nil ());

                PortableServer::ServantBase_var s = ll_servant;

                this->least_loaded_ =
                  ll_servant->_this (ACE_ENV_SINGLE_ARG_PARAMETER);
                ACE_CHECK_RETURN (CosLoadBalancing::Strategy::_nil ());
              }
          }

          return
            CosLoadBalancing::Strategy::_duplicate (this->least_loaded_.in ());
        }
      else
        {
          TAO_LB_LeastLoaded * ll_servant;
          ACE_NEW_THROW_EX (ll_servant,
                            TAO_LB_LeastLoaded (this->root_poa_.in ()),
                            CORBA::NO_MEMORY ());
          ACE_CHECK_RETURN (CosLoadBalancing::Strategy::_nil ());

          PortableServer::ServantBase_var s = ll_servant;

          ll_servant->init (info->props
                            ACE_ENV_ARG_PARAMETER);
          ACE_CHECK_RETURN (CosLoadBalancing::Strategy::_nil ());

          return ll_servant->_this (ACE_ENV_SINGLE_ARG_PARAMETER);
        }
    }

  return CosLoadBalancing::Strategy::_nil ();
}

// void
// TAO_LB_LoadManager::update_strategy ()
// {
// }

// void
// TAO_LB_LoadManager::deactivate_strategy (ACE_ENV_ARG_DECL)
// {
//   PortableServer::ObjectId_var oid =
//     this->poa_->reference_to_id (
//   this->poa_->deactivate_object ();
// }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -