Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
move NetCard to its own header file
[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/NetCard.hpp"
13 #include "src/surf/network_interface.hpp" // Link FIXME: move to proper header
14 #include "src/surf/surf_routing.hpp"
15
16 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_as,"S4U autonomous systems");
17
18 namespace simgrid {
19   namespace s4u {
20
21   As::As(As* father, const char* name) : father_(father), name_(xbt_strdup(name))
22   {
23   }
24   void As::seal()
25   {
26     sealed_ = true;
27   }
28   As::~As()
29   {
30     xbt_dict_cursor_t cursor = nullptr;
31     char* key;
32     AS_t elem;
33     xbt_dict_foreach(children_, cursor, key, elem) { delete (As*)elem; }
34
35     xbt_dict_free(&children_);
36     for (auto& kv : bypassRoutes_)
37       delete kv.second;
38     xbt_free(name_);
39   }
40
41   xbt_dict_t As::children()
42   {
43     return children_;
44   }
45   char* As::name()
46   {
47     return name_;
48   }
49   As* As::father()
50   {
51     return father_;
52   }
53
54   xbt_dynar_t As::hosts()
55   {
56     xbt_dynar_t res = xbt_dynar_new(sizeof(sg_host_t), nullptr);
57
58     for (auto card : vertices_) {
59       s4u::Host* host = simgrid::s4u::Host::by_name_or_null(card->name());
60       if (host != nullptr)
61         xbt_dynar_push(res, &host);
62     }
63     return res;
64   }
65
66   int As::addComponent(kernel::routing::NetCard* elm)
67   {
68     vertices_.push_back(elm);
69     return vertices_.size() - 1; // The rank of the newly created object
70   }
71
72   void As::addRoute(sg_platf_route_cbarg_t /*route*/)
73   {
74     xbt_die("AS %s does not accept new routes (wrong class).", name_);
75   }
76
77   void As::addBypassRoute(sg_platf_route_cbarg_t e_route)
78   {
79     /* Argument validity checks */
80     if (e_route->gw_dst) {
81       XBT_DEBUG("Load bypassASroute from %s@%s to %s@%s", e_route->src->name().c_str(), e_route->gw_src->name().c_str(),
82                 e_route->dst->name().c_str(), e_route->gw_dst->name().c_str());
83       xbt_assert(!e_route->link_list->empty(), "Bypass route between %s@%s and %s@%s cannot be empty.",
84                  e_route->src->name().c_str(), e_route->gw_src->name().c_str(), e_route->dst->name().c_str(),
85                  e_route->gw_dst->name().c_str());
86       xbt_assert(bypassRoutes_.find({e_route->src, e_route->dst}) == bypassRoutes_.end(),
87                  "The bypass route between %s@%s and %s@%s already exists.", e_route->src->name().c_str(),
88                  e_route->gw_src->name().c_str(), e_route->dst->name().c_str(), e_route->gw_dst->name().c_str());
89     } else {
90       XBT_DEBUG("Load bypassRoute from %s to %s", e_route->src->name().c_str(), e_route->dst->name().c_str());
91       xbt_assert(!e_route->link_list->empty(), "Bypass route between %s and %s cannot be empty.",
92                  e_route->src->name().c_str(), e_route->dst->name().c_str());
93       xbt_assert(bypassRoutes_.find({e_route->src, e_route->dst}) == bypassRoutes_.end(),
94                  "The bypass route between %s and %s already exists.", e_route->src->name().c_str(),
95                  e_route->dst->name().c_str());
96     }
97
98     /* Build a copy that will be stored in the dict */
99     kernel::routing::AsRoute* newRoute = new kernel::routing::AsRoute(e_route->gw_src, e_route->gw_dst);
100     for (auto link : *e_route->link_list)
101       newRoute->links.push_back(link);
102
103     /* Store it */
104     bypassRoutes_.insert({{e_route->src, e_route->dst}, newRoute});
105   }
106 }  }; // namespace simgrid::s4u