Offer_Database.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 //=============================================================================
00004 /**
00005  *  @file    Offer_Database.h
00006  *
00007  *  Offer_Database.h,v 1.22 2006/04/25 11:35:29 jwillemsen Exp
00008  *
00009  *  @author Seth Widoff <sbw1@cs.wustl.edu>
00010  */
00011 //=============================================================================
00012 
00013 
00014 #ifndef TAO_OFFER_DATABASE_H
00015 #define TAO_OFFER_DATABASE_H
00016 #include /**/ "ace/pre.h"
00017 
00018 #include "orbsvcs/Trader/Trader.h"
00019 #include "orbsvcs/Trader/Offer_Iterators.h"
00020 #include "ace/Null_Mutex.h"
00021 
00022 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00023 
00024 template <class LOCK_TYPE> class TAO_Service_Offer_Iterator;
00025 
00026 /**
00027  * @class TAO_Offer_Database
00028  *
00029  * The TAO_Offer_Database encapsulates the mapping of service
00030  * types to those offers exported with that service types. The
00031  * underlying structure is a map of maps. The first maps maps the
00032  * service type name to a map of exported offers. The second map
00033  * maps the identifying index for that offer within the service
00034  * types. So a service type name and an index uniquely identifies an
00035  * exported offer. In fact, when the register export interface
00036  * returns a CosTrading::OfferId, it's returning no more than a
00037  * simple string concatenation of these two values. In addition to
00038  * all these wonderful things, the TAO_Offer_Database has built-in
00039  * locking, one reader/writer-style lock for modifying the top-level
00040  * map and a reader/writer-style for each of the offer
00041  * maps. Needless to say the locks are acquired when the
00042  * TAO_Offer_Database performs operations on the structures they
00043  * guard.
00044  * NOTE: TAO_Offer_Database needs to be parameterized by a
00045  * READER/WRITER LOCK, a RECURSIVE MUTEX, or a NULL MUTEX, not a
00046  * simple binary mutex! Mutexes will cause deadlock when you try to
00047  * contruct an iterator (which acquires a read lock on the map under
00048  * an existing read lock). Just don't do it, ok?
00049  */
00050 template <class LOCK_TYPE>
00051 class TAO_Offer_Database
00052 {
00053   friend class TAO_Service_Offer_Iterator<LOCK_TYPE>;
00054 public:
00055 
00056   // Traits
00057   typedef TAO_Service_Offer_Iterator<LOCK_TYPE> offer_iterator;
00058 
00059   /// No arg constructor.
00060   TAO_Offer_Database (void);
00061 
00062   ~TAO_Offer_Database (void);
00063 
00064   /// Add an offer of type <type> and generate a CosTrading::OfferId
00065   /// for it. Returns 0 on failure.
00066   CosTrading::OfferId insert_offer (const char* type,
00067                                     CosTrading::Offer* offer);
00068 
00069   int remove_offer (const CosTrading::OfferId offer_id
00070                     ACE_ENV_ARG_DECL)
00071     ACE_THROW_SPEC ((CosTrading::IllegalOfferId,
00072                     CosTrading::UnknownOfferId));
00073 
00074   /// Lookup an offer whose offer_id is <offer_id>, and return
00075   /// it. Otherwise, throw the appropriate exception.
00076   CosTrading::Offer* lookup_offer (const CosTrading::OfferId offer_id
00077                                    ACE_ENV_ARG_DECL)
00078     ACE_THROW_SPEC ((CosTrading::IllegalOfferId,
00079                     CosTrading::UnknownOfferId));
00080 
00081   /**
00082    * Lookup an offer whose OfferId is <offer_id> and return in
00083    * <type_name> the type name of the object. Type name is just a
00084    * pointer to a location in offer_id, so DON'T DELETE IT.
00085    */
00086   CosTrading::Offer* lookup_offer (const CosTrading::OfferId offer_id,
00087                                    char*& type_name
00088                                    ACE_ENV_ARG_DECL)
00089     ACE_THROW_SPEC ((CosTrading::IllegalOfferId,
00090                     CosTrading::UnknownOfferId));
00091 
00092   /// Return an iterator that will traverse and return all the offer
00093   /// ids in the service type map.
00094   TAO_Offer_Id_Iterator* retrieve_all_offer_ids (void);
00095 
00096   struct Offer_Map_Entry
00097   {
00098     TAO_Offer_Map* offer_map_;
00099     CORBA::ULong counter_;
00100     LOCK_TYPE lock_;
00101   };
00102 
00103   typedef ACE_Hash_Map_Manager_Ex
00104     <
00105     CORBA::String_var,
00106     Offer_Map_Entry*,
00107     ACE_Hash<CORBA::String_var>,
00108     ACE_Equal_To<CORBA::String_var>,
00109     ACE_Null_Mutex
00110     >
00111     Offer_Database;
00112 
00113 private:
00114 
00115   // The internal id is a pointer here, not only to avoid copying,
00116   // since we would only copy on insertion, and we only insert once
00117   // --- with an empty Offer_Map_Entry --- but also since most locks
00118   // have unimplemented copy constructors.
00119 
00120   /// Lookup an offer whose type is <type> and id, <id>. Return 0 on
00121   /// failure.
00122   CosTrading::Offer* lookup_offer (const char* type,
00123                                    CORBA::ULong id);
00124 
00125   /**
00126    * Remove an offers whose id is <offer_id>. Returns 0 on success, -1
00127    * on failure, and throws a CosTrading::IllegalOfferId if it can't
00128    * parse the CosTrading::OfferId.
00129    */
00130   int remove_offer (const char* type, CORBA::ULong id);
00131 
00132   /// Take in a service type name for the offer the current value of
00133   /// of the counter and generate an offer id.
00134   static CosTrading::OfferId generate_offer_id (const char *type_name,
00135                                                 CORBA::ULong id);
00136 
00137   /// Take in a previously generated offer id and return the type
00138   /// and id that were used to generate the offer id.
00139   static void parse_offer_id (const CosTrading::OfferId offer_id,
00140                               char* &service_type,
00141                               CORBA::ULong& id
00142                               ACE_ENV_ARG_DECL)
00143     ACE_THROW_SPEC ((CosTrading::IllegalOfferId));
00144 
00145   // = Disallow these operations.
00146   ACE_UNIMPLEMENTED_FUNC (void operator= (const TAO_Offer_Database<LOCK_TYPE> &))
00147   ACE_UNIMPLEMENTED_FUNC (TAO_Offer_Database (const TAO_Offer_Database<LOCK_TYPE> &))
00148 
00149   LOCK_TYPE db_lock_;
00150 
00151   Offer_Database offer_db_;
00152   // The protected data structure.
00153 };
00154 
00155 /**
00156  * @class TAO_Service_Offer_Iterator
00157  *
00158  * @brief TAO_Service_Offer_Iterator iterates over the set of exported
00159  * offers for a given type. Handily, it takes care of all the
00160  * necessary locking, acquiring them in the constructor, and
00161  * releasing them in the destructor.
00162  */
00163 template <class LOCK_TYPE>
00164 class TAO_Service_Offer_Iterator
00165 {
00166  public:
00167 
00168   typedef TAO_Offer_Database<LOCK_TYPE> Offer_Database;
00169 
00170   TAO_Service_Offer_Iterator (const char* type,
00171                               TAO_Offer_Database<LOCK_TYPE>& offer_database);
00172 
00173   /// Release all the locks acquired.
00174   ~TAO_Service_Offer_Iterator (void);
00175 
00176   /// Returns 1 if there are more offers, 0 otherwise.
00177   int has_more_offers (void);
00178 
00179   /// Get the id for the current offer.
00180   CosTrading::OfferId get_id (void);
00181 
00182   /// Returns the next offer in the series.
00183   CosTrading::Offer* get_offer (void);
00184 
00185   /// Advances the iterator 1.
00186   void next_offer (void);
00187 
00188  private:
00189   // Protected constructor.
00190 
00191   /// Lock the top_level map.
00192   TAO_Offer_Database<LOCK_TYPE>& stm_;
00193 
00194   /// Lock for the internal map.
00195   LOCK_TYPE* lock_;
00196 
00197   /// Iterator over the actual offer map.
00198   TAO_Offer_Map::iterator* offer_iter_;
00199 
00200   /// The name of the type. Used for constructing offer ids.
00201   const char* type_;
00202 };
00203 
00204 TAO_END_VERSIONED_NAMESPACE_DECL
00205 
00206 #if defined (ACE_TEMPLATES_REQUIRE_SOURCE)
00207 #include "orbsvcs/Trader/Offer_Database.cpp"
00208 #endif  /* ACE_TEMPLATES_REQUIRE_SOURCE */
00209 
00210 #include /**/ "ace/post.h"
00211 #endif /* TAO_SERVICE_TYPE_MAP_H */

Generated on Thu Nov 9 13:59:57 2006 for TAO_CosTrader by doxygen 1.3.6