YARP
Yet Another Robot Platform
primresource.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
10 
11 #include <cstring>
12 
13 using namespace yarp::manager;
14 using namespace std;
15 
16 
21 {
22  totalSpace = (Capacity)0;
23  freeSpace = (Capacity)0;
24 }
25 
26 
27 Memory::Memory(const char* szName) : GenericResource("Memory")
28 {
29  setName(szName);
30  totalSpace = (Capacity)0;
31  freeSpace = (Capacity)0;
32 }
33 
35 {
36  if(!getAvailability() || getDisable())
37  return false;
38 
39  auto* mem = dynamic_cast<Memory*>(resource);
40  if(!mem)
41  return false;
42  return ( (freeSpace >= mem->getFreeSpace()) &&
43  (totalSpace >= mem->getTotalSpace()) );
44 }
45 
47 {
48  auto* resource = new Memory(*this);
49  return resource;
50 }
51 
52 
53 Memory::~Memory() = default;
54 
55 
56 
61 {
62  totalSpace = (Capacity)0;
63  freeSpace = (Capacity)0;
64 }
65 
66 
67 Storage::Storage(const char* szName) : GenericResource("Storage")
68 {
69  setName(szName);
70  totalSpace = (Capacity)0;
71  freeSpace = (Capacity)0;
72 }
73 
75 {
76  if(!getAvailability() || getDisable())
77  return false;
78 
79  auto* mem = dynamic_cast<Storage*>(resource);
80  if(!mem)
81  return false;
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  auto* net = dynamic_cast<Network*>(resource);
116  if(!net)
117  return false;
118  bool ret = (!strlen(net->getIP4()))? true : (strIP4 == string(net->getIP4()));
119  ret &= (!strlen(net->getIP6()))? true : (strIP6 == string(net->getIP6()));
120  ret &= (!strlen(net->getMAC()))? true : (strMAC == string(net->getMAC()));
121  return ret;
122 }
123 
125 {
126  auto* resource = new Network(*this);
127  return resource;
128 }
129 
130 
131 Network::~Network() = default;
132 
133 
134 
139 {
140  cores = (size_t)0;
141  frequency = (double)0.0;
142  siblings = (size_t)0;
143  cpuload.loadAverage1 = (double)0.0;
144  cpuload.loadAverage5 = (double)0.0;
145  cpuload.loadAverage15 = (double)0.0;
146  cpuload.loadAverageInstant = (double)0.0;
147 }
148 
149 
150 Processor::Processor(const char* szName) : GenericResource("Processor")
151 {
152  setName(szName);
153  cores = (size_t)0;
154  frequency = (double)0.0;
155  siblings = (size_t)0;
156  cpuload.loadAverage1 = (double)0.0;
157  cpuload.loadAverage5 = (double)0.0;
158  cpuload.loadAverage15 = (double)0.0;
159  cpuload.loadAverageInstant = (double)0.0;
160 }
161 
163 {
164  if(!getAvailability() || getDisable())
165  return false;
166 
167  auto* proc = dynamic_cast<Processor*>(resource);
168  if(!proc)
169  return false;
170 
171  bool ret = (!strlen(proc->getArchitecture()))? true : (strArchitecure == string(proc->getArchitecture()));
172  ret &= (!strlen(proc->getModel()))? true : (strModel == string(proc->getModel()));
173  ret &= (cores >= proc->getCores());
174  ret &= (siblings >= proc->getSiblings());
175  ret &= (frequency >= proc->getFrequency());
176  return ret;
177 }
178 
179 
181 {
182  auto* resource = new Processor(*this);
183  return resource;
184 }
185 
186 
187 Processor::~Processor() = default;
188 
189 
190 
196 {
197 }
198 
199 
200 Computer::Computer(const char* szName) : GenericResource("Computer")
201 {
202  setName(szName);
203  network.setName(szName);
204 }
205 
206 
207 Computer::Computer(const Computer &resource) : GenericResource(resource)
208 {
209  Computer::swap(resource);
210 }
211 
212 
214 {
215  GenericResource::operator=(rhs);
216  Computer::swap(rhs);
217  return *this;
218 }
219 
220 
222 {
223  auto* newres = (GenericResource*) res.clone();
224  peripheralResources.push_back(newres);
225  return true;
226 }
227 
228 
230 {
231  if(!getAvailability() || getDisable())
232  return false;
233 
234  auto* mres = dynamic_cast<MultiResource*>(resource);
235  if(mres)
236  {
237  if(!mres->resourceCount())
238  return true;
239  for(int i=0; i<mres->resourceCount(); i++)
240  {
241  auto* comp = dynamic_cast<Computer*>(&mres->getResourceAt(i));
242  if(comp &&satisfyComputer(comp))
243  return true;
244  else if(satisfyComputerResource(&mres->getResourceAt(i)))
245  return true;
246  }
247  return false;
248  }
249 
250  auto* comp = dynamic_cast<Computer*>(resource);
251  if(comp)
252  return satisfyComputer(comp);
253 
254  return satisfyComputerResource(resource);
255 }
256 
257 
258 bool Computer::satisfyComputer(Computer* comp)
259 {
260  bool ret = satisfyComputerResource(&comp->getMemory());
261  ret &= satisfyComputerResource(&comp->getStorage());
262  ret &= satisfyComputerResource(&comp->getProcessor());
263  ret &= satisfyComputerResource(&comp->getNetwork());
264  ret &= satisfyComputerResource(&comp->getPlatform());
265  for(int i=0; i<comp->peripheralCount(); i++)
266  ret &= satisfyComputerResource(&comp->getPeripheralAt(i));
267  return ret;
268 }
269 
270 
271 bool Computer::satisfyComputerResource(GenericResource* resource)
272 {
273  if(memory.satisfy(resource))
274  return true;
275  if(storage.satisfy(resource))
276  return true;
277  if(network.satisfy(resource))
278  return true;
279  if(processor.satisfy(resource))
280  return true;
281  if(platform.satisfy(resource))
282  return true;
283 
284  ResourcePIterator itr;
285  for(itr=peripheralResources.begin(); itr!=peripheralResources.end(); itr++)
286  if((*itr)->satisfy(resource))
287  return true;
288  return false;
289 }
290 
292 {
293  auto* resource = new Computer(*this);
294  return resource;
295 }
296 
297 void Computer::swap(const Computer &comp)
298 {
299  clear();
300  memory = comp.memory;
301  storage = comp.storage;
302  processor = comp.processor;
303  network = comp.network;
304  platform = comp.platform;
305  processes = comp.processes;
306  // deep copy
307  for(int i=0; i<comp.peripheralCount(); i++)
309 }
310 
312 {
313  for(auto& peripheralResource : peripheralResources)
314  {
315  delete peripheralResource;
316  peripheralResource = nullptr;
317  }
318  peripheralResources.clear();
319  processes.clear();
320 }
321 
322 
324 {
325  clear();
326 }
yarp::manager::Computer::clone
Node * clone() override
Definition: primresource.cpp:291
yarp::manager::_LoadAvg::loadAverageInstant
double loadAverageInstant
Definition: primresource.h:101
yarp::manager::Processor::~Processor
virtual ~Processor()
yarp::manager::Storage::Storage
Storage()
Class Storage.
Definition: primresource.cpp:60
yarp::manager::Processor
Definition: primresource.h:111
yarp::manager::Computer::clear
void clear()
Definition: primresource.cpp:311
yarp::manager::Storage::~Storage
virtual ~Storage()
yarp::manager::Processor::clone
Node * clone() override
Definition: primresource.cpp:180
yarp::manager::Network::clone
Node * clone() override
Definition: primresource.cpp:124
yarp::manager::Computer::satisfy
bool satisfy(GenericResource *resource) override
Definition: primresource.cpp:229
yarp::manager::Computer
Definition: primresource.h:155
yarp::manager::Computer::getPeripheralAt
GenericResource & getPeripheralAt(int index) const
Definition: primresource.h:178
yarp::manager::Storage::satisfy
bool satisfy(GenericResource *resource) override
Definition: primresource.cpp:74
yarp::manager
Definition: application.h:24
yarp::manager::Node::clone
virtual Node * clone()=0
yarp::manager::Processor::Processor
Processor()
Class Processor.
Definition: primresource.cpp:138
yarp::manager::GenericResource::getDisable
bool getDisable()
Definition: resource.h:29
ret
bool ret
Definition: ImplementAxisInfo.cpp:72
yarp::manager::Computer::Computer
Computer()
Class Computer.
Definition: primresource.cpp:195
yarp::manager::Network::satisfy
bool satisfy(GenericResource *resource) override
Definition: primresource.cpp:110
yarp::manager::Computer::operator=
Computer & operator=(const Computer &rhs)
Definition: primresource.cpp:213
yarp::manager::Network
Definition: primresource.h:76
yarp::manager::Storage
Definition: primresource.h:52
yarp::manager::Computer::peripheralCount
int peripheralCount() const
Definition: primresource.h:177
yarp::manager::Storage::clone
Node * clone() override
Definition: primresource.cpp:86
yarp::manager::Computer::getProcessor
Processor & getProcessor()
Definition: primresource.h:173
yarp::manager::Memory
Class Memory.
Definition: primresource.h:27
yarp::manager::_LoadAvg::loadAverage15
double loadAverage15
Definition: primresource.h:104
yarp::manager::Memory::Memory
Memory()
Class Memory.
Definition: primresource.cpp:20
yarp::manager::Node
a Node of a Graph
Definition: node.h:68
yarp::manager::Computer::~Computer
virtual ~Computer()
Definition: primresource.cpp:323
primresource.h
yarp::manager::Computer::getStorage
Storage & getStorage()
Definition: primresource.h:172
yarp::manager::Platform::satisfy
bool satisfy(GenericResource *resource) override
Definition: logicresource.cpp:36
yarp::manager::Network::Network
Network()
Class Network.
Definition: primresource.cpp:100
yarp::manager::Capacity
size_t Capacity
Definition: primresource.h:22
yarp::manager::Memory::clone
Node * clone() override
Definition: primresource.cpp:46
yarp::manager::GenericResource::getAvailability
bool getAvailability()
Definition: resource.h:27
yarp::manager::MultiResource
Class MultiResource.
Definition: resource.h:71
yarp::manager::Computer::addPeripheral
bool addPeripheral(GenericResource &res)
Definition: primresource.cpp:221
yarp::manager::Processor::satisfy
bool satisfy(GenericResource *resource) override
Definition: primresource.cpp:162
yarp::manager::Network::~Network
virtual ~Network()
yarp::manager::GenericResource
Definition: resource.h:21
yarp::manager::Memory::~Memory
~Memory() override
yarp::manager::Computer::getMemory
Memory & getMemory()
Definition: primresource.h:171
yarp::manager::ResourcePIterator
std::vector< GenericResource * >::iterator ResourcePIterator
Definition: resource.h:63
yarp::manager::_LoadAvg::loadAverage5
double loadAverage5
Definition: primresource.h:103
yarp::manager::Memory::satisfy
bool satisfy(GenericResource *resource) override
Definition: primresource.cpp:34
yarp::manager::Computer::getPlatform
Platform & getPlatform()
Definition: primresource.h:175
yarp::manager::_LoadAvg::loadAverage1
double loadAverage1
Definition: primresource.h:102
yarp::manager::GenericResource::setName
void setName(const char *szName)
Definition: resource.h:31
yarp::manager::Computer::getNetwork
Network & getNetwork()
Definition: primresource.h:174