System_Time.cpp

Go to the documentation of this file.
00001 // $Id: System_Time.cpp 80826 2008-03-04 14:51:23Z wotte $
00002 
00003 #include "ace/System_Time.h"
00004 #include "ace/MMAP_Memory_Pool.h"
00005 #include "ace/Malloc_T.h"
00006 #include "ace/Null_Mutex.h"
00007 #include "ace/Time_Value.h"
00008 #include "ace/OS_NS_string.h"
00009 #include "ace/OS_NS_time.h"
00010 
00011 ACE_RCSID(ace, System_Time, "$Id: System_Time.cpp 80826 2008-03-04 14:51:23Z wotte $")
00012 
00013 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00014 
00015 ACE_System_Time::ACE_System_Time (const ACE_TCHAR *poolname)
00016   : shmem_ (0)
00017   , delta_time_ (0)
00018 {
00019   ACE_TRACE ("ACE_System_Time::ACE_System_Time");
00020 
00021   // Only create a new unique filename for the memory pool file
00022   // if the user didn't supply one...
00023   if (poolname == 0)
00024     {
00025 #if defined (ACE_DEFAULT_BACKING_STORE)
00026       // Create a temporary file.
00027       ACE_OS::strcpy (this->poolname_,
00028                       ACE_DEFAULT_BACKING_STORE);
00029 #else /* ACE_DEFAULT_BACKING_STORE */
00030       if (ACE::get_temp_dir (this->poolname_,
00031                                       MAXPATHLEN - 17) == -1)
00032         // -17 for ace-malloc-XXXXXX
00033         {
00034           ACE_ERROR ((LM_ERROR,
00035                       ACE_TEXT ("Temporary path too long, ")
00036                       ACE_TEXT ("defaulting to current directory\n")));
00037           this->poolname_[0] = 0;
00038         }
00039 
00040       // Add the filename to the end
00041       ACE_OS::strcat (this->poolname_, ACE_TEXT ("ace-malloc-XXXXXX"));
00042 
00043 #endif /* ACE_DEFAULT_BACKING_STORE */
00044     }
00045   else
00046     ACE_OS::strsncpy (this->poolname_,
00047                       poolname,
00048                       (sizeof this->poolname_ / sizeof (ACE_TCHAR)));
00049 
00050   ACE_NEW (this->shmem_,
00051            ALLOCATOR (this->poolname_));
00052 }
00053 
00054 ACE_System_Time::~ACE_System_Time (void)
00055 {
00056   ACE_TRACE ("ACE_System_Time::~ACE_System_Time");
00057   delete this->shmem_;
00058 }
00059 
00060 // Get the local system time.
00061 
00062 int
00063 ACE_System_Time::get_local_system_time (time_t & time_out)
00064 {
00065   ACE_TRACE ("ACE_System_Time::get_local_system_time");
00066   time_out = ACE_OS::time (0);
00067   return 0;
00068 }
00069 
00070 int
00071 ACE_System_Time::get_local_system_time (ACE_Time_Value &time_out)
00072 {
00073   ACE_TRACE ("ACE_System_Time::get_local_system_time");
00074   time_out.set (ACE_OS::time (0), 0);
00075   return 0;
00076 }
00077 
00078 // Get the system time of the central time server.
00079 
00080 int
00081 ACE_System_Time::get_master_system_time (time_t &time_out)
00082 {
00083   ACE_TRACE ("ACE_System_Time::get_master_system_time");
00084 
00085   if (this->delta_time_ == 0)
00086     {
00087       // Try to find it
00088       void * temp;
00089       if (this->shmem_->find (ACE_DEFAULT_TIME_SERVER_STR, temp) == -1)
00090         {
00091           // No time entry in shared memory (meaning no Clerk exists)
00092           // so return the local time of the host.
00093           return this->get_local_system_time (time_out);
00094         }
00095       else
00096         // Extract the delta time.
00097         this->delta_time_ = static_cast<long *> (temp);
00098     }
00099 
00100   time_t local_time;
00101 
00102   // If delta_time is positive, it means that the system clock is
00103   // ahead of our local clock so add delta to the local time to get an
00104   // approximation of the system time. Else if delta time is negative,
00105   // it means that our local clock is ahead of the system clock, so
00106   // return the last local time stored (to avoid time conflicts).
00107   if (*this->delta_time_ >= 0 )
00108     {
00109       this->get_local_system_time (local_time);
00110       time_out = local_time + static_cast<ACE_UINT32> (*this->delta_time_);
00111     }
00112   else
00113     // Return the last local time. Note that this is stored as the
00114     // second field in shared memory.
00115     time_out = *(this->delta_time_ + 1);
00116   return 0;
00117 }
00118 
00119 int
00120 ACE_System_Time::get_master_system_time (ACE_Time_Value &time_out)
00121 {
00122   ACE_TRACE ("ACE_System_Time::get_master_system_time");
00123   time_t to;
00124   if (this->get_master_system_time (to) == -1)
00125     return -1;
00126   time_out.sec (to);
00127   return 0;
00128 }
00129 
00130 // Synchronize local system time with the central time server using
00131 // specified mode (currently unimplemented).
00132 
00133 int
00134 ACE_System_Time::sync_local_system_time (ACE_System_Time::Sync_Mode)
00135 {
00136   ACE_TRACE ("ACE_System_Time::sync_local_system_time");
00137   ACE_NOTSUP_RETURN (-1);
00138 }
00139 
00140 ACE_END_VERSIONED_NAMESPACE_DECL

Generated on Tue Feb 2 17:18:43 2010 for ACE by  doxygen 1.4.7