Persistent_Context_Index.cpp

Go to the documentation of this file.
00001 // $Id: Persistent_Context_Index.cpp 76589 2007-01-25 18:04:11Z elliott_c $
00002 
00003 #include "orbsvcs/Naming/Persistent_Context_Index.h"
00004 #include "orbsvcs/Naming/Persistent_Naming_Context.h"
00005 
00006 #include "tao/debug.h"
00007 
00008 #include "ace/Auto_Ptr.h"
00009 #include "ace/OS_NS_unistd.h"
00010 
00011 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00012 
00013 int
00014 TAO_Persistent_Context_Index::unbind (const char *poa_id)
00015 {
00016   ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->lock_, -1);
00017 
00018   TAO_Persistent_Index_ExtId name (poa_id);
00019   TAO_Persistent_Index_IntId entry;
00020   if (this->index_->unbind (name, entry, this->allocator_) != 0)
00021     return -1;
00022   else
00023     {
00024       // Free up the memory we allocated in bind().
00025       this->allocator_->free ((void *) (entry.counter_));
00026       return 0;
00027     }
00028 }
00029 
00030 int
00031 TAO_Persistent_Context_Index::bind (const char *poa_id,
00032                                     ACE_UINT32 *&counter,
00033                                     TAO_Persistent_Context_Index::CONTEXT *hash_map)
00034 {
00035   ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->lock_, -1);
00036 
00037   // Allocate memory for items to be stored in the table.
00038   size_t poa_id_len = ACE_OS::strlen (poa_id) + 1;
00039   size_t counter_len = sizeof (ACE_UINT32);
00040   char *ptr = (char *) this->allocator_->malloc (poa_id_len + counter_len);
00041 
00042   if (ptr == 0)
00043     return -1;
00044   else
00045     {
00046       // Populate memory with data.
00047       counter = reinterpret_cast<ACE_UINT32 *> (ptr);
00048       *counter = 0;
00049       char * poa_id_ptr = ptr + counter_len;
00050       ACE_OS::strcpy (poa_id_ptr, poa_id);
00051 
00052       TAO_Persistent_Index_ExtId name (poa_id_ptr);
00053       TAO_Persistent_Index_IntId entry (counter, hash_map);
00054       int result = -1;
00055 
00056       // Do a normal bind.  This will fail if there's already an
00057       // entry with the same name.
00058       result = this->index_->bind (name, entry, this->allocator_);
00059 
00060       if (result == 1)
00061         {
00062           // Entry already existed so bind failed. Free our dynamically
00063           // allocated memory.
00064           this->allocator_->free ((void *) ptr);
00065           return result;
00066         }
00067 
00068       if (result == -1)
00069         // Free our dynamically allocated memory.
00070         this->allocator_->free ((void *) ptr);
00071       else
00072         // If bind() succeed, it will automatically sync
00073         // up the map manager entry.  However, we must sync up our
00074         // name/value memory.
00075         this->allocator_->sync (ptr, poa_id_len + counter_len);
00076 
00077       return result;
00078     }
00079 }
00080 
00081 TAO_Persistent_Context_Index::TAO_Persistent_Context_Index
00082   (CORBA::ORB_ptr orb,
00083    PortableServer::POA_ptr poa)
00084   : allocator_ (0),
00085     index_ (0),
00086     index_file_ (0),
00087     base_address_ (0),
00088     orb_ (CORBA::ORB::_duplicate (orb)),
00089     poa_ (PortableServer::POA::_duplicate (poa))
00090 {
00091 }
00092 
00093 TAO_Persistent_Context_Index::~TAO_Persistent_Context_Index (void)
00094 {
00095   delete allocator_;
00096   ACE_OS::free (reinterpret_cast<void *> (const_cast<ACE_TCHAR *> (index_file_)));
00097 }
00098 
00099 ACE_Allocator*
00100 TAO_Persistent_Context_Index::allocator (void)
00101 {
00102   return allocator_;
00103 }
00104 
00105 CosNaming::NamingContext_ptr
00106 TAO_Persistent_Context_Index::root_context (void)
00107 {
00108   return CosNaming::NamingContext::_duplicate (root_context_.in ());
00109 }
00110 
00111 CORBA::ORB_ptr
00112 TAO_Persistent_Context_Index::orb (void)
00113 {
00114   return orb_.in ();
00115 }
00116 
00117 int
00118 TAO_Persistent_Context_Index::open (const ACE_TCHAR *file_name,
00119                                     void *base_address)
00120 {
00121   this->base_address_ = base_address;
00122 
00123   index_file_ = ACE_OS::strdup (file_name);
00124   if (index_file_ == 0)
00125     return -1;
00126 
00127   return create_index ();
00128 }
00129 
00130 int
00131 TAO_Persistent_Context_Index::init (size_t context_size)
00132 {
00133   // Note: in case of an early exit from this (or helper) function due
00134   // to an error condition, we rely on POA to clean up all of the servants
00135   // already registered with it.
00136 
00137   // Return value of this function (necessary to keep compilers quiet).
00138   int status = 0;
00139 
00140   if (index_->current_size () == 0)
00141     // CASE 1:there are no Naming Contexts registered.  We need to create
00142     // one.
00143     {
00144 
00145       this->root_context_ =
00146         TAO_Persistent_Naming_Context::make_new_context (poa_.in (),
00147                                                          TAO_ROOT_NAMING_CONTEXT,
00148                                                          context_size,
00149                                                          this);
00150     }
00151 
00152   else
00153     // CASE 2:Recreate all Naming Contexts.
00154     status = recreate_all ();
00155 
00156   return status;
00157 }
00158 
00159 int
00160 TAO_Persistent_Context_Index::recreate_all (void)
00161 {
00162   CONTEXT_INDEX::ITERATOR *index_iter = 0;
00163 
00164   ACE_NEW_RETURN (index_iter,
00165                   (CONTEXT_INDEX::ITERATOR) (*index_),
00166                   -1);
00167 
00168   ACE_Auto_Basic_Ptr<CONTEXT_INDEX::ITERATOR> it (index_iter);
00169 
00170   // Because of broken old g++!!!
00171   typedef ACE_Hash_Map_With_Allocator<TAO_Persistent_Index_ExtId,
00172     TAO_Persistent_Index_IntId>  IND_DEF;
00173 
00174   IND_DEF::ENTRY *entry = 0;
00175 
00176   if (TAO_debug_level > 0)
00177     ACE_DEBUG ((LM_DEBUG, "Starting to recreate Naming Contexts from the file... \n"));
00178 
00179   // For each entry in <index_>, create a Naming Context servant.
00180   do
00181     {
00182       index_iter->next (entry);
00183 
00184       // Put together a servant for the new Naming Context.
00185 
00186       TAO_Persistent_Naming_Context *context_impl = 0;
00187       ACE_NEW_RETURN (context_impl,
00188                       TAO_Persistent_Naming_Context (poa_.in (),
00189                                                      entry->ext_id_.poa_id_,
00190                                                      this,
00191                                                      entry->int_id_.hash_map_,
00192                                                      entry->int_id_.counter_),
00193                   -1);
00194 
00195 
00196       // Put <context_impl> into the auto pointer temporarily, in case next
00197       // allocation fails.
00198       ACE_Auto_Basic_Ptr<TAO_Persistent_Naming_Context> temp (context_impl);
00199 
00200       TAO_Naming_Context *context = 0;
00201       ACE_NEW_RETURN (context,
00202                       TAO_Naming_Context (context_impl),
00203                       -1);
00204 
00205       // Let <implementation> know about it's <interface>.
00206       context_impl->interface (context);
00207 
00208       // Release auto pointer and start using reference counting to
00209       // control our servant.
00210       temp.release ();
00211       PortableServer::ServantBase_var s = context;
00212 
00213       // Register with the POA.
00214       PortableServer::ObjectId_var id =
00215         PortableServer::string_to_ObjectId (entry->ext_id_.poa_id_);
00216 
00217       this->poa_->activate_object_with_id (id.in (),
00218                                            context);
00219 
00220       CosNaming::NamingContext_var result = context->_this ();
00221 
00222       // If this is the root Naming Context, take a note of it.
00223       if (context_impl->root ())
00224           this->root_context_= result._retn ();
00225 
00226     } while (index_iter->advance ());
00227 
00228   return 0;
00229 }
00230 
00231 int
00232 TAO_Persistent_Context_Index::create_index (void)
00233 {
00234   // Make sure that the file name is of the legal length.
00235   if (ACE_OS::strlen (index_file_) >= MAXNAMELEN + MAXPATHLEN)
00236     {
00237       errno = ENAMETOOLONG;
00238       return -1;
00239     }
00240 
00241 #if !defined (CHORUS)
00242   ACE_MMAP_Memory_Pool::OPTIONS options (base_address_);
00243 #else
00244   // Use base address == 0, don't use a fixed address.
00245   ACE_MMAP_Memory_Pool::OPTIONS options (0,
00246                                          0,
00247                                          0,
00248                                          ACE_CHORUS_LOCAL_NAME_SPACE_T_SIZE);
00249 #endif /* CHORUS */
00250 
00251   // Create the allocator with the appropriate options.  The name used
00252   // for  the lock is the same as one used for the file.
00253   ACE_NEW_RETURN (this->allocator_,
00254                   ALLOCATOR (this->index_file_,
00255                              this->index_file_,
00256                              &options),
00257                   -1);
00258 
00259 #if !defined (ACE_LACKS_ACCESS)
00260   // Now check if the backing store has been created successfully.
00261   if (ACE_OS::access (this->index_file_, F_OK) != 0)
00262     ACE_ERROR_RETURN ((LM_ERROR,
00263                        "create_index\n"),
00264                       -1);
00265 #endif /* ACE_LACKS_ACCESS */
00266 
00267   void *context_index = 0;
00268 
00269   // This is the easy case since if we find hash table in the
00270   // memory-mapped file we know it's already initialized.
00271   if (this->allocator_->find (TAO_NAMING_CONTEXT_INDEX, context_index) == 0)
00272     this->index_ = (CONTEXT_INDEX *) context_index;
00273 
00274   // Create a new <index_> (because we've just created a new
00275   // memory-mapped file).
00276   else
00277     {
00278       size_t index_size = sizeof (CONTEXT_INDEX);
00279       context_index = this->allocator_->malloc (index_size);
00280 
00281       if (context_index == 0
00282           || create_index_helper (context_index) == -1
00283           || this->allocator_->bind (TAO_NAMING_CONTEXT_INDEX,
00284                                      context_index) == -1)
00285         {
00286           // Attempt to clean up.
00287           ACE_ERROR ((LM_ERROR,
00288                       "create_index\n"));
00289           this->allocator_->remove ();
00290           return -1;
00291         }
00292     }
00293   return 0;
00294 }
00295 
00296 int
00297 TAO_Persistent_Context_Index::create_index_helper (void *buffer)
00298 {
00299   this->index_ = new (buffer) CONTEXT_INDEX (this->allocator_);
00300   return 0;
00301 }
00302 
00303 TAO_END_VERSIONED_NAMESPACE_DECL

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