Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1a8587b16d051625c576b313bfe4842af1bdef7a
[simgrid.git] / src / s4u / s4u_as.cpp
1 /* Copyright (c) 2006-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <xbt/log.h>
8
9 #include <simgrid/s4u/host.hpp>
10 #include <simgrid/s4u/As.hpp>
11
12 #include "src/kernel/routing/BypassRoute.hpp"
13 #include "src/kernel/routing/NetCard.hpp"
14 #include "src/surf/network_interface.hpp" // Link FIXME: move to proper header
15 #include "src/surf/surf_routing.hpp"
16
17 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_as,"S4U autonomous systems");
18
19 namespace simgrid {
20   namespace s4u {
21
22   simgrid::xbt::signal<void(bool symmetrical, kernel::routing::NetCard* src, kernel::routing::NetCard* dst,
23                             kernel::routing::NetCard* gw_src, kernel::routing::NetCard* gw_dst,
24                             std::vector<Link*>* link_list)>
25       As::onRouteCreation;
26
27   As::As(As* father, const char* name) : father_(father), name_(xbt_strdup(name))
28   {
29   }
30   void As::seal()
31   {
32     sealed_ = true;
33   }
34   As::~As()
35   {
36     xbt_dict_cursor_t cursor = nullptr;
37     char* key;
38     AS_t elem;
39     xbt_dict_foreach(children_, cursor, key, elem) { delete (As*)elem; }
40
41     xbt_dict_free(&children_);
42     for (auto& kv : bypassRoutes_)
43       delete kv.second;
44     xbt_free(name_);
45   }
46
47   xbt_dict_t As::children()
48   {
49     return children_;
50   }
51   char* As::name()
52   {
53     return name_;
54   }
55   As* As::father()
56   {
57     return father_;
58   }
59
60   xbt_dynar_t As::hosts()
61   {
62     xbt_dynar_t res = xbt_dynar_new(sizeof(sg_host_t), nullptr);
63
64     for (auto card : vertices_) {
65       s4u::Host* host = simgrid::s4u::Host::by_name_or_null(card->name());
66       if (host != nullptr)
67         xbt_dynar_push(res, &host);
68     }
69     return res;
70   }
71
72   int As::addComponent(kernel::routing::NetCard* elm)
73   {
74     vertices_.push_back(elm);
75     return vertices_.size() - 1; // The rank of the newly created object
76   }
77
78   void As::addRoute(sg_platf_route_cbarg_t /*route*/)
79   {
80     xbt_die("AS %s does not accept new routes (wrong class).", name_);
81   }
82
83   void As::addBypassRoute(sg_platf_route_cbarg_t e_route)
84   {
85     /* Argument validity checks */
86     if (e_route->gw_dst) {
87       XBT_DEBUG("Load bypassASroute from %s@%s to %s@%s", e_route->src->name().c_str(), e_route->gw_src->name().c_str(),
88                 e_route->dst->name().c_str(), e_route->gw_dst->name().c_str());
89       xbt_assert(!e_route->link_list->empty(), "Bypass route between %s@%s and %s@%s cannot be empty.",
90                  e_route->src->name().c_str(), e_route->gw_src->name().c_str(), e_route->dst->name().c_str(),
91                  e_route->gw_dst->name().c_str());
92       xbt_assert(bypassRoutes_.find({e_route->src, e_route->dst}) == bypassRoutes_.end(),
93                  "The bypass route between %s@%s and %s@%s already exists.", e_route->src->name().c_str(),
94                  e_route->gw_src->name().c_str(), e_route->dst->name().c_str(), e_route->gw_dst->name().c_str());
95     } else {
96       XBT_DEBUG("Load bypassRoute from %s to %s", e_route->src->name().c_str(), e_route->dst->name().c_str());
97       xbt_assert(!e_route->link_list->empty(), "Bypass route between %s and %s cannot be empty.",
98                  e_route->src->name().c_str(), e_route->dst->name().c_str());
99       xbt_assert(bypassRoutes_.find({e_route->src, e_route->dst}) == bypassRoutes_.end(),
100                  "The bypass route between %s and %s already exists.", e_route->src->name().c_str(),
101                  e_route->dst->name().c_str());
102     }
103
104     /* Build a copy that will be stored in the dict */
105     kernel::routing::BypassRoute* newRoute = new kernel::routing::BypassRoute(e_route->gw_src, e_route->gw_dst);
106     for (auto link : *e_route->link_list)
107       newRoute->links.push_back(link);
108
109     /* Store it */
110     bypassRoutes_.insert({{e_route->src, e_route->dst}, newRoute});
111   }
112 }  }; // namespace simgrid::s4u