Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix scan-build warning (link may be null).
[simgrid.git] / src / s4u / s4u_netzone.cpp
1 /* Copyright (c) 2006-2018. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "simgrid/kernel/routing/NetPoint.hpp"
7 #include "simgrid/s4u/Engine.hpp"
8 #include "simgrid/s4u/Host.hpp"
9 #include "simgrid/s4u/NetZone.hpp"
10 #include "simgrid/zone.h"
11
12 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_netzone, "S4U Networking Zones");
13
14 namespace simgrid {
15 namespace s4u {
16
17 simgrid::xbt::signal<void(bool symmetrical, kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst,
18                           kernel::routing::NetPoint* gw_src, kernel::routing::NetPoint* gw_dst,
19                           std::vector<kernel::resource::LinkImpl*>& link_list)>
20     NetZone::onRouteCreation;
21 simgrid::xbt::signal<void(NetZone&)> NetZone::onCreation;
22 simgrid::xbt::signal<void(NetZone&)> NetZone::onSeal;
23
24 NetZone::NetZone(NetZone* father, std::string name) : father_(father), name_(name)
25 {
26   children_ = new std::vector<NetZone*>();
27 }
28
29 void NetZone::seal()
30 {
31   sealed_ = true;
32 }
33
34 NetZone::~NetZone()
35 {
36   for (auto const& nz : *children_)
37     delete nz;
38   delete children_;
39 }
40
41 std::unordered_map<std::string, std::string>* NetZone::getProperties()
42 {
43   return simgrid::simix::kernelImmediate([this] {
44       return &properties_;
45   });
46 }
47
48 /** Retrieve the property value (or nullptr if not set) */
49 const char* NetZone::getProperty(const char* key)
50 {
51   return properties_.at(key).c_str();
52 }
53 void NetZone::setProperty(const char* key, const char* value)
54 {
55   simgrid::simix::kernelImmediate([this,key,value] {
56     properties_[key] = value;
57   });
58 }
59
60 /** @brief Returns the list of direct children (no grand-children)
61  *
62  * This function returns the internal copy of the children, not a copy. Don't mess with it!
63  */
64 std::vector<NetZone*>* NetZone::getChildren()
65 {
66   return children_;
67 }
68
69 const char* NetZone::get_cname() const
70 {
71   return name_.c_str();
72 }
73 NetZone* NetZone::getFather()
74 {
75   return father_;
76 }
77
78 void NetZone::getHosts(std::vector<s4u::Host*>* whereto)
79 {
80   for (auto const& card : vertices_) {
81     s4u::Host* host = simgrid::s4u::Host::by_name_or_null(card->get_name());
82     if (host != nullptr)
83       whereto->push_back(host);
84   }
85 }
86
87 int NetZone::getHostCount()
88 {
89   int count = 0;
90   for (auto const& card : vertices_) {
91     s4u::Host* host = simgrid::s4u::Host::by_name_or_null(card->get_name());
92     if (host != nullptr)
93       count++;
94   }
95   return count;
96 }
97
98 int NetZone::addComponent(kernel::routing::NetPoint* elm)
99 {
100   vertices_.push_back(elm);
101   return vertices_.size() - 1; // The rank of the newly created object
102 }
103
104 void NetZone::add_route(kernel::routing::NetPoint* /*src*/, kernel::routing::NetPoint* /*dst*/,
105                         kernel::routing::NetPoint* /*gw_src*/, kernel::routing::NetPoint* /*gw_dst*/,
106                         std::vector<kernel::resource::LinkImpl*>& /*link_list*/, bool /*symmetrical*/)
107 {
108   xbt_die("NetZone '%s' does not accept new routes (wrong class).", name_.c_str());
109 }
110
111 } // namespace s4u
112 } // namespace simgrid
113
114 /* **************************** Public C interface *************************** */
115
116 sg_netzone_t sg_zone_get_root()
117 {
118   return simgrid::s4u::Engine::getInstance()->getNetRoot();
119 }
120
121 const char* sg_zone_get_name(sg_netzone_t netzone)
122 {
123   return netzone->get_cname();
124 }
125
126 sg_netzone_t sg_zone_get_by_name(const char* name)
127 {
128   return simgrid::s4u::Engine::getInstance()->getNetzoneByNameOrNull(name);
129 }
130
131 void sg_zone_get_sons(sg_netzone_t netzone, xbt_dict_t whereto)
132 {
133   for (auto const& elem : *netzone->getChildren()) {
134     xbt_dict_set(whereto, elem->get_cname(), static_cast<void*>(elem), nullptr);
135   }
136 }
137
138 const char* sg_zone_get_property_value(sg_netzone_t netzone, const char* name)
139 {
140   return netzone->getProperty(name);
141 }
142
143 void sg_zone_set_property_value(sg_netzone_t netzone, const char* name, char* value)
144 {
145   netzone->setProperty(name, value);
146 }
147
148 void sg_zone_get_hosts(sg_netzone_t netzone, xbt_dynar_t whereto)
149 {
150   /* converts vector to dynar */
151   std::vector<simgrid::s4u::Host*> hosts;
152   netzone->getHosts(&hosts);
153   for (auto const& host : hosts)
154     xbt_dynar_push(whereto, &host);
155 }