Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
56864b5f473b0d7d86fe4df647e82a18cdce4adc
[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(const char *name)
21     : 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) {
34         delete (As*)elem;
35       }
36
37
38       xbt_dict_free(&children_);
39       xbt_dynar_free(&vertices_);
40       for (auto &kv : bypassRoutes_)
41         delete kv.second;
42       xbt_free(name_);
43     }
44
45     xbt_dict_t As::children()
46     {
47       return children_;
48     }
49     char *As::name()
50     {
51       return name_;
52     }
53     As *As::father() {
54       return father_;
55     }
56
57     xbt_dynar_t As::hosts()
58     {
59       xbt_dynar_t res =  xbt_dynar_new(sizeof(sg_host_t), nullptr);
60
61       for (unsigned int index = 0; index < xbt_dynar_length(vertices_); index++) {
62         kernel::routing::NetCard *card = xbt_dynar_get_as(vertices_, index, kernel::routing::NetCard*);
63         s4u::Host                *host = simgrid::s4u::Host::by_name_or_null(card->name());
64         if (host!=nullptr)
65           xbt_dynar_push(res, &host);
66       }
67       return res;
68     }
69
70     int As::addComponent(kernel::routing::NetCard *elm) {
71       xbt_dynar_push_as(vertices_, kernel::routing::NetCard*, elm);
72       return xbt_dynar_length(vertices_)-1;
73     }
74
75     void As::addRoute(sg_platf_route_cbarg_t /*route*/){
76       xbt_die("AS %s does not accept new routes (wrong class).",name_);
77     }
78
79     void As::addBypassRoute(sg_platf_route_cbarg_t e_route){
80       /* Argument validity checks */
81       if (e_route->gw_dst) {
82         XBT_DEBUG("Load bypassASroute from %s@%s to %s@%s",
83             e_route->src->name(), e_route->gw_src->name(),
84             e_route->dst->name(), e_route->gw_dst->name());
85         xbt_assert(!e_route->link_list->empty(), "Bypass route between %s@%s and %s@%s cannot be empty.",
86             e_route->src->name(), e_route->gw_src->name(),
87             e_route->dst->name(), e_route->gw_dst->name());
88         xbt_assert(bypassRoutes_.find({e_route->src->name(),e_route->dst->name()}) == bypassRoutes_.end(),
89             "The bypass route between %s@%s and %s@%s already exists.",
90             e_route->src->name(), e_route->gw_src->name(), e_route->dst->name(), e_route->gw_dst->name());
91       } else {
92         XBT_DEBUG("Load bypassRoute from %s to %s", e_route->src->name(), e_route->dst->name());
93         xbt_assert(!e_route->link_list->empty(),                         "Bypass route between %s and %s cannot be empty.",
94             e_route->src->name(), e_route->dst->name());
95         xbt_assert(bypassRoutes_.find({e_route->src->name(),e_route->dst->name()}) == bypassRoutes_.end(),
96             "The bypass route between %s and %s already exists.",
97             e_route->src->name(), e_route->dst->name());
98       }
99
100       /* Build a copy that will be stored in the dict */
101       std::vector<surf::Link*> *newRoute = new std::vector<surf::Link*>();
102       for (auto link: *e_route->link_list)
103         newRoute->push_back(link);
104
105       /* Store it */
106       bypassRoutes_.insert({{e_route->src->name(),e_route->dst->name()}, newRoute});
107     }
108
109 }  }; // namespace simgrid::s4u