Alexander Afanasyev | 0fb80b9 | 2013-07-20 08:20:50 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 19 | * : Saran Tarnoi <saran.tarnoi@gmail.com> |
| 20 | */ |
| 21 | |
Alexander Afanasyev | 0c39537 | 2014-12-20 15:54:02 -0800 | [diff] [blame] | 22 | #include "ndn-link-control-helper.hpp" |
Alexander Afanasyev | 0fb80b9 | 2013-07-20 08:20:50 -0700 | [diff] [blame] | 23 | |
| 24 | #include "ns3/assert.h" |
| 25 | #include "ns3/names.h" |
| 26 | #include "ns3/point-to-point-net-device.h" |
| 27 | #include "ns3/point-to-point-channel.h" |
| 28 | #include "ns3/channel.h" |
| 29 | #include "ns3/log.h" |
Alexander Afanasyev | 3f7dd87 | 2014-12-31 14:10:33 -0800 | [diff] [blame] | 30 | #include "ns3/error-model.h" |
| 31 | #include "ns3/string.h" |
| 32 | #include "ns3/boolean.h" |
| 33 | #include "ns3/double.h" |
| 34 | #include "ns3/pointer.h" |
Alexander Afanasyev | 0fb80b9 | 2013-07-20 08:20:50 -0700 | [diff] [blame] | 35 | |
Alexander Afanasyev | 3f7dd87 | 2014-12-31 14:10:33 -0800 | [diff] [blame] | 36 | #include "model/ndn-l3-protocol.hpp" |
| 37 | #include "model/ndn-net-device-face.hpp" |
| 38 | |
| 39 | #include "fw/forwarder.hpp" |
Alexander Afanasyev | 0fb80b9 | 2013-07-20 08:20:50 -0700 | [diff] [blame] | 40 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 41 | NS_LOG_COMPONENT_DEFINE("ndn.LinkControlHelper"); |
Alexander Afanasyev | 0fb80b9 | 2013-07-20 08:20:50 -0700 | [diff] [blame] | 42 | |
| 43 | namespace ns3 { |
| 44 | namespace ndn { |
| 45 | |
| 46 | void |
Alexander Afanasyev | 3f7dd87 | 2014-12-31 14:10:33 -0800 | [diff] [blame] | 47 | LinkControlHelper::setErrorRate(Ptr<Node> node1, Ptr<Node> node2, double errorRate) |
Alexander Afanasyev | 0fb80b9 | 2013-07-20 08:20:50 -0700 | [diff] [blame] | 48 | { |
Alexander Afanasyev | 3f7dd87 | 2014-12-31 14:10:33 -0800 | [diff] [blame] | 49 | NS_LOG_FUNCTION(node1 << node2 << errorRate); |
Alexander Afanasyev | 0fb80b9 | 2013-07-20 08:20:50 -0700 | [diff] [blame] | 50 | |
Alexander Afanasyev | 3f7dd87 | 2014-12-31 14:10:33 -0800 | [diff] [blame] | 51 | NS_ASSERT(node1 != nullptr && node2 != nullptr); |
| 52 | NS_ASSERT(errorRate <= 1.0); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 53 | |
| 54 | Ptr<ndn::L3Protocol> ndn1 = node1->GetObject<ndn::L3Protocol>(); |
| 55 | Ptr<ndn::L3Protocol> ndn2 = node2->GetObject<ndn::L3Protocol>(); |
| 56 | |
Alexander Afanasyev | 3f7dd87 | 2014-12-31 14:10:33 -0800 | [diff] [blame] | 57 | NS_ASSERT(ndn1 != nullptr && ndn2 != nullptr); |
Alexander Afanasyev | 0fb80b9 | 2013-07-20 08:20:50 -0700 | [diff] [blame] | 58 | |
| 59 | // iterate over all faces to find the right one |
Alexander Afanasyev | 3f7dd87 | 2014-12-31 14:10:33 -0800 | [diff] [blame] | 60 | for (const auto& face : ndn1->getForwarder()->getFaceTable()) { |
| 61 | shared_ptr<ndn::NetDeviceFace> ndFace = std::dynamic_pointer_cast<NetDeviceFace>(face); |
| 62 | if (ndFace == nullptr) |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 63 | continue; |
Alexander Afanasyev | 0fb80b9 | 2013-07-20 08:20:50 -0700 | [diff] [blame] | 64 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 65 | Ptr<PointToPointNetDevice> nd1 = ndFace->GetNetDevice()->GetObject<PointToPointNetDevice>(); |
Alexander Afanasyev | 3f7dd87 | 2014-12-31 14:10:33 -0800 | [diff] [blame] | 66 | if (nd1 == nullptr) |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 67 | continue; |
Alexander Afanasyev | 0fb80b9 | 2013-07-20 08:20:50 -0700 | [diff] [blame] | 68 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 69 | Ptr<Channel> channel = nd1->GetChannel(); |
Alexander Afanasyev | 3f7dd87 | 2014-12-31 14:10:33 -0800 | [diff] [blame] | 70 | if (channel == nullptr) |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 71 | continue; |
Alexander Afanasyev | 0fb80b9 | 2013-07-20 08:20:50 -0700 | [diff] [blame] | 72 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 73 | Ptr<PointToPointChannel> ppChannel = DynamicCast<PointToPointChannel>(channel); |
Alexander Afanasyev | 0fb80b9 | 2013-07-20 08:20:50 -0700 | [diff] [blame] | 74 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 75 | Ptr<NetDevice> nd2 = ppChannel->GetDevice(0); |
| 76 | if (nd2->GetNode() == node1) |
| 77 | nd2 = ppChannel->GetDevice(1); |
Alexander Afanasyev | 0fb80b9 | 2013-07-20 08:20:50 -0700 | [diff] [blame] | 78 | |
Alexander Afanasyev | 3f7dd87 | 2014-12-31 14:10:33 -0800 | [diff] [blame] | 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)); |
Alexander Afanasyev | 0fb80b9 | 2013-07-20 08:20:50 -0700 | [diff] [blame] | 84 | } |
Alexander Afanasyev | 3f7dd87 | 2014-12-31 14:10:33 -0800 | [diff] [blame] | 85 | |
| 86 | nd1->SetAttribute("ReceiveErrorModel", PointerValue(errorFactory.Create<ErrorModel>())); |
| 87 | nd2->SetAttribute("ReceiveErrorModel", PointerValue(errorFactory.Create<ErrorModel>())); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 88 | } |
Alexander Afanasyev | 0fb80b9 | 2013-07-20 08:20:50 -0700 | [diff] [blame] | 89 | } |
Alexander Afanasyev | 3f7dd87 | 2014-12-31 14:10:33 -0800 | [diff] [blame] | 90 | |
| 91 | void |
| 92 | LinkControlHelper::FailLink(Ptr<Node> node1, Ptr<Node> node2) |
| 93 | { |
| 94 | setErrorRate(node1, node2, 1.0); |
| 95 | } |
| 96 | |
Alexander Afanasyev | 0fb80b9 | 2013-07-20 08:20:50 -0700 | [diff] [blame] | 97 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 98 | LinkControlHelper::FailLinkByName(const std::string& node1, const std::string& node2) |
Alexander Afanasyev | 0fb80b9 | 2013-07-20 08:20:50 -0700 | [diff] [blame] | 99 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 100 | FailLink(Names::Find<Node>(node1), Names::Find<Node>(node2)); |
Alexander Afanasyev | 0fb80b9 | 2013-07-20 08:20:50 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 104 | LinkControlHelper::UpLink(Ptr<Node> node1, Ptr<Node> node2) |
Alexander Afanasyev | 0fb80b9 | 2013-07-20 08:20:50 -0700 | [diff] [blame] | 105 | { |
Alexander Afanasyev | 3f7dd87 | 2014-12-31 14:10:33 -0800 | [diff] [blame] | 106 | setErrorRate(node1, node2, -0.1); // this will ensure error model is disabled |
Alexander Afanasyev | 0fb80b9 | 2013-07-20 08:20:50 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 110 | LinkControlHelper::UpLinkByName(const std::string& node1, const std::string& node2) |
Alexander Afanasyev | 0fb80b9 | 2013-07-20 08:20:50 -0700 | [diff] [blame] | 111 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 112 | UpLink(Names::Find<Node>(node1), Names::Find<Node>(node2)); |
Alexander Afanasyev | 0fb80b9 | 2013-07-20 08:20:50 -0700 | [diff] [blame] | 113 | } |
Alexander Afanasyev | 3f7dd87 | 2014-12-31 14:10:33 -0800 | [diff] [blame] | 114 | |
| 115 | } // namespace ndn |
| 116 | } // namespace ns3 |