TAO_Offer_Database< LOCK_TYPE > Class Template Reference

#include <Offer_Database.h>

Collaboration diagram for TAO_Offer_Database< LOCK_TYPE >:

Collaboration graph
[legend]
List of all members.

Public Types

typedef TAO_Service_Offer_Iterator<
LOCK_TYPE > 
offer_iterator
typedef ACE_Hash_Map_Manager_Ex<
CORBA::String_var, Offer_Map_Entry *,
ACE_Hash< CORBA::String_var >,
ACE_Equal_To< CORBA::String_var >,
ACE_Null_Mutex
Offer_Database

Public Member Functions

 TAO_Offer_Database (void)
 No arg constructor.
 ~TAO_Offer_Database (void)
CosTrading::OfferId insert_offer (const char *type, CosTrading::Offer *offer)
int remove_offer (const CosTrading::OfferId offer_id)
CosTrading::Offerlookup_offer (const CosTrading::OfferId offer_id)
CosTrading::Offerlookup_offer (const CosTrading::OfferId offer_id, char *&type_name)
TAO_Offer_Id_Iteratorretrieve_all_offer_ids (void)

Private Member Functions

CosTrading::Offerlookup_offer (const char *type, CORBA::ULong id)
int remove_offer (const char *type, CORBA::ULong id)
void operator= (const TAO_Offer_Database< LOCK_TYPE > &)
 TAO_Offer_Database (const TAO_Offer_Database< LOCK_TYPE > &)

Static Private Member Functions

static CosTrading::OfferId generate_offer_id (const char *type_name, CORBA::ULong id)
static void parse_offer_id (const CosTrading::OfferId offer_id, char *&service_type, CORBA::ULong &id)

Private Attributes

LOCK_TYPE db_lock_
Offer_Database offer_db_

Friends

class TAO_Service_Offer_Iterator< LOCK_TYPE >

Classes

struct  Offer_Map_Entry

Detailed Description

template<class LOCK_TYPE>
class TAO_Offer_Database< LOCK_TYPE >

The TAO_Offer_Database encapsulates the mapping of service types to those offers exported with that service types. The underlying structure is a map of maps. The first maps maps the service type name to a map of exported offers. The second map maps the identifying index for that offer within the service types. So a service type name and an index uniquely identifies an exported offer. In fact, when the register export interface returns a CosTrading::OfferId, it's returning no more than a simple string concatenation of these two values. In addition to all these wonderful things, the TAO_Offer_Database has built-in locking, one reader/writer-style lock for modifying the top-level map and a reader/writer-style for each of the offer maps. Needless to say the locks are acquired when the TAO_Offer_Database performs operations on the structures they guard. NOTE: TAO_Offer_Database needs to be parameterized by a READER/WRITER LOCK, a RECURSIVE MUTEX, or a NULL MUTEX, not a simple binary mutex! Mutexes will cause deadlock when you try to contruct an iterator (which acquires a read lock on the map under an existing read lock). Just don't do it, ok?

Definition at line 51 of file Offer_Database.h.


Member Typedef Documentation

template<class LOCK_TYPE>
typedef ACE_Hash_Map_Manager_Ex< CORBA::String_var, Offer_Map_Entry*, ACE_Hash<CORBA::String_var>, ACE_Equal_To<CORBA::String_var>, ACE_Null_Mutex > TAO_Offer_Database< LOCK_TYPE >::Offer_Database

Definition at line 102 of file Offer_Database.h.

template<class LOCK_TYPE>
typedef TAO_Service_Offer_Iterator<LOCK_TYPE> TAO_Offer_Database< LOCK_TYPE >::offer_iterator

Definition at line 57 of file Offer_Database.h.


Constructor & Destructor Documentation

template<class LOCK_TYPE>
TAO_BEGIN_VERSIONED_NAMESPACE_DECL TAO_Offer_Database< LOCK_TYPE >::TAO_Offer_Database ( void   ) 

No arg constructor.

Definition at line 13 of file Offer_Database.cpp.

00014 {
00015 }

template<class LOCK_TYPE>
TAO_Offer_Database< LOCK_TYPE >::~TAO_Offer_Database ( void   ) 

Definition at line 18 of file Offer_Database.cpp.

References ACE_WRITE_GUARD.

00019 {
00020   ACE_WRITE_GUARD (LOCK_TYPE, ace_mon, this->db_lock_);
00021 
00022   for (typename Offer_Database::iterator type_iter (this->offer_db_);
00023        ! type_iter.done ();
00024        type_iter++)
00025     {
00026       Offer_Map_Entry* offer_map_entry = (*type_iter).int_id_;
00027 
00028       {
00029         // Artificial scope, so the guard will release the lock before
00030         // we delete the lock along with the offer_map_entry.
00031         ACE_WRITE_GUARD (LOCK_TYPE, ace_mon, offer_map_entry->lock_);
00032 
00033         for (TAO_Offer_Map::iterator offer_iter (*offer_map_entry->offer_map_);
00034              ! offer_iter.done ();
00035              offer_iter++)
00036           {
00037             // Delete all the offers in the offer map.
00038             CosTrading::Offer* offer = (*offer_iter).int_id_;
00039             delete offer;
00040           }
00041 
00042         delete offer_map_entry->offer_map_;
00043       }
00044 
00045       delete offer_map_entry;
00046     }
00047 }

template<class LOCK_TYPE>
TAO_Offer_Database< LOCK_TYPE >::TAO_Offer_Database ( const TAO_Offer_Database< LOCK_TYPE > &   )  [private]


Member Function Documentation

template<class LOCK_TYPE>
CosTrading::OfferId TAO_Offer_Database< LOCK_TYPE >::generate_offer_id ( const char *  type_name,
CORBA::ULong  id 
) [static, private]

Take in a service type name for the offer the current value of of the counter and generate an offer id.

Definition at line 285 of file Offer_Database.cpp.

References ACE_OS::sprintf(), CORBA::string_alloc(), CORBA::string_dup(), and ACE_OS::strlen().

Referenced by TAO_Service_Offer_Iterator< LOCK_TYPE >::get_id(), and TAO_Offer_Database< LOCK_TYPE >::insert_offer().

00286 {
00287   // holds final id string.
00288   size_t total_size = 16 + ACE_OS::strlen (service_type_name);
00289 
00290   // hold portions of id string.
00291   CosTrading::OfferId offer_id =
00292      CORBA::string_alloc (static_cast<CORBA::ULong> (total_size));
00293   char* return_value = 0;
00294   ACE_OS::sprintf (offer_id, "%016u%s", id, service_type_name);
00295 
00296   return_value = CORBA::string_dup (offer_id);
00297   delete [] offer_id;
00298 
00299   return return_value;
00300 }

template<class LOCK_TYPE>
CosTrading::OfferId TAO_Offer_Database< LOCK_TYPE >::insert_offer ( const char *  type,
CosTrading::Offer offer 
)

Add an offer of type <type> and generate a CosTrading::OfferId for it. Returns 0 on failure.

Definition at line 51 of file Offer_Database.cpp.

References ACE_NEW_RETURN, ACE_READ_GUARD_RETURN, ACE_WRITE_GUARD_RETURN, ACE_Hash_Map_Manager_Ex< EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK >::bind(), TAO_Offer_Database< LOCK_TYPE >::Offer_Map_Entry::counter_, TAO_Offer_Database< LOCK_TYPE >::generate_offer_id(), TAO_Offer_Database< LOCK_TYPE >::Offer_Map_Entry::lock_, TAO_Offer_Database< LOCK_TYPE >::offer_db_, and TAO_Offer_Database< LOCK_TYPE >::Offer_Map_Entry::offer_map_.

Referenced by TAO_Register< TRADER_LOCK_TYPE, MAP_LOCK_TYPE >::_cxx_export().

00052 {
00053   CosTrading::OfferId return_value = 0;
00054   typename Offer_Database::ENTRY* database_entry = 0;
00055   CORBA::String_var service_type (type);
00056 
00057   ACE_READ_GUARD_RETURN (LOCK_TYPE, ace_mon, this->db_lock_, 0);
00058 
00059   if (this->offer_db_.find (service_type, database_entry) == -1)
00060     {
00061       // If there's no entry for the service type, create one.
00062 
00063       Offer_Map_Entry* new_offer_map_entry = 0;
00064       ACE_NEW_RETURN (new_offer_map_entry, Offer_Map_Entry, 0);
00065       ACE_NEW_RETURN (new_offer_map_entry->offer_map_, TAO_Offer_Map, 0);
00066       new_offer_map_entry->counter_ = 1;
00067 
00068       if (this->db_lock_.release () == -1)
00069         return 0;
00070       else
00071         {
00072           // Add the new entry; upgrade lock.
00073           ACE_WRITE_GUARD_RETURN (LOCK_TYPE, ace_mon, this->db_lock_, 0);
00074           this->offer_db_.bind (service_type,
00075                                 new_offer_map_entry,
00076                                 database_entry);
00077         }
00078 
00079       if (this->db_lock_.acquire_read () == -1)
00080         return 0;
00081     }
00082 
00083   Offer_Map_Entry* offer_map_entry =
00084     database_entry->int_id_;
00085   ACE_WRITE_GUARD_RETURN (LOCK_TYPE, ace_mon2, offer_map_entry->lock_, 0);
00086 
00087   // Add the offer to the service offer table for this service type.
00088   offer_map_entry->offer_map_->bind (offer_map_entry->counter_,
00089                                      offer);
00090   return_value = this->generate_offer_id (type,
00091                                           offer_map_entry->counter_);
00092   offer_map_entry->counter_++;
00093 
00094   return return_value;
00095 }

template<class LOCK_TYPE>
CosTrading::Offer * TAO_Offer_Database< LOCK_TYPE >::lookup_offer ( const char *  type,
CORBA::ULong  id 
) [private]

Lookup an offer whose type is <type> and id, <id>. Return 0 on failure.

Definition at line 203 of file Offer_Database.cpp.

References ACE_READ_GUARD_RETURN.

00204 {
00205   ACE_READ_GUARD_RETURN (LOCK_TYPE, ace_mon, this->db_lock_, 0);
00206 
00207   CosTrading::Offer* return_value = 0;
00208   typename Offer_Database::ENTRY* db_entry = 0;
00209   CORBA::String_var service_type (type);
00210 
00211   if (this->offer_db_.find (service_type, db_entry) == 0)
00212     {
00213       Offer_Map_Entry* offer_map_entry = db_entry->int_id_;
00214       ACE_READ_GUARD_RETURN (LOCK_TYPE, ace_mon, offer_map_entry->lock_, 0);
00215 
00216       TAO_Offer_Map::ENTRY* offer_entry_ptr = 0;
00217       if (offer_map_entry->offer_map_->find (id, offer_entry_ptr) == 0)
00218         return_value = offer_entry_ptr->int_id_;
00219     }
00220 
00221   return return_value;
00222 }

template<class LOCK_TYPE>
CosTrading::Offer * TAO_Offer_Database< LOCK_TYPE >::lookup_offer ( const CosTrading::OfferId  offer_id,
char *&  type_name 
)

Lookup an offer whose OfferId is <offer_id> and return in <type_name> the type name of the object. Type name is just a pointer to a location in offer_id, so DON'T DELETE IT.

Definition at line 171 of file Offer_Database.cpp.

References TAO_Offer_Database< LOCK_TYPE >::parse_offer_id().

00173 {
00174   CORBA::ULong index;
00175   CosTrading::Offer* offer = 0;
00176   this->parse_offer_id (offer_id, type_name, index);
00177 
00178   if ((offer = this->lookup_offer (type_name, index)) == 0)
00179     throw CosTrading::UnknownOfferId (offer_id);
00180 
00181   return offer;
00182 }

template<class LOCK_TYPE>
CosTrading::Offer * TAO_Offer_Database< LOCK_TYPE >::lookup_offer ( const CosTrading::OfferId  offer_id  ) 

Lookup an offer whose offer_id is <offer_id>, and return it. Otherwise, throw the appropriate exception.

Definition at line 187 of file Offer_Database.cpp.

References TAO_Offer_Database< LOCK_TYPE >::parse_offer_id().

Referenced by TAO_Register< TRADER_LOCK_TYPE, MAP_LOCK_TYPE >::describe(), and TAO_Register_Offer_Iterator< MAP_LOCK_TYPE >::next_n().

00188 {
00189   char* type_name;
00190   CORBA::ULong index;
00191   CosTrading::Offer* offer = 0;
00192 
00193   this->parse_offer_id (offer_id, type_name, index);
00194 
00195   if ((offer = this->lookup_offer (type_name, index)) == 0)
00196     throw CosTrading::UnknownOfferId (offer_id);
00197 
00198   return offer;
00199 }

template<class LOCK_TYPE>
void TAO_Offer_Database< LOCK_TYPE >::operator= ( const TAO_Offer_Database< LOCK_TYPE > &   )  [private]

template<class LOCK_TYPE>
void TAO_Offer_Database< LOCK_TYPE >::parse_offer_id ( const CosTrading::OfferId  offer_id,
char *&  service_type,
CORBA::ULong id 
) [static, private]

Take in a previously generated offer id and return the type and id that were used to generate the offer id.

Definition at line 263 of file Offer_Database.cpp.

References ACE_OS::atoi(), and TAO_Trader_Base::is_valid_identifier_name().

Referenced by TAO_Offer_Database< LOCK_TYPE >::lookup_offer(), and TAO_Offer_Database< LOCK_TYPE >::remove_offer().

00266 {
00267   // Get service type: it is everything from 17th character to the end.
00268   service_type = (char *) offer_id + 16;
00269 
00270   // Get id: temporarily put the end of string character where the service
00271   // type starts, convert to number, replace the character back.
00272   char* start_of_type =  (char *) (offer_id + 16);
00273   char temp = *start_of_type;
00274   *start_of_type = '\0';
00275   id = ACE_OS::atoi (offer_id);
00276 
00277   *start_of_type = temp;
00278 
00279   if (! TAO_Trader_Base::is_valid_identifier_name (service_type))
00280     throw CosTrading::IllegalOfferId (offer_id);
00281 }

template<class LOCK_TYPE>
int TAO_Offer_Database< LOCK_TYPE >::remove_offer ( const char *  type,
CORBA::ULong  id 
) [private]

Remove an offers whose id is <offer_id>. Returns 0 on success, -1 on failure, and throws a CosTrading::IllegalOfferId if it can't parse the CosTrading::OfferId.

Definition at line 99 of file Offer_Database.cpp.

References ACE_READ_GUARD_RETURN, ACE_Hash_Map_Manager_Ex< EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK >::current_size(), TAO_Offer_Database< LOCK_TYPE >::Offer_Map_Entry::lock_, TAO_Offer_Database< LOCK_TYPE >::offer_db_, TAO_Offer_Database< LOCK_TYPE >::Offer_Map_Entry::offer_map_, and ACE_Hash_Map_Manager_Ex< EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK >::unbind().

00100 {
00101   ACE_READ_GUARD_RETURN (LOCK_TYPE, ace_mon, this->db_lock_, -1);
00102 
00103   int return_value = -1;
00104   typename Offer_Database::ENTRY* db_entry = 0;
00105   CORBA::String_var service_type (type);
00106 
00107   if (this->offer_db_.find (service_type, db_entry) == 0)
00108     {
00109       CosTrading::Offer* offer = 0;
00110       Offer_Map_Entry* offer_map_entry = db_entry->int_id_;
00111 
00112       if (offer_map_entry->lock_.acquire_write () == -1)
00113         return -1;
00114 
00115       return_value = offer_map_entry->offer_map_->unbind (id, offer);
00116       delete offer;
00117 
00118       // If the service type has no more offers, free the map, lest
00119       // the memory forever persist.
00120       if (offer_map_entry->offer_map_->current_size () == 0)
00121         {
00122           if (this->db_lock_.release () == -1)
00123             return -1;
00124           else
00125             {
00126               // Promote the database lock. Will be released by
00127               // guard.
00128               if (this->db_lock_.acquire_write () == -1)
00129                 return -1;
00130 
00131               // Unbind the service type from the database.
00132               this->offer_db_.unbind (service_type);
00133 
00134               // Now that the type has been removed, we can release
00135               // its lock.
00136               if (offer_map_entry->lock_.release () == -1)
00137                 return -1;
00138 
00139               // Delete the database resources for this type.
00140               delete offer_map_entry->offer_map_;
00141               delete offer_map_entry;
00142             }
00143         }
00144       else if (offer_map_entry->lock_.release () == -1)
00145         return -1;
00146     }
00147 
00148   return return_value;
00149 }

template<class LOCK_TYPE>
int TAO_Offer_Database< LOCK_TYPE >::remove_offer ( const CosTrading::OfferId  offer_id  ) 

Definition at line 153 of file Offer_Database.cpp.

References TAO_Offer_Database< LOCK_TYPE >::parse_offer_id().

Referenced by TAO_Register< TRADER_LOCK_TYPE, MAP_LOCK_TYPE >::withdraw(), and TAO_Register< TRADER_LOCK_TYPE, MAP_LOCK_TYPE >::withdraw_using_constraint().

00154 {
00155   char* stype = 0;
00156   CORBA::ULong index;
00157 
00158   this->parse_offer_id (offer_id,
00159                         stype,
00160                         index);
00161 
00162   if (this->remove_offer (stype,
00163                           index) == -1)
00164     throw CosTrading::UnknownOfferId (offer_id);
00165 
00166   return 0;
00167 }

template<class LOCK_TYPE>
TAO_Offer_Id_Iterator * TAO_Offer_Database< LOCK_TYPE >::retrieve_all_offer_ids ( void   ) 

Return an iterator that will traverse and return all the offer ids in the service type map.

Definition at line 225 of file Offer_Database.cpp.

References ACE_NEW_RETURN, ACE_READ_GUARD_RETURN, TAO_Offer_Id_Iterator::insert_id(), TAO_Offer_Database< LOCK_TYPE >::Offer_Map_Entry::lock_, and TAO_Offer_Database< LOCK_TYPE >::Offer_Map_Entry::offer_map_.

Referenced by TAO_Admin< TRADER_LOCK_TYPE, MAP_LOCK_TYPE >::list_offers().

00226 {
00227   // Fill an TAO_Offer_Id_Iterator with the offer_ids of all offers
00228   // exported to the TAO_Offer_Database. Iterates through the entire
00229   // map, cramming offer_id strings into a newly constructed
00230   // TAO_Offer_Id_Iterator.
00231   TAO_Offer_Id_Iterator* id_iterator;
00232   ACE_NEW_RETURN (id_iterator,
00233                   TAO_Offer_Id_Iterator (),
00234                   0);
00235   ACE_READ_GUARD_RETURN (LOCK_TYPE, ace_mon, this->db_lock_, 0);
00236 
00237   for (typename Offer_Database::iterator type_iter (this->offer_db_);
00238        ! type_iter.done ();
00239        type_iter++)
00240     {
00241       const char* type_name = (*type_iter).ext_id_.in ();
00242       Offer_Map_Entry* offer_map_entry = (*type_iter).int_id_;
00243 
00244       ACE_READ_GUARD_RETURN (LOCK_TYPE, ace_mon, offer_map_entry->lock_, 0);
00245 
00246       for (TAO_Offer_Map::iterator offer_iter (*offer_map_entry->offer_map_);
00247            ! offer_iter.done ();
00248            offer_iter++)
00249         {
00250           CORBA::ULong offer_index = (*offer_iter).ext_id_;
00251           CosTrading::OfferId offer_id =
00252             this->generate_offer_id (type_name, offer_index);
00253 
00254           id_iterator->insert_id (offer_id);
00255         }
00256     }
00257 
00258   return id_iterator;
00259 }


Friends And Related Function Documentation

template<class LOCK_TYPE>
friend class TAO_Service_Offer_Iterator< LOCK_TYPE > [friend]

Definition at line 53 of file Offer_Database.h.


Member Data Documentation

template<class LOCK_TYPE>
LOCK_TYPE TAO_Offer_Database< LOCK_TYPE >::db_lock_ [private]

Definition at line 138 of file Offer_Database.h.

template<class LOCK_TYPE>
Offer_Database TAO_Offer_Database< LOCK_TYPE >::offer_db_ [private]

Definition at line 140 of file Offer_Database.h.

Referenced by TAO_Offer_Database< LOCK_TYPE >::insert_offer(), and TAO_Offer_Database< LOCK_TYPE >::remove_offer().


The documentation for this class was generated from the following files:
Generated on Tue Feb 2 17:49:33 2010 for TAO_CosTrader by  doxygen 1.4.7