Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [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. |
Alexander Afanasyev | 0fb80b9 | 2013-07-20 08:20:50 -0700 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 5 | * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and |
| 6 | * contributors. |
Alexander Afanasyev | 0fb80b9 | 2013-07-20 08:20:50 -0700 | [diff] [blame] | 7 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 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. |
Alexander Afanasyev | 0fb80b9 | 2013-07-20 08:20:50 -0700 | [diff] [blame] | 11 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 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. |
Alexander Afanasyev | 0fb80b9 | 2013-07-20 08:20:50 -0700 | [diff] [blame] | 15 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 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 | **/ |
Alexander Afanasyev | 0fb80b9 | 2013-07-20 08:20:50 -0700 | [diff] [blame] | 19 | |
Alexander Afanasyev | 0c39537 | 2014-12-20 15:54:02 -0800 | [diff] [blame] | 20 | #include "ndn-link-control-helper.hpp" |
Alexander Afanasyev | 0fb80b9 | 2013-07-20 08:20:50 -0700 | [diff] [blame] | 21 | |
| 22 | #include "ns3/assert.h" |
| 23 | #include "ns3/names.h" |
| 24 | #include "ns3/point-to-point-net-device.h" |
| 25 | #include "ns3/point-to-point-channel.h" |
| 26 | #include "ns3/channel.h" |
| 27 | #include "ns3/log.h" |
Alexander Afanasyev | 3f7dd87 | 2014-12-31 14:10:33 -0800 | [diff] [blame] | 28 | #include "ns3/error-model.h" |
| 29 | #include "ns3/string.h" |
| 30 | #include "ns3/boolean.h" |
| 31 | #include "ns3/double.h" |
| 32 | #include "ns3/pointer.h" |
Alexander Afanasyev | 0fb80b9 | 2013-07-20 08:20:50 -0700 | [diff] [blame] | 33 | |
Alexander Afanasyev | 3f7dd87 | 2014-12-31 14:10:33 -0800 | [diff] [blame] | 34 | #include "model/ndn-l3-protocol.hpp" |
Spyridon Mastorakis | 5ea3322 | 2016-12-07 14:33:53 -0800 | [diff] [blame^] | 35 | #include "model/ndn-net-device-transport.hpp" |
Alexander Afanasyev | ca3c67e | 2016-09-08 15:48:23 -0700 | [diff] [blame] | 36 | #include "NFD/daemon/face/face.hpp" |
Alexander Afanasyev | 3f7dd87 | 2014-12-31 14:10:33 -0800 | [diff] [blame] | 37 | |
| 38 | #include "fw/forwarder.hpp" |
Alexander Afanasyev | 0fb80b9 | 2013-07-20 08:20:50 -0700 | [diff] [blame] | 39 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 40 | NS_LOG_COMPONENT_DEFINE("ndn.LinkControlHelper"); |
Alexander Afanasyev | 0fb80b9 | 2013-07-20 08:20:50 -0700 | [diff] [blame] | 41 | |
| 42 | namespace ns3 { |
| 43 | namespace ndn { |
| 44 | |
| 45 | void |
Alexander Afanasyev | 3f7dd87 | 2014-12-31 14:10:33 -0800 | [diff] [blame] | 46 | LinkControlHelper::setErrorRate(Ptr<Node> node1, Ptr<Node> node2, double errorRate) |
Alexander Afanasyev | 0fb80b9 | 2013-07-20 08:20:50 -0700 | [diff] [blame] | 47 | { |
Alexander Afanasyev | 3f7dd87 | 2014-12-31 14:10:33 -0800 | [diff] [blame] | 48 | NS_LOG_FUNCTION(node1 << node2 << errorRate); |
Alexander Afanasyev | 0fb80b9 | 2013-07-20 08:20:50 -0700 | [diff] [blame] | 49 | |
Alexander Afanasyev | 3f7dd87 | 2014-12-31 14:10:33 -0800 | [diff] [blame] | 50 | NS_ASSERT(node1 != nullptr && node2 != nullptr); |
| 51 | NS_ASSERT(errorRate <= 1.0); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 52 | |
| 53 | Ptr<ndn::L3Protocol> ndn1 = node1->GetObject<ndn::L3Protocol>(); |
| 54 | Ptr<ndn::L3Protocol> ndn2 = node2->GetObject<ndn::L3Protocol>(); |
| 55 | |
Alexander Afanasyev | 3f7dd87 | 2014-12-31 14:10:33 -0800 | [diff] [blame] | 56 | NS_ASSERT(ndn1 != nullptr && ndn2 != nullptr); |
Alexander Afanasyev | 0fb80b9 | 2013-07-20 08:20:50 -0700 | [diff] [blame] | 57 | |
| 58 | // iterate over all faces to find the right one |
Alexander Afanasyev | 3f7dd87 | 2014-12-31 14:10:33 -0800 | [diff] [blame] | 59 | for (const auto& face : ndn1->getForwarder()->getFaceTable()) { |
Spyridon Mastorakis | 5ea3322 | 2016-12-07 14:33:53 -0800 | [diff] [blame^] | 60 | auto transport = dynamic_cast<NetDeviceTransport*>(face.getTransport()); |
| 61 | if (transport == nullptr) |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 62 | continue; |
Alexander Afanasyev | 0fb80b9 | 2013-07-20 08:20:50 -0700 | [diff] [blame] | 63 | |
Spyridon Mastorakis | 5ea3322 | 2016-12-07 14:33:53 -0800 | [diff] [blame^] | 64 | Ptr<PointToPointNetDevice> nd1 = transport->GetNetDevice()->GetObject<PointToPointNetDevice>(); |
Alexander Afanasyev | 3f7dd87 | 2014-12-31 14:10:33 -0800 | [diff] [blame] | 65 | if (nd1 == nullptr) |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 66 | continue; |
Alexander Afanasyev | 0fb80b9 | 2013-07-20 08:20:50 -0700 | [diff] [blame] | 67 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 68 | Ptr<Channel> channel = nd1->GetChannel(); |
Alexander Afanasyev | 3f7dd87 | 2014-12-31 14:10:33 -0800 | [diff] [blame] | 69 | if (channel == nullptr) |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 70 | continue; |
Alexander Afanasyev | 0fb80b9 | 2013-07-20 08:20:50 -0700 | [diff] [blame] | 71 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 72 | Ptr<PointToPointChannel> ppChannel = DynamicCast<PointToPointChannel>(channel); |
Alexander Afanasyev | 0fb80b9 | 2013-07-20 08:20:50 -0700 | [diff] [blame] | 73 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 74 | Ptr<NetDevice> nd2 = ppChannel->GetDevice(0); |
| 75 | if (nd2->GetNode() == node1) |
| 76 | nd2 = ppChannel->GetDevice(1); |
Alexander Afanasyev | 0fb80b9 | 2013-07-20 08:20:50 -0700 | [diff] [blame] | 77 | |
Spyridon Mastorakis | da32591 | 2015-05-28 22:45:33 -0700 | [diff] [blame] | 78 | if (nd2->GetNode() == node2) { |
| 79 | ObjectFactory errorFactory("ns3::RateErrorModel"); |
| 80 | errorFactory.Set("ErrorUnit", StringValue("ERROR_UNIT_PACKET")); |
| 81 | errorFactory.Set("ErrorRate", DoubleValue(errorRate)); |
| 82 | if (errorRate <= 0) { |
| 83 | errorFactory.Set("IsEnabled", BooleanValue(false)); |
| 84 | } |
Alexander Afanasyev | 3f7dd87 | 2014-12-31 14:10:33 -0800 | [diff] [blame] | 85 | |
Spyridon Mastorakis | da32591 | 2015-05-28 22:45:33 -0700 | [diff] [blame] | 86 | nd1->SetAttribute("ReceiveErrorModel", PointerValue(errorFactory.Create<ErrorModel>())); |
| 87 | nd2->SetAttribute("ReceiveErrorModel", PointerValue(errorFactory.Create<ErrorModel>())); |
| 88 | return; |
| 89 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 90 | } |
Spyridon Mastorakis | da32591 | 2015-05-28 22:45:33 -0700 | [diff] [blame] | 91 | NS_FATAL_ERROR("There is no link to fail between the requested nodes"); |
Alexander Afanasyev | 0fb80b9 | 2013-07-20 08:20:50 -0700 | [diff] [blame] | 92 | } |
Alexander Afanasyev | 3f7dd87 | 2014-12-31 14:10:33 -0800 | [diff] [blame] | 93 | |
| 94 | void |
| 95 | LinkControlHelper::FailLink(Ptr<Node> node1, Ptr<Node> node2) |
| 96 | { |
| 97 | setErrorRate(node1, node2, 1.0); |
| 98 | } |
| 99 | |
Alexander Afanasyev | 0fb80b9 | 2013-07-20 08:20:50 -0700 | [diff] [blame] | 100 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 101 | LinkControlHelper::FailLinkByName(const std::string& node1, const std::string& node2) |
Alexander Afanasyev | 0fb80b9 | 2013-07-20 08:20:50 -0700 | [diff] [blame] | 102 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 103 | FailLink(Names::Find<Node>(node1), Names::Find<Node>(node2)); |
Alexander Afanasyev | 0fb80b9 | 2013-07-20 08:20:50 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 107 | LinkControlHelper::UpLink(Ptr<Node> node1, Ptr<Node> node2) |
Alexander Afanasyev | 0fb80b9 | 2013-07-20 08:20:50 -0700 | [diff] [blame] | 108 | { |
Alexander Afanasyev | 3f7dd87 | 2014-12-31 14:10:33 -0800 | [diff] [blame] | 109 | setErrorRate(node1, node2, -0.1); // this will ensure error model is disabled |
Alexander Afanasyev | 0fb80b9 | 2013-07-20 08:20:50 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 113 | LinkControlHelper::UpLinkByName(const std::string& node1, const std::string& node2) |
Alexander Afanasyev | 0fb80b9 | 2013-07-20 08:20:50 -0700 | [diff] [blame] | 114 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 115 | UpLink(Names::Find<Node>(node1), Names::Find<Node>(node2)); |
Alexander Afanasyev | 0fb80b9 | 2013-07-20 08:20:50 -0700 | [diff] [blame] | 116 | } |
Alexander Afanasyev | 3f7dd87 | 2014-12-31 14:10:33 -0800 | [diff] [blame] | 117 | |
| 118 | } // namespace ndn |
| 119 | } // namespace ns3 |