vrpn 07.36
Virtual Reality Peripheral Network
Loading...
Searching...
No Matches
vrpn_LamportClock.C
Go to the documentation of this file.
1#include "vrpn_LamportClock.h"
2#include <string.h>
3#include <stdio.h>
4#include <new>
5
7 vrpn_uint32 * vector) :
8 d_timestampSize (vectorLength),
9 d_timestamp(NULL)
10{
11 d_timestamp = new vrpn_uint32[vectorLength];
12 copy(vector);
13}
14
16 (const vrpn_LamportTimestamp & r) :
17 d_timestampSize (r.d_timestampSize),
18 d_timestamp(NULL)
19{
20 d_timestamp = new vrpn_uint32[r.d_timestampSize];
21 copy(r.d_timestamp);
22}
23
24
25
27 if (d_timestamp) {
28 try {
29 delete[] d_timestamp;
30 } catch (...) {
31 fprintf(stderr, "vrpn_LamportTimestamp::~vrpn_LamportTimestamp(): delete failed\n");
32 return;
33 }
34 }
35}
36
37vrpn_LamportTimestamp & vrpn_LamportTimestamp::operator =
38 (const vrpn_LamportTimestamp & r) {
39
40 if (d_timestamp) {
41 try {
42 delete[] d_timestamp;
43 } catch (...) {
44 fprintf(stderr, "vrpn_LamportTimestamp::operator =(): delete failed\n");
45 return *this;
46 }
47 d_timestamp = NULL;
48 }
49
50 d_timestampSize = r.d_timestampSize;
51 try { d_timestamp = new vrpn_uint32[r.d_timestampSize]; }
52 catch (...) {
53 d_timestamp = NULL;
54 return *this;
55 }
56
57 copy(r.d_timestamp);
58
59 return *this;
60}
61
62
63
65 const {
66 int i;
67
68 // TODO
69 // What's the right thing to do here? Throw an exception?
70 if (d_timestampSize != r.d_timestampSize) {
71 return d_timestampSize < r.d_timestampSize;
72 }
73
74 // TODO
75 // How do we compare these correctly?
76
77 for (i = 0; i < d_timestampSize; i++) {
78 if (d_timestamp[i] > r.d_timestamp[i]) {
79 return vrpn_false;
80 }
81 }
82
83 for (i = 0; i < d_timestampSize; i++) {
84 if (d_timestamp[i] < r.d_timestamp[i]) {
85 return vrpn_true;
86 }
87 }
88
89 return vrpn_false; // equal
90}
91
92vrpn_uint32 vrpn_LamportTimestamp::operator [] (int i) const {
93 if ((i < 0) || (i >= d_timestampSize)) {
94 return 0;
95 }
96 return d_timestamp[i];
97}
98
100 return d_timestampSize;
101}
102
103
104void vrpn_LamportTimestamp::copy (const vrpn_uint32 * vector) {
105 int i;
106
107 if (d_timestamp && vector) {
108 for (i = 0; i < d_timestampSize; i++) {
109 d_timestamp[i] = vector[i];
110 }
111 }
112}
113
114
115
116vrpn_LamportClock::vrpn_LamportClock (int numHosts, int ourIndex) :
117 d_numHosts (numHosts),
118 d_ourIndex (ourIndex),
119 d_currentTimestamp(NULL)
120{
121 d_currentTimestamp = new(std::nothrow) vrpn_uint32[numHosts];
122
123 int i;
124 if (d_currentTimestamp) {
125 for (i = 0; i < numHosts; i++) {
126 d_currentTimestamp[i] = 0;
127 }
128 }
129}
130
131
133 if (d_currentTimestamp) {
134 try {
135 delete[] d_currentTimestamp;
136 } catch (...) {
137 fprintf(stderr, "vrpn_LamportClock::~vrpn_LamportClock(): delete failed\n");
138 return;
139 }
140 }
141}
142
144 int i;
145
146 if (r.size() != d_numHosts) {
147 // Throw exception!
148 return;
149 }
150
151 for (i = 0; i < d_numHosts; i++) {
152 if (r[i] > d_currentTimestamp[i]) {
153 d_currentTimestamp[i] = r[i];
154 }
155 }
156
157}
158
160{
161 d_currentTimestamp[d_ourIndex]++;
162
163 vrpn_LamportTimestamp *ret = NULL;
164 try { ret = new vrpn_LamportTimestamp(d_numHosts, d_currentTimestamp); }
165 catch (...) { return NULL; }
166 return ret;
167}
168
169
170
171
172
173
174
175
176
void receive(const vrpn_LamportTimestamp &)
Updates this clock to reflect a timestamp received from another clock/host.
vrpn_LamportClock(int numHosts, int ourIndex)
vrpn_LamportTimestamp * getTimestampAndAdvance(void)
Increments the current timestamp and returns it.
Timestamp for a single event, produced by a vrpn_LamportClock and hopefully generally usable in place...
vrpn_uint32 operator[](int i) const
Returns the event count for the i'th host.
int size(void) const
Returns the number of hosts participating in the timestamp.
vrpn_bool operator<(const vrpn_LamportTimestamp &r) const
Returns vrpn_true if this timestamp precedes r. It'd be nice if we could throw an exception here,...
vrpn_LamportTimestamp(int vectorLength, vrpn_uint32 *vector)
class VRPN_API vrpn_LamportTimestamp