Standard_Event_Persistence.cpp

Go to the documentation of this file.
00001 // Standard_Event_Persistence.cpp,v 1.8 2006/03/14 06:14:34 jtc Exp
00002 
00003 #include "orbsvcs/Notify/Standard_Event_Persistence.h"
00004 #include "orbsvcs/Notify/Persistent_File_Allocator.h"
00005 #include "tao/debug.h"
00006 #include "ace/Dynamic_Service.h"
00007 #include "ace/OS_NS_strings.h"
00008 
00009 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00010 
00011 namespace TAO_Notify
00012 {
00013 
00014 Standard_Event_Persistence::Standard_Event_Persistence ()
00015   : filename_ (ACE_TEXT ("__PERSISTENT_EVENT__.DB"))
00016   , block_size_ (512)
00017   , factory_ (0)
00018 {
00019 }
00020 
00021 Standard_Event_Persistence::~Standard_Event_Persistence ()
00022 {
00023 }
00024 
00025 // get the current factory, creating it if necessary
00026 Event_Persistence_Factory *
00027 Standard_Event_Persistence::get_factory ()
00028 {
00029   //@@todo guard? ; doublecheck?
00030   if (this->factory_ == 0)
00031   {
00032     ACE_NEW_NORETURN (
00033       this->factory_,
00034       Standard_Event_Persistence_Factory ()
00035       );
00036     if (this->factory_ != 0)
00037     {
00038       if (!this->factory_->open (this->filename_.c_str ()))
00039       {
00040         this->factory_ = 0;
00041       }
00042     }
00043   }
00044   return this->factory_;
00045 }
00046 
00047 // release the current factory so a new one can be created
00048 void
00049 Standard_Event_Persistence::reset ()
00050 {
00051   delete this->factory_;
00052   this->factory_ = 0;
00053 }
00054 
00055 int
00056 Standard_Event_Persistence::init (int argc, ACE_TCHAR *argv[])
00057 {
00058   int result = 0;
00059   bool verbose = false;
00060   for (int narg = 0; narg < argc; ++narg)
00061   {
00062     ACE_TCHAR * av = argv[narg];
00063     if (ACE_OS::strcasecmp (av, ACE_TEXT ("-v")) == 0)
00064     {
00065       verbose = true;
00066       ACE_DEBUG ((LM_DEBUG,
00067         ACE_TEXT ("(%P|%t) Standard_Event_Persistence: -verbose\n")
00068         ));
00069     }
00070     else if (ACE_OS::strcasecmp (av, ACE_TEXT ("-file_path")) == 0 && narg + 1 < argc)
00071     {
00072       this->filename_ = argv[narg + 1];
00073       if (TAO_debug_level > 0 || verbose)
00074       {
00075         ACE_DEBUG ((LM_DEBUG,
00076           ACE_TEXT ("(%P|%t) Standard_Event_Persistence: Setting -file_path: %s\n"),
00077           this->filename_.c_str ()
00078         ));
00079       }
00080       narg += 1;
00081     }
00082     else if (ACE_OS::strcasecmp (av, ACE_TEXT ("-block_size")) == 0 && narg + 1 < argc)
00083     {
00084       this->block_size_ = ACE_OS::atoi(argv[narg + 1]);
00085       if (TAO_debug_level > 0 || verbose)
00086       {
00087         ACE_DEBUG ((LM_DEBUG,
00088           ACE_TEXT ("(%P|%t) Standard_Event_Persistence: Setting -block_size: %d\n"),
00089           this->block_size_
00090         ));
00091       }
00092       narg += 1;
00093     }
00094     else
00095     {
00096       ACE_ERROR ((LM_ERROR,
00097         ACE_TEXT ("(%P|%t) Unknown parameter to Standard Event Persistence: %s\n"),
00098         argv[narg]
00099         ));
00100       result = -1;
00101     }
00102   }
00103   return result;
00104 }
00105 
00106 int
00107 Standard_Event_Persistence::fini ()
00108 {
00109   delete this->factory_;
00110   this->factory_ = 0;
00111   return 0;
00112 }
00113 
00114 Standard_Event_Persistence_Factory::Standard_Event_Persistence_Factory ()
00115   : allocator_()
00116   , root_(this)
00117   , psb_(0)
00118   , serial_number_(ROUTING_SLIP_ROOT_SERIAL_NUMBER + 1)
00119   , is_reloading_ (false)
00120 {
00121 }
00122 
00123 bool
00124 Standard_Event_Persistence_Factory::open (const ACE_TCHAR* filename,
00125                                           ACE_UINT32 block_size)
00126 {
00127   bool result = false;
00128   if (allocator_.open (filename, block_size))
00129   {
00130     this->is_reloading_ = this->root_.load(ROUTING_SLIP_ROOT_BLOCK_NUMBER, ROUTING_SLIP_ROOT_SERIAL_NUMBER);
00131     if (! this->is_reloading_)
00132     {
00133       ACE_ASSERT (this->psb_ == 0);
00134 //      this->psb_ = this->allocator_.allocate();
00135       this->root_.store_root();
00136     }
00137     result = true;
00138   }
00139   return result;
00140 }
00141 
00142 Standard_Event_Persistence_Factory::~Standard_Event_Persistence_Factory()
00143 {
00144   if (TAO_debug_level > 0)
00145   {
00146     ACE_DEBUG ((LM_DEBUG,
00147       ACE_TEXT ("(%P|%t) Standard_Event_Persistence_Factory::~Standard_Event_Persistence_Factory\n")
00148     ));
00149   }
00150   this->root_.release_all ();
00151   delete this->psb_;
00152   this->psb_ = 0;
00153   this->allocator_.shutdown();
00154 }
00155 
00156 Routing_Slip_Persistence_Manager*
00157 Standard_Event_Persistence_Factory::create_routing_slip_persistence_manager(
00158   Persistent_Callback* callback)
00159 {
00160   Routing_Slip_Persistence_Manager* rspm = 0;
00161   ACE_NEW_RETURN(rspm, Routing_Slip_Persistence_Manager(this), rspm);
00162   rspm->set_callback(callback);
00163   return rspm;
00164 }
00165 
00166 Routing_Slip_Persistence_Manager *
00167 Standard_Event_Persistence_Factory::first_reload_manager()
00168 {
00169   Routing_Slip_Persistence_Manager * result = 0;
00170   if (this->is_reloading_)
00171   {
00172     result = this->root_.load_next();
00173   }
00174   return result;
00175 }
00176 
00177 void
00178 Standard_Event_Persistence_Factory::done_reloading(
00179   Persistent_Storage_Block * next_psb,
00180   ACE_UINT64 current_serial_number)
00181 {
00182   ACE_ASSERT (this->psb_ == 0);
00183   this->psb_ = next_psb;
00184   this->serial_number_ = current_serial_number;
00185   this->is_reloading_ = false;
00186 }
00187 
00188 void
00189 Standard_Event_Persistence_Factory::preallocate_next_record(
00190   ACE_UINT64& current_serial_number,
00191   Persistent_Storage_Block*& current_psb,
00192   ACE_UINT64& next_serial_number,
00193   ACE_UINT32& next_block_number)
00194 {
00195   // return current serial number and
00196   // a psb containing current record number
00197   current_serial_number = this->serial_number_;
00198   this->psb_->set_allocator_owns(false); // give up ownership
00199   this->psb_->set_sync();
00200   current_psb = this->psb_;
00201   this->get_preallocated_pointer (next_serial_number, next_block_number);
00202 }
00203 
00204 void
00205 Standard_Event_Persistence_Factory::get_preallocated_pointer(
00206   ACE_UINT64& next_serial_number,
00207   ACE_UINT32& next_block_number)
00208 {
00209   ++this->serial_number_;
00210   this->psb_ = this->allocator_.allocate();
00211 
00212   next_serial_number = this->serial_number_;
00213   next_block_number = this->psb_->block_number();
00214 }
00215 
00216 Persistent_File_Allocator*
00217 Standard_Event_Persistence_Factory::allocator()
00218 {
00219   return &this->allocator_;
00220 }
00221 
00222 Routing_Slip_Persistence_Manager &
00223 Standard_Event_Persistence_Factory::root()
00224 {
00225   return this->root_;
00226 }
00227 
00228 } // End TAO_Notify_Namespace
00229 
00230 TAO_END_VERSIONED_NAMESPACE_DECL
00231 
00232 ACE_FACTORY_NAMESPACE_DEFINE (TAO_Notify_Serv,
00233                               TAO_Notify_Standard_Event_Persistence,
00234                               TAO_Notify::Standard_Event_Persistence)

Generated on Thu Nov 9 13:24:16 2006 for TAO_CosNotification by doxygen 1.3.6