NLNET::_CUniTime Class Reference

#include <unitime.h>

Inheritance diagram for NLNET::_CUniTime:

NLMISC::CTime

Detailed Description

This class provide a independant universal time system.
Author:
Vianney Lecroart

Nevrax France

Date:
2000
THIS CLASS IS DEPRECATED, DON'T USE IT

Definition at line 51 of file unitime.h.

Static Public Member Functions

TTime getLocalTime ()
TTicks getPerformanceTime ()
uint32 getSecondsSince1970 ()
const char * getStringUniTime (NLMISC::TTime ut)
 Return the time in a string format to be display.

const char * getStringUniTime ()
 Return the time in a string format to be display.

NLMISC::TTime getUniTime ()
 Return the time in millisecond. This time is the same on all computers at the same moment.

void installServer (CCallbackServer *server)
void setUniTime (NLMISC::TTime uTime)
void setUniTime (NLMISC::TTime uTime, NLMISC::TTime lTime)
 used by the time service to set the universal time the first time

void simulate ()
void syncUniTimeFromServer (CCallbackClient *client)
void syncUniTimeFromService (CCallbackNetBase::TRecordingState rec=CCallbackNetBase::Off, const CInetAddress *addr=NULL)
double ticksToSecond (TTicks ticks)

Static Public Attributes

bool Sync = false

Static Private Attributes

bool _Simulate = false
NLMISC::TTime _SyncLocalTime = 0
NLMISC::TTime _SyncUniTime = 0


Member Function Documentation

TTime NLMISC::CTime::getLocalTime  )  [static, inherited]
 

get the time in millisecond. It's a local time, it means that the value is different on 2 differents computers. Use the CUniTime to get a universal time that is the same on all computers.

Warning:
On Windows, the value is on 32 bits only.

Definition at line 55 of file time_nl.cpp.

References NLMISC::CTime::getPerformanceTime(), nlerror, and NLMISC::TTime.

Referenced by NLAIAGENT::CAgentWatchTimer::attach(), NLSOUND::CAudioMixerUser::curTime(), NL3D::CWaterModel::doSimpleRender(), NLSOUND::CComplexSource::getTime(), loadForm(), NLSOUND::CComplexSource::onEvent(), NLSOUND::CComplexSource::onUpdate(), NLSOUND::CComplexSource::playStuf(), NLAIAGENT::CAgentManagerTimer::CRunTimer::run(), NLAIAGENT::CAgentClockTimer::runActivity(), NLAIAGENT::CAgentWatchTimer::runActivity(), NL3D::CWaterModel::setupMaterialNVertexShader(), NLMISC::CTime::ticksToSecond(), NLSOUND::CSoundDriverDSound::TimerCallback(), NLSOUND::CSoundDriverDSound::update(), NLSOUND::CAudioMixerUser::update(), and NLSOUND::CBackgroundSoundManager::updateBackgroundStatus().

00056 {
00057 #ifdef NL_OS_WINDOWS
00058 
00059         static bool byperfcounter;
00060         static bool initdone = false;
00061 
00062         // Initialization
00063         if ( ! initdone )
00064         {
00065                 byperfcounter = (getPerformanceTime() != 0);
00066                 initdone = true;
00067         }
00068 
00069         /* Retrieve time is ms
00070      * Why do we prefer getPerformanceTime() to timeGetTime() ? Because on one dual-processor Win2k
00071          * PC, we have noticed that timeGetTime() slows down when the client is running !!!
00072          */
00073 
00074 
00075         /* Now we have noticed that on all WinNT4 PC the getPerformanceTime can give us value that
00076          * are less than previous
00077          */
00078 
00079         //if ( byperfcounter )
00080         //{
00081         //      return (TTime)(ticksToSecond(getPerformanceTime()) * 1000.0f);
00082         //}
00083         //else
00084         //{
00085                 return timeGetTime();
00086         //}
00087 
00088 #elif defined (NL_OS_UNIX)
00089 
00090         struct timeval tv;
00091 
00092         if ( gettimeofday( &tv, NULL) == -1 )
00093         {
00094                 nlerror ("Cannot get time of day.");
00095         }
00096         return (TTime)tv.tv_sec * (TTime)1000 + (TTime)tv.tv_usec / (TTime)1000;
00097 
00098 #endif
00099 }

TTicks NLMISC::CTime::getPerformanceTime  )  [static, inherited]
 

get the time in processor ticks. Use it for profile purpose. Only implemented under windows for the time. If the performance time is not supported on this hardware, it returns 0.

Definition at line 105 of file time_nl.cpp.

References nlwarning, NLMISC::TTicks, and x.

Referenced by NLMISC::CTime::getLocalTime(), NLMISC::CSystemInfo::getProcessorFrequency(), NLAIAGENT::CAgentScript::run(), NLAIAGENT::CAgentScript::sendMessage(), and NLMISC::CTime::ticksToSecond().

00106 {
00107 #ifdef NL_OS_WINDOWS
00108         LARGE_INTEGER ret;
00109         if (QueryPerformanceCounter (&ret))
00110                 return ret.QuadPart;
00111         else
00112                 return 0;
00113 #else // NL_OS_WINDOWS
00114 
00115 #ifdef HAVE_X86
00116         unsigned long long int x;
00117         __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
00118         return x;
00119 #else // HAVE_X86
00120         static bool firstWarn = true;
00121         if (firstWarn)
00122         {
00123                 nlwarning ("TTicks CTime::getPerformanceTime () is not implemented for your processor, returning 0");
00124                 firstWarn = false;
00125         }
00126         return 0;
00127 #endif // HAVE_X86
00128 
00129 #endif // NL_OS_WINDOWS
00130 }

uint32 NLMISC::CTime::getSecondsSince1970  )  [static, inherited]
 

returns the number of second since midnight (00:00:00), January 1, 1970, coordinated universal time, according to the system clock. This values is the same on all computer if computers are synchronized (with NTP for example).

Definition at line 45 of file time_nl.cpp.

References uint32.

Referenced by NLAINIMAT::CMHiCSagent::CTemporaryPriority::CTemporaryPriority(), and NLAINIMAT::CMHiCSagent::learningComputation().

00046 {
00047         return (uint32) time (NULL);
00048 }

const char * NLNET::_CUniTime::getStringUniTime NLMISC::TTime  ut  )  [static]
 

Return the time in a string format to be display.

Definition at line 96 of file unitime.cpp.

References nlstop, s, NLMISC::smprintf(), NLMISC::TTime, uint, and uint32.

00097 {
00098         nlstop;
00099         static char str[512];
00100 
00101         uint32 ms = (uint32) (ut % 1000); // time in ms 1000ms dans 1s
00102         ut /= 1000;
00103 
00104         uint32 s = (uint32) (ut % 60); // time in seconds 60s dans 1mn
00105         ut /= 60;
00106 
00107         uint32 m = (uint32) (ut % 60); // time in minutes 60m dans 1h
00108         ut /= 60;
00109 
00110         uint32 h = (uint32) (ut % 9); // time in hours 9h dans 1j
00111         ut /= 9;
00112 
00113         uint32 day = (uint32) (ut % (8*4)); // time in days 8day dans 1month
00114         ut /= 8;
00115 
00116         uint32 week = (uint32) (ut % 4); // time in weeks 4week dans 1month
00117         ut /= 4;
00118 
00119         uint32 month = (uint32) (ut % 12); // time in months 12month dans 1year
00120         ut /= 12;
00121 
00122         uint  year =  (uint32) ut;      // time in years
00123 
00124         smprintf (str, 512, "%02d/%02d/%04d (week %d) %02d:%02d:%02d.%03d", day+1, month+1, year+1, week+1, h, m, s, ms);
00125         return str;
00126 }

const char * NLNET::_CUniTime::getStringUniTime  )  [static]
 

Return the time in a string format to be display.

Definition at line 89 of file unitime.cpp.

References nlstop.

00090 {
00091         nlstop;
00092         return getStringUniTime(_CUniTime::getUniTime());
00093 }

TTime NLNET::_CUniTime::getUniTime  )  [static]
 

Return the time in millisecond. This time is the same on all computers at the same moment.

Definition at line 76 of file unitime.cpp.

References nlstop, and NLMISC::TTime.

00077 {
00078         nlstop;
00079         return 0;
00080 /*      if (!Sync)
00081         {
00082                 nlerror ("called getUniTime before calling syncUniTimeFromServer");
00083         }
00084         return getLocalTime () - (_SyncLocalTime - _SyncUniTime);
00085 */
00086 }

void NLNET::_CUniTime::installServer CCallbackServer server  )  [static]
 

Call this function in the init part of the front end service to enable time syncro between shard and clients.

Definition at line 280 of file unitime.cpp.

References NLNET::CCallbackNetBase::addCallbackArray(), nlassert, nlstop, and NLNET::ServerTimeServiceCallbackArray.

00281 {
00282         nlstop;
00283         static bool alreadyAddedCallback = false;
00284         nlassert (server != NULL);
00285         nlassert (!alreadyAddedCallback);
00286 
00287         server->addCallbackArray (ServerTimeServiceCallbackArray, sizeof (ServerTimeServiceCallbackArray) / sizeof (ServerTimeServiceCallbackArray[0]));
00288         alreadyAddedCallback = true;
00289 }

void NLNET::_CUniTime::setUniTime NLMISC::TTime  uTime  )  [static]
 

Definition at line 68 of file unitime.cpp.

References nlstop, and NLMISC::TTime.

00069 {
00070         nlstop;
00071 //      setUniTime (uTime, getLocalTime ());
00072 }

void NLNET::_CUniTime::setUniTime NLMISC::TTime  uTime,
NLMISC::TTime  lTime
[static]
 

used by the time service to set the universal time the first time

Definition at line 49 of file unitime.cpp.

References nlstop, and NLMISC::TTime.

00050 {
00051         nlstop;
00052 /*      if (Sync)
00053         {
00054                 TTime lt = getLocalTime ();
00055                 TTime delta = uTime - lTime + _SyncLocalTime - _SyncUniTime;
00056 
00057                 nlinfo ("_CUniTime::setUniTime(%"NL_I64"d, %"NL_I64"d): Resyncing delta %"NL_I64"dms",uTime,lTime,delta);
00058         }
00059         else
00060         {
00061                 nlinfo ("_CUniTime::setUniTime(%"NL_I64"d, %"NL_I64"d)",uTime,lTime);
00062                 Sync = true;
00063         }
00064         _SyncUniTime = uTime;
00065         _SyncLocalTime = lTime;
00066 */}

void NLNET::_CUniTime::simulate  )  [inline, static]
 

Call this method before to prevent syncUniTimeFromService() from real synchronization: syncUniTimeFromService() will still communicate with the time service, as usual, but the local time will not be synchronized.

Definition at line 96 of file unitime.h.

References _Simulate, and nlstop.

00096 { nlstop; _Simulate = true; }

void NLNET::_CUniTime::syncUniTimeFromServer CCallbackClient client  )  [static]
 

Call this functions in the init part of the client side to synchronize between client and shard. client is the connection between the client and the front end. The connection must be established before calling this function.

Definition at line 314 of file unitime.cpp.

References nlstop.

00315 {
00316         nlstop;
00317 /***************************************************************/
00318 /******************* THE FOLLOWING CODE IS COMMENTED OUT *******/
00319 /***************************************************************
00320 
00321         static bool alreadyAddedCallback = false;
00322         nlassert (client != NULL);
00323 
00324         if (!alreadyAddedCallback)
00325         {
00326                 client->addCallbackArray (ClientUniTimeCallbackArray, sizeof (ClientUniTimeCallbackArray) / sizeof (ClientUniTimeCallbackArray[0]));
00327                 alreadyAddedCallback = true;
00328         }
00329 
00330         sint attempt = 0;
00331         TTime bestdelta = 60000;        // 1 minute
00332 
00333         if (!client->connected ()) goto error;
00334 
00335         while (attempt < 10)
00336         {
00337                 CMessage msgout (client->getSIDA(), "AUT");
00338 
00339                 if (!client->connected()) goto error;
00340 
00341                 // send the message
00342                 client->send (msgout);
00343 
00344                 // before time
00345                 TTime before = CTime::getLocalTime ();
00346 
00347                 // receive the answer
00348                 GetClientUniversalTime = false;
00349                 while (!GetClientUniversalTime)
00350                 {
00351                         if (!client->connected()) goto error;
00352                                 
00353                         client->update ();
00354                 }
00355 
00356                 TTime after = CTime::getLocalTime (), delta = after - before;
00357 
00358                 if (delta < 10 || delta < bestdelta)
00359                 {
00360                         bestdelta = delta;
00361 
00362                         _CUniTime::setUniTime (GetClientUniversalTimeUniTime, (before+after)/2);
00363 
00364                         if (delta < 10) break;
00365                 }
00366                 attempt++;
00367         }
00368         client->disconnect ();
00369         nlinfo ("Universal time is %"NL_I64"dms with a mean error of %"NL_I64"dms", _CUniTime::getUniTime(), bestdelta/2);
00370         return;
00371 error:
00372         nlwarning ("there's no connection or lost or can't synchronize universal time");
00373 ***************************************************************/
00374 }

void NLNET::_CUniTime::syncUniTimeFromService CCallbackNetBase::TRecordingState  rec = CCallbackNetBase::Off,
const CInetAddress addr = NULL
[static]
 

You need to call this function before calling getUniTime or an assert will occured. This function will connect to the time service and synchronize your computer. This function assumes that all services run on server that are time synchronized with NTP for example. If addr is NULL, the function will connect to the Time Service via the Naming Service. In this case, the CNamingClient must be connected to a Naming Service. This function can be called *ONLY* by services that are inside of the shard. Don't use it for a client or a service outside of the shard.

Definition at line 157 of file unitime.cpp.

References addr, and nlstop.

00158 {
00159         nlstop;
00160 /***************************************************************/
00161 /******************* THE FOLLOWING CODE IS COMMENTED OUT *******/
00162 /***************************************************************
00163         TTime deltaAdjust, lt;
00164         uint32 firstsecond, nextsecond;
00165         TTime before, after, delta;
00166 
00167         // create a message with type in the full text format
00168         CMessage msgout ("AUT");
00169         CCallbackClient server( rec, "TS.nmr" );
00170         server.addCallbackArray (UniTimeCallbackArray, sizeof (UniTimeCallbackArray) / sizeof (UniTimeCallbackArray[0]));
00171 
00172         if (addr == NULL)
00173         {
00174                 CNamingClient::lookupAndConnect ("TS", server);
00175         }
00176         else
00177         {
00178                 server.connect (*addr);
00179         }
00180 
00181         if (!server.connected()) goto error;
00182 
00183         server.send (msgout);
00184 
00185         // before time
00186         before = CTime::getLocalTime ();
00187 
00188         // receive the answer
00189         GetUniversalTime = false;
00190         while (!GetUniversalTime)
00191         {
00192                 if (!server.connected()) goto error;
00193                         
00194                 server.update ();
00195 
00196                 nlSleep( 0 );
00197         }
00198 
00199         // after, before and delta is not used. It's only for information purpose.
00200         after = CTime::getLocalTime ();
00201         delta = after - before;
00202 
00203         nlinfo ("_CUniTime::syncUniTimeFromService(): ping:%"NL_I64"dms, time:%ds, unitime:%"NL_I64"dms", delta, GetUniversalTimeSecondsSince1970, GetUniversalTimeUniTime);
00204 
00205 // <-- from here to the "-->" comment, the block must be executed in less than one second or an infinite loop occurs
00206 
00207         // get the second
00208         firstsecond = CTime::getSecondsSince1970 ();
00209         nextsecond = firstsecond+1;
00210         
00211         // wait the next start of the second (take 100% of CPU to be more accurate)
00212         while (nextsecond != CTime::getSecondsSince1970 ())
00213                 nlassert (CTime::getSecondsSince1970 () <= nextsecond);
00214 
00215 // -->
00216 
00217         // get the local time of the beginning of the next second
00218         lt = CTime::getLocalTime ();
00219 
00220         if ( ! _Simulate )
00221         {
00222                 if (abs((sint32)((TTime)nextsecond - (TTime)GetUniversalTimeSecondsSince1970)) > 10)
00223                 {
00224                         nlerror ("the time delta (between me and the Time Service) is too big (more than 10s), servers aren't NTP synchronized");
00225                         goto error;
00226                 }
00227                 
00228                 // compute the delta between the other side and our side number of second since 1970
00229                 deltaAdjust = ((TTime) nextsecond - (TTime) GetUniversalTimeSecondsSince1970) * 1000;
00230 
00231                 // adjust the unitime to the current localtime
00232                 GetUniversalTimeUniTime += deltaAdjust;
00233 
00234                 nlinfo ("_CUniTime::syncUniTimeFromService(): rtime:%ds, runitime:%"NL_I64"ds, rlocaltime:%"NL_I64"d, deltaAjust:%"NL_I64"dms", nextsecond, GetUniversalTimeUniTime, lt, deltaAdjust);
00235         }
00236         else
00237         {
00238                 nlinfo ("_CUniTime::syncUniTimeFromService(): runitime:%"NL_I64"ds, rlocaltime:%"NL_I64"d", GetUniversalTimeUniTime, lt);
00239         }
00240 
00241         _CUniTime::setUniTime (GetUniversalTimeUniTime, lt);
00242 
00243         server.disconnect ();
00244         return;
00245 
00246 error:
00247         nlerror ("Time Service is not found, lost or can't synchronize universal time");
00248 ***************************************************************/
00249 
00250 }

double NLMISC::CTime::ticksToSecond TTicks  ticks  )  [static, inherited]
 

convert a ticks count into second. If the performance time is not supported on this hardware, it returns 0.0.

Definition at line 143 of file time_nl.cpp.

References NLMISC::CTime::getLocalTime(), NLMISC::CTime::getPerformanceTime(), sint64, NLMISC::TTicks, and NLMISC::TTime.

Referenced by NL3D::CScene::render().

00144 {
00145 #ifdef NL_OS_WINDOWS
00146         LARGE_INTEGER ret;
00147         if (QueryPerformanceFrequency(&ret))
00148         {
00149                 return (double)(sint64)ticks/(double)ret.QuadPart;
00150         }
00151         else
00152 #endif // NL_OS_WINDOWS
00153         {
00154                 static bool benchFrequency = true;
00155                 static sint64 freq = 0;
00156                 if (benchFrequency)
00157                 {
00158                         // try to have an estimation of the cpu frequency
00159 
00160                         TTicks tickBefore = getPerformanceTime ();
00161                         TTicks tickAfter = tickBefore;
00162                         TTime timeBefore = getLocalTime ();
00163                         TTime timeAfter = timeBefore;
00164                         while (true)
00165                         {
00166                                 if (timeAfter - timeBefore > 1000)
00167                                         break;
00168                                 timeAfter = getLocalTime ();
00169                                 tickAfter = getPerformanceTime ();
00170                         }
00171 
00172                         TTime timeDelta = timeAfter - timeBefore;
00173                         TTicks tickDelta = tickAfter - tickBefore;
00174 
00175                         freq = 1000 * tickDelta / timeDelta;
00176                         benchFrequency = false;
00177                 }
00178 
00179                 return (double)(sint64)ticks/(double)freq;
00180         }
00181 }


Field Documentation

bool NLNET::_CUniTime::_Simulate = false [static, private]
 

Definition at line 44 of file unitime.cpp.

Referenced by simulate().

TTime NLNET::_CUniTime::_SyncLocalTime = 0 [static, private]
 

Definition at line 43 of file unitime.cpp.

TTime NLNET::_CUniTime::_SyncUniTime = 0 [static, private]
 

Definition at line 42 of file unitime.cpp.

bool NLNET::_CUniTime::Sync = false [static]
 

Definition at line 46 of file unitime.cpp.


The documentation for this class was generated from the following files:
Generated on Tue Mar 16 13:51:20 2004 for NeL by doxygen 1.3.6