Logo AND Algorithmique Numérique Distribuée

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