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

📄 trader_utils.cpp

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

#include "Trader_Utils.h"
#include "ace/OS_NS_string.h"

ACE_RCSID(Trader, Trader_Utils, "Trader_Utils.cpp,v 1.49 2003/11/04 08:12:59 dhinton Exp")

TAO_Policy_Creator::TAO_Policy_Creator (int num_policies)
  : policies_ (num_policies),
    num_policies_ (0)
{
  for (int i = 0; i < TAO_Policies::REQUEST_ID + 1; i++)
    this->poltable_[i] = -1;
}

void
TAO_Policy_Creator::search_card (CORBA::ULong scard)
{
  CosTrading::Policy& policy =
    this->fetch_next_policy (TAO_Policies::SEARCH_CARD);
  policy.value <<= scard;
}

void
TAO_Policy_Creator::match_card (CORBA::ULong mcard)
{
  CosTrading::Policy& policy =
    this->fetch_next_policy (TAO_Policies::MATCH_CARD);
  policy.value <<= mcard;
}

void
TAO_Policy_Creator::return_card (CORBA::ULong rcard)
{
  CosTrading::Policy& policy =
    this->fetch_next_policy (TAO_Policies::RETURN_CARD);
  policy.value <<= rcard;
}

void
TAO_Policy_Creator::use_modifiable_properties (CORBA::Boolean mod_props)
{
  CosTrading::Policy& policy =
    this->fetch_next_policy (TAO_Policies::USE_MODIFIABLE_PROPERTIES);
  policy.value <<= CORBA::Any::from_boolean (mod_props);
}

void
TAO_Policy_Creator::use_dynamic_properties (CORBA::Boolean dyn_props)
{
  CosTrading::Policy& policy =
    this->fetch_next_policy (TAO_Policies::USE_DYNAMIC_PROPERTIES);
  policy.value <<= CORBA::Any::from_boolean (dyn_props);
}

void
TAO_Policy_Creator::use_proxy_offers (CORBA::Boolean prox_offs)
{
  CosTrading::Policy& policy =
    this->fetch_next_policy (TAO_Policies::USE_PROXY_OFFERS);
  policy.value <<= CORBA::Any::from_boolean (prox_offs);
}

void
TAO_Policy_Creator::starting_trader (const CosTrading::TraderName& name)
{
  CosTrading::Policy& policy =
    this->fetch_next_policy (TAO_Policies::STARTING_TRADER);
  policy.value <<= name;
}

void
TAO_Policy_Creator::starting_trader (CosTrading::TraderName* name)
{
  CosTrading::Policy& policy =
    this->fetch_next_policy (TAO_Policies::STARTING_TRADER);
  policy.value <<= name;
}

void
TAO_Policy_Creator::
link_follow_rule (CosTrading::FollowOption follow_option)
{
  CosTrading::Policy& policy =
    this->fetch_next_policy (TAO_Policies::LINK_FOLLOW_RULE);
  policy.value <<= follow_option;
}

void
TAO_Policy_Creator::hop_count (CORBA::ULong hop_count)
{
  CosTrading::Policy& policy =
    this->fetch_next_policy (TAO_Policies::HOP_COUNT);
  policy.value <<= hop_count;
}

void
TAO_Policy_Creator::exact_type_match (CORBA::Boolean exact_type)
{
  CosTrading::Policy& policy =
    this->fetch_next_policy (TAO_Policies::EXACT_TYPE_MATCH);
  policy.value <<= CORBA::Any::from_boolean (exact_type);
}

void
TAO_Policy_Creator::request_id (const CosTrading::Admin::OctetSeq& request_id)
{
  CosTrading::Policy& policy =
    this->fetch_next_policy (TAO_Policies::REQUEST_ID);
  policy.value <<= request_id;
}

TAO_Policy_Creator::operator const CosTrading::PolicySeq& (void) const
{
  return this->policies_;
}

const CosTrading::PolicySeq&
TAO_Policy_Creator::policy_seq (void) const
{
  return this->policies_;
}

CosTrading::Policy&
TAO_Policy_Creator::fetch_next_policy (TAO_Policies::POLICY_TYPE pol_type)
{
  CORBA::ULong index = 0;

  if (this->poltable_[pol_type] == -1)
    {
      // Expand the policy sequence, and copy in the policy name into
      // the new element.
      CORBA::ULong length = this->policies_.length ();
      this->num_policies_++;

      if (length < this->num_policies_)
        this->policies_.length (this->num_policies_);

      index = this->num_policies_ - 1;

      // Ensure the starting trader policy gets the first slot.
      if (pol_type != TAO_Policies::STARTING_TRADER
          || index == 0)
        {
          this->policies_[index].name = TAO_Policies::POLICY_NAMES[pol_type];
          this->poltable_[pol_type] = index;
        }
      else
        {
          // Copy the element in the first slot to the newly
          // allocated slot.
          TAO_Policies::POLICY_TYPE occupying_policy =
            TAO_Policies::STARTING_TRADER;
          for (CORBA::ULong i = 0; i < this->num_policies_ - 1; i++)
            {
              if (this->poltable_[i] == 0)
                {
                  occupying_policy =
                    ACE_static_cast (TAO_Policies::POLICY_TYPE, i);
                  break;
                }
            }

          this->poltable_[occupying_policy] = index;
          this->poltable_[TAO_Policies::STARTING_TRADER] = 0;
          this->policies_[index].name =
            TAO_Policies::POLICY_NAMES[occupying_policy];
          this->policies_[index].value = this->policies_[0].value;
          this->policies_[0].name =
            TAO_Policies::POLICY_NAMES[TAO_Policies::STARTING_TRADER];

          index = 0;
        }
    }
  else
    index = this->poltable_[pol_type];

  return this->policies_[index];
}

// Constructor

TAO_Property_Evaluator::
TAO_Property_Evaluator(const CosTrading::PropertySeq& props,
                       CORBA::Boolean supports_dp)
  : props_ (props),
    supports_dp_ (supports_dp),
    dp_cache_ (new CORBA::Any*[props.length ()])
{
  if (this->dp_cache_ != 0)
    {
      for (CORBA::ULong i = 0; i < this->props_.length (); i++)
        this->dp_cache_[i] = 0;
    }
}


TAO_Property_Evaluator::
TAO_Property_Evaluator(CosTrading::Offer& offer,
                       CORBA::Boolean supports_dp)
  : props_ (offer.properties),
    supports_dp_ (supports_dp),
    dp_cache_ (new CORBA::Any*[offer.properties.length ()])
{
  if (this->dp_cache_ != 0)
    for (CORBA::ULong i = 0; i < this->props_.length (); i++)
      this->dp_cache_[i] = 0;
}

TAO_Property_Evaluator::~TAO_Property_Evaluator (void)
{
  // Clean up the results of any dynamic properties.
  for (CORBA::ULong i = 0; i < this->props_.length (); i++)
    if (this->dp_cache_[i] != 0)
      delete this->dp_cache_[i];

  delete [] this->dp_cache_;
}

int
TAO_Property_Evaluator::is_dynamic_property (int index)
{
  int return_value = 0,
    num_properties = this->props_.length();

  // Ensure index is in bounds.
  if (index >= 0 && index < num_properties)
    {
      // Obtain the value of the property at index <index>.
      const CORBA::Any& value = this->props_[index].value;
      CORBA::TypeCode_var type = value.type ();

      // @@ Seth, this will not work on platforms using environment variable.
      ACE_DECLARE_NEW_CORBA_ENV;

      if (type->equal (CosTradingDynamic::_tc_DynamicProp
                       ACE_ENV_ARG_PARAMETER))
        return_value = 1;
    }

  return return_value;
}

CORBA::Any*
TAO_Property_Evaluator::property_value (int index
                                        ACE_ENV_ARG_DECL)
    ACE_THROW_SPEC ((CosTradingDynamic::DPEvalFailure))
{
  CORBA::Any* prop_val = 0;
  CORBA::Boolean in_cache =
    this->dp_cache_ != 0 && this->dp_cache_[index] != 0;

  if (! this->is_dynamic_property (index))
    prop_val = (CORBA::Any *) &(this->props_[index].value);
  else if (this->supports_dp_ && in_cache)
    prop_val = this->dp_cache_[index];
  else if (this->supports_dp_)
    {
      // Property is defined at this point.
      CosTradingDynamic::DynamicProp* dp_struct;
      const CORBA::String_var name = this->props_[index].name.in ();
      const CORBA::Any& value = this->props_[index].value;

      // Extract the DP_Struct.
      value >>= dp_struct;

      CosTradingDynamic::DynamicPropEval_var dp_eval =
        CosTradingDynamic::DynamicPropEval::_duplicate (dp_struct->eval_if.in ());

      if (CORBA::is_nil (dp_eval.in ()))
        {
          ACE_THROW_RETURN (CosTradingDynamic::
                            DPEvalFailure (name,
                                           CORBA::TypeCode::_nil (),
                                           CORBA::Any ()),
                            prop_val);
        }
      else
        {
          CORBA::TypeCode* type = dp_struct->returned_type.in ();
          CORBA::Any& info = dp_struct->extra_info;

          ACE_TRY
            {
              // Retrieve the value of the dynamic property.
              prop_val = dp_eval->evalDP(name, type, info ACE_ENV_ARG_PARAMETER);
              ACE_TRY_CHECK;

              if (this->dp_cache_ != 0)
                this->dp_cache_[index] = prop_val;
            }
          ACE_CATCH (CORBA::SystemException, excp)
            {
              ACE_TRY_THROW
                (CosTradingDynamic::DPEvalFailure (name, type, info));
            }
          ACE_ENDTRY;
          ACE_CHECK_RETURN (prop_val);
        }
    }

  return prop_val;
}

CORBA::TypeCode_ptr
TAO_Property_Evaluator::property_type (int index)
{
  CORBA::TypeCode_ptr prop_type = CORBA::TypeCode::_nil();

  // Determine if property is both defined and dynamic.
  if (this->is_dynamic_property (index))
    {
      // Extract type information from the DP_Struct.
      const CORBA::Any& value = this->props_[index].value;
      CosTradingDynamic::DynamicProp* dp_struct;
      value >>= dp_struct;

      // Grab a pointer to the returned_type description
      prop_type = CORBA::TypeCode::_duplicate (dp_struct->returned_type.in ());
    }
  else
    // TypeCode is self-evident at this point.
    prop_type = this->props_[index].value.type ();

  return prop_type;
}

TAO_Property_Evaluator_By_Name::
TAO_Property_Evaluator_By_Name (const CosTrading::PropertySeq& properties
                                ACE_ENV_ARG_DECL,
                                CORBA::Boolean supports_dp)
  ACE_THROW_SPEC ((CosTrading::DuplicatePropertyName,
                   CosTrading::IllegalPropertyName))
    : TAO_Property_Evaluator (properties, supports_dp)
{
  int length = this->props_.length();

  for (int i = 0; i < length; i++)
    {
      const CosTrading::Property& prop = this->props_[i];

      if (! TAO_Trader_Base::is_valid_identifier_name (prop.name))
        ACE_THROW (CosTrading::IllegalPropertyName (prop.name));

      TAO_String_Hash_Key prop_name = prop.name.in ();
      if (this->table_.bind (prop_name, i))
        ACE_THROW (CosTrading::DuplicatePropertyName (prop.name));
    }
}

TAO_Property_Evaluator_By_Name::
TAO_Property_Evaluator_By_Name(CosTrading::Offer& offer,
                               CORBA::Boolean supports_dp)
  : TAO_Property_Evaluator(offer, supports_dp)
{
  int length = this->props_.length();

  for (int i = 0; i < length; i++)
    {
      TAO_String_Hash_Key prop_name = (const char*) this->props_[i].name;
      this->table_.bind (prop_name, i);
    }
}

int
TAO_Property_Evaluator_By_Name::
is_dynamic_property(const char* property_name)
{
  int predicate = 0, index = 0;
  TAO_String_Hash_Key prop_name (property_name);

  // If the property name is in the map, delegate evaluation to our
  // superclass. Otherwise, throw an exception.
  if (this->table_.find (prop_name, index) == 0)
    predicate = this->TAO_Property_Evaluator::is_dynamic_property(index);

  return predicate;
}

CORBA::Any*
TAO_Property_Evaluator_By_Name::property_value (const char* property_name
                                                ACE_ENV_ARG_DECL)
  ACE_THROW_SPEC ((CosTradingDynamic::DPEvalFailure))
{
  int index = 0;
  CORBA::Any* prop_value = 0;
  TAO_String_Hash_Key prop_name (property_name);

⌨️ 快捷键说明

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