| 1 |
#ifndef _PROTO_TIME |
| 2 |
#define _PROTO_TIME |
| 3 |
|
| 4 |
#include "protoDefs.h" |
| 5 |
|
| 6 |
#ifdef WIN32 |
| 7 |
#include <winsock2.h> |
| 8 |
typedef long suseconds_t; |
| 9 |
#else // UNIX |
| 10 |
#include <arpa/inet.h> // for htonl() |
| 11 |
#endif // if/else WIN32 |
| 12 |
|
| 13 |
/** |
| 14 |
* @class ProtoTime |
| 15 |
* |
| 16 |
* @brief System time conversion routines. |
| 17 |
*/ |
| 18 |
|
| 19 |
class ProtoTime |
| 20 |
{ |
| 21 |
public: |
| 22 |
ProtoTime(); |
| 23 |
ProtoTime(const struct timeval& theTime); |
| 24 |
ProtoTime(double seconds); |
| 25 |
ProtoTime(unsigned long sec, unsigned long usec); |
| 26 |
|
| 27 |
ProtoTime& GetCurrentTime() |
| 28 |
{ |
| 29 |
ProtoSystemTime(tval); |
| 30 |
return *this; |
| 31 |
} |
| 32 |
|
| 33 |
unsigned long sec() const |
| 34 |
{return ((unsigned long)tval.tv_sec);} |
| 35 |
unsigned long usec() const |
| 36 |
{return ((unsigned long)tval.tv_usec);} |
| 37 |
unsigned long nsec() const |
| 38 |
{return ((unsigned long)(1000* tval.tv_usec));} |
| 39 |
|
| 40 |
double GetValue() const |
| 41 |
{ |
| 42 |
double val = (double)sec(); |
| 43 |
val += 1.0e-06 * (double)usec(); |
| 44 |
return val; |
| 45 |
} |
| 46 |
double GetOffsetValue() const; |
| 47 |
|
| 48 |
bool IsZero() const |
| 49 |
{return ((0 == tval.tv_sec) && (0 == tval.tv_usec));} |
| 50 |
void Zeroize() |
| 51 |
{tval.tv_sec = tval.tv_usec = 0;} |
| 52 |
|
| 53 |
// for debugging |
| 54 |
void Invalidate() |
| 55 |
{Zeroize();} |
| 56 |
bool IsValid() const |
| 57 |
{ |
| 58 |
return ((tval.tv_sec > 0) || (tval.tv_usec > 0)); |
| 59 |
} |
| 60 |
|
| 61 |
const struct timeval& GetTimeVal() const |
| 62 |
{return tval;} |
| 63 |
struct timeval& AccessTimeVal() |
| 64 |
{return tval;} |
| 65 |
|
| 66 |
void operator+=(const ProtoTime& t); |
| 67 |
void operator+=(double seconds); |
| 68 |
void operator-=(double seconds) |
| 69 |
{return operator+=(-seconds);} |
| 70 |
bool operator==(const ProtoTime& t) const |
| 71 |
{ |
| 72 |
return ((sec() == t.sec()) && |
| 73 |
(usec() == t.usec())); |
| 74 |
} |
| 75 |
bool operator!=(const ProtoTime& t) const |
| 76 |
{return !(*this == t);} |
| 77 |
bool operator>(const ProtoTime& t) const |
| 78 |
{ |
| 79 |
return ((sec() > t.sec()) || |
| 80 |
((sec() == t.sec()) && |
| 81 |
(usec() > t.usec()))); |
| 82 |
} |
| 83 |
bool operator>=(const ProtoTime& t) const |
| 84 |
{ |
| 85 |
return ((sec() > t.sec()) || |
| 86 |
((sec() == t.sec()) && |
| 87 |
(usec() >= t.usec()))); |
| 88 |
} |
| 89 |
bool operator<(const ProtoTime& t) const |
| 90 |
{ |
| 91 |
return ((sec() < t.sec()) || |
| 92 |
((sec() == t.sec()) && |
| 93 |
(usec() < t.usec()))); |
| 94 |
} |
| 95 |
bool operator<=(const ProtoTime& t) const |
| 96 |
{ |
| 97 |
return ((sec() < t.sec()) || |
| 98 |
((sec() == t.sec()) && |
| 99 |
(usec() <= t.usec()))); |
| 100 |
} |
| 101 |
|
| 102 |
// Computes (t1 - t2) |
| 103 |
static double Delta(const ProtoTime& t1, const ProtoTime& t2); |
| 104 |
double operator-(const ProtoTime& t) const |
| 105 |
{return Delta(*this, t);} |
| 106 |
|
| 107 |
class Key |
| 108 |
{ |
| 109 |
public: |
| 110 |
Key() {Invalidate();} |
| 111 |
void SetValue(const ProtoTime& theTime) |
| 112 |
{ |
| 113 |
key.tv_sec = htont(theTime.tval.tv_sec); |
| 114 |
key.tv_usec = htontu(theTime.tval.tv_usec); |
| 115 |
} |
| 116 |
void Invalidate() |
| 117 |
{ |
| 118 |
key.tv_sec = 0; |
| 119 |
key.tv_usec = 1000000; |
| 120 |
} |
| 121 |
bool IsValid() const |
| 122 |
{return (1000000 != key.tv_usec);} |
| 123 |
|
| 124 |
const char* GetKey() const |
| 125 |
{return (const char*)&key;} |
| 126 |
unsigned int GetKeysize() const |
| 127 |
{return ((sizeof(time_t) + sizeof(suseconds_t)) << 3);} |
| 128 |
|
| 129 |
private: |
| 130 |
// These helper methods convert the tv_sec and tv_usec |
| 131 |
// fields to network byte order (Big Endian) to provide |
| 132 |
// a key suitable for ProtoTree use. (e.g., with ProtoSortedTree, |
| 133 |
// the key can be used to build time-sorted lists. ProtoTimerMgr |
| 134 |
// uses this for managing timeouts. |
| 135 |
static time_t htont(time_t t) |
| 136 |
{ |
| 137 |
if (8 == sizeof(time_t)) |
| 138 |
{ |
| 139 |
UINT32* t1 = (UINT32*)&t; |
| 140 |
time_t result; |
| 141 |
UINT32* t2 = (UINT32*)&result; |
| 142 |
t2[0] = htonl(t1[1]); |
| 143 |
t2[1] = htonl(t1[0]); |
| 144 |
return result; |
| 145 |
} |
| 146 |
else // if (4 = sizeof(time_t)) |
| 147 |
{ |
| 148 |
return ((time_t)htonl((UINT32)t)); |
| 149 |
} |
| 150 |
} // end ProtoTime::Key::htont() |
| 151 |
static suseconds_t htontu(suseconds_t t) |
| 152 |
{ |
| 153 |
if (8 == sizeof(suseconds_t)) |
| 154 |
{ |
| 155 |
UINT32* t1 = (UINT32*)&t; |
| 156 |
suseconds_t result; |
| 157 |
UINT32* t2 = (UINT32*)&result; |
| 158 |
t2[0] = htonl(t1[1]); |
| 159 |
t2[1] = htonl(t1[0]); |
| 160 |
return result; |
| 161 |
} |
| 162 |
else // if (4 = sizeof(suseconds_t)) |
| 163 |
{ |
| 164 |
return ((suseconds_t)htonl((UINT32)t)); |
| 165 |
} |
| 166 |
} // end htontu() |
| 167 |
|
| 168 |
struct timeval key; // network byte ordered version |
| 169 |
|
| 170 |
}; // end class ProtoTime::Key |
| 171 |
|
| 172 |
private: |
| 173 |
// (TBD) for now we use struct timeval for convenience, but in future |
| 174 |
// we may want to control the layout of the struct so it can be |
| 175 |
// used in a ProtoSortedTree as a sortable key. |
| 176 |
// We also may want to maintain more _precise_ time as well |
| 177 |
struct timeval tval; |
| 178 |
|
| 179 |
}; // end class ProtoTime |
| 180 |
|
| 181 |
#endif // _PROTO_TIME |