NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
ndn-link-control-helper.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
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"
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"
33 
36 
37 #include "fw/forwarder.hpp"
38 
39 NS_LOG_COMPONENT_DEFINE("ndn.LinkControlHelper");
40 
41 namespace ns3 {
42 namespace ndn {
43 
44 void
45 LinkControlHelper::setErrorRate(Ptr<Node> node1, Ptr<Node> node2, double errorRate)
46 {
47  NS_LOG_FUNCTION(node1 << node2 << errorRate);
48 
49  NS_ASSERT(node1 != nullptr && node2 != nullptr);
50  NS_ASSERT(errorRate <= 1.0);
51 
52  Ptr<ndn::L3Protocol> ndn1 = node1->GetObject<ndn::L3Protocol>();
53  Ptr<ndn::L3Protocol> ndn2 = node2->GetObject<ndn::L3Protocol>();
54 
55  NS_ASSERT(ndn1 != nullptr && ndn2 != nullptr);
56 
57  // iterate over all faces to find the right one
58  for (const auto& face : ndn1->getForwarder()->getFaceTable()) {
59  shared_ptr<ndn::NetDeviceFace> ndFace = std::dynamic_pointer_cast<NetDeviceFace>(face);
60  if (ndFace == nullptr)
61  continue;
62 
63  Ptr<PointToPointNetDevice> nd1 = ndFace->GetNetDevice()->GetObject<PointToPointNetDevice>();
64  if (nd1 == nullptr)
65  continue;
66 
67  Ptr<Channel> channel = nd1->GetChannel();
68  if (channel == nullptr)
69  continue;
70 
71  Ptr<PointToPointChannel> ppChannel = DynamicCast<PointToPointChannel>(channel);
72 
73  Ptr<NetDevice> nd2 = ppChannel->GetDevice(0);
74  if (nd2->GetNode() == node1)
75  nd2 = ppChannel->GetDevice(1);
76 
77  if (nd2->GetNode() == node2) {
78  ObjectFactory errorFactory("ns3::RateErrorModel");
79  errorFactory.Set("ErrorUnit", StringValue("ERROR_UNIT_PACKET"));
80  errorFactory.Set("ErrorRate", DoubleValue(errorRate));
81  if (errorRate <= 0) {
82  errorFactory.Set("IsEnabled", BooleanValue(false));
83  }
84 
85  nd1->SetAttribute("ReceiveErrorModel", PointerValue(errorFactory.Create<ErrorModel>()));
86  nd2->SetAttribute("ReceiveErrorModel", PointerValue(errorFactory.Create<ErrorModel>()));
87  return;
88  }
89  }
90  NS_FATAL_ERROR("There is no link to fail between the requested nodes");
91 }
92 
93 void
94 LinkControlHelper::FailLink(Ptr<Node> node1, Ptr<Node> node2)
95 {
96  setErrorRate(node1, node2, 1.0);
97 }
98 
99 void
100 LinkControlHelper::FailLinkByName(const std::string& node1, const std::string& node2)
101 {
102  FailLink(Names::Find<Node>(node1), Names::Find<Node>(node2));
103 }
104 
105 void
106 LinkControlHelper::UpLink(Ptr<Node> node1, Ptr<Node> node2)
107 {
108  setErrorRate(node1, node2, -0.1); // this will ensure error model is disabled
109 }
110 
111 void
112 LinkControlHelper::UpLinkByName(const std::string& node1, const std::string& node2)
113 {
114  UpLink(Names::Find<Node>(node1), Names::Find<Node>(node2));
115 }
116 
117 } // namespace ndn
118 } // namespace ns3
Copyright (c) 2011-2015 Regents of the University of California.
ndn NetDeviceFace
Copyright (c) 2011-2015 Regents of the University of California.
static void UpLinkByName(const std::string &node1, const std::string &node2)
Re-enable NDN link between two nodes.
static void UpLink(Ptr< Node > node1, Ptr< Node > node2)
Re-enable NDN link between two nodes.
static void FailLink(Ptr< Node > node1, Ptr< Node > node2)
Fail NDN link between two nodes.
Copyright (c) 2011-2015 Regents of the University of California.
ndn L3Protocol
Copyright (c) 2011-2015 Regents of the University of California.
static void FailLinkByName(const std::string &node1, const std::string &node2)
Fail NDN link between two nodes.