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            "Timer_Helper.cpp,v 1.34 2006/03/14 06:14:35 jtc Exp")
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   ACE_DECLARE_NEW_CORBA_ENV;
00050   ACE_TRY
00051     {
00052       IORS::TYPE* value;
00053       for (IORS::ITERATOR server_iterator (this->clerk_->server_);
00054            server_iterator.next (value) != 0;
00055            server_iterator.advance ())
00056         {
00057           // This is a remote call.
00058           CosTime::UTO_var UTO_server =
00059             (*value)->universal_time (ACE_ENV_SINGLE_ARG_PARAMETER);
00060           ACE_TRY_CHECK;
00061 
00062 #if defined (ACE_LACKS_LONGLONG_T)
00063 
00064           if (TAO_debug_level > 0)
00065             ACE_DEBUG ((LM_DEBUG,
00066                         "\nTime = %Q\nInaccuracy = %Q\nTimeDiff = %d\nstruct.time = %Q\n"
00067                         "struct.inacclo = %d\nstruct.inacchi = %d\nstruct.Tdf = %d\n",
00068                         ACE_U64_TO_U32 (UTO_server->time ()),
00069                         ACE_U64_TO_U32 (UTO_server->inaccuracy ()),
00070                         UTO_server->tdf (),
00071                         ACE_U64_TO_U32 ((UTO_server->utc_time ()).time),
00072                         (UTO_server->utc_time ()).inacclo,
00073                         (UTO_server->utc_time ()).inacchi,
00074                         (UTO_server->utc_time ()).tdf));
00075 
00076 #else
00077 
00078           if (TAO_debug_level > 0)
00079             ACE_DEBUG ((LM_DEBUG,
00080                         "\nTime = %Q\nInaccuracy = %Q\nTimeDiff = %d\nstruct.time = %Q\n"
00081                         "struct.inacclo = %d\nstruct.inacchi = %d\nstruct.Tdf = %d\n",
00082                         UTO_server->time (),
00083                         UTO_server->inaccuracy (),
00084                         UTO_server->tdf (),
00085                         (UTO_server->utc_time ()).time,
00086                         (UTO_server->utc_time ()).inacclo,
00087                         (UTO_server->utc_time ()).inacchi,
00088                         (UTO_server->utc_time ()).tdf));
00089 #endif
00090 
00091           CORBA::ULongLong curr_server_time =
00092             UTO_server->time (ACE_ENV_SINGLE_ARG_PARAMETER);
00093           ACE_TRY_CHECK;
00094 
00095           sum += curr_server_time;
00096 
00097           ++no_of_servers;
00098 
00099           // Set the highest time to the largest time seen so far.
00100           if (curr_server_time > highest_time)
00101             highest_time = curr_server_time;
00102 
00103           // Set the lowest time to the smallest time seen so far.
00104           if (curr_server_time < lowest_time)
00105             lowest_time = curr_server_time;
00106 
00107         }
00108 
00109       if (TAO_debug_level > 0)
00110         ACE_DEBUG ((LM_DEBUG,
00111                     "\nUpdated time from %d servers in the network",
00112                     no_of_servers));
00113 
00114       // Return the average of the times retrieved from the various
00115       // servers.
00116       clerk_->time_ = sum / no_of_servers ;
00117 
00118       // Set the Time Displacement Factor. The TZ environment variable is
00119       // read to set the time zone. We convert the timezone value from seconds
00120       // to minutes.
00121 
00122       ACE_OS::tzset ();
00123       long arg = ACE_OS::timezone () / 60;
00124       CORBA::Short goodarg = static_cast<CORBA::Short> (arg);
00125       clerk_->time_displacement_factor (goodarg);
00126 
00127       // Set the inaccuracy.
00128       if (highest_time > lowest_time)
00129         clerk_->inaccuracy (highest_time - lowest_time);
00130       else
00131         clerk_->inaccuracy (0);
00132 
00133       const ACE_Time_Value timeofday = ACE_OS::gettimeofday ();
00134 
00135       // Record the current time in a timestamp to know when global
00136       // updation of time was done.
00137       clerk_->update_timestamp_ =
00138         static_cast<CORBA::ULongLong> (timeofday.sec ()) *
00139         static_cast<ACE_UINT32> (10000000) +
00140         static_cast<CORBA::ULongLong> (timeofday.usec () * 10);
00141     }
00142   ACE_CATCHANY
00143     {
00144       if (TAO_debug_level > 0)
00145         ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
00146                              "Exception in handle_timeout()\n");
00147 
00148       return -1;
00149     }
00150   ACE_ENDTRY;
00151   ACE_CHECK_RETURN (-1);
00152 
00153   return 0;
00154 }
00155 
00156 TAO_END_VERSIONED_NAMESPACE_DECL

Generated on Thu Nov 9 13:58:37 2006 for TAO_CosTime by doxygen 1.3.6