Timer_Helper.cpp

Go to the documentation of this file.
00001 #include "orbsvcs/Time/Timer_Helper.h"
00002 #include "orbsvcs/Time/TAO_Time_Service_Clerk.h"
00003 
00004 #include "tao/debug.h"
00005 
00006 #include "ace/OS_NS_time.h"
00007 #include "ace/OS_NS_sys_time.h"
00008 
00009 ACE_RCSID (Time,
00010            Timer_Helper,
00011            "$Id: Timer_Helper.cpp 76601 2007-01-25 19:59:59Z elliott_c $")
00012 
00013 
00014 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00015 
00016 Timer_Helper::Timer_Helper (void)
00017   : clerk_ (0)
00018 {
00019 }
00020 
00021 Timer_Helper::Timer_Helper (TAO_Time_Service_Clerk *clerk)
00022   : clerk_ (clerk)
00023 {
00024 
00025 }
00026 
00027 Timer_Helper::~Timer_Helper (void)
00028 {
00029 }
00030 
00031 int
00032 Timer_Helper::handle_timeout (const ACE_Time_Value &,
00033                               const void *)
00034 {
00035   int no_of_servers = 0;
00036   CORBA::ULongLong sum = 0;
00037 
00038   // The following are used to keep a track of the inaccuracy
00039   // in synchronization.
00040 
00041 #if defined (ACE_LACKS_LONGLONG_T)
00042   CORBA::ULongLong lowest_time (0xFFFFFFFF, 0xFFFFFFFF);
00043 #else
00044   CORBA::ULongLong lowest_time = ACE_UINT64_LITERAL (0xFFFFFFFFFFFFFFFF);
00045 #endif
00046 
00047   CORBA::ULongLong highest_time  = 0;
00048 
00049   try
00050     {
00051       IORS::TYPE* value;
00052       for (IORS::ITERATOR server_iterator (this->clerk_->server_);
00053            server_iterator.next (value) != 0;
00054            server_iterator.advance ())
00055         {
00056           // This is a remote call.
00057           CosTime::UTO_var UTO_server =
00058             (*value)->universal_time ();
00059 
00060 #if defined (ACE_LACKS_LONGLONG_T)
00061 
00062           if (TAO_debug_level > 0)
00063             ACE_DEBUG ((LM_DEBUG,
00064                         "\nTime = %Q\nInaccuracy = %Q\nTimeDiff = %d\nstruct.time = %Q\n"
00065                         "struct.inacclo = %d\nstruct.inacchi = %d\nstruct.Tdf = %d\n",
00066                         ACE_U64_TO_U32 (UTO_server->time ()),
00067                         ACE_U64_TO_U32 (UTO_server->inaccuracy ()),
00068                         UTO_server->tdf (),
00069                         ACE_U64_TO_U32 ((UTO_server->utc_time ()).time),
00070                         (UTO_server->utc_time ()).inacclo,
00071                         (UTO_server->utc_time ()).inacchi,
00072                         (UTO_server->utc_time ()).tdf));
00073 
00074 #else
00075 
00076           if (TAO_debug_level > 0)
00077             ACE_DEBUG ((LM_DEBUG,
00078                         "\nTime = %Q\nInaccuracy = %Q\nTimeDiff = %d\nstruct.time = %Q\n"
00079                         "struct.inacclo = %d\nstruct.inacchi = %d\nstruct.Tdf = %d\n",
00080                         UTO_server->time (),
00081                         UTO_server->inaccuracy (),
00082                         UTO_server->tdf (),
00083                         (UTO_server->utc_time ()).time,
00084                         (UTO_server->utc_time ()).inacclo,
00085                         (UTO_server->utc_time ()).inacchi,
00086                         (UTO_server->utc_time ()).tdf));
00087 #endif
00088 
00089           CORBA::ULongLong curr_server_time =
00090             UTO_server->time ();
00091 
00092           sum += curr_server_time;
00093 
00094           ++no_of_servers;
00095 
00096           // Set the highest time to the largest time seen so far.
00097           if (curr_server_time > highest_time)
00098             highest_time = curr_server_time;
00099 
00100           // Set the lowest time to the smallest time seen so far.
00101           if (curr_server_time < lowest_time)
00102             lowest_time = curr_server_time;
00103 
00104         }
00105 
00106       if (TAO_debug_level > 0)
00107         ACE_DEBUG ((LM_DEBUG,
00108                     "\nUpdated time from %d servers in the network",
00109                     no_of_servers));
00110 
00111       // Return the average of the times retrieved from the various
00112       // servers.
00113       clerk_->time_ = sum / no_of_servers ;
00114 
00115       // Set the Time Displacement Factor. The TZ environment variable is
00116       // read to set the time zone. We convert the timezone value from seconds
00117       // to minutes.
00118 
00119       ACE_OS::tzset ();
00120       long arg = ACE_OS::timezone () / 60;
00121       CORBA::Short goodarg = static_cast<CORBA::Short> (arg);
00122       clerk_->time_displacement_factor (goodarg);
00123 
00124       // Set the inaccuracy.
00125       if (highest_time > lowest_time)
00126         clerk_->inaccuracy (highest_time - lowest_time);
00127       else
00128         clerk_->inaccuracy (0);
00129 
00130       const ACE_Time_Value timeofday = ACE_OS::gettimeofday ();
00131 
00132       // Record the current time in a timestamp to know when global
00133       // updation of time was done.
00134       clerk_->update_timestamp_ =
00135         static_cast<CORBA::ULongLong> (timeofday.sec ()) *
00136         static_cast<ACE_UINT32> (10000000) +
00137         static_cast<CORBA::ULongLong> (timeofday.usec () * 10);
00138     }
00139   catch (const CORBA::Exception& ex)
00140     {
00141       if (TAO_debug_level > 0)
00142         ex._tao_print_exception ("Exception in handle_timeout()\n");
00143 
00144       return -1;
00145     }
00146 
00147   return 0;
00148 }
00149 
00150 TAO_END_VERSIONED_NAMESPACE_DECL

Generated on Tue Feb 2 17:49:17 2010 for TAO_CosTime by  doxygen 1.4.7