summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/orbsvcs/Trader/Offer_Iterators_T.cpp
blob: 8cafb6703003776efd3f4f7904e59250525b655c (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
/* -*- C++ -*- */

// ============================================================================
// $Id$
// 
// = LIBRARY
//    orbsvcs
// 
// = FILENAME
//    Offer_Iterators_T.cpp
//
// = AUTHOR
//    Marina Spivak <marina@cs.wustl.edu>
//    Seth Widoff <sbw1@cs.wustl.edu>
//    Irfan Pyarali <irfan@cs.wustl.edu>
// ============================================================================

#if !defined (TAO_OFFER_ITERATORS_T_C)
#define  TAO_OFFER_ITERATORS_T_C

#include "Offer_Iterators_T.h"

template <class TRADER>
TAO_Register_Offer_Iterator<TRADER>::
TAO_Register_Offer_Iterator (TRADER &trader,
			     const TAO_Property_Filter& pfilter)
  : TAO_Offer_Iterator (pfilter),
    trader_ (trader)
{
}

template <class TRADER>
TAO_Register_Offer_Iterator<TRADER>::~TAO_Register_Offer_Iterator (void)
{
  while (! this->offer_ids_.is_empty ())
    {
      CosTrading::OfferId offer_id = 0;
      this->offer_ids_.dequeue_head (offer_id);

      CORBA::string_free (offer_id);
    }
}

template <class TRADER> void
TAO_Register_Offer_Iterator<TRADER>::
add_offer (CosTrading::OfferId id,
	   const CosTrading::Offer* offer)
{
  this->offer_ids_.enqueue_tail (id);
}

template <class TRADER> CORBA::ULong 
TAO_Register_Offer_Iterator<TRADER>::max_left (CORBA::Environment& _env) 
  TAO_THROW_SPEC ((CORBA::SystemException, 
		  CosTrading::UnknownMaxLeft))
{
  return this->offer_ids_.size ();
}

template <class TRADER> CORBA::Boolean 
TAO_Register_Offer_Iterator<TRADER>::next_n (CORBA::ULong n, 
                                             CosTrading::OfferSeq_out offers,
					     CORBA::Environment& _env) 
  TAO_THROW_SPEC (CORBA::SystemException)
{
  offers = new CosTrading::OfferSeq;

  CORBA::ULong ret_offers = 0;
  
  // Get service type map (monitor object).
  TRADER::Offer_Database &offer_database =
    this->trader_.offer_database ();

  CORBA::ULong max_possible_offers_in_sequence =
    (n <  this->offer_ids_.size ()) ? n : this->offer_ids_.size ();
  offers->length (max_possible_offers_in_sequence);  
  
  // While there are entries left and we haven't filled <offers>
  // with requested number.
  while (! this->offer_ids_.is_empty () 
	 && n > ret_offers)
    {
      // If offer is found, put it into the sequence.	  
      // remove this id irrespective of whether the offer is found
      // or not.
      CosTrading::OfferId id;
      this->offer_ids_.dequeue_head (id);
      
      TAO_TRY
	{
	  CosTrading::OfferId_var offerid_var (id);
	  CosTrading::Offer* offer =
	    offer_database.lookup_offer (id, TAO_TRY_ENV);
	  TAO_CHECK_ENV;
	  
	  if (offer != 0)
	    {
	      CosTrading::Offer& destination = (*offers)[ret_offers++];
	      this->pfilter_.filter_offer (*offer, destination);
	    }
	}
      TAO_CATCHANY {}
      TAO_ENDTRY;      
    }
  
  // Reset the length to the correct value
  offers->length (ret_offers);

  return (CORBA::Boolean)(ret_offers != 0);
}

#endif /* TAO_REGISTER_OFFER_ITERATOR_C */