~freenx-team/nx-x11/nxcomp-upstream

« back to all changes in this revision

Viewing changes to Timestamp.h

  • Committer: Marcelo Boveto Shima
  • Date: 2009-03-28 22:24:56 UTC
  • Revision ID: mshima@ufserv-20090328222456-rdtaq3oedfyq890c
Import nxcomp 3.3.0-3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**************************************************************************/
 
2
/*                                                                        */
 
3
/* Copyright (c) 2001, 2007 NoMachine, http://www.nomachine.com/.         */
 
4
/*                                                                        */
 
5
/* NXCOMP, NX protocol compression and NX extensions to this software     */
 
6
/* are copyright of NoMachine. Redistribution and use of the present      */
 
7
/* software is allowed according to terms specified in the file LICENSE   */
 
8
/* which comes in the source distribution.                                */
 
9
/*                                                                        */
 
10
/* Check http://www.nomachine.com/licensing.html for applicability.       */
 
11
/*                                                                        */
 
12
/* NX and NoMachine are trademarks of NoMachine S.r.l.                    */
 
13
/*                                                                        */
 
14
/* All rights reserved.                                                   */
 
15
/*                                                                        */
 
16
/**************************************************************************/
 
17
 
 
18
#ifndef Timestamp_H
 
19
#define Timestamp_H
 
20
 
 
21
#include <stdio.h>
 
22
#include <stdlib.h>
 
23
#include <string.h>
 
24
 
 
25
#include <time.h>
 
26
#include <sys/time.h>
 
27
 
 
28
#include "Misc.h"
 
29
 
 
30
//
 
31
// Log level.
 
32
//
 
33
 
 
34
#define PANIC
 
35
#define WARNING
 
36
#undef  TEST
 
37
#undef  DEBUG
 
38
 
 
39
//
 
40
// If not defined, always query the system time.
 
41
//
 
42
 
 
43
#undef  CACHE_TIMESTAMP
 
44
 
 
45
//
 
46
// Log a warning if the time difference since
 
47
// the last update exceeds the given number
 
48
// of milliseconds.
 
49
//
 
50
 
 
51
#define DRIFT_TIMESTAMP  1
 
52
 
 
53
//
 
54
// Type used for timeout manipulation.
 
55
//
 
56
 
 
57
typedef struct timeval T_timestamp;
 
58
 
 
59
//
 
60
// Last timestamp taken from the system. If the
 
61
// timestamp is cached, we need to explicitly
 
62
// get a new timestamp after any operation that
 
63
// may have required a relevant amount of time.
 
64
//
 
65
 
 
66
extern T_timestamp timestamp;
 
67
 
 
68
//
 
69
// Get a timestamp instance with values set
 
70
// at the given amount of milliseconds.
 
71
//
 
72
 
 
73
inline T_timestamp getTimestamp(long ms)
 
74
{
 
75
  struct timeval ts;
 
76
 
 
77
  ts.tv_sec  = ms / 1000;
 
78
  ts.tv_usec = (ms % 1000) * 1000;
 
79
 
 
80
  return ts;
 
81
}
 
82
 
 
83
//
 
84
// Return the difference in milliseconds
 
85
// between the two timestamps.
 
86
//
 
87
 
 
88
inline long diffTimestamp(const T_timestamp &ts1, const T_timestamp &ts2)
 
89
{
 
90
  //
 
91
  // Add 500 microseconds to round up
 
92
  // to the nearest millisecond.
 
93
  //
 
94
 
 
95
  return ((ts2.tv_sec * 1000 + (ts2.tv_usec + 500) / 1000) -
 
96
             (ts1.tv_sec * 1000 + (ts1.tv_usec + 500) / 1000));
 
97
}
 
98
 
 
99
//
 
100
// The same in microseconds. It doesn't
 
101
// round the value.
 
102
//
 
103
 
 
104
inline long diffUsTimestamp(const T_timestamp &ts1, const T_timestamp &ts2)
 
105
{
 
106
  return ((ts2.tv_sec * 1000000 + ts2.tv_usec) -
 
107
             (ts1.tv_sec * 1000000 + ts1.tv_usec));
 
108
}
 
109
 
 
110
//
 
111
// Return the last timestamp taken from the
 
112
// system. It doesn't update the timestamp.
 
113
//
 
114
 
 
115
inline T_timestamp getTimestamp()
 
116
{
 
117
  #ifdef CACHE_TIMESTAMP
 
118
 
 
119
  #ifdef TEST
 
120
 
 
121
  T_timestamp ts;
 
122
 
 
123
  gettimeofday(&ts, NULL);
 
124
 
 
125
  long diffTs = diffTimestamp(timestamp, ts);
 
126
 
 
127
  if (diffTs > DRIFT_TIMESTAMP)
 
128
  {
 
129
    *logofs << "Timestamp: WARNING! Time difference since the "
 
130
            << "current timestamp is " << diffTs << " Ms.\n"
 
131
            << logofs_flush;
 
132
  }
 
133
 
 
134
  #endif
 
135
 
 
136
  return timestamp;
 
137
 
 
138
  #else
 
139
 
 
140
  gettimeofday(&timestamp, NULL);
 
141
 
 
142
  return timestamp;
 
143
 
 
144
  #endif
 
145
}
 
146
 
 
147
inline T_timestamp &setTimestamp(T_timestamp &ts, long ms)
 
148
{
 
149
  ts.tv_sec  = ms / 1000;
 
150
  ts.tv_usec = (ms % 1000) * 1000;
 
151
 
 
152
  return ts;
 
153
}
 
154
 
 
155
//
 
156
// Return the smaller between two timestamps.
 
157
//
 
158
 
 
159
inline T_timestamp &setMinTimestamp(T_timestamp &ts, long ms)
 
160
{
 
161
  if ((ts.tv_sec * 1000 + ts.tv_usec / 1000) > ms)
 
162
  {
 
163
    ts.tv_sec  = ms / 1000;
 
164
    ts.tv_usec = (ms % 1000) * 1000;
 
165
  }
 
166
 
 
167
  return ts;
 
168
}
 
169
 
 
170
inline T_timestamp &setMinTimestamp(T_timestamp &ts1, T_timestamp &ts2)
 
171
{
 
172
  if ((ts1.tv_sec * 1000000 + ts1.tv_usec) >
 
173
           (ts2.tv_sec * 1000000 + ts2.tv_usec))
 
174
  {
 
175
    ts1.tv_sec  = ts2.tv_sec;
 
176
    ts1.tv_usec = ts2.tv_usec;
 
177
  }
 
178
 
 
179
  return ts1;
 
180
}
 
181
 
 
182
//
 
183
// Convert a timestamp in the total number
 
184
// of milliseconds.
 
185
//
 
186
 
 
187
inline long getMsTimestamp(const T_timestamp &ts)
 
188
{
 
189
  return ts.tv_sec * 1000 + ts.tv_usec / 1000;
 
190
}
 
191
 
 
192
//
 
193
// A 0 value on both seconds and microseconds
 
194
// fields means that timestamp is invalid or
 
195
// not set.
 
196
//
 
197
 
 
198
inline T_timestamp nullTimestamp()
 
199
{
 
200
  struct timeval ts;
 
201
 
 
202
  ts.tv_sec  = 0;
 
203
  ts.tv_usec = 0;
 
204
 
 
205
  return ts;
 
206
}
 
207
 
 
208
inline bool isTimestamp(const T_timestamp &ts)
 
209
{
 
210
  if (ts.tv_sec == 0 && ts.tv_usec == 0)
 
211
  {
 
212
     return 0;
 
213
  }
 
214
 
 
215
  return 1;
 
216
}
 
217
 
 
218
inline void subMsTimestamp(T_timestamp &ts, long ms)
 
219
{
 
220
  ts.tv_sec  -= ms / 1000;
 
221
  ts.tv_usec -= (ms % 1000) * 1000;
 
222
}
 
223
 
 
224
inline void addMsTimestamp(T_timestamp &ts, long ms)
 
225
{
 
226
  ts.tv_sec  += ms / 1000;
 
227
  ts.tv_usec += (ms % 1000) * 1000;
 
228
}
 
229
 
 
230
//
 
231
// Check the difference between timestamps.
 
232
// Return 0 if the system time went backward
 
233
// compared to the second timestamp, or the
 
234
// difference between the timestamps exceeds
 
235
// the given number of milliseconds.
 
236
//
 
237
 
 
238
inline int checkDiffTimestamp(const T_timestamp &ts1, const T_timestamp &ts2,
 
239
                                  long ms = 30000)
 
240
{
 
241
  long diffTs = diffTimestamp(ts1, ts2);
 
242
 
 
243
  if (diffTs < 0 || diffTs > ms)
 
244
  {
 
245
    return 0;
 
246
  }
 
247
 
 
248
  return 1;
 
249
}
 
250
 
 
251
//
 
252
// Return a string representing the timestamp.
 
253
//
 
254
 
 
255
char *strTimestamp(const T_timestamp &ts);
 
256
char *strMsTimestamp(const T_timestamp &ts);
 
257
 
 
258
inline char *strTimestamp()
 
259
{
 
260
  return strTimestamp(getTimestamp());
 
261
}
 
262
 
 
263
inline char *strMsTimestamp()
 
264
{
 
265
  return strMsTimestamp(getTimestamp());
 
266
}
 
267
 
 
268
//
 
269
// Update the current timestamp.
 
270
//
 
271
 
 
272
inline T_timestamp getNewTimestamp()
 
273
{
 
274
  #ifdef TEST
 
275
 
 
276
  T_timestamp ts;
 
277
 
 
278
  gettimeofday(&ts, NULL);
 
279
 
 
280
  *logofs << "Timestamp: Updating the current timestamp at "
 
281
          << strMsTimestamp(ts) << ".\n" << logofs_flush;
 
282
 
 
283
  long diffTs = diffTimestamp(timestamp, ts);
 
284
 
 
285
  if (diffTs > DRIFT_TIMESTAMP)
 
286
  {
 
287
    *logofs << "Timestamp: WARNING! Time difference since the "
 
288
            << "old timestamp is " << diffTs << " Ms.\n"
 
289
            << logofs_flush;
 
290
  }
 
291
 
 
292
  #endif
 
293
 
 
294
  gettimeofday(&timestamp, NULL);
 
295
 
 
296
  return timestamp;
 
297
}
 
298
 
 
299
#endif /* Timestamp_H */