YARP
Yet Another Robot Platform
primresource.cpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
7 
8 #include <cstring>
9 
10 using namespace yarp::manager;
11 
12 
17 {
18  totalSpace = (Capacity)0;
19  freeSpace = (Capacity)0;
20 }
21 
22 
23 Memory::Memory(const char* szName) : GenericResource("Memory")
24 {
25  setName(szName);
26  totalSpace = (Capacity)0;
27  freeSpace = (Capacity)0;
28 }
29 
31 {
32  if (!getAvailability() || getDisable()) {
33  return false;
34  }
35 
36  auto* mem = dynamic_cast<Memory*>(resource);
37  if (!mem) {
38  return false;
39  }
40  return ( (freeSpace >= mem->getFreeSpace()) &&
41  (totalSpace >= mem->getTotalSpace()) );
42 }
43 
45 {
46  auto* resource = new Memory(*this);
47  return resource;
48 }
49 
50 
51 Memory::~Memory() = default;
52 
53 
54 
59 {
60  totalSpace = (Capacity)0;
61  freeSpace = (Capacity)0;
62 }
63 
64 
65 Storage::Storage(const char* szName) : GenericResource("Storage")
66 {
67  setName(szName);
68  totalSpace = (Capacity)0;
69  freeSpace = (Capacity)0;
70 }
71 
73 {
74  if (!getAvailability() || getDisable()) {
75  return false;
76  }
77 
78  auto* mem = dynamic_cast<Storage*>(resource);
79  if (!mem) {
80  return false;
81  }
82  return ( (freeSpace >= mem->getFreeSpace()) &&
83  (totalSpace >= mem->getTotalSpace()) );
84 }
85 
87 {
88  auto* resource = new Storage(*this);
89  return resource;
90 }
91 
92 
93 Storage::~Storage() = default;
94 
95 
96 
101 {
102 }
103 
104 
105 Network::Network(const char* szName) : GenericResource("Network")
106 {
107  setName(szName);
108 }
109 
111 {
112  if (!getAvailability() || getDisable()) {
113  return false;
114  }
115 
116  auto* net = dynamic_cast<Network*>(resource);
117  if (!net) {
118  return false;
119  }
120  bool ret = (!strlen(net->getIP4()))? true : (strIP4 == std::string(net->getIP4()));
121  ret &= (!strlen(net->getIP6()))? true : (strIP6 == std::string(net->getIP6()));
122  ret &= (!strlen(net->getMAC()))? true : (strMAC == std::string(net->getMAC()));
123  return ret;
124 }
125 
127 {
128  auto* resource = new Network(*this);
129  return resource;
130 }
131 
132 
133 Network::~Network() = default;
134 
135 
136 
141 {
142  cores = (size_t)0;
143  frequency = (double)0.0;
144  siblings = (size_t)0;
145  cpuload.loadAverage1 = (double)0.0;
146  cpuload.loadAverage5 = (double)0.0;
147  cpuload.loadAverage15 = (double)0.0;
148  cpuload.loadAverageInstant = (double)0.0;
149 }
150 
151 
152 Processor::Processor(const char* szName) : GenericResource("Processor")
153 {
154  setName(szName);
155  cores = (size_t)0;
156  frequency = (double)0.0;
157  siblings = (size_t)0;
158  cpuload.loadAverage1 = (double)0.0;
159  cpuload.loadAverage5 = (double)0.0;
160  cpuload.loadAverage15 = (double)0.0;
161  cpuload.loadAverageInstant = (double)0.0;
162 }
163 
165 {
166  if (!getAvailability() || getDisable()) {
167  return false;
168  }
169 
170  auto* proc = dynamic_cast<Processor*>(resource);
171  if (!proc) {
172  return false;
173  }
174 
175  bool ret = (!strlen(proc->getArchitecture()))? true : (strArchitecure == std::string(proc->getArchitecture()));
176  ret &= (!strlen(proc->getModel()))? true : (strModel == std::string(proc->getModel()));
177  ret &= (cores >= proc->getCores());
178  ret &= (siblings >= proc->getSiblings());
179  ret &= (frequency >= proc->getFrequency());
180  return ret;
181 }
182 
183 
185 {
186  auto* resource = new Processor(*this);
187  return resource;
188 }
189 
190 
191 Processor::~Processor() = default;
192 
193 
194 
200 {
201 }
202 
203 
204 Computer::Computer(const char* szName) : GenericResource("Computer")
205 {
206  setName(szName);
207  network.setName(szName);
208 }
209 
210 
211 Computer::Computer(const Computer &resource) : GenericResource(resource)
212 {
213  Computer::swap(resource);
214 }
215 
216 
218 {
219  GenericResource::operator=(rhs);
220  Computer::swap(rhs);
221  return *this;
222 }
223 
224 
226 {
227  auto* newres = (GenericResource*) res.clone();
228  peripheralResources.push_back(newres);
229  return true;
230 }
231 
232 
234 {
235  if (!getAvailability() || getDisable()) {
236  return false;
237  }
238 
239  auto* mres = dynamic_cast<MultiResource*>(resource);
240  if(mres)
241  {
242  if (!mres->resourceCount()) {
243  return true;
244  }
245  for(int i=0; i<mres->resourceCount(); i++)
246  {
247  auto* comp = dynamic_cast<Computer*>(&mres->getResourceAt(i));
248  if (comp && satisfyComputer(comp)) {
249  return true;
250  } else if (satisfyComputerResource(&mres->getResourceAt(i))) {
251  return true;
252  }
253  }
254  return false;
255  }
256 
257  auto* comp = dynamic_cast<Computer*>(resource);
258  if (comp) {
259  return satisfyComputer(comp);
260  }
261 
262  return satisfyComputerResource(resource);
263 }
264 
265 
266 bool Computer::satisfyComputer(Computer* comp)
267 {
268  bool ret = satisfyComputerResource(&comp->getMemory());
269  ret &= satisfyComputerResource(&comp->getStorage());
270  ret &= satisfyComputerResource(&comp->getProcessor());
271  ret &= satisfyComputerResource(&comp->getNetwork());
272  ret &= satisfyComputerResource(&comp->getPlatform());
273  for (int i = 0; i < comp->peripheralCount(); i++) {
274  ret &= satisfyComputerResource(&comp->getPeripheralAt(i));
275  }
276  return ret;
277 }
278 
279 
280 bool Computer::satisfyComputerResource(GenericResource* resource)
281 {
282  if (memory.satisfy(resource)) {
283  return true;
284  }
285  if (storage.satisfy(resource)) {
286  return true;
287  }
288  if (network.satisfy(resource)) {
289  return true;
290  }
291  if (processor.satisfy(resource)) {
292  return true;
293  }
294  if (platform.satisfy(resource)) {
295  return true;
296  }
297 
298  ResourcePIterator itr;
299  for (itr = peripheralResources.begin(); itr != peripheralResources.end(); itr++) {
300  if ((*itr)->satisfy(resource)) {
301  return true;
302  }
303  }
304  return false;
305 }
306 
308 {
309  auto* resource = new Computer(*this);
310  return resource;
311 }
312 
313 void Computer::swap(const Computer &comp)
314 {
315  clear();
316  memory = comp.memory;
317  storage = comp.storage;
318  processor = comp.processor;
319  network = comp.network;
320  platform = comp.platform;
321  processes = comp.processes;
322  // deep copy
323  for (int i = 0; i < comp.peripheralCount(); i++) {
325  }
326 }
327 
329 {
330  for(auto& peripheralResource : peripheralResources)
331  {
332  delete peripheralResource;
333  peripheralResource = nullptr;
334  }
335  peripheralResources.clear();
336  processes.clear();
337 }
338 
339 
341 {
342  clear();
343 }
bool ret
GenericResource & getPeripheralAt(int index) const
Definition: primresource.h:195
bool satisfy(GenericResource *resource) override
Processor & getProcessor()
Definition: primresource.h:190
Computer()
Class Computer.
int peripheralCount() const
Definition: primresource.h:194
Platform & getPlatform()
Definition: primresource.h:192
bool addPeripheral(GenericResource &res)
Computer & operator=(const Computer &rhs)
Node * clone() override
void setName(const char *szName)
Definition: resource.h:28
Node * clone() override
Memory()
Class Memory.
bool satisfy(GenericResource *resource) override
Class MultiResource.
Definition: resource.h:68
Network()
Class Network.
bool satisfy(GenericResource *resource) override
Node * clone() override
a Node of a Graph
Definition: node.h:65
virtual Node * clone()=0
bool satisfy(GenericResource *resource) override
bool satisfy(GenericResource *resource) override
Processor()
Class Processor.
Node * clone() override
bool satisfy(GenericResource *resource) override
Node * clone() override
Storage()
Class Storage.
std::vector< GenericResource * >::iterator ResourcePIterator
Definition: resource.h:60
size_t Capacity
Definition: primresource.h:19