1 : |
agomez |
1 |
#include "netdatacontrol.h" |
2 : |
|
|
|
3 : |
|
|
NetInfo::NetInfo() {
|
4 : |
|
|
set = 0;
|
5 : |
|
|
sent = 0;
|
6 : |
|
|
received = 0;
|
7 : |
|
|
numPendingAnswers = 0;
|
8 : |
|
|
}
|
9 : |
|
|
|
10 : |
|
|
NetInfo::~NetInfo() {
|
11 : |
|
|
}
|
12 : |
|
|
|
13 : |
|
|
int NetInfo::hasSet() {
|
14 : |
|
|
return set;
|
15 : |
|
|
}
|
16 : |
|
|
|
17 : |
|
|
int NetInfo::hasReceived() {
|
18 : |
|
|
return (sent && received);
|
19 : |
|
|
}
|
20 : |
|
|
|
21 : |
|
|
int NetInfo::hasSent() {
|
22 : |
|
|
return sent;
|
23 : |
|
|
}
|
24 : |
|
|
|
25 : |
|
|
int NetInfo::numCopiesSent() {
|
26 : |
|
|
return numPendingAnswers;
|
27 : |
|
|
}
|
28 : |
|
|
|
29 : |
|
|
Coordinates::Coordinates() {
|
30 : |
|
|
y = 0.0;
|
31 : |
|
|
}
|
32 : |
|
|
|
33 : |
|
|
Coordinates::~Coordinates() {
|
34 : |
|
|
}
|
35 : |
|
|
|
36 : |
|
|
double Coordinates::getY() {
|
37 : |
|
|
return y;
|
38 : |
|
|
}
|
39 : |
|
|
|
40 : |
|
|
const DoubleVector& Coordinates::getX() {
|
41 : |
|
|
return x;
|
42 : |
|
|
}
|
43 : |
|
|
|
44 : |
|
|
double Coordinates::getParameter(int num) {
|
45 : |
|
|
assert(num >= 0);
|
46 : |
|
|
if (x.Size() <= 0) {
|
47 : |
|
|
cerr << "Error in coordinates - vector has not been initialised\n";
|
48 : |
|
|
exit(EXIT_FAILURE);
|
49 : |
|
|
}
|
50 : |
|
|
return x[num];
|
51 : |
|
|
}
|
52 : |
|
|
|
53 : |
|
|
void Coordinates::setX(const DoubleVector& x1) {
|
54 : |
|
|
x = x1;
|
55 : |
|
|
}
|
56 : |
|
|
|
57 : |
|
|
void Coordinates::setParameter(double p, int num) {
|
58 : |
|
|
if ((num < 0) || (num >= x.Size())) {
|
59 : |
|
|
cerr << "Error in coordinates - number is out of bounds\n";
|
60 : |
|
|
exit(EXIT_FAILURE);
|
61 : |
|
|
}
|
62 : |
|
|
x[num] = p;
|
63 : |
|
|
}
|
64 : |
|
|
|
65 : |
|
|
void Coordinates::setY(double y1) {
|
66 : |
|
|
y = y1;
|
67 : |
|
|
}
|
68 : |
|
|
|
69 : |
|
|
int Coordinates::getNumParameters() {
|
70 : |
|
|
return x.Size();
|
71 : |
|
|
}
|
72 : |
|
|
|
73 : |
|
|
NetDataControl::NetDataControl(int numx, int numberOfParameters, int t) {
|
74 : |
|
|
numberOfx = 0;
|
75 : |
|
|
totalNumx = numx;
|
76 : |
|
|
numberSent = 0;
|
77 : |
|
|
numberAnswers = 0;
|
78 : |
|
|
resendID = -1;
|
79 : |
|
|
tag = t;
|
80 : |
|
|
numPar = numberOfParameters;
|
81 : |
|
|
xyCoord = new Coordinates*[numx];
|
82 : |
|
|
nInfo = new NetInfo*[numx];
|
83 : |
|
|
int i;
|
84 : |
|
|
for (i = 0; i < numx; i++) {
|
85 : |
|
|
xyCoord[i] = new Coordinates();
|
86 : |
|
|
nInfo[i] = new NetInfo;
|
87 : |
|
|
}
|
88 : |
|
|
}
|
89 : |
|
|
|
90 : |
|
|
NetDataControl::~NetDataControl() {
|
91 : |
|
|
int i;
|
92 : |
|
|
for (i = 0; i < totalNumx; i++) {
|
93 : |
|
|
delete xyCoord[i];
|
94 : |
|
|
xyCoord[i] = NULL;
|
95 : |
|
|
delete nInfo[i];
|
96 : |
|
|
nInfo[i] = NULL;
|
97 : |
|
|
}
|
98 : |
|
|
delete[] xyCoord;
|
99 : |
|
|
delete[] nInfo;
|
100 : |
|
|
}
|
101 : |
|
|
|
102 : |
|
|
// ----------------------------------------------------------------------------
|
103 : |
|
|
// Functions for setting data
|
104 : |
|
|
void NetDataControl::setX(const DoubleVector& x1) {
|
105 : |
|
|
if (numberOfx < 0 || numberOfx >= totalNumx) {
|
106 : |
|
|
cerr << "Error in netdatacontrol - cannot store vector\n";
|
107 : |
|
|
exit(EXIT_FAILURE);
|
108 : |
|
|
}
|
109 : |
|
|
if ((x1.Size()) != numPar) {
|
110 : |
|
|
cerr << "Error in netdatacontrol - wrong number of parameters\n";
|
111 : |
|
|
exit(EXIT_FAILURE);
|
112 : |
|
|
}
|
113 : |
|
|
xyCoord[numberOfx]->setX(x1);
|
114 : |
|
|
nInfo[numberOfx]->set = 1;
|
115 : |
|
|
numberOfx++;
|
116 : |
|
|
}
|
117 : |
|
|
|
118 : |
|
|
int NetDataControl::getIDToSetNext() {
|
119 : |
|
|
// might change
|
120 : |
|
|
if (numberOfx == totalNumx)
|
121 : |
|
|
return -1;
|
122 : |
|
|
else
|
123 : |
|
|
return numberOfx;
|
124 : |
|
|
}
|
125 : |
|
|
|
126 : |
|
|
void NetDataControl::setY(int id, double fx) {
|
127 : |
|
|
if (id < 0 || id >= totalNumx) {
|
128 : |
|
|
cerr << "Error in netdatacontrol - invalid id\n";
|
129 : |
|
|
exit(EXIT_FAILURE);
|
130 : |
|
|
}
|
131 : |
|
|
if (!nInfo[id]->hasReceived()) {
|
132 : |
|
|
// have not received an answer yet
|
133 : |
|
|
xyCoord[id]->setY(fx);
|
134 : |
|
|
nInfo[id]->received = 1;
|
135 : |
|
|
numberAnswers++;
|
136 : |
|
|
}
|
137 : |
|
|
nInfo[id]->numPendingAnswers--;
|
138 : |
|
|
|
139 : |
|
|
if (nInfo[id]->sent == 0)
|
140 : |
|
|
cerr << "Warning in netdatacontrol - setting f(x) but have not sent x with identity: " << id << endl;
|
141 : |
|
|
}
|
142 : |
|
|
|
143 : |
|
|
void NetDataControl::setDataPair(const DoubleVector& x1, double fx) {
|
144 : |
|
|
setX(x1);
|
145 : |
|
|
sentOne(numberOfx-1);
|
146 : |
|
|
setY(numberOfx-1, fx);
|
147 : |
|
|
}
|
148 : |
|
|
|
149 : |
|
|
// ----------------------------------------------------------------------------
|
150 : |
|
|
// Functions for getting data
|
151 : |
|
|
const DoubleVector& NetDataControl::getX(int id) {
|
152 : |
|
|
if (id < 0 || id >= numberOfx) {
|
153 : |
|
|
cerr << "Error in netdatacontrol - have not set vector with id " << id << endl;
|
154 : |
|
|
exit(EXIT_FAILURE);
|
155 : |
|
|
}
|
156 : |
|
|
return xyCoord[id]->getX();
|
157 : |
|
|
}
|
158 : |
|
|
|
159 : |
|
|
const DoubleVector& NetDataControl::getNextXToSend() {
|
160 : |
|
|
int i = 0;
|
161 : |
|
|
int FOUND = 0;
|
162 : |
|
|
|
163 : |
|
|
if (allSent()) {
|
164 : |
|
|
cerr << "Error in netdatacontrol - no vector to send\n";
|
165 : |
|
|
exit(EXIT_FAILURE);
|
166 : |
|
|
}
|
167 : |
|
|
if (numberSent < 0 || numberSent >= numberOfx) {
|
168 : |
|
|
cerr << "Error in netdatacontrol - number ot send out of bounds\n";
|
169 : |
|
|
exit(EXIT_FAILURE);
|
170 : |
|
|
}
|
171 : |
|
|
while (i < totalNumx && FOUND == 0) {
|
172 : |
|
|
if (nInfo[i]->hasSet() == 1 && nInfo[i]->hasSent() == 0)
|
173 : |
|
|
FOUND = 1;
|
174 : |
|
|
i++;
|
175 : |
|
|
}
|
176 : |
|
|
return xyCoord[i - 1]->getX();
|
177 : |
|
|
}
|
178 : |
|
|
|
179 : |
|
|
double NetDataControl::getY(int id) {
|
180 : |
|
|
if (id < 0 || id >= totalNumx) {
|
181 : |
|
|
cerr << "Error in netdatacontrol - invalid id\n";
|
182 : |
|
|
exit(EXIT_FAILURE);
|
183 : |
|
|
}
|
184 : |
|
|
if (!nInfo[id]->hasSet())
|
185 : |
|
|
cerr << "Warning in netdatacontrol - x has not been set yet\n";
|
186 : |
|
|
if (!nInfo[id]->hasReceived())
|
187 : |
|
|
cerr << "Warning in netdatacontrol - y has not been set yet\n";
|
188 : |
|
|
return xyCoord[id]->getY();
|
189 : |
|
|
}
|
190 : |
|
|
|
191 : |
|
|
const DoubleVector& NetDataControl::getNextAnsweredX() {
|
192 : |
|
|
if (numberAnswers == 0) {
|
193 : |
|
|
cerr << "Error in netdatacontrol - no answers received\n";
|
194 : |
|
|
exit(EXIT_FAILURE);
|
195 : |
|
|
}
|
196 : |
|
|
if (nextAns >= numberOfx) {
|
197 : |
|
|
cerr << "Error in netdatacontrol - invalid answer received\n";
|
198 : |
|
|
exit(EXIT_FAILURE);
|
199 : |
|
|
}
|
200 : |
|
|
|
201 : |
|
|
while (!nInfo[nextAns]->received)
|
202 : |
|
|
nextAns++;
|
203 : |
|
|
|
204 : |
|
|
int temp = nextAns;
|
205 : |
|
|
nextAns++;
|
206 : |
|
|
return xyCoord[temp]->getX();
|
207 : |
|
|
}
|
208 : |
|
|
|
209 : |
|
|
double NetDataControl::getNextAnsweredY() {
|
210 : |
|
|
int temp = nextAns - 1;
|
211 : |
|
|
if (temp < 0 || temp >= numberOfx) {
|
212 : |
|
|
cerr << "Error in netdatacontrol - invalid answer received\n";
|
213 : |
|
|
exit(EXIT_FAILURE);
|
214 : |
|
|
}
|
215 : |
|
|
return xyCoord[temp]->getY();
|
216 : |
|
|
}
|
217 : |
|
|
|
218 : |
|
|
// ----------------------------------------------------------------------------
|
219 : |
|
|
// Functions concerning sending data
|
220 : |
|
|
int NetDataControl::getNextSendID() {
|
221 : |
|
|
int i = 0;
|
222 : |
|
|
int FOUND = 0;
|
223 : |
|
|
if (allSent()) {
|
224 : |
|
|
cerr << "Error in netdatacontrol - no more vectors to send\n";
|
225 : |
|
|
exit(EXIT_FAILURE);
|
226 : |
|
|
}
|
227 : |
|
|
while (i < numberOfx && FOUND == 0) {
|
228 : |
|
|
if (nInfo[i]->hasSet() == 1 && nInfo[i]->hasSent() == 0)
|
229 : |
|
|
FOUND = 1;
|
230 : |
|
|
i++;
|
231 : |
|
|
}
|
232 : |
|
|
return (i - 1);
|
233 : |
|
|
}
|
234 : |
|
|
|
235 : |
|
|
int NetDataControl::getNextXToResend() {
|
236 : |
|
|
int numPending = 1;
|
237 : |
|
|
int counter = 0;
|
238 : |
|
|
if (allReceived()) {
|
239 : |
|
|
cerr << "Error in netdatacontrol - no data to resend\n";
|
240 : |
|
|
exit(EXIT_FAILURE);
|
241 : |
|
|
}
|
242 : |
|
|
resendID++;
|
243 : |
|
|
if (resendID == totalNumx)
|
244 : |
|
|
resendID = 0;
|
245 : |
|
|
while ((nInfo[resendID]->hasReceived() || !nInfo[resendID]->hasSent()
|
246 : |
|
|
|| nInfo[resendID]->numPendingAnswers > numPending)) {
|
247 : |
|
|
counter++;
|
248 : |
|
|
resendID++;
|
249 : |
|
|
if (resendID == totalNumx)
|
250 : |
|
|
resendID = 0;
|
251 : |
|
|
if (counter == totalNumx)
|
252 : |
|
|
numPending++;
|
253 : |
|
|
}
|
254 : |
|
|
return resendID;
|
255 : |
|
|
}
|
256 : |
|
|
|
257 : |
|
|
void NetDataControl::sentOne(int id) {
|
258 : |
|
|
if ((id < 0) || (id >= numberOfx)) {
|
259 : |
|
|
cerr << "Error in netdatacontrol - invalid id\n";
|
260 : |
|
|
exit(EXIT_FAILURE);
|
261 : |
|
|
}
|
262 : |
|
|
if (nInfo[id]->sent == 1)
|
263 : |
|
|
cerr << "Warning in netdatacontrol - already sent data with identity: " << id << endl;
|
264 : |
|
|
else {
|
265 : |
|
|
nInfo[id]->sent = 1;
|
266 : |
|
|
nInfo[id]->numPendingAnswers++;
|
267 : |
|
|
numberSent++;
|
268 : |
|
|
}
|
269 : |
|
|
}
|
270 : |
|
|
|
271 : |
|
|
void NetDataControl::resentOne(int id) {
|
272 : |
|
|
if ((id < 0) || (id >= numberOfx)) {
|
273 : |
|
|
cerr << "Error in netdatacontrol - invalid id\n";
|
274 : |
|
|
exit(EXIT_FAILURE);
|
275 : |
|
|
}
|
276 : |
|
|
nInfo[id]->numPendingAnswers++;
|
277 : |
|
|
}
|
278 : |
|
|
|
279 : |
|
|
// ----------------------------------------------------------------------------
|
280 : |
|
|
// Functions for getting general information about state of datagroup
|
281 : |
|
|
int NetDataControl::allSent() {
|
282 : |
|
|
assert(numberOfx >= numberSent);
|
283 : |
|
|
return (numberOfx == numberSent);
|
284 : |
|
|
}
|
285 : |
|
|
|
286 : |
|
|
int NetDataControl:: getNumNotAnswered() {
|
287 : |
|
|
return (numberSent - numberAnswers);
|
288 : |
|
|
}
|
289 : |
|
|
|
290 : |
|
|
int NetDataControl::getTotalReceived() {
|
291 : |
|
|
return numberAnswers;
|
292 : |
|
|
}
|
293 : |
|
|
|
294 : |
|
|
int NetDataControl::getTotalSet() {
|
295 : |
|
|
return numberOfx;
|
296 : |
|
|
}
|
297 : |
|
|
|
298 : |
|
|
void NetDataControl::setFirstAnswered() {
|
299 : |
|
|
nextAns = 0;
|
300 : |
|
|
}
|
301 : |
|
|
|
302 : |
|
|
int NetDataControl::allReceived() {
|
303 : |
|
|
return (numberSent == numberAnswers);
|
304 : |
|
|
}
|
305 : |
|
|
|
306 : |
|
|
int NetDataControl::getNumAnswered() {
|
307 : |
|
|
return numberAnswers;
|
308 : |
|
|
}
|
309 : |
|
|
|
310 : |
|
|
int NetDataControl::getNumLeftToSend() {
|
311 : |
|
|
return (numberOfx - numberSent);
|
312 : |
|
|
}
|
313 : |
|
|
|
314 : |
|
|
int NetDataControl::getTag() {
|
315 : |
|
|
return tag;
|
316 : |
|
|
}
|
317 : |
|
|
|
318 : |
|
|
int NetDataControl::getLastSetID() {
|
319 : |
|
|
return numberOfx - 1;
|
320 : |
|
|
}
|
321 : |
|
|
|
322 : |
|
|
int NetDataControl::getMaxNumData() {
|
323 : |
|
|
return totalNumx;
|
324 : |
|
|
}
|
325 : |
|
|
|
326 : |
|
|
int NetDataControl::hasAnswer(int id) {
|
327 : |
|
|
if (id < 0 || id >= totalNumx) {
|
328 : |
|
|
cerr << "Error in netdatacontrol - id out of bounds\n";
|
329 : |
|
|
exit(EXIT_FAILURE);
|
330 : |
|
|
}
|
331 : |
|
|
return nInfo[id]->hasReceived();
|
332 : |
|
|
}
|
333 : |
|
|
|
334 : |
|
|
int NetDataControl::isFull() {
|
335 : |
|
|
return (getMaxNumData() == getTotalSet());
|
336 : |
|
|
}
|