summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/orbsvcs/Trader/Policies.cpp
blob: c7b6228508af592748fbff8d67604d5534267983 (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
// $Id$

#include "Policies.h"

const char* TAO_Policies::POLICY_NAMES[] = 
{
  "exact_type_match",
  "hop_count",
  "link_follow_rule",
  "match_card",
  "return_card",
  "search_card",
  "starting_trader",
  "use_dynamic_properties",
  "use_modifiable_properties",
  "use_proxy_offers"
};

TAO_Policies::TAO_Policies (TAO_Trader_Base& trader,
			    CosTrading::PolicySeq& policies,
			    CORBA::Environment& _env)
  TAO_THROW_SPEC (CosTrading::Lookup::IllegalPolicyName,
		  CosTrading::DuplicatePolicyName)
  : trader_ (trader),
    policies_ (USE_PROXY_OFFERS + 1)
{
  for (int i = 0; i <= USE_PROXY_OFFERS; i++)
    this->policies_[i] = 0;

  for (int j = 0; j < policies.length (); j++)
    {      
      const char* pol_name = (const char*) policies[j].name;
      int length = (pol_name == 0) ? 0 : ACE_OS::strlen (pol_name),
	index = -1;

      if (length < ACE_OS::strlen (POLICY_NAMES[HOP_COUNT]))
	TAO_THROW (CosTrading::Lookup::IllegalPolicyName (pol_name))
      
      switch (pol_name[0])
	{
	case 'e':
	  index = EXACT_TYPE_MATCH;
	  break;
	case 'h':
	  index = HOP_COUNT;
	  break;
	case 'l':
	  index = LINK_FOLLOW_RULE;
	  break;
	case 'm':
	  index = MATCH_CARD;
	  break;
	case 'r':
	  index = RETURN_CARD;
	  break;
	case 's':
	  index = SEARCH_CARD;
	  break;
	case 'u':
	  if (pol_name[4] == 'd')
	    index = USE_DYNAMIC_PROPERTIES;
	  if (pol_name[4] == 'm')
	    index = USE_MODIFIABLE_PROPERTIES;
	  if (pol_name[4] == 'p')
	    index = USE_PROXY_OFFERS;
	}

      // Match the name of the policy, and insert its value into the
      // vector. 
      if (index == -1 || ::strcmp (POLICY_NAMES[index], pol_name) != 0)
	TAO_THROW (CosTrading::Lookup::IllegalPolicyName (pol_name));
      else
	{
	  if (this->policies_[index] != 0)
	    TAO_THROW (CosTrading::DuplicatePolicyName (pol_name));
	  else
	    this->policies_[index] = &(policies[j]);
	}
    }
}

CORBA::ULong
TAO_Policies::ulong_prop (POLICY_TYPE pol,
			  CORBA::Environment& _env)
  TAO_THROW_SPEC (CosTrading::Lookup::PolicyTypeMismatch)
{
  CORBA::ULong return_value = 0, max_value;
  TAO_Import_Attributes_Impl& import_attrs =
    this->trader_.import_attributes ();

  // Discover the default values for each of the possible cardinality
  // policies. 
  switch (pol)
    {
    case SEARCH_CARD:
      return_value = import_attrs.def_search_card ();
      max_value = import_attrs.max_search_card ();
      break;
    case MATCH_CARD:
      return_value = import_attrs.def_match_card ();
      max_value = import_attrs.max_match_card ();
      break;
    case RETURN_CARD:
      return_value = import_attrs.def_return_card ();
      max_value = import_attrs.max_return_card ();
      break;
    }
  
  if (this->policies_[pol] != 0)
    {
      // Extract the desired policy value.
      CosTrading::Policy* policy = this->policies_[pol];
      CosTrading::PolicyValue& value = policy->value;
      CORBA::TypeCode* type = value.type ();
      
      if (!type->equal (CORBA::_tc_ulong))
	TAO_THROW (CosTrading::Lookup::PolicyTypeMismatch (*policy));
      else
	value >>= return_value;

      if (max_value < return_value)
	return_value = max_value;
      else
	this->limits_.push_back ((char *) POLICY_NAMES[pol]);
    }

  return return_value;
}

CORBA::ULong
TAO_Policies::search_card (CORBA::Environment& _env)
  TAO_THROW_SPEC (CosTrading::Lookup::PolicyTypeMismatch)
{
  return this->ulong_prop (SEARCH_CARD, _env);
}

CORBA::ULong
TAO_Policies::match_card (CORBA::Environment& env_)
  TAO_THROW_SPEC (CosTrading::Lookup::PolicyTypeMismatch)
{
  return this->ulong_prop (MATCH_CARD, _env);
}

CORBA::ULong
TAO_Policies::return_card (CORBA::Environment& env_)
  TAO_THROW_SPEC (CosTrading::Lookup::PolicyTypeMismatch)
{
  return this->ulong_prop (RETURN_CARD, _env);
}

CORBA::Boolean
TAO_Policies::boolean_prop (POLICY_TYPE pol,
			    CORBA::Environment& _env)
  TAO_THROW_SPEC (CosTrading::Lookup::PolicyTypeMismatch)
{
  CORBA::Boolean def_value = (CORBA::Boolean) 1,
    return_value = (CORBA::Boolean) 1;
  TAO_Support_Attributes_Impl& support_attrs =
    this->trader_.support_attributes ();

  switch (pol)
    {
    case USE_MODIFIABLE_PROPERTIES:
      def_value = support_attrs.supports_modifiable_properties ();
      break;
    case USE_DYNAMIC_PROPERTIES:
      def_value = support_attrs.supports_dynamic_properties ();
      break;
    case USE_PROXY_OFFERS:
      def_value = support_attrs.supports_proxy_offers ();
      break;
    }
  
  if (this->policies_[pol] != 0)
    {
      CosTrading::Policy* policy = this->policies_[pol];
      CosTrading::PolicyValue& value = policy->value;
      CORBA::TypeCode* type = value.type ();
      
      if (!type->equal (CORBA::_tc_boolean))
	TAO_THROW (CosTrading::Lookup::PolicyTypeMismatch (*policy));
      else
	value >>= to_boolean (return_value);

      if (def_value == (CORBA::Boolean) 0)
	return_value = (CORBA::Boolean) 0;
      else
	this->limits_.push_back ((char *) POLICY_NAMES[pol]);
    }

  return return_value;
}


CORBA::Boolean
TAO_Policies::use_modifiable_properties (CORBA::Environment& _env)
  TAO_THROW_SPEC (CosTrading::Lookup::PolicyTypeMismatch)
{
  return this->boolean_prop (USE_MODIFIABLE_PROPERTIES, _env);
}

CORBA::Boolean
TAO_Policies::use_dynamic_properties (CORBA::Environment& _env)
  TAO_THROW_SPEC (CosTrading::Lookup::PolicyTypeMismatch)
{
  return this->boolean_prop (USE_DYNAMIC_PROPERTIES, _env);
}

CORBA::Boolean
TAO_Policies::use_proxy_offers (CORBA::Environment& _env)
  TAO_THROW_SPEC (CosTrading::Lookup::PolicyTypeMismatch)
{
  return this->boolean_prop (USE_PROXY_OFFERS, _env);
}

CORBA::Boolean
TAO_Policies::exact_type_match (CORBA::Environment& _env)
    TAO_THROW_SPEC (CosTrading::Lookup::PolicyTypeMismatch)
{
  return this->boolean_prop (EXACT_TYPE_MATCH, _env);
}


CosTrading::TraderName*
TAO_Policies::starting_trader (CORBA::Environment& _env)
  TAO_THROW_SPEC (CosTrading::Lookup::PolicyTypeMismatch,
	 CosTrading::Lookup::InvalidPolicyValue)
{
  return 0;
}

CosTrading::FollowOption
TAO_Policies::link_follow_rule (CORBA::Environment& _env)
    TAO_THROW_SPEC (CosTrading::Lookup::PolicyTypeMismatch,
	   CosTrading::Lookup::InvalidPolicyValue)
{
  return CosTrading::local_only;
}

CORBA::ULong
TAO_Policies::hop_count (CORBA::Environment& _env)
  TAO_THROW_SPEC (CosTrading::Lookup::PolicyTypeMismatch)
{
  return (CORBA::ULong) 0;
}


CosTrading::PolicyNameSeq*
TAO_Policies::limits_applied (void)
{
  int i = 0;
  CORBA::ULong size = this->limits_.size ();
  CosTrading::PolicyName* temp = CosTrading::PolicyNameSeq::allocbuf (size);

  for (POL_QUEUE::iterator p_iter = this->limits_.begin();
       p_iter != this->limits_.end ();
       p_iter++)  
    temp[i++] = CORBA::string_dup (*p_iter);
  
  return new CosTrading::PolicyNameSeq (i, i, temp, 1);
}