aboutsummaryrefslogtreecommitdiff
path: root/docs/doxygen/nel/unitime_8cpp-source.html
blob: ea58659a0e2a85efe2b9d1f162e9fd155d1196a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>
	<TITLE>nevrax.org : docs</TITLE>
	<LINK REL=stylesheet TYPE="text/css" HREF="http://www.nevrax.org/inc/css/nevrax.css">
	<link href="doxygen.css" rel="stylesheet" type="text/css">
</HEAD>
<BODY MARGINHEIGHT="0" MARGINWIDTH="0">

<!-- uplinks -->
<TABLE CELLSPACING=0 CELLPADDING=0  BORDER=0>
 <TR>
        <TD WIDTH=16><IMG  SRC="http://www.nevrax.org/inc/img/pixel.gif" WIDTH="16" HEIGHT="16" BORDER=0 ALT=""></TD>
        <TD WIDTH=140 BGCOLOR=#dddddd><IMG  SRC="http://www.nevrax.org/inc/img/pixel.gif" WIDTH="140" HEIGHT="16" BORDER=0 ALT=""></TD>
        <TD WIDTH=16><IMG  SRC="http://www.nevrax.org/inc/img/pixel.gif" WIDTH="16" HEIGHT="16" BORDER=0 ALT=""></TD>
        <TD><IMG width=6 height=14 SRC="http://www.nevrax.org/inc/img/reddots.gif" ALT="#" VSPACE=2 HSPACE=2 BORDER=0 ></TD><TD VALIGN=middle>&nbsp;<A CLASS=uplinks HREF=http://www.nevrax.org><b>Home</B></FONT></A>&nbsp;&nbsp;&nbsp;</TD>
        <TD><IMG  width=6 height=14  SRC="http://www.nevrax.org/inc/img/reddots.gif" ALT="#" VSPACE=2 HSPACE=2 BORDER=0 ></TD><TD VALIGN=middle>&nbsp;<A CLASS=uplinks HREF=http://www.nevrax.com><b>nevrax.com</B></FONT></A>&nbsp;&nbsp;&nbsp;</TD>
 </TR>
</TABLE> 

<!-- banner Nevrax -->
<TABLE CELLSPACING=0 CELLPADDING=0  BORDER=0 WIDTH=100%>
 <TR><TD  BGCOLOR="#000000" BACKGROUND="http://www.nevrax.org/inc/img/black_banner.jpg"><A HREF="http://www.nevrax.org"><IMG  SRC="http://www.nevrax.org/inc/img/nevrax.gif" WIDTH="170" HEIGHT="45" BORDER=0 ALT="Nevrax" ></A></TD></TR>
</TABLE>

<!-- main table -->
<TABLE CELLSPACING=0 CELLPADDING=0  BORDER=0 height=100%>
 <TR>
	<TD WIDTH=16><IMG  SRC="http://www.nevrax.org/inc/img/pixel.gif" WIDTH="16" HEIGHT="10" BORDER=0 ALT=""></TD>
	<TD WIDTH=140   BGCOLOR=#dddddd VALIGN=TOP ALIGN=middle><IMG  SRC="http://www.nevrax.org/inc/img/pixel.gif" WIDTH="140" HEIGHT="10" BORDER=0 ALT="">

		<!------ Begin Box ------>
		<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 BGCOLOR=black><TR><TD><TABLE border=0  cellspacing=2 cellpadding=0 width=120><tr><TD ALIGN=middle bgcolor=black>
		<FONT COLOR=white FACE="sans-serif"><B>Nevrax.org</B></FONT></TD></TR><tr><td  colspan=2 bgcolor=#FFFFFF>
		<TABLE cellspacing=0 cellpadding=1 border=0>
			<tr><td ALIGN=middle><a  class='linkbox' href="http://www.nevrax.org/news/" TITLE="Rubrique news"><img width=13 height=15  hspace=5 border=0 src=http://www.nevrax.org/inc/img/picto-news.gif ALT=#></A></td><td><a  class='linkbox' href="http://www.nevrax.org/news/" TITLE="News">News</a></td></tr>
			<tr><td ALIGN=middle><a  class='linkbox' href="http://www.nevrax.org/mail/" TITLE="Rubrique mail"><img width=15 height=11  hspace=5 border=0 src=http://www.nevrax.org/inc/img/picto-mail.gif ALT=#></A></td><td><a  class='linkbox' href="http://www.nevrax.org/mail/" TITLE="Mailing list archive">Mailing-list</a></td></tr>
			<tr><td ALIGN=middle><a  class='linkbox' href="http://www.nevrax.org/docs/" TITLE="Rubrique docs"><img width=14 height=16  hspace=5 border=0 src=http://www.nevrax.org/inc/img/picto-docs.gif ALT=#></A></td><td><a  class='linkbox' href="http://www.nevrax.org/docs/" TITLE="Documentation">Documentation</a></td></tr>
			<tr><td ALIGN=middle><a  class='linkbox' href="http://www.nevrax.org/cvs/" TITLE="Rubrique cvs"><img width=13 height=17  hspace=5 border=0 src=http://www.nevrax.org/inc/img/picto-cvs.gif ALT=#></A></td><td><a  class='linkbox' href="http://www.nevrax.org/cvs/" TITLE="CVS Web">CVS</a></td></tr>
			<tr><td ALIGN=middle><a  class='linkbox' href="http://www.nevrax.org/bugs/" TITLE="Rubrique bugs"><img width=20 height=16  hspace=5 border=0 src=http://www.nevrax.org/inc/img/picto-bugs.gif ALT=#></A></td><td><a  class='linkbox' href="http://www.nevrax.org/bugs/" TITLE="Bugtracking">Bugs</a></td></tr>
			<tr><td ALIGN=middle><a  class='linkbox' href="http://www.nevrax.org/GPL.php3" TITLE="Rubrique license"><img  width=18 height=12   hspace=5 border=0 src=http://www.nevrax.org/inc/img/picto-gpl.gif ALT=#></A></td><td><a  class='linkbox' href="http://www.nevrax.org/GPL.php3" TITLE="License">License</a></td></tr>
		</TABLE>
		</TD></TR></TABLE></TD></TR></TABLE>
		<!------ End Box  ------>

	</TD>
	<TD WIDTH=15><IMG  SRC="http://www.nevrax.org/inc/img/pixel.gif" WIDTH="16" HEIGHT="16" BORDER=0 ALT=""></TD>
	<TD ALIGN=left valign=top><IMG  SRC="http://www.nevrax.org/inc/img/pixel.gif" WIDTH="140" HEIGHT="10" BORDER=0 ALT="">

<!-- title -->
<TABLE  background="http://www.nevrax.org/inc/img/redline.gif" CELLSPACING=0 CELLPADDING=0  BORDER=0 width=100%><tr><td>
<A HREF="http://www.nevrax.org/docs/"><img src="http://www.nevrax.org/inc/img/t_docs.gif" ALT="Docs" HEIGHT=20 BORDER=0></A>
</td><td><IMG  SRC="http://www.nevrax.org/inc/img/pixel.gif" WIDTH="1" HEIGHT="1" BORDER=0 ALT="">
</td></tr></table>
&nbsp;

<!-- block -->
<TABLE  bgcolor="#dddddd" CELLSPACING=0 CELLPADDING=0  BORDER=0 width=100%><tr><td width=1% valign=middle><img width=6 height=14 hspace=2 vspace=2 src="http://www.nevrax.org/inc/img/reddots.gif"></TD>
	<TD><B>Documentation</B></TD>
	<TD ALIGN=RIGHT>&nbsp;</td>
</tr></table>
<!-- Generated by Doxygen 1.2.14 -->
<center>
<a class="qindex" href="index.html">Main Page</a> &nbsp; <a class="qindex" href="namespaces.html">Namespace List</a> &nbsp; <a class="qindex" href="hierarchy.html">Class Hierarchy</a> &nbsp; <a class="qindex" href="classes.html">Alphabetical List</a> &nbsp; <a class="qindex" href="annotated.html">Compound List</a> &nbsp; <a class="qindex" href="files.html">File List</a> &nbsp; <a class="qindex" href="namespacemembers.html">Namespace Members</a> &nbsp; <a class="qindex" href="functions.html">Compound Members</a> &nbsp; <a class="qindex" href="globals.html">File Members</a> &nbsp; <a class="qindex" href="pages.html">Related Pages</a> &nbsp; <a class="qindexRef" doxygen="_cgi:http://www.nevrax.org/cgi-bin/nel-search.cgi" href="http://www.nevrax.org/cgi-bin/nel-search.cgi">Search</a> &nbsp; </center>
<hr><h1>unitime.cpp</h1><a href="unitime_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><pre>00001 
00007 <font class="comment">/* Copyright, 2000 Nevrax Ltd.</font>
00008 <font class="comment"> *</font>
00009 <font class="comment"> * This file is part of NEVRAX NEL.</font>
00010 <font class="comment"> * NEVRAX NEL is free software; you can redistribute it and/or modify</font>
00011 <font class="comment"> * it under the terms of the GNU General Public License as published by</font>
00012 <font class="comment"> * the Free Software Foundation; either version 2, or (at your option)</font>
00013 <font class="comment"> * any later version.</font>
00014 <font class="comment"></font>
00015 <font class="comment"> * NEVRAX NEL is distributed in the hope that it will be useful, but</font>
00016 <font class="comment"> * WITHOUT ANY WARRANTY; without even the implied warranty of</font>
00017 <font class="comment"> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU</font>
00018 <font class="comment"> * General Public License for more details.</font>
00019 <font class="comment"></font>
00020 <font class="comment"> * You should have received a copy of the GNU General Public License</font>
00021 <font class="comment"> * along with NEVRAX NEL; see the file COPYING. If not, write to the</font>
00022 <font class="comment"> * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,</font>
00023 <font class="comment"> * MA 02111-1307, USA.</font>
00024 <font class="comment"> */</font>
00025 
00026 <font class="preprocessor">#include "<a class="code" href="stdnet_8h.html">stdnet.h</a>"</font>
00027 
00028 <font class="preprocessor">#include "<a class="code" href="callback__client_8h.html">nel/net/callback_client.h</a>"</font>
00029 <font class="preprocessor">#include "<a class="code" href="callback__server_8h.html">nel/net/callback_server.h</a>"</font>
00030 <font class="preprocessor">#include "<a class="code" href="naming__client_8h.html">nel/net/naming_client.h</a>"</font>
00031 <font class="preprocessor">#include "<a class="code" href="message_8h.html">nel/net/message.h</a>"</font>
00032 
00033 <font class="preprocessor">#include "<a class="code" href="unitime_8h.html">nel/net/unitime.h</a>"</font>
00034 
00035 <font class="keyword">using</font> <font class="keyword">namespace </font>NLMISC;
00036 <font class="keyword">using</font> <font class="keyword">namespace </font>std;
00037 
00038 <font class="keyword">namespace </font>NLNET
00039 {
00040 
<a name="l00041"></a><a class="code" href="classNLNET_1_1__CUniTime.html#r0">00041</a> <a class="code" href="namespaceNLMISC.html#a183">TTime</a> _CUniTime::_SyncUniTime = 0;
<a name="l00042"></a><a class="code" href="classNLNET_1_1__CUniTime.html#r1">00042</a> <a class="code" href="namespaceNLMISC.html#a183">TTime</a> _CUniTime::_SyncLocalTime = 0;
<a name="l00043"></a><a class="code" href="classNLNET_1_1__CUniTime.html#r2">00043</a> <font class="keywordtype">bool</font> _CUniTime::_Simulate = <font class="keyword">false</font>;
00044 
<a name="l00045"></a><a class="code" href="classNLNET_1_1__CUniTime.html#p0">00045</a> <font class="keywordtype">bool</font> _CUniTime::Sync = <font class="keyword">false</font>;
00046 
00047 
<a name="l00048"></a><a class="code" href="classNLNET_1_1__CUniTime.html#d6">00048</a> <font class="keywordtype">void</font> _CUniTime::setUniTime (NLMISC::TTime uTime, NLMISC::TTime lTime)
00049 {
00050         <a class="code" href="debug_8h.html#a12">nlstop</a>;
00051 <font class="comment">/*      if (Sync)</font>
00052 <font class="comment">        {</font>
00053 <font class="comment">                TTime lt = getLocalTime ();</font>
00054 <font class="comment">                TTime delta = uTime - lTime + _SyncLocalTime - _SyncUniTime;</font>
00055 <font class="comment"></font>
00056 <font class="comment">                nlinfo ("_CUniTime::setUniTime(%"NL_I64"d, %"NL_I64"d): Resyncing delta %"NL_I64"dms",uTime,lTime,delta);</font>
00057 <font class="comment">        }</font>
00058 <font class="comment">        else</font>
00059 <font class="comment">        {</font>
00060 <font class="comment">                nlinfo ("_CUniTime::setUniTime(%"NL_I64"d, %"NL_I64"d)",uTime,lTime);</font>
00061 <font class="comment">                Sync = true;</font>
00062 <font class="comment">        }</font>
00063 <font class="comment">        _SyncUniTime = uTime;</font>
00064 <font class="comment">        _SyncLocalTime = lTime;</font>
00065 <font class="comment">*/</font>}
00066 
<a name="l00067"></a><a class="code" href="classNLNET_1_1__CUniTime.html#d7">00067</a> <font class="keywordtype">void</font> _CUniTime::setUniTime (NLMISC::TTime uTime)
00068 {
00069         <a class="code" href="debug_8h.html#a12">nlstop</a>;
00070 <font class="comment">//      setUniTime (uTime, getLocalTime ());</font>
00071 }
00072 
00073 
00074 
<a name="l00075"></a><a class="code" href="classNLNET_1_1__CUniTime.html#d0">00075</a> <a class="code" href="namespaceNLMISC.html#a183">TTime</a> _CUniTime::getUniTime ()
00076 {
00077         <a class="code" href="debug_8h.html#a12">nlstop</a>;
00078         <font class="keywordflow">return</font> 0;
00079 <font class="comment">/*      if (!Sync)</font>
00080 <font class="comment">        {</font>
00081 <font class="comment">                nlerror ("called getUniTime before calling syncUniTimeFromServer");</font>
00082 <font class="comment">        }</font>
00083 <font class="comment">        return getLocalTime () - (_SyncLocalTime - _SyncUniTime);</font>
00084 <font class="comment">*/</font>
00085 }
00086 
00087 
<a name="l00088"></a><a class="code" href="classNLNET_1_1__CUniTime.html#d1">00088</a> <font class="keyword">const</font> <font class="keywordtype">char</font> *_CUniTime::getStringUniTime ()
00089 {
00090         <a class="code" href="debug_8h.html#a12">nlstop</a>;
00091         <font class="keywordflow">return</font> <a class="code" href="classNLNET_1_1__CUniTime.html#d1">getStringUniTime</a>(_CUniTime::getUniTime());
00092 }
00093 
00094 
<a name="l00095"></a><a class="code" href="classNLNET_1_1__CUniTime.html#d2">00095</a> <font class="keyword">const</font> <font class="keywordtype">char</font> *_CUniTime::getStringUniTime (<a class="code" href="namespaceNLMISC.html#a183">TTime</a> ut)
00096 {
00097         <a class="code" href="debug_8h.html#a12">nlstop</a>;
00098         <font class="keyword">static</font> <font class="keywordtype">char</font> str[512];
00099 
00100         uint32 ms = (uint32) (ut % 1000); <font class="comment">// time in ms 1000ms dans 1s</font>
00101         ut /= 1000;
00102 
00103         uint32 <a class="code" href="driver__opengl__extension__def_8h.html#a383">s</a> = (uint32) (ut % 60); <font class="comment">// time in seconds 60s dans 1mn</font>
00104         ut /= 60;
00105 
00106         uint32 m = (uint32) (ut % 60); <font class="comment">// time in minutes 60m dans 1h</font>
00107         ut /= 60;
00108 
00109         uint32 h = (uint32) (ut % 9); <font class="comment">// time in hours 9h dans 1j</font>
00110         ut /= 9;
00111 
00112         uint32 day = (uint32) (ut % (8*4)); <font class="comment">// time in days 8day dans 1month</font>
00113         ut /= 8;
00114 
00115         uint32 week = (uint32) (ut % 4); <font class="comment">// time in weeks 4week dans 1month</font>
00116         ut /= 4;
00117 
00118         uint32 month = (uint32) (ut % 12); <font class="comment">// time in months 12month dans 1year</font>
00119         ut /= 12;
00120 
00121         uint  year =  (uint32) ut;      <font class="comment">// time in years</font>
00122 
00123         <a class="code" href="namespaceNLMISC.html#a211">smprintf</a> (str, 512, <font class="stringliteral">"%02d/%02d/%04d (week %d) %02d:%02d:%02d.%03d"</font>, day+1, month+1, year+1, week+1, h, m, <a class="code" href="driver__opengl__extension__def_8h.html#a383">s</a>, ms);
00124         <font class="keywordflow">return</font> str;
00125 }
00126 
00132 
00133 <font class="keyword">static</font> <font class="keywordtype">bool</font> <a class="code" href="namespaceNLNET.html#a72">GetUniversalTime</a>;
00134 <font class="keyword">static</font> uint32 <a class="code" href="namespaceNLNET.html#a73">GetUniversalTimeSecondsSince1970</a>;
00135 <font class="keyword">static</font> <a class="code" href="namespaceNLMISC.html#a183">TTime</a> <a class="code" href="namespaceNLNET.html#a74">GetUniversalTimeUniTime</a>;
00136 
00137 
00138 <font class="keyword">static</font> <font class="keywordtype">void</font> <a class="code" href="namespaceNLNET.html#a153">cbGetUniversalTime</a> (CMessage &amp;msgin, <a class="code" href="namespaceNLNET.html#a0">TSockId</a> from, CCallbackNetBase &amp;netbase)
00139 {
00140         <a class="code" href="debug_8h.html#a12">nlstop</a>;
00141         <font class="comment">// get the association between a date and unitime</font>
00142         msgin.serial (<a class="code" href="namespaceNLNET.html#a73">GetUniversalTimeSecondsSince1970</a>);
00143         msgin.serial (<a class="code" href="namespaceNLNET.html#a74">GetUniversalTimeUniTime</a>);
00144         <a class="code" href="namespaceNLNET.html#a72">GetUniversalTime</a> = <font class="keyword">true</font>;
00145 }
00146 
00147 <font class="comment">/***************************************************************/</font>
00148 <font class="comment">/******************* THE FOLLOWING CODE IS COMMENTED OUT *******/</font>
00149 <font class="comment">/***************************************************************</font>
00150 <font class="comment">static TCallbackItem UniTimeCallbackArray[] =</font>
00151 <font class="comment">{</font>
00152 <font class="comment">        { "GUT", cbGetUniversalTime }</font>
00153 <font class="comment">};</font>
00154 <font class="comment">***************************************************************/</font>
00155 
<a name="l00156"></a><a class="code" href="classNLNET_1_1__CUniTime.html#d3">00156</a> <font class="keywordtype">void</font> _CUniTime::syncUniTimeFromService (CCallbackNetBase::TRecordingState rec, <font class="keyword">const</font> CInetAddress *<a class="code" href="driver__opengl__extension__def_8h.html#a414">addr</a>)
00157 {
00158         <a class="code" href="debug_8h.html#a12">nlstop</a>;
00159 <font class="comment">/***************************************************************/</font>
00160 <font class="comment">/******************* THE FOLLOWING CODE IS COMMENTED OUT *******/</font>
00161 <font class="comment">/***************************************************************</font>
00162 <font class="comment">        TTime deltaAdjust, lt;</font>
00163 <font class="comment">        uint32 firstsecond, nextsecond;</font>
00164 <font class="comment">        TTime before, after, delta;</font>
00165 <font class="comment"></font>
00166 <font class="comment">        // create a message with type in the full text format</font>
00167 <font class="comment">        CMessage msgout ("AUT");</font>
00168 <font class="comment">        CCallbackClient server( rec, "TS.nmr" );</font>
00169 <font class="comment">        server.addCallbackArray (UniTimeCallbackArray, sizeof (UniTimeCallbackArray) / sizeof (UniTimeCallbackArray[0]));</font>
00170 <font class="comment"></font>
00171 <font class="comment">        if (addr == NULL)</font>
00172 <font class="comment">        {</font>
00173 <font class="comment">                CNamingClient::lookupAndConnect ("TS", server);</font>
00174 <font class="comment">        }</font>
00175 <font class="comment">        else</font>
00176 <font class="comment">        {</font>
00177 <font class="comment">                server.connect (*addr);</font>
00178 <font class="comment">        }</font>
00179 <font class="comment"></font>
00180 <font class="comment">        if (!server.connected()) goto error;</font>
00181 <font class="comment"></font>
00182 <font class="comment">        server.send (msgout);</font>
00183 <font class="comment"></font>
00184 <font class="comment">        // before time</font>
00185 <font class="comment">        before = CTime::getLocalTime ();</font>
00186 <font class="comment"></font>
00187 <font class="comment">        // receive the answer</font>
00188 <font class="comment">        GetUniversalTime = false;</font>
00189 <font class="comment">        while (!GetUniversalTime)</font>
00190 <font class="comment">        {</font>
00191 <font class="comment">                if (!server.connected()) goto error;</font>
00192 <font class="comment">                        </font>
00193 <font class="comment">                server.update ();</font>
00194 <font class="comment"></font>
00195 <font class="comment">                nlSleep( 0 );</font>
00196 <font class="comment">        }</font>
00197 <font class="comment"></font>
00198 <font class="comment">        // after, before and delta is not used. It's only for information purpose.</font>
00199 <font class="comment">        after = CTime::getLocalTime ();</font>
00200 <font class="comment">        delta = after - before;</font>
00201 <font class="comment"></font>
00202 <font class="comment">        nlinfo ("_CUniTime::syncUniTimeFromService(): ping:%"NL_I64"dms, time:%ds, unitime:%"NL_I64"dms", delta, GetUniversalTimeSecondsSince1970, GetUniversalTimeUniTime);</font>
00203 <font class="comment"></font>
00204 <font class="comment">// &lt;-- from here to the "--&gt;" comment, the block must be executed in less than one second or an infinite loop occurs</font>
00205 <font class="comment"></font>
00206 <font class="comment">        // get the second</font>
00207 <font class="comment">        firstsecond = CTime::getSecondsSince1970 ();</font>
00208 <font class="comment">        nextsecond = firstsecond+1;</font>
00209 <font class="comment">        </font>
00210 <font class="comment">        // wait the next start of the second (take 100% of CPU to be more accurate)</font>
00211 <font class="comment">        while (nextsecond != CTime::getSecondsSince1970 ())</font>
00212 <font class="comment">                nlassert (CTime::getSecondsSince1970 () &lt;= nextsecond);</font>
00213 <font class="comment"></font>
00214 <font class="comment">// --&gt;</font>
00215 <font class="comment"></font>
00216 <font class="comment">        // get the local time of the beginning of the next second</font>
00217 <font class="comment">        lt = CTime::getLocalTime ();</font>
00218 <font class="comment"></font>
00219 <font class="comment">        if ( ! _Simulate )</font>
00220 <font class="comment">        {</font>
00221 <font class="comment">                if (abs((sint32)((TTime)nextsecond - (TTime)GetUniversalTimeSecondsSince1970)) &gt; 10)</font>
00222 <font class="comment">                {</font>
00223 <font class="comment">                        nlerror ("the time delta (between me and the Time Service) is too big (more than 10s), servers aren't NTP synchronized");</font>
00224 <font class="comment">                        goto error;</font>
00225 <font class="comment">                }</font>
00226 <font class="comment">                </font>
00227 <font class="comment">                // compute the delta between the other side and our side number of second since 1970</font>
00228 <font class="comment">                deltaAdjust = ((TTime) nextsecond - (TTime) GetUniversalTimeSecondsSince1970) * 1000;</font>
00229 <font class="comment"></font>
00230 <font class="comment">                // adjust the unitime to the current localtime</font>
00231 <font class="comment">                GetUniversalTimeUniTime += deltaAdjust;</font>
00232 <font class="comment"></font>
00233 <font class="comment">                nlinfo ("_CUniTime::syncUniTimeFromService(): rtime:%ds, runitime:%"NL_I64"ds, rlocaltime:%"NL_I64"d, deltaAjust:%"NL_I64"dms", nextsecond, GetUniversalTimeUniTime, lt, deltaAdjust);</font>
00234 <font class="comment">        }</font>
00235 <font class="comment">        else</font>
00236 <font class="comment">        {</font>
00237 <font class="comment">                nlinfo ("_CUniTime::syncUniTimeFromService(): runitime:%"NL_I64"ds, rlocaltime:%"NL_I64"d", GetUniversalTimeUniTime, lt);</font>
00238 <font class="comment">        }</font>
00239 <font class="comment"></font>
00240 <font class="comment">        _CUniTime::setUniTime (GetUniversalTimeUniTime, lt);</font>
00241 <font class="comment"></font>
00242 <font class="comment">        server.disconnect ();</font>
00243 <font class="comment">        return;</font>
00244 <font class="comment"></font>
00245 <font class="comment">error:</font>
00246 <font class="comment">        nlerror ("Time Service is not found, lost or can't synchronize universal time");</font>
00247 <font class="comment">***************************************************************/</font>
00248 
00249 }
00250 
00251 
00252 
00258 
00259 <font class="comment">// Server part</font>
00260 
00261 <font class="keyword">static</font> <font class="keywordtype">void</font> <a class="code" href="namespaceNLNET.html#a154">cbServerAskUniversalTime</a> (CMessage&amp; msgin, <a class="code" href="namespaceNLNET.html#a0">TSockId</a> from, CCallbackNetBase &amp;netbase)
00262 {
00263         <a class="code" href="debug_8h.html#a12">nlstop</a>;
00264         <a class="code" href="namespaceNLMISC.html#a183">TTime</a> ut = _CUniTime::getUniTime ();
00265 
00266         <font class="comment">// afficher l adresse de celui qui demande</font>
00267         <a class="code" href="debug_8h.html#a1">nlinfo</a>(<font class="stringliteral">"Send the universal time %"</font>NL_I64<font class="stringliteral">"d to '%s'"</font>, ut, netbase.hostAddress(from).asString().c_str());
00268         
00269         CMessage msgout (netbase.getSIDA(), <font class="stringliteral">"GUT"</font>);
00270         msgout.serial (ut);
00271         netbase.send (msgout, from);
00272 }
00273 
00274 TCallbackItem <a class="code" href="namespaceNLNET.html#a75">ServerTimeServiceCallbackArray</a>[] =
00275 {
00276         { <font class="stringliteral">"AUT"</font>, <a class="code" href="namespaceNLNET.html#a154">cbServerAskUniversalTime</a> },
00277 };
00278 
<a name="l00279"></a><a class="code" href="classNLNET_1_1__CUniTime.html#d4">00279</a> <font class="keywordtype">void</font> _CUniTime::installServer (CCallbackServer *server)
00280 {
00281         <a class="code" href="debug_8h.html#a12">nlstop</a>;
00282         <font class="keyword">static</font> <font class="keywordtype">bool</font> alreadyAddedCallback = <font class="keyword">false</font>;
00283         <a class="code" href="debug_8h.html#a6">nlassert</a> (server != NULL);
00284         <a class="code" href="debug_8h.html#a6">nlassert</a> (!alreadyAddedCallback);
00285 
00286         server-&gt;addCallbackArray (<a class="code" href="namespaceNLNET.html#a75">ServerTimeServiceCallbackArray</a>, <font class="keyword">sizeof</font> (<a class="code" href="namespaceNLNET.html#a75">ServerTimeServiceCallbackArray</a>) / <font class="keyword">sizeof</font> (<a class="code" href="namespaceNLNET.html#a75">ServerTimeServiceCallbackArray</a>[0]));
00287         alreadyAddedCallback = <font class="keyword">true</font>;
00288 }
00289 
00290 <font class="comment">// Client part</font>
00291 
00292 <font class="keyword">static</font> <font class="keywordtype">bool</font> <a class="code" href="namespaceNLNET.html#a76">GetClientUniversalTime</a>;
00293 <font class="keyword">static</font> <a class="code" href="namespaceNLMISC.html#a183">TTime</a> <a class="code" href="namespaceNLNET.html#a77">GetClientUniversalTimeUniTime</a>;
00294 
00295 <font class="keyword">static</font> <font class="keywordtype">void</font> <a class="code" href="namespaceNLNET.html#a155">cbClientGetUniversalTime</a> (CMessage &amp;msgin, <a class="code" href="namespaceNLNET.html#a0">TSockId</a> from, CCallbackNetBase &amp;netbase)
00296 {
00297         <a class="code" href="debug_8h.html#a12">nlstop</a>;
00298         <font class="comment">// get the association between a date and unitime</font>
00299         msgin.serial (<a class="code" href="namespaceNLNET.html#a77">GetClientUniversalTimeUniTime</a>);
00300         <a class="code" href="namespaceNLNET.html#a76">GetClientUniversalTime</a> = <font class="keyword">true</font>;
00301 }
00302 
00303 <font class="comment">/***************************************************************/</font>
00304 <font class="comment">/******************* THE FOLLOWING CODE IS COMMENTED OUT *******/</font>
00305 <font class="comment">/***************************************************************</font>
00306 <font class="comment">static TCallbackItem ClientUniTimeCallbackArray[] =</font>
00307 <font class="comment">{</font>
00308 <font class="comment">        { "GUT", cbClientGetUniversalTime }</font>
00309 <font class="comment">};</font>
00310 <font class="comment">***************************************************************/</font>
00311 
00312 
<a name="l00313"></a><a class="code" href="classNLNET_1_1__CUniTime.html#d5">00313</a> <font class="keywordtype">void</font> _CUniTime::syncUniTimeFromServer (CCallbackClient *client)
00314 {
00315         <a class="code" href="debug_8h.html#a12">nlstop</a>;
00316 <font class="comment">/***************************************************************/</font>
00317 <font class="comment">/******************* THE FOLLOWING CODE IS COMMENTED OUT *******/</font>
00318 <font class="comment">/***************************************************************</font>
00319 <font class="comment"></font>
00320 <font class="comment">        static bool alreadyAddedCallback = false;</font>
00321 <font class="comment">        nlassert (client != NULL);</font>
00322 <font class="comment"></font>
00323 <font class="comment">        if (!alreadyAddedCallback)</font>
00324 <font class="comment">        {</font>
00325 <font class="comment">                client-&gt;addCallbackArray (ClientUniTimeCallbackArray, sizeof (ClientUniTimeCallbackArray) / sizeof (ClientUniTimeCallbackArray[0]));</font>
00326 <font class="comment">                alreadyAddedCallback = true;</font>
00327 <font class="comment">        }</font>
00328 <font class="comment"></font>
00329 <font class="comment">        sint attempt = 0;</font>
00330 <font class="comment">        TTime bestdelta = 60000;        // 1 minute</font>
00331 <font class="comment"></font>
00332 <font class="comment">        if (!client-&gt;connected ()) goto error;</font>
00333 <font class="comment"></font>
00334 <font class="comment">        while (attempt &lt; 10)</font>
00335 <font class="comment">        {</font>
00336 <font class="comment">                CMessage msgout (client-&gt;getSIDA(), "AUT");</font>
00337 <font class="comment"></font>
00338 <font class="comment">                if (!client-&gt;connected()) goto error;</font>
00339 <font class="comment"></font>
00340 <font class="comment">                // send the message</font>
00341 <font class="comment">                client-&gt;send (msgout);</font>
00342 <font class="comment"></font>
00343 <font class="comment">                // before time</font>
00344 <font class="comment">                TTime before = CTime::getLocalTime ();</font>
00345 <font class="comment"></font>
00346 <font class="comment">                // receive the answer</font>
00347 <font class="comment">                GetClientUniversalTime = false;</font>
00348 <font class="comment">                while (!GetClientUniversalTime)</font>
00349 <font class="comment">                {</font>
00350 <font class="comment">                        if (!client-&gt;connected()) goto error;</font>
00351 <font class="comment">                                </font>
00352 <font class="comment">                        client-&gt;update ();</font>
00353 <font class="comment">                }</font>
00354 <font class="comment"></font>
00355 <font class="comment">                TTime after = CTime::getLocalTime (), delta = after - before;</font>
00356 <font class="comment"></font>
00357 <font class="comment">                if (delta &lt; 10 || delta &lt; bestdelta)</font>
00358 <font class="comment">                {</font>
00359 <font class="comment">                        bestdelta = delta;</font>
00360 <font class="comment"></font>
00361 <font class="comment">                        _CUniTime::setUniTime (GetClientUniversalTimeUniTime, (before+after)/2);</font>
00362 <font class="comment"></font>
00363 <font class="comment">                        if (delta &lt; 10) break;</font>
00364 <font class="comment">                }</font>
00365 <font class="comment">                attempt++;</font>
00366 <font class="comment">        }</font>
00367 <font class="comment">        client-&gt;disconnect ();</font>
00368 <font class="comment">        nlinfo ("Universal time is %"NL_I64"dms with a mean error of %"NL_I64"dms", _CUniTime::getUniTime(), bestdelta/2);</font>
00369 <font class="comment">        return;</font>
00370 <font class="comment">error:</font>
00371 <font class="comment">        nlwarning ("there's no connection or lost or can't synchronize universal time");</font>
00372 <font class="comment">***************************************************************/</font>
00373 }
00374 
00375 
00376 <font class="comment">//</font>
00377 <font class="comment">// Commands</font>
00378 <font class="comment">//</font>
00379 <font class="comment">/*</font>
00380 <font class="comment">NLMISC_COMMAND (time, "displays the universal time", "")</font>
00381 <font class="comment">{</font>
00382 <font class="comment">        if(args.size() != 0) return false;</font>
00383 <font class="comment"></font>
00384 <font class="comment">        if ( _CUniTime::Sync )</font>
00385 <font class="comment">        {</font>
00386 <font class="comment">                log.displayNL ("CTime::getLocalTime(): %"NL_I64"dms, _CUniTime::getUniTime(): %"NL_I64"dms", CTime::getLocalTime (), _CUniTime::getUniTime ());</font>
00387 <font class="comment">                log.displayNL ("_CUniTime::getStringUniTime(): '%s'", _CUniTime::getStringUniTime());</font>
00388 <font class="comment">        }</font>
00389 <font class="comment">        else</font>
00390 <font class="comment">        {</font>
00391 <font class="comment">                log.displayNL ("CTime::getLocalTime(): %"NL_I64"dms &lt;Universal time not sync&gt;", CTime::getLocalTime ());</font>
00392 <font class="comment">        }</font>
00393 <font class="comment"></font>
00394 <font class="comment">        return true;</font>
00395 <font class="comment">}</font>
00396 <font class="comment">*/</font>
00397 
00398 } <font class="comment">// NLNET</font>
</pre></div>

<!-- footer -->
<BR><FONT Size=+5>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </FONT>
</TD>
<TD WIDTH=15><IMG  SRC=http://www.nevrax.org/inc/img/pixel.gif WIDTH=15 HEIGHT=15 BORDER=0 ALT=""></TD>
</TR>
</TABLE>
</BODY>
</HTML>