Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Constructor priorities are still not supported on Apple.
[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::simcall([this] { return &properties_; });
44 }
45
46 /** Retrieve the property value (or nullptr if not set) */
47 const char* NetZone::getProperty(const char* key)
48 {
49   return properties_.at(key).c_str();
50 }
51 void NetZone::setProperty(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::getChildren()
61 {
62   return children_;
63 }
64
65 const char* NetZone::get_cname() const
66 {
67   return name_.c_str();
68 }
69 NetZone* NetZone::getFather()
70 {
71   return father_;
72 }
73
74 void NetZone::getHosts(std::vector<s4u::Host*>* whereto)
75 {
76   for (auto const& card : vertices_) {
77     s4u::Host* host = simgrid::s4u::Host::by_name_or_null(card->get_name());
78     if (host != nullptr)
79       whereto->push_back(host);
80   }
81 }
82
83 int NetZone::getHostCount()
84 {
85   int count = 0;
86   for (auto const& card : vertices_) {
87     s4u::Host* host = simgrid::s4u::Host::by_name_or_null(card->get_name());
88     if (host != nullptr)
89       count++;
90   }
91   return count;
92 }
93
94 int NetZone::addComponent(kernel::routing::NetPoint* elm)
95 {
96   vertices_.push_back(elm);
97   return vertices_.size() - 1; // The rank of the newly created object
98 }
99
100 void NetZone::add_route(kernel::routing::NetPoint* /*src*/, kernel::routing::NetPoint* /*dst*/,
101                         kernel::routing::NetPoint* /*gw_src*/, kernel::routing::NetPoint* /*gw_dst*/,
102                         std::vector<kernel::resource::LinkImpl*>& /*link_list*/, bool /*symmetrical*/)
103 {
104   xbt_die("NetZone '%s' does not accept new routes (wrong class).", name_.c_str());
105 }
106
107 } // namespace s4u
108 } // namespace simgrid
109
110 /* **************************** Public C interface *************************** */
111
112 sg_netzone_t sg_zone_get_root()
113 {
114   return simgrid::s4u::Engine::get_instance()->get_netzone_root();
115 }
116
117 const char* sg_zone_get_name(sg_netzone_t netzone)
118 {
119   return netzone->get_cname();
120 }
121
122 sg_netzone_t sg_zone_get_by_name(const char* name)
123 {
124   return simgrid::s4u::Engine::get_instance()->netzone_by_name_or_null(name);
125 }
126
127 void sg_zone_get_sons(sg_netzone_t netzone, xbt_dict_t whereto)
128 {
129   for (auto const& elem : *netzone->getChildren()) {
130     xbt_dict_set(whereto, elem->get_cname(), static_cast<void*>(elem), nullptr);
131   }
132 }
133
134 const char* sg_zone_get_property_value(sg_netzone_t netzone, const char* name)
135 {
136   return netzone->getProperty(name);
137 }
138
139 void sg_zone_set_property_value(sg_netzone_t netzone, const char* name, char* value)
140 {
141   netzone->setProperty(name, value);
142 }
143
144 void sg_zone_get_hosts(sg_netzone_t netzone, xbt_dynar_t whereto)
145 {
146   /* converts vector to dynar */
147   std::vector<simgrid::s4u::Host*> hosts;
148   netzone->getHosts(&hosts);
149   for (auto const& host : hosts)
150     xbt_dynar_push(whereto, &host);
151 }