-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNetworkResources.cpp
166 lines (130 loc) · 5.5 KB
/
NetworkResources.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* Copyright (C) 2015 Willi Menapace <willi.menapace@gmail.com>, Simone Lorengo - All Rights Reserved
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary and confidential
* Written by Willi Menapace <willi.menapace@gmail.com>
*/
#include "NetworkResources.h"
#include "GlobalDefines.h"
#ifdef OFFBOARD
#include <assert.h>
#endif // OFFBOARD
NetworkDeviceStatus *NetworkResources::getDeviceStatusByAddress(unsigned char address[ADDRESS_BYTES]) {
assert(address != 0);
InstanceIterator<NetworkDeviceStatus> *devicesIterator = networkDevices.getInstanceIterator();
NetworkDevice *currentDevice = 0;
NetworkDeviceStatus *currentStatus = 0;
unsigned char *currentAddress;
while(devicesIterator->hasNext()) {
currentStatus = devicesIterator->next();
currentDevice = currentStatus->getNetworkDevice();
currentAddress = currentDevice->getAddress();
for(int i = 0; i < ADDRESS_BYTES; ++i) {
if(currentAddress[i] != address[i]) {
break;
}
//Se l'indirizzo non e' ancora stato scartato dopo
//aver esaminato l'ultimo byte allora e' uguale a quello cercato
if(i == ADDRESS_BYTES - 1) {
return currentStatus;
}
}
}
//Se non e' ancora stato ritornato nulla allora lo status cercato non e' presente
return 0;
}
void NetworkResources::addDevice(unsigned char address[ADDRESS_BYTES],
unsigned char sensorsCount,
SensorType sensorTypes[],
unsigned char outputsCount,
OutputType outputTypes[],
unsigned long maxSilencePeriod,
DateTime *now) {
NetworkDeviceStatus *newDevice = networkDevices.allocate();
newDevice->initialize(address, sensorsCount, sensorTypes, outputsCount, outputTypes, maxSilencePeriod, now);
}
unsigned char NetworkResources::removeDeadDevices(DateTime *now) {
assert(now != 0);
unsigned char removedDevices = 0;
InstanceIterator<NetworkDeviceStatus> *iterator = networkDevices.getInstanceIterator();
//Scorre tutti i device presenti nella rete
while(iterator->hasNext()) {
NetworkDeviceStatus *deviceStatus = iterator->next();
ComputedDeviceStatus computedStatus = deviceStatus->getComputedStatus(now);
//Se il device e' considerato perso viene rimosso
if(computedStatus == LOST) {
bool success = networkDevices.deallocate(deviceStatus);
assert(success);
++removedDevices;
}
}
return removedDevices;
}
NetworkDevice *NetworkResources::getDeviceByAddress(unsigned char address[ADDRESS_BYTES]) {
NetworkDeviceStatus *deviceStatus = getDeviceStatusByAddress(address);
if(deviceStatus == 0) {
return 0;
}
return deviceStatus->getNetworkDevice();
}
NetworkResources::NetworkResources() : networkDevices(MAX_NETWORK_DEVICES) {
}
void NetworkResources::initialize(FileReader *fileReader) {
assert(fileReader != 0);
//Dealloca tutte le istanze preesistenti
networkDevices.clear();
unsigned char devicesCount = fileReader->readByte();
//Alloca tutti i device descritti dal file di stato
for(int i = 0; i < devicesCount; ++i) {
NetworkDeviceStatus *currentDevice = networkDevices.allocate();
currentDevice->initialize(fileReader);
}
}
void NetworkResources::writeToFile(FileWriter *fileWriter) {
//Scrive il numero di device
unsigned char devicesCount = networkDevices.getInstanceCount();
fileWriter->writeByte(devicesCount);
//Scrive le informazioni di tutti i device
InstanceIterator<NetworkDeviceStatus> *iterator = networkDevices.getInstanceIterator();
#ifdef OFFBOARD
unsigned char writtenDevices = 0;
#endif // OFFBOARD
while(iterator->hasNext()) {
NetworkDeviceStatus *currentStatus = iterator->next();
currentStatus->writeToFile(fileWriter);
#ifdef OFFBOARD
++writtenDevices;
#endif // OFFBOARD
}
//Si assicura che siano state scritte il
//numero di device attesi
#ifdef OFFBOARD
assert(writtenDevices == devicesCount);
#endif // OFFBOARD
}
/**
* @return true se le due istanze sono logicamente equivalenti
*/
bool NetworkResources::equals(NetworkResources *otherResources) {
if(otherResources == 0) {
return false;
}
//Un'istanza e' uguale a se stessa
if(this == otherResources) {
return true;
}
//Se ci sono numeri diversi di device allora lo stato logico rappresentato e' diverso
//Serve anche a garantire la riflettivita' dell'uguaglianza
if(networkDevices.getInstanceCount() != otherResources->networkDevices.getInstanceCount()) {
return false;
}
//Verifica che nelle istanze siano contenuti gli stessi stati
InstanceIterator<NetworkDeviceStatus> *iterator = networkDevices.getInstanceIterator();
while(iterator->hasNext()) {
NetworkDeviceStatus *currentDevice = iterator->next();
NetworkDeviceStatus *otherDeviceStatus = otherResources->getDeviceStatusByAddress(currentDevice->getNetworkDevice()->getAddress());
if(currentDevice->equals(otherDeviceStatus) == false) {
return false;
}
}
return true;
}