Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
more legacy MSG functions
[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::on_route_creation;
21 simgrid::xbt::signal<void(NetZone&)> NetZone::on_creation;
22 simgrid::xbt::signal<void(NetZone&)> NetZone::on_seal;
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::get_properties()
42 {
43   return simgrid::simix::simcall([this] { return &properties_; });
44 }
45
46 /** Retrieve the property value (or nullptr if not set) */
47 const char* NetZone::get_property(const char* key)
48 {
49   return properties_.at(key).c_str();
50 }
51 void NetZone::set_property(const char* key, const char* value)
52 {
53   simgrid::simix::simcall([this, key, value] { properties_[key] = value; });
54 }
55
56 /** @brief Returns the list of direct children (no grand-children)
57  *
58  * This function returns the internal copy of the children, not a copy. Don't mess with it!
59  */
60 std::vector<NetZone*>* NetZone::get_children()
61 {
62   return children_;
63 }
64
65 const char* NetZone::get_cname() const
66 {
67   return name_.c_str();
68 }
69 NetZone* NetZone::get_father()
70 {
71   return father_;
72 }
73
74 /** @brief Returns the list of the hosts found in this NetZone (not recursively)
75  *
76  * Only the hosts that are directly contained in this NetZone are retrieved,
77  * not the ones contained in sub-netzones.
78  */
79 std::vector<Host*> NetZone::get_all_hosts()
80 {
81   std::vector<Host*> res;
82   for (auto const& card : vertices_) {
83     s4u::Host* host = simgrid::s4u::Host::by_name_or_null(card->get_name());
84     if (host != nullptr)
85       res.push_back(host);
86   }
87   return res;
88 }
89
90 void NetZone::getHosts(std::vector<s4u::Host*>* whereto)
91 {
92   for (auto const& card : vertices_) {
93     s4u::Host* host = simgrid::s4u::Host::by_name_or_null(card->get_name());
94     if (host != nullptr)
95       whereto->push_back(host);
96   }
97 }
98
99 int NetZone::get_host_count()
100 {
101   int count = 0;
102   for (auto const& card : vertices_) {
103     s4u::Host* host = simgrid::s4u::Host::by_name_or_null(card->get_name());
104     if (host != nullptr)
105       count++;
106   }
107   return count;
108 }
109
110 int NetZone::add_component(kernel::routing::NetPoint* elm)
111 {
112   vertices_.push_back(elm);
113   return vertices_.size() - 1; // The rank of the newly created object
114 }
115
116 void NetZone::add_route(kernel::routing::NetPoint* /*src*/, kernel::routing::NetPoint* /*dst*/,
117                         kernel::routing::NetPoint* /*gw_src*/, kernel::routing::NetPoint* /*gw_dst*/,
118                         std::vector<kernel::resource::LinkImpl*>& /*link_list*/, bool /*symmetrical*/)
119 {
120   xbt_die("NetZone '%s' does not accept new routes (wrong class).", name_.c_str());
121 }
122
123 } // namespace s4u
124 } // namespace simgrid
125
126 /* **************************** Public C interface *************************** */
127
128 sg_netzone_t sg_zone_get_root()
129 {
130   return simgrid::s4u::Engine::get_instance()->get_netzone_root();
131 }
132
133 const char* sg_zone_get_name(sg_netzone_t netzone)
134 {
135   return netzone->get_cname();
136 }
137
138 sg_netzone_t sg_zone_get_by_name(const char* name)
139 {
140   return simgrid::s4u::Engine::get_instance()->netzone_by_name_or_null(name);
141 }
142
143 void sg_zone_get_sons(sg_netzone_t netzone, xbt_dict_t whereto)
144 {
145   for (auto const& elem : *netzone->get_children()) {
146     xbt_dict_set(whereto, elem->get_cname(), static_cast<void*>(elem), nullptr);
147   }
148 }
149
150 const char* sg_zone_get_property_value(sg_netzone_t netzone, const char* name)
151 {
152   return netzone->get_property(name);
153 }
154
155 void sg_zone_set_property_value(sg_netzone_t netzone, const char* name, char* value)
156 {
157   netzone->set_property(name, value);
158 }
159
160 void sg_zone_get_hosts(sg_netzone_t netzone, xbt_dynar_t whereto)
161 {
162   /* converts vector to dynar */
163   std::vector<simgrid::s4u::Host*> hosts = netzone->get_all_hosts();
164   for (auto const& host : hosts)
165     xbt_dynar_push(whereto, &host);
166 }