NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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  ObjectFactory errorFactory("ns3::RateErrorModel");
78  errorFactory.Set("ErrorUnit", StringValue("ERROR_UNIT_PACKET"));
79  errorFactory.Set("ErrorRate", DoubleValue(errorRate));
80  if (errorRate <= 0) {
81  errorFactory.Set("IsEnabled", BooleanValue(false));
82  }
83 
84  nd1->SetAttribute("ReceiveErrorModel", PointerValue(errorFactory.Create<ErrorModel>()));
85  nd2->SetAttribute("ReceiveErrorModel", PointerValue(errorFactory.Create<ErrorModel>()));
86  }
87 }
88 
89 void
90 LinkControlHelper::FailLink(Ptr<Node> node1, Ptr<Node> node2)
91 {
92  setErrorRate(node1, node2, 1.0);
93 }
94 
95 void
96 LinkControlHelper::FailLinkByName(const std::string& node1, const std::string& node2)
97 {
98  FailLink(Names::Find<Node>(node1), Names::Find<Node>(node2));
99 }
100 
101 void
102 LinkControlHelper::UpLink(Ptr<Node> node1, Ptr<Node> node2)
103 {
104  setErrorRate(node1, node2, -0.1); // this will ensure error model is disabled
105 }
106 
107 void
108 LinkControlHelper::UpLinkByName(const std::string& node1, const std::string& node2)
109 {
110  UpLink(Names::Find<Node>(node1), Names::Find<Node>(node2));
111 }
112 
113 } // namespace ndn
114 } // namespace ns3
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.
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.