History

SimulationOwner as one property

Andrew Meadows committed 11 years ago

Commit 12fc1801 parent b6d5ada12 files · +435 −332Browse files

Showing the changes to libraries/entities/src/SimulationOwner.cpp (1 of 12 files). Show all

Unified · Split

libraries/entities/src/SimulationOwner.cpp+188−0
@@ -0,0 +1,188 @@
1//
2// SimulationOwner.cpp
3// libraries/entities/src
4//
5// Created by Andrew Meadows on 2015.06.19
6// Copyright 2015 High Fidelity, Inc.
7//
8// Distributed under the Apache License, Version 2.0.
9// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
10//
11
12#include "SimulationOwner.h"
13
14#include <assert.h>
15
16#include <NumericalConstants.h>
17
18// static
19const int SimulationOwner::NUM_BYTES_ENCODED = NUM_BYTES_RFC4122_UUID + 1;
20
21
22SimulationOwner::SimulationOwner(const SimulationOwner& other)
23 : _id(other._id), _priority(other._priority), _expiry(other._expiry) {
24}
25
26QByteArray SimulationOwner::toByteArray() const {
27 QByteArray data = _id.toRfc4122();
28 data.append(_priority);
29 return data;
30}
31
32bool SimulationOwner::fromByteArray(const QByteArray& data) {
33 if (data.size() == NUM_BYTES_ENCODED) {
34 QByteArray idBytes = data.left(NUM_BYTES_RFC4122_UUID);
35 _id = QUuid::fromRfc4122(idBytes);
36 _priority = data[NUM_BYTES_RFC4122_UUID];
37 return true;
38 }
39 return false;
40}
41
42void SimulationOwner::clear() {
43 _id = QUuid();
44 _priority = 0;
45 _expiry = 0;
46}
47
48void SimulationOwner::setPriority(quint8 priority) {
49 _priority = priority;
50 if (_priority == MAX_SIMULATOR_PRIORITY) {
51 // we extend the the expiry whenever we set MAX_SIMULATOR_PRIORITY
52 updateExpiry();
53 } else if (_priority == 0) {
54 // when priority is zero we clear everything
55 _expiry = 0;
56 _id = QUuid();
57 }
58}
59
60bool SimulationOwner::setID(const QUuid& id) {
61 if (_id != id) {
62 _id = id;
63 updateExpiry();
64 if (_id.isNull()) {
65 // when id is null we clear everything
66 _priority = 0;
67 _expiry = 0;
68 }
69 return true;
70 }
71 return false;
72}
73
74bool SimulationOwner::set(const QUuid& id, quint8 priority) {
75 setPriority(priority);
76 return setID(id);
77}
78
79bool SimulationOwner::set(const SimulationOwner& owner) {
80 setPriority(owner._priority);
81 return setID(owner._id);
82}
83
84void SimulationOwner::updateExpiry() {
85 const quint64 OWNERSHIP_LOCKOUT_EXPIRY = USECS_PER_SECOND / 5;
86 _expiry = usecTimestampNow() + OWNERSHIP_LOCKOUT_EXPIRY;
87}
88
89// TODO: move this test code out
90// static debug
91void SimulationOwner::test() {
92 { // test default constructor
93 SimulationOwner simOwner;
94 if (!simOwner.isNull()) {
95 std::cout << __FILE__ << ":" << __LINE__ << " ERROR : SimulationOwner should be NULL" << std::endl;
96 }
97
98 if (simOwner.getPriority() != 0) {
99 std::cout << __FILE__ << ":" << __LINE__ << " ERROR : unexpeced SimulationOwner priority" << std::endl;
100 }
101 }
102
103 { // test set constructor
104 QUuid id = QUuid::createUuid();
105 quint8 priority = 128;
106 SimulationOwner simOwner(id, priority);
107 if (simOwner.isNull()) {
108 std::cout << __FILE__ << ":" << __LINE__ << " ERROR : SimulationOwner should NOT be NULL" << std::endl;
109 }
110
111 if (simOwner.getID() != id) {
112 std::cout << __FILE__ << ":" << __LINE__ << " ERROR : SimulationOwner with unexpected id" << std::endl;
113 }
114
115 if (simOwner.getPriority() != priority) {
116 std::cout << __FILE__ << ":" << __LINE__ << " ERROR : unexpeced SimulationOwner priority" << std::endl;
117 }
118
119 QUuid otherID = QUuid::createUuid();
120 if (simOwner.getID() == otherID) {
121 std::cout << __FILE__ << ":" << __LINE__ << " ERROR : SimulationOwner with unexpected id" << std::endl;
122 }
123 }
124
125 { // test set()
126 QUuid id = QUuid::createUuid();
127 quint8 priority = 1;
128 SimulationOwner simOwner;
129 simOwner.set(id, priority);
130 if (simOwner.isNull()) {
131 std::cout << __FILE__ << ":" << __LINE__ << " ERROR : SimulationOwner should NOT be NULL" << std::endl;
132 }
133
134 if (simOwner.getID() != id) {
135 std::cout << __FILE__ << ":" << __LINE__ << " ERROR : SimulationOwner with unexpected id" << std::endl;
136 }
137
138 if (simOwner.getPriority() != priority) {
139 std::cout << __FILE__ << ":" << __LINE__ << " ERROR : unexpeced SimulationOwner priority" << std::endl;
140 }
141 }
142
143 { // test encode/decode
144 SimulationOwner ownerA(QUuid::createUuid(), 1);
145 SimulationOwner ownerB(QUuid::createUuid(), 2);
146
147 QByteArray data = ownerA.toByteArray();
148 ownerB.fromByteArray(data);
149
150 if (ownerA.getID() != ownerB.getID()) {
151 std::cout << __FILE__ << ":" << __LINE__ << " ERROR : ownerA._id should be equal to ownerB._id" << std::endl;
152 }
153 }
154}
155
156bool SimulationOwner::operator==(const SimulationOwner& other) {
157 return (_id == other._id && _priority == other._priority);
158}
159
160bool SimulationOwner::operator!=(const SimulationOwner& other) {
161 return (_id != other._id && _priority != other._priority);
162}
163
164SimulationOwner& SimulationOwner::operator=(const SimulationOwner& other) {
165 _priority = other._priority;
166 if (_priority == 0) {
167 _id = QUuid();
168 _expiry = 0;
169 } else {
170 if (_id != other._id) {
171 updateExpiry();
172 }
173 _id = other._id;
174 }
175 return *this;
176}
177
178// friend of SimulationOwner
179std::ostream& operator<<(std::ostream& s, const SimulationOwner& simOwner) {
180 s << "{ id : " << simOwner._id.toString().toStdString() << ", priority : " << (int)simOwner._priority << " }";
181 return s;
182}
183
184QDebug& operator<<(QDebug& d, const SimulationOwner& simOwner) {
185 d << "{ id : " << simOwner << ", priority : " << (int)simOwner._priority << " }";
186 return d;
187}
188

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t