vrpn 07.35
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
6 vrpn_uint32 * vector) :
7 d_timestampSize (vectorLength),
8 d_timestamp(NULL)
9{
10 d_timestamp = new vrpn_uint32[vectorLength];
11 copy(vector);
12}
13
15 (const vrpn_LamportTimestamp & r) :
16 d_timestampSize (r.d_timestampSize),
17 d_timestamp(NULL)
18{
19 d_timestamp = new vrpn_uint32[r.d_timestampSize];
20 copy(r.d_timestamp);
21}
22
23
24
26 if (d_timestamp) {
27 try {
28 delete[] d_timestamp;
29 } catch (...) {
30 fprintf(stderr, "vrpn_LamportTimestamp::~vrpn_LamportTimestamp(): delete failed\n");
31 return;
32 }
33 }
34}
35
36vrpn_LamportTimestamp & vrpn_LamportTimestamp::operator =
37 (const vrpn_LamportTimestamp & r) {
38
39 if (d_timestamp) {
40 try {
41 delete[] d_timestamp;
42 } catch (...) {
43 fprintf(stderr, "vrpn_LamportTimestamp::operator =(): delete failed\n");
44 return *this;
45 }
46 d_timestamp = NULL;
47 }
48
49 d_timestampSize = r.d_timestampSize;
50 try { d_timestamp = new vrpn_uint32[r.d_timestampSize]; }
51 catch (...) {
52 d_timestamp = NULL;
53 return *this;
54 }
55
56 copy(r.d_timestamp);
57
58 return *this;
59}
60
61
62
64 const {
65 int i;
66
67 // TODO
68 // What's the right thing to do here? Throw an exception?
69 if (d_timestampSize != r.d_timestampSize) {
70 return d_timestampSize < r.d_timestampSize;
71 }
72
73 // TODO
74 // How do we compare these correctly?
75
76 for (i = 0; i < d_timestampSize; i++) {
77 if (d_timestamp[i] > r.d_timestamp[i]) {
78 return vrpn_false;
79 }
80 }
81
82 for (i = 0; i < d_timestampSize; i++) {
83 if (d_timestamp[i] < r.d_timestamp[i]) {
84 return vrpn_true;
85 }
86 }
87
88 return vrpn_false; // equal
89}
90
91vrpn_uint32 vrpn_LamportTimestamp::operator [] (int i) const {
92 if ((i < 0) || (i >= d_timestampSize)) {
93 return 0;
94 }
95 return d_timestamp[i];
96}
97
99 return d_timestampSize;
100}
101
102
103void vrpn_LamportTimestamp::copy (const vrpn_uint32 * vector) {
104 int i;
105
106 if (d_timestamp && vector) {
107 for (i = 0; i < d_timestampSize; i++) {
108 d_timestamp[i] = vector[i];
109 }
110 }
111}
112
113
114
115vrpn_LamportClock::vrpn_LamportClock (int numHosts, int ourIndex) :
116 d_numHosts (numHosts),
117 d_ourIndex (ourIndex),
118 d_currentTimestamp(NULL)
119{
120 d_currentTimestamp = new vrpn_uint32[numHosts];
121
122 int i;
123 if (d_currentTimestamp) {
124 for (i = 0; i < numHosts; i++) {
125 d_currentTimestamp[i] = 0;
126 }
127 }
128}
129
130
132 if (d_currentTimestamp) {
133 try {
134 delete[] d_currentTimestamp;
135 } catch (...) {
136 fprintf(stderr, "vrpn_LamportClock::~vrpn_LamportClock(): delete failed\n");
137 return;
138 }
139 }
140}
141
143 int i;
144
145 if (r.size() != d_numHosts) {
146 // Throw exception!
147 return;
148 }
149
150 for (i = 0; i < d_numHosts; i++) {
151 if (r[i] > d_currentTimestamp[i]) {
152 d_currentTimestamp[i] = r[i];
153 }
154 }
155
156}
157
159{
160 d_currentTimestamp[d_ourIndex]++;
161
162 vrpn_LamportTimestamp *ret = NULL;
163 try { ret = new vrpn_LamportTimestamp(d_numHosts, d_currentTimestamp); }
164 catch (...) { return NULL; }
165 return ret;
166}
167
168
169
170
171
172
173
174
175
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)