Logo AND Algorithmique Numérique Distribuée

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