blob: 09137bffa7f8d88385e94a6965b2a99b45afaa5f [file] [log] [blame]
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2011-2015 Regents of the University of California.
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -07004 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08005 * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
6 * contributors.
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -07007 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08008 * 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 Afanasyev0fb80b92013-07-20 08:20:50 -070011 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080012 * 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 Afanasyev0fb80b92013-07-20 08:20:50 -070015 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080016 * 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 Afanasyev0fb80b92013-07-20 08:20:50 -070019
Alexander Afanasyev0c395372014-12-20 15:54:02 -080020#include "ndn-link-control-helper.hpp"
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -070021
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 Afanasyev3f7dd872014-12-31 14:10:33 -080028#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 Afanasyev0fb80b92013-07-20 08:20:50 -070033
Alexander Afanasyev3f7dd872014-12-31 14:10:33 -080034#include "model/ndn-l3-protocol.hpp"
35#include "model/ndn-net-device-face.hpp"
36
37#include "fw/forwarder.hpp"
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -070038
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080039NS_LOG_COMPONENT_DEFINE("ndn.LinkControlHelper");
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -070040
41namespace ns3 {
42namespace ndn {
43
44void
Alexander Afanasyev3f7dd872014-12-31 14:10:33 -080045LinkControlHelper::setErrorRate(Ptr<Node> node1, Ptr<Node> node2, double errorRate)
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -070046{
Alexander Afanasyev3f7dd872014-12-31 14:10:33 -080047 NS_LOG_FUNCTION(node1 << node2 << errorRate);
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -070048
Alexander Afanasyev3f7dd872014-12-31 14:10:33 -080049 NS_ASSERT(node1 != nullptr && node2 != nullptr);
50 NS_ASSERT(errorRate <= 1.0);
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080051
52 Ptr<ndn::L3Protocol> ndn1 = node1->GetObject<ndn::L3Protocol>();
53 Ptr<ndn::L3Protocol> ndn2 = node2->GetObject<ndn::L3Protocol>();
54
Alexander Afanasyev3f7dd872014-12-31 14:10:33 -080055 NS_ASSERT(ndn1 != nullptr && ndn2 != nullptr);
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -070056
57 // iterate over all faces to find the right one
Alexander Afanasyev3f7dd872014-12-31 14:10:33 -080058 for (const auto& face : ndn1->getForwarder()->getFaceTable()) {
59 shared_ptr<ndn::NetDeviceFace> ndFace = std::dynamic_pointer_cast<NetDeviceFace>(face);
60 if (ndFace == nullptr)
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080061 continue;
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -070062
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080063 Ptr<PointToPointNetDevice> nd1 = ndFace->GetNetDevice()->GetObject<PointToPointNetDevice>();
Alexander Afanasyev3f7dd872014-12-31 14:10:33 -080064 if (nd1 == nullptr)
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080065 continue;
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -070066
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080067 Ptr<Channel> channel = nd1->GetChannel();
Alexander Afanasyev3f7dd872014-12-31 14:10:33 -080068 if (channel == nullptr)
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080069 continue;
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -070070
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080071 Ptr<PointToPointChannel> ppChannel = DynamicCast<PointToPointChannel>(channel);
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -070072
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080073 Ptr<NetDevice> nd2 = ppChannel->GetDevice(0);
74 if (nd2->GetNode() == node1)
75 nd2 = ppChannel->GetDevice(1);
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -070076
Spyridon Mastorakisda325912015-05-28 22:45:33 -070077 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 }
Alexander Afanasyev3f7dd872014-12-31 14:10:33 -080084
Spyridon Mastorakisda325912015-05-28 22:45:33 -070085 nd1->SetAttribute("ReceiveErrorModel", PointerValue(errorFactory.Create<ErrorModel>()));
86 nd2->SetAttribute("ReceiveErrorModel", PointerValue(errorFactory.Create<ErrorModel>()));
87 return;
88 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080089 }
Spyridon Mastorakisda325912015-05-28 22:45:33 -070090 NS_FATAL_ERROR("There is no link to fail between the requested nodes");
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -070091}
Alexander Afanasyev3f7dd872014-12-31 14:10:33 -080092
93void
94LinkControlHelper::FailLink(Ptr<Node> node1, Ptr<Node> node2)
95{
96 setErrorRate(node1, node2, 1.0);
97}
98
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -070099void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800100LinkControlHelper::FailLinkByName(const std::string& node1, const std::string& node2)
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -0700101{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800102 FailLink(Names::Find<Node>(node1), Names::Find<Node>(node2));
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -0700103}
104
105void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800106LinkControlHelper::UpLink(Ptr<Node> node1, Ptr<Node> node2)
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -0700107{
Alexander Afanasyev3f7dd872014-12-31 14:10:33 -0800108 setErrorRate(node1, node2, -0.1); // this will ensure error model is disabled
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -0700109}
110
111void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800112LinkControlHelper::UpLinkByName(const std::string& node1, const std::string& node2)
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -0700113{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800114 UpLink(Names::Find<Node>(node1), Names::Find<Node>(node2));
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -0700115}
Alexander Afanasyev3f7dd872014-12-31 14:10:33 -0800116
117} // namespace ndn
118} // namespace ns3