summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/orbsvcs/Trader/Policies.cpp
blob: 4a666ed667bc6b7e5c1a09fac68cfb2ff17fae43 (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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
// $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",
  "request_id"
};

TAO_Policies::TAO_Policies (TAO_Trader_Base& trader,
			    const CosTrading::PolicySeq& policies,
			    CORBA::Environment& _env)
  TAO_THROW_SPEC ((CosTrading::Lookup::IllegalPolicyName,
		  CosTrading::DuplicatePolicyName))
  : trader_ (trader)
{
  for (int i = 0; i < TAO_NUM_POLICIES; 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':
	  if (pol_name[2] == 't')
	    index = RETURN_CARD;
	  else if (pol_name[2] == 'q')
	    index = REQUEST_ID;
	  break;
	case 's':
	  if (pol_name[1] == 't')
	    index == STARTING_TRADER;
	  else if (pol_name[1] == 'e')
	    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] = (CosTrading::Policy *) &(policies[j]);
	}
    }
}

TAO_Policies::~TAO_Policies (void)
{
}

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;
    case HOP_COUNT:
      return_value = import_attrs.def_hop_count ();
      max_value = import_attrs.max_hop_count ();
      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, _env))
	TAO_THROW_RETURN (CosTrading::Lookup::PolicyTypeMismatch (*policy),
			  return_value);
      else
	value >>= return_value;

      if (max_value < return_value)
	return_value = max_value;
    }
  
  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::B_TRUE,
    return_value = CORBA::B_TRUE;
  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;
    case EXACT_TYPE_MATCH:
      def_value = CORBA::B_FALSE;
      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, _env))
	TAO_THROW_RETURN (CosTrading::Lookup::PolicyTypeMismatch (*policy),
			  return_value);
      else
	value >>= to_boolean (return_value);

      if (def_value == CORBA::B_FALSE)
	return_value = CORBA::B_FALSE;
    }
  else
    return_value = def_value;

  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))
{
  CosTrading::TraderName* trader_name = 0;
  
  if (this->policies_[STARTING_TRADER] != 0)
    {
      CosTrading::Policy* policy = this->policies_[STARTING_TRADER];
      CosTrading::PolicyValue& value = policy->value;
      CORBA::TypeCode* type = value.type ();
      
      if (!type->equal (CosTrading::_tc_TraderName, _env))
	TAO_THROW_RETURN (CosTrading::Lookup::PolicyTypeMismatch (*policy),
			  trader_name);
      else
	{
	  //	  ACE_NEW_RETURN (trader_name, CosTrading::TraderName, 0);
	  value >>= trader_name;
	}
    }

  return trader_name;
}

CosTrading::FollowOption
TAO_Policies::link_follow_rule (CORBA::Environment& _env)
    TAO_THROW_SPEC ((CosTrading::Lookup::PolicyTypeMismatch))
{
  CosTrading::FollowOption return_value = 
    this->trader_.import_attributes ().def_follow_policy ();

  if (this->policies_[LINK_FOLLOW_RULE] != 0)
    {
      CosTrading::FollowOption max_follow_policy = 
	this->trader_.import_attributes ().max_follow_policy ();

      CosTrading::Policy* policy = this->policies_[LINK_FOLLOW_RULE];
      CosTrading::PolicyValue& value = policy->value;
      CORBA::TypeCode* type = value.type ();

      // Extract the link follow rule 
      if (!type->equal (CosTrading::_tc_FollowOption, _env))
	TAO_THROW_RETURN (CosTrading::Lookup::PolicyTypeMismatch (*policy),
			  return_value);
      else
	value >>= return_value;

      if (return_value > max_follow_policy)
	return_value = max_follow_policy;
    }

  return return_value;
}

CosTrading::FollowOption
TAO_Policies::link_follow_rule (const char* link_name,
				CORBA::Environment& _env)
  TAO_THROW_SPEC ((CosTrading::Lookup::PolicyTypeMismatch,
		   CosTrading::Lookup::InvalidPolicyValue,
		   CosTrading::Link::IllegalLinkName,
		   CosTrading::Link::UnknownLinkName))
{
  CosTrading::FollowOption return_value = CosTrading::local_only;
  CosTrading::Link_var link
    (this->trader_.trading_components ().link_if ());

  if (link != CosTrading::Link::_nil ())
    {
      CosTrading::Link::LinkInfo_var
	link_info (link->describe_link (link_name, _env));
      TAO_CHECK_ENV_RETURN (_env, return_value);
      CosTrading::FollowOption trader_max_follow_policy = 
	this->trader_.import_attributes ().max_follow_policy ();
      
      if (this->policies_[LINK_FOLLOW_RULE] != 0)
	{
	  CosTrading::FollowOption query_link_follow_rule;
	  CosTrading::FollowOption link_limiting_follow_rule =
	    link_info->limiting_follow_rule;
	  CosTrading::Policy* policy = this->policies_[LINK_FOLLOW_RULE];
	  CosTrading::PolicyValue& value = policy->value;
	  CORBA::TypeCode* type = value.type ();

	  // Extract the link follow rule 
	  if (!type->equal (CosTrading::_tc_FollowOption, _env))
	    TAO_THROW_RETURN (CosTrading::Lookup::PolicyTypeMismatch (*policy),
			      return_value);
	  else
	    value >>= query_link_follow_rule;
	  
	  return_value = (query_link_follow_rule < trader_max_follow_policy) 
	    ? query_link_follow_rule : trader_max_follow_policy;
	  return_value = (return_value < link_limiting_follow_rule)
	    ? return_value : link_limiting_follow_rule;
	}
      else
	{
	  CosTrading::FollowOption link_def_follow_rule =
	    this->trader_.import_attributes ().max_follow_policy ();

	  return_value = (link_def_follow_rule < trader_max_follow_policy)
	    ? link_def_follow_rule : trader_max_follow_policy;
	}           
    }

  return return_value;
}

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

CosTrading::Admin::OctetSeq*
TAO_Policies::request_id (CORBA::Environment& _env)
  TAO_THROW_SPEC ((CosTrading::Lookup::PolicyTypeMismatch))
{
  CosTrading::Admin::OctetSeq* request_id = 0;
  
  if (this->policies_[REQUEST_ID] != 0)
    {
      CosTrading::Policy* policy = this->policies_[REQUEST_ID];
      CosTrading::PolicyValue& value = policy->value;
      CORBA::TypeCode* type = value.type ();
      
      if (!type->equal (CosTrading::Admin::_tc_OctetSeq_seq, _env))
	TAO_THROW_RETURN (CosTrading::Lookup::PolicyTypeMismatch (*policy),
			  request_id);
      else
	{
	  //ACE_NEW_RETURN (request_id, CosTrading::Admin::OctetSeq, 0);
	  value >>= request_id;
	}
    }

  return request_id;
}

CosTrading::PolicySeq*
TAO_Policies::policies_to_forward (void)
{
  // Create a new policy sequence, shortening the starting trader
  // policy by one link.
  
  CORBA::Environment env;
  CORBA::ULong counter = 0;
  CosTrading::Policy* policy_buffer =
    CosTrading::PolicySeq::allocbuf (REQUEST_ID + 1);
  
  if (policy_buffer == 0)
    return 0;
  
  for (int i = EXACT_TYPE_MATCH; i <= REQUEST_ID; i++)
    {
      CosTrading::Policy& new_policy = policy_buffer[counter];
      
      if (this->policies_[i] != 0)
	{
	  // Copy in the existing policies.
	  new_policy.name = this->policies_[i]->name;

	  if (i == STARTING_TRADER)
	    {
	      TAO_TRY
		{
		  // Eliminate the first link of the trader name.
		  CosTrading::TraderName* trader_name =
		    this->starting_trader (env);
		  CORBA::ULong length = trader_name->length ();
		  
		  for (int j = 1; j < length; j++)
		    trader_name[j - 1] = trader_name[j];		  
		  trader_name->length (length - 1);

		  new_policy.value <<= *trader_name;
		}
	      TAO_CATCHANY {}
	      TAO_ENDTRY;	      
	    }
	  else
	    new_policy.value = this->policies_[i]->value;
	}
    }

  // Create the new sequence
  return new CosTrading::PolicySeq (REQUEST_ID + 1, counter,
				    policy_buffer, CORBA::B_TRUE);
}

CosTrading::PolicySeq*
TAO_Policies::
policies_to_pass (CosTrading::FollowOption def_pass_on_follow_rule,
		  CORBA::ULong offers_returned,
		  CosTrading::Admin_ptr admin_if)
{
  // Create a new policy sequence to pass to a federated
  // query. Decrement the hop count, add a stem id if none exists, and 
  // add a follow rule if none exists. Also adjust the return_card to
  // account for previous queries.
  CORBA::Environment env;
  CORBA::ULong counter = 0;
  CosTrading::Policy* policy_buffer =
    CosTrading::PolicySeq::allocbuf (REQUEST_ID + 1);

  if (policy_buffer == 0)
    return 0;
  
  for (int i = EXACT_TYPE_MATCH; i <= REQUEST_ID; i++)
    {
      CosTrading::Policy& new_policy = policy_buffer[counter];
      
      if (this->policies_[i] != 0)
	{
	  // Add an existing query.
	  new_policy.name = this->policies_[i]->name;
	  new_policy.value = this->policies_[i]->value;
	}
      else
	{	  
	  if (i == LINK_FOLLOW_RULE)
	    {
	      // Add a link follow rule if one didn't exist.
	      new_policy.name = POLICY_NAMES[i];
	      new_policy.value <<= def_pass_on_follow_rule;
	    }
	  else if (i == REQUEST_ID)
	    {
	      // Add the request id if one didn't exist.
	      new_policy.name = POLICY_NAMES[i];
	      new_policy.value <<= *(admin_if->request_id_stem (env));
	    }
	}    

      if (i == HOP_COUNT)
	{
	  // Decrement the hop count.
	  new_policy.name =  POLICY_NAMES[i];
	  new_policy.value <<= this->hop_count (env) - 1;
	}
      else if (i == RETURN_CARD)
	{
	  // Adjust the return card.
	  new_policy.name =  POLICY_NAMES[i];
	  new_policy.value <<= this->return_card (env) - offers_returned;
	}
    }

  // Create the new sequence.
  return new CosTrading::PolicySeq (REQUEST_ID + 1, counter,
				    policy_buffer, CORBA::B_TRUE);
}