Alexander Afanasyev | abc0d91 | 2015-08-13 16:49:02 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2011-2015 Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and |
| 6 | * contributors. |
| 7 | * |
| 8 | * ndnSIM is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | **/ |
| 19 | |
| 20 | #include "ndn-scenario-helper.hpp" |
| 21 | #include "ndn-fib-helper.hpp" |
| 22 | #include "ndn-app-helper.hpp" |
| 23 | |
| 24 | #include "ns3/ndnSIM/model/ndn-l3-protocol.hpp" |
| 25 | |
| 26 | #include "ns3/names.h" |
| 27 | #include "ns3/point-to-point-helper.h" |
| 28 | #include "ns3/string.h" |
| 29 | |
| 30 | namespace ns3 { |
| 31 | namespace ndn { |
| 32 | |
| 33 | ScenarioHelper::ScenarioHelper() |
| 34 | : m_isTopologyInitialized(false) |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | void |
| 39 | ScenarioHelper::createTopology(std::initializer_list<std::initializer_list<std::string>/*node clique*/> topology) |
| 40 | { |
| 41 | if (m_isTopologyInitialized) { |
| 42 | throw std::logic_error("Topology cannot be created twice"); |
| 43 | } |
| 44 | |
| 45 | PointToPointHelper p2p; |
| 46 | |
| 47 | for (auto&& clique : topology) { |
| 48 | for (auto i = clique.begin(); i != clique.end(); ++i) { |
| 49 | auto node1 = getOrCreateNode(*i); |
| 50 | for (auto j = i + 1; j != clique.end(); ++j) { |
| 51 | auto node2 = getOrCreateNode(*j); |
| 52 | |
| 53 | auto link = p2p.Install(node1, node2); |
| 54 | links[*i][*j] = link.Get(0); |
| 55 | links[*j][*i] = link.Get(1); |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | ndnHelper.InstallAll(); |
| 61 | m_isTopologyInitialized = true; |
| 62 | } |
| 63 | |
| 64 | void |
spirosmastorakis | 0df15ba | 2015-11-14 08:46:24 -0800 | [diff] [blame] | 65 | ScenarioHelper::disableRibManager() |
| 66 | { |
| 67 | ndnHelper.disableRibManager(); |
| 68 | } |
| 69 | |
| 70 | void |
| 71 | ScenarioHelper::disableFaceManager() |
| 72 | { |
| 73 | ndnHelper.disableFaceManager(); |
| 74 | } |
| 75 | |
| 76 | void |
| 77 | ScenarioHelper::disableStrategyChoiceManager() |
| 78 | { |
| 79 | ndnHelper.disableStrategyChoiceManager(); |
| 80 | } |
| 81 | |
| 82 | void |
| 83 | ScenarioHelper::disableStatusServer() |
| 84 | { |
| 85 | ndnHelper.disableStatusServer(); |
| 86 | } |
| 87 | |
| 88 | void |
Alexander Afanasyev | abc0d91 | 2015-08-13 16:49:02 -0700 | [diff] [blame] | 89 | ScenarioHelper::addRoutes(std::initializer_list<ScenarioHelper::RouteInfo> routes) |
| 90 | { |
| 91 | for (auto&& route : routes) { |
| 92 | FibHelper::AddRoute(getNode(route.node1), route.prefix, |
| 93 | getFace(route.node1, route.node2), route.metric); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | void |
| 98 | ScenarioHelper::addApps(std::initializer_list<ScenarioHelper::AppInfo> apps) |
| 99 | { |
| 100 | for (auto&& app : apps) { |
| 101 | AppHelper appHelper(app.name); |
| 102 | for (auto&& param : app.params) { |
| 103 | appHelper.SetAttribute(param.first, StringValue(param.second)); |
| 104 | } |
| 105 | auto installedApp = appHelper.Install(getNode(app.node)); |
| 106 | installedApp.Start(Time(app.start)); |
| 107 | installedApp.Stop(Time(app.end)); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | Ptr<Node> |
| 112 | ScenarioHelper::getOrCreateNode(const std::string& nodeName) |
| 113 | { |
| 114 | auto node = nodes.find(nodeName); |
| 115 | if (node == nodes.end()) { |
| 116 | std::tie(node, std::ignore) = nodes.insert(std::make_pair(nodeName, CreateObject<Node>())); |
| 117 | Names::Add(nodeName, node->second); |
| 118 | } |
| 119 | return node->second; |
| 120 | } |
| 121 | |
| 122 | Ptr<Node> |
| 123 | ScenarioHelper::getNode(const std::string& nodeName) |
| 124 | { |
| 125 | auto node = nodes.find(nodeName); |
| 126 | if (node != nodes.end()) { |
| 127 | return node->second; |
| 128 | } |
| 129 | |
| 130 | throw std::invalid_argument("Node " + nodeName + " does not exist"); |
| 131 | } |
| 132 | |
| 133 | shared_ptr<Face> |
| 134 | ScenarioHelper::getFace(const std::string& node1, const std::string& node2) |
| 135 | { |
| 136 | auto i = links.find(node1); |
| 137 | if (i != links.end()) { |
| 138 | auto j = i->second.find(node2); |
| 139 | if (j != i->second.end()) { |
| 140 | return j->second->GetNode()->GetObject<L3Protocol>()->getFaceByNetDevice(j->second); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | throw std::invalid_argument("Link between " + node1 + " and " + node2 + " does not exist"); |
| 145 | } |
| 146 | |
| 147 | } // namespace ndn |
| 148 | } // namespace ns3 |