00001 // -*- C++ -*- 00002 00003 //============================================================================= 00004 /** 00005 * @file Persistent_Context_Index.h 00006 * 00007 * $Id: Persistent_Context_Index.h 76950 2007-02-07 20:56:17Z johnnyw $ 00008 * 00009 * @author Marina Spivak <marina@cs.wustl.edu> 00010 */ 00011 //============================================================================= 00012 00013 00014 #ifndef TAO_PERSISTENT_CONTEXT_INDEX_H 00015 #define TAO_PERSISTENT_CONTEXT_INDEX_H 00016 #include /**/ "ace/pre.h" 00017 00018 #include "orbsvcs/Naming/Persistent_Entries.h" 00019 #include "orbsvcs/Naming/naming_serv_export.h" 00020 #include "orbsvcs/Naming/nsconf.h" 00021 #include "tao/PortableServer/PortableServer.h" 00022 00023 #include "ace/Malloc_T.h" 00024 #include "ace/MMAP_Memory_Pool.h" 00025 00026 TAO_BEGIN_VERSIONED_NAMESPACE_DECL 00027 00028 /** 00029 * @class TAO_Persistent_Context_Index 00030 * 00031 * @brief This class facilitates implementation of Persistent 00032 * Naming Service. It keeps track, centrally, of several pieces of 00033 * info for each Persistent Naming Context, allowing to perform the 00034 * initialization necessary for each Naming Context to 00035 * restore the state of the Naming Service from persistent storage 00036 * on server start-up. 00037 * 00038 * This class creates a memory-mapped file, allocates a hash 00039 * table from that file, and uses the hash table to store POA id, 00040 * and table and counter pointers for each Persistent Naming 00041 * Context. There are methods for adding and deleting entries 00042 * from this hash table as new Persistent Naming Contexts are 00043 * created and old ones are destroyed. This hash table 00044 * facilitates Persistent Naming Context servant initialization 00045 * upon Naming Server start-up. 00046 */ 00047 class TAO_Naming_Serv_Export TAO_Persistent_Context_Index 00048 { 00049 public: 00050 // = Some typedefs for convenience. 00051 00052 /// Hash map in which we will store info about each Persistent Naming Context. 00053 typedef ACE_Hash_Map_With_Allocator<TAO_Persistent_Index_ExtId, 00054 TAO_Persistent_Index_IntId> CONTEXT_INDEX; 00055 00056 /// Hash map used by Persistent Naming Context to keep its state. 00057 typedef ACE_Hash_Map_With_Allocator<TAO_Persistent_ExtId, 00058 TAO_Persistent_IntId> CONTEXT; 00059 00060 /// Allocator we will be using to make the Naming Service persistent. 00061 typedef ACE_Allocator_Adapter <ACE_Malloc <ACE_MMAP_MEMORY_POOL, TAO_SYNCH_MUTEX> 00062 > ALLOCATOR; 00063 00064 // = Initialization and termination methods. 00065 00066 /// Constructor. 00067 TAO_Persistent_Context_Index (CORBA::ORB_ptr orb, 00068 PortableServer::POA_ptr poa); 00069 00070 /** 00071 * Create ACE_Allocator, open/create memory-mapped file with the 00072 * specified file name/base address. Find or allocate <index_>. 00073 * Return 0 on success or -1 on failure. 00074 */ 00075 int open (const ACE_TCHAR *file_name, 00076 void * base_address = TAO_NAMING_BASE_ADDR); 00077 00078 /** 00079 * If <index_> contains no entries (i.e., was just created), create 00080 * a root Persistent Naming Context servant with table of size 00081 * <context_size>, and make an entry for it 00082 * in the <index_>. If <index_> contains entries, create a 00083 * Persistent Naming Context servant for each entry. Return 0 on 00084 * success and -1 on failure. 00085 */ 00086 int init (size_t context_size); 00087 00088 /** 00089 * Destructor. The memory mapped file that was opened/created is 00090 * not deleted, since we want it to keep the state of the Naming 00091 * Service until the next run. 00092 */ 00093 ~TAO_Persistent_Context_Index (void); 00094 00095 // = Methods for adding/removing entries. 00096 00097 /** 00098 * Create an entry for a Persistent Naming Context in <index_>, 00099 * i.e., a context with <poa_id>, <counter> and <hash_map> has just 00100 * been created, and is registering with us. 00101 */ 00102 int bind (const char *poa_id, ACE_UINT32 *&counter, CONTEXT *hash_map); 00103 00104 /// Remove an entry for the Persistent Naming Context with <poa_id> 00105 /// from <index_> (i.e., this context has just been destroyed). 00106 int unbind (const char *poa_id); 00107 00108 // = Accessors. 00109 00110 /// Return allocator. 00111 ACE_Allocator *allocator (void); 00112 00113 /// Return orb pointer. 00114 CORBA::ORB_ptr orb (void); 00115 00116 /// Return a pointer to the root Naming Context (returns a copy - must be 00117 /// deallocated by the user). 00118 CosNaming::NamingContext_ptr root_context (void); 00119 00120 private: 00121 00122 /// Helper for the <init> method. Iterates over <index_>, and 00123 /// creates a servant for each entry. 00124 int recreate_all (void); 00125 00126 /// Helper for the <open> method. 00127 int create_index (void); 00128 00129 /// Helper for <create_index> method: places hash table into an 00130 /// allocated space. 00131 int create_index_helper (void *buffer); 00132 00133 /// Lock to prevent multiple threads from modifying entries in the 00134 /// <index_> simultanneously. 00135 TAO_SYNCH_MUTEX lock_; 00136 00137 /** 00138 * Allocator that deals out memory from a memory-mapped file. We 00139 * use it here, and in TAO_Persistent_Naming_Context, whenever we 00140 * deal with data that should be kept in persistent store. 00141 */ 00142 ALLOCATOR *allocator_; 00143 00144 /// Hash map where we keep entries for all Persistent Naming 00145 /// Contexts. 00146 CONTEXT_INDEX *index_; 00147 00148 /// Name of the memory-mapped file used by <allocator_>. 00149 const ACE_TCHAR *index_file_; 00150 00151 /// Base address for the memory-mapped file. 00152 void *base_address_; 00153 00154 /// ORB. We use it for several object_to_string conversions, and 00155 /// keep it around for Persistent Naming Contexts' use. 00156 CORBA::ORB_var orb_; 00157 00158 /// POA under which to register Persistent Naming Context servants 00159 /// during start-up. 00160 PortableServer::POA_var poa_; 00161 00162 /// The reference to the root Naming Context. 00163 CosNaming::NamingContext_var root_context_; 00164 }; 00165 00166 TAO_END_VERSIONED_NAMESPACE_DECL 00167 00168 #include /**/ "ace/post.h" 00169 #endif /* TAO_PERSISTENT_CONTEXT_INDEX_H */