Logo AND Algorithmique Numérique Distribuée

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