vrpn  07.33
Virtual Reality Peripheral Network
vrpn_LamportClock.C
Go to the documentation of this file.
1 #include "vrpn_LamportClock.h"
2 
4  vrpn_uint32 * vector) :
5  d_timestampSize (vectorLength),
6  d_timestamp (new vrpn_uint32 [vectorLength]) {
7 
8  copy(vector);
9 }
10 
12  (const vrpn_LamportTimestamp & r) :
13  d_timestampSize (r.d_timestampSize),
14  d_timestamp (new vrpn_uint32 [r.d_timestampSize]) {
15 
16  copy(r.d_timestamp);
17 }
18 
19 
20 
22  if (d_timestamp) {
23  delete [] d_timestamp;
24  }
25 }
26 
27 vrpn_LamportTimestamp & vrpn_LamportTimestamp::operator =
28  (const vrpn_LamportTimestamp & r) {
29 
30  if (d_timestamp) {
31  delete [] d_timestamp;
32  }
33 
34  d_timestampSize = r.d_timestampSize;
35  d_timestamp = new vrpn_uint32 [r.d_timestampSize];
36 
37  copy(r.d_timestamp);
38 
39  return *this;
40 }
41 
42 
43 
45  const {
46  int i;
47 
48  // TODO
49  // What's the right thing to do here? Throw an exception?
50  if (d_timestampSize != r.d_timestampSize) {
51  return d_timestampSize < r.d_timestampSize;
52  }
53 
54  // TODO
55  // How do we compare these correctly?
56 
57  for (i = 0; i < d_timestampSize; i++) {
58  if (d_timestamp[i] > r.d_timestamp[i]) {
59  return vrpn_false;
60  }
61  }
62 
63  for (i = 0; i < d_timestampSize; i++) {
64  if (d_timestamp[i] < r.d_timestamp[i]) {
65  return vrpn_true;
66  }
67  }
68 
69  return vrpn_false; // equal
70 }
71 
72 vrpn_uint32 vrpn_LamportTimestamp::operator [] (int i) const {
73  if ((i < 0) || (i >= d_timestampSize)) {
74  return 0;
75  }
76  return d_timestamp[i];
77 }
78 
79 int vrpn_LamportTimestamp::size (void) const {
80  return d_timestampSize;
81 }
82 
83 
84 void vrpn_LamportTimestamp::copy (const vrpn_uint32 * vector) {
85  int i;
86 
87  if (d_timestamp && vector) {
88  for (i = 0; i < d_timestampSize; i++) {
89  d_timestamp[i] = vector[i];
90  }
91  }
92 }
93 
94 
95 
96 vrpn_LamportClock::vrpn_LamportClock (int numHosts, int ourIndex) :
97  d_numHosts (numHosts),
98  d_ourIndex (ourIndex),
99  d_currentTimestamp (new vrpn_uint32 [numHosts]) {
100 
101  int i;
102 
103  if (d_currentTimestamp) {
104  for (i = 0; i < numHosts; i++) {
105  d_currentTimestamp[i] = 0;
106  }
107  }
108 }
109 
110 
112  if (d_currentTimestamp) {
113  delete [] d_currentTimestamp;
114  }
115 }
116 
118  int i;
119 
120  if (r.size() != d_numHosts) {
121  // Throw exception!
122  return;
123  }
124 
125  for (i = 0; i < d_numHosts; i++) {
126  if (r[i] > d_currentTimestamp[i]) {
127  d_currentTimestamp[i] = r[i];
128  }
129  }
130 
131 }
132 
134 
135  d_currentTimestamp[d_ourIndex]++;
136 
137  return new vrpn_LamportTimestamp (d_numHosts, d_currentTimestamp);
138 }
139 
140 
141 
142 
143 
144 
145 
146 
147 
vrpn_LamportClock::getTimestampAndAdvance
vrpn_LamportTimestamp * getTimestampAndAdvance(void)
Increments the current timestamp and returns it.
Definition: vrpn_LamportClock.C:133
vrpn_LamportClock::vrpn_LamportClock
vrpn_LamportClock(int numHosts, int ourIndex)
Definition: vrpn_LamportClock.C:96
vrpn_LamportTimestamp::vrpn_LamportTimestamp
vrpn_LamportTimestamp(int vectorLength, vrpn_uint32 *vector)
Definition: vrpn_LamportClock.C:3
vrpn_LamportClock::receive
void receive(const vrpn_LamportTimestamp &)
Updates this clock to reflect a timestamp received from another clock/host.
Definition: vrpn_LamportClock.C:117
vrpn_LamportTimestamp::operator[]
vrpn_uint32 operator[](int i) const
Returns the event count for the i'th host.
Definition: vrpn_LamportClock.C:72
vrpn_LamportTimestamp::operator<
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,...
Definition: vrpn_LamportClock.C:44
vrpn_LamportClock.h
vrpn_LamportClock::~vrpn_LamportClock
~vrpn_LamportClock(void)
Definition: vrpn_LamportClock.C:111
vrpn_LamportTimestamp::~vrpn_LamportTimestamp
~vrpn_LamportTimestamp(void)
Definition: vrpn_LamportClock.C:21
vrpn_LamportTimestamp
class VRPN_API vrpn_LamportTimestamp
Definition: vrpn_SharedObject.h:17
vrpn_LamportTimestamp
Definition: vrpn_LamportClock.h:16
vrpn_LamportTimestamp::size
int size(void) const
Returns the number of hosts participating in the timestamp.
Definition: vrpn_LamportClock.C:79