Persistent_Naming_Context.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 //=============================================================================
00004 /**
00005  *  @file   Persistent_Naming_Context.h
00006  *
00007  *  $Id: Persistent_Naming_Context.h 76589 2007-01-25 18:04:11Z elliott_c $
00008  *
00009  *  @author Marina Spivak <marina@cs.wustl.edu>
00010  */
00011 //=============================================================================
00012 
00013 
00014 #ifndef TAO_PERSISTENT_NAMING_CONTEXT_H
00015 #define TAO_PERSISTENT_NAMING_CONTEXT_H
00016 #include /**/ "ace/pre.h"
00017 
00018 #include "orbsvcs/Naming/Hash_Naming_Context.h"
00019 #include "orbsvcs/Naming/Persistent_Entries.h"
00020 #include "orbsvcs/Naming/naming_serv_export.h"
00021 
00022 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00023 
00024 /**
00025  * @class TAO_Persistent_Bindings_Map
00026  *
00027  * @brief Provides hash-table-based persistent storage for
00028  * name to object  bindings in a Naming Context.
00029  *
00030  * Wrapper on top of ACE_Hash_Map_With_Allocator (which is a wrapper
00031  * around ACE_Hash_Map_Manager).  Uses ACE_Allocator (allocating
00032  * from persistent storage) to make bindings persistent and
00033  * supports TAO_Bindings_Map interface.  Used by TAO_Persistent_Naming_Context.
00034  */
00035 class TAO_Naming_Serv_Export TAO_Persistent_Bindings_Map
00036   : public TAO_Bindings_Map
00037 {
00038 public:
00039 
00040   /// Underlying data structure - typedef for ease of use.
00041   typedef ACE_Hash_Map_With_Allocator<TAO_Persistent_ExtId, TAO_Persistent_IntId> HASH_MAP;
00042 
00043   // = Initialization and termination methods.
00044 
00045   /// Constructor.
00046   TAO_Persistent_Bindings_Map (CORBA::ORB_ptr orb);
00047 
00048   /// Allocate hash map of size <hash_map_size> from persistent storage
00049   /// using the <alloc>.
00050   int open (size_t hash_map_size,
00051             ACE_Allocator *alloc);
00052 
00053   /// The hash map has already been preallocated for us.  We just need
00054   /// to set our data members take ownership of it.
00055   void set (HASH_MAP *map,
00056             ACE_Allocator *alloc);
00057 
00058   /// Destructor.  Does not deallocate the hash map: if an instance of
00059   /// this class goes out of scope, its hash_map remains in persistent storage.
00060   virtual ~TAO_Persistent_Bindings_Map (void);
00061 
00062   /**
00063    * This method removes the hash map from persistent storage/frees up
00064    * the memory.  The hash map better be empty, since we are not
00065    * cleaning up the insides. (We could add <close> to clean entries,
00066    * but not the data inside the entries.
00067    */
00068   void destroy (void);
00069 
00070   // = Accessor methods.
00071 
00072   /// Get a pointer to the underlying hash map.
00073   HASH_MAP *map (void);
00074 
00075   /// Return the size of the underlying hash table.
00076   size_t total_size (void);
00077 
00078   /// Return the size of the underlying hash table.
00079   virtual size_t current_size (void);
00080 
00081   // = Name bindings manipulation methods.
00082 
00083   /**
00084    * Add a binding with the specified parameters to the table.
00085    * Return 0 on success and -1 on failure, 1 if there already is a
00086    * binding with <id> and <kind>.
00087    */
00088   virtual int bind (const char *id,
00089                     const char *kind,
00090                     CORBA::Object_ptr obj,
00091                     CosNaming::BindingType type);
00092 
00093   /**
00094    * Overwrite a binding containing <id> and <kind> (or create a new
00095    * one if one doesn't exist) with the specified parameters.  Return
00096    * 0 or 1 on success.  Return -1 or -2 on failure. (-2 is returned
00097    * if the new and old bindings differ in type).
00098    */
00099   virtual int rebind (const char *id,
00100                       const char *kind,
00101                       CORBA::Object_ptr obj,
00102                       CosNaming::BindingType type);
00103 
00104   /**
00105    * Remove a binding containing <id> and <kind> from the table.
00106    * Return 0 on success and -1 on failure.
00107    */
00108   virtual int unbind (const char * id,
00109                       const char * kind);
00110 
00111   /**
00112    * Find the binding containing <id> and <kind> in the table, and
00113    * pass binding's type and object back to the caller by reference.
00114    * Return 0 on success and -1 on failure.  Note: a 'duplicated' object
00115    * reference is assigned to <obj>, so the caller is responsible for
00116    * its deallocation.
00117    */
00118   virtual int find (const char * id,
00119                     const char * kind,
00120                     CORBA::Object_ptr & obj,
00121                     CosNaming::BindingType &type);
00122 
00123 protected:
00124 
00125   /**
00126    * Helper to the <open> method.  By isolating placement new into a
00127    * separate method, we can deal with memory allocation failures more
00128    * efficiently.  If there is a problem in HASH_MAP constructor, we
00129    * can clean up preallocated space.
00130    */
00131   int open_helper (size_t hash_table_size,
00132                    void *buffer);
00133 
00134   /// Helper: factors common code from <bind> and <rebind>.
00135   int shared_bind (const char *id,
00136                    const char *kind,
00137                    CORBA::Object_ptr obj,
00138                    CosNaming::BindingType type,
00139                    int rebind);
00140 
00141   /// Pointer to the allocator we use to make bindings persistent.
00142   ACE_Allocator *allocator_;
00143 
00144   /// Pointer to the underlying hash map.
00145   HASH_MAP *map_;
00146 
00147   /// Pointer to the orb.  We need it to do string/object conversions.
00148   CORBA::ORB_var orb_;
00149 };
00150 
00151 class TAO_Persistent_Context_Index;
00152 
00153 /**
00154  * @class TAO_Persistent_Naming_Context
00155  *
00156  * @brief This class plays a role of a 'ConcreteImplementor' in the
00157  * Bridge pattern architecture of the CosNaming::NamingContext implementation.
00158  *
00159  * This class provides a persistent implementation of the
00160  * NamingContext functionality, i.e., the state is preserved across
00161  * process boundaries.  Derives from TAO_Hash_Naming_Context and
00162  * uses TAO_Persistent_Bindings_Map to store name to object bindings.
00163  */
00164 class TAO_Naming_Serv_Export TAO_Persistent_Naming_Context : public TAO_Hash_Naming_Context
00165 {
00166 public:
00167   /// Underlying data structure - typedef for ease of use.
00168   typedef TAO_Persistent_Bindings_Map::HASH_MAP HASH_MAP;
00169 
00170   // = Initialization and termination methods.
00171 
00172   /// Constructor.  MUST be followed up by <init> to allocate the
00173   /// underlying data structure from persistent storage!
00174   TAO_Persistent_Naming_Context (PortableServer::POA_ptr poa,
00175                                  const char *poa_id,
00176                                  TAO_Persistent_Context_Index *context_index);
00177 
00178   /// Allocate the underlying data structure from persistent storage.
00179   /// Returns 0 on success and -1 on failure.
00180   int init (size_t hash_table_size = ACE_DEFAULT_MAP_SIZE);
00181 
00182   /**
00183    * Constructor that takes in preallocated data structure and takes
00184    * ownership of it.  This constructor is for 'recreating' servants
00185    * from persistent state.
00186    */
00187   TAO_Persistent_Naming_Context (PortableServer::POA_ptr poa,
00188                                  const char *poa_id,
00189                                  TAO_Persistent_Context_Index *context_index,
00190                                  HASH_MAP * map,
00191                                  ACE_UINT32 *counter);
00192 
00193   /// Destructor.
00194   virtual ~TAO_Persistent_Naming_Context (void);
00195 
00196   // = Utility methods.
00197   /**
00198    * This utility method factors out the code needed to create a new
00199    * Persistent Naming Context servant and activate it under the
00200    * specified POA with the specified id.  This function is static so
00201    * that the code can be used, both from inside the class (e.g., <new_context>),
00202    * and from outside (e.g., Naming_Utils.cpp).
00203    */
00204   static CosNaming::NamingContext_ptr make_new_context (PortableServer::POA_ptr poa,
00205                                                         const char *poa_id,
00206                                                         size_t context_size,
00207                                                         TAO_Persistent_Context_Index *ind);
00208 
00209   // = Methods not implemented in TAO_Hash_Naming_Context.
00210 
00211   /**
00212    * This operation returns a new naming context implemented by the
00213    * same naming server in which the operation was invoked.  The
00214    * context is not bound.
00215    */
00216   virtual CosNaming::NamingContext_ptr new_context (void);
00217 
00218   /**
00219    * Returns at most the requested number of bindings <how_many> in
00220    * <bl>.  If the naming context contains additional bindings, they
00221    * are returned with a BindingIterator.  In the naming context does
00222    * not contain any additional bindings <bi> returned as null.
00223    */
00224   virtual void list (CORBA::ULong how_many,
00225                      CosNaming::BindingList_out &bl,
00226                      CosNaming::BindingIterator_out &bi);
00227 
00228 protected:
00229 
00230   /**
00231    * Set <destroyed_> flag (inherited from TAO_Hash_Naming_Context) to
00232    * <level>.  Legal values for <destroyed_> are 0, 1, and 2.  The
00233    * values specify the extent of cleanup that should take place in the
00234    * context's destructor:
00235    * '0' - no cleanup (e.g., if the context goes out of scope, but
00236    * it's state is to remain in persistent storage);
00237    * '1' - free up the underlying data structure in persistent storage
00238    * (e.g., if the initialization of this context was only partially completed
00239    * due to some failures, and we need to roll back);
00240    * '2' - free up the underlying data structure, and deregister this
00241    * naming context from its <index_> (e.g., if the context had
00242    * <destroy> method invoked and needs to be completely removed from existence).
00243    */
00244   void set_cleanup_level (int level);
00245 
00246   /// Counter used for generation of POA ids for children Naming
00247   /// Contexts.
00248   ACE_UINT32 *counter_;
00249 
00250   /**
00251    * A pointer to the underlying data structure used to store name
00252    * bindings. While our superclass (TAO_Hash_Naming_Context) also
00253    * maintains a pointer to the data structure, keeping this pointer
00254    * around saves us from the need to downcast when invoking
00255    * non-virtual methods.
00256    */
00257   TAO_Persistent_Bindings_Map *persistent_context_;
00258 
00259   /**
00260    * A pointer to the index object of this naming service: it keeps
00261    * track of all the naming contexts created.  Every time we make a
00262    * new context or destroy one, we need to make an entry there.
00263    * Also, we get the allocator needed to initialize us from this guy.
00264    */
00265   TAO_Persistent_Context_Index *index_;
00266 };
00267 
00268 TAO_END_VERSIONED_NAMESPACE_DECL
00269 
00270 #include /**/ "ace/post.h"
00271 #endif /* TAO_PERSISTENT_NAMING_CONTEXT_H */

Generated on Sun Jan 27 16:15:31 2008 for TAO_CosNaming by doxygen 1.3.6