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 #include "NFD/daemon/face/face.hpp"
37 
38 #include "fw/forwarder.hpp"
39 
40 NS_LOG_COMPONENT_DEFINE("ndn.LinkControlHelper");
41 
42 namespace ns3 {
43 namespace ndn {
44 
45 void
46 LinkControlHelper::setErrorRate(Ptr<Node> node1, Ptr<Node> node2, double errorRate)
47 {
48  NS_LOG_FUNCTION(node1 << node2 << errorRate);
49 
50  NS_ASSERT(node1 != nullptr && node2 != nullptr);
51  NS_ASSERT(errorRate <= 1.0);
52 
53  Ptr<ndn::L3Protocol> ndn1 = node1->GetObject<ndn::L3Protocol>();
54  Ptr<ndn::L3Protocol> ndn2 = node2->GetObject<ndn::L3Protocol>();
55 
56  NS_ASSERT(ndn1 != nullptr && ndn2 != nullptr);
57 
58  // iterate over all faces to find the right one
59  for (const auto& face : ndn1->getForwarder()->getFaceTable()) {
60  auto linkService = dynamic_cast<NetDeviceLinkService*>(face->getLinkService());
61  if (linkService == nullptr)
62  continue;
63 
64  Ptr<PointToPointNetDevice> nd1 = linkService->GetNetDevice()->GetObject<PointToPointNetDevice>();
65  if (nd1 == nullptr)
66  continue;
67 
68  Ptr<Channel> channel = nd1->GetChannel();
69  if (channel == nullptr)
70  continue;
71 
72  Ptr<PointToPointChannel> ppChannel = DynamicCast<PointToPointChannel>(channel);
73 
74  Ptr<NetDevice> nd2 = ppChannel->GetDevice(0);
75  if (nd2->GetNode() == node1)
76  nd2 = ppChannel->GetDevice(1);
77 
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  }
85 
86  nd1->SetAttribute("ReceiveErrorModel", PointerValue(errorFactory.Create<ErrorModel>()));
87  nd2->SetAttribute("ReceiveErrorModel", PointerValue(errorFactory.Create<ErrorModel>()));
88  return;
89  }
90  }
91  NS_FATAL_ERROR("There is no link to fail between the requested nodes");
92 }
93 
94 void
95 LinkControlHelper::FailLink(Ptr<Node> node1, Ptr<Node> node2)
96 {
97  setErrorRate(node1, node2, 1.0);
98 }
99 
100 void
101 LinkControlHelper::FailLinkByName(const std::string& node1, const std::string& node2)
102 {
103  FailLink(Names::Find<Node>(node1), Names::Find<Node>(node2));
104 }
105 
106 void
107 LinkControlHelper::UpLink(Ptr<Node> node1, Ptr<Node> node2)
108 {
109  setErrorRate(node1, node2, -0.1); // this will ensure error model is disabled
110 }
111 
112 void
113 LinkControlHelper::UpLinkByName(const std::string& node1, const std::string& node2)
114 {
115  UpLink(Names::Find<Node>(node1), Names::Find<Node>(node2));
116 }
117 
118 } // namespace ndn
119 } // namespace ns3
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.