blob: 524ba46c6fdc8b889122c4eac397ae71fbe915da [file] [log] [blame]
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -07001/* -*- 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 Afanasyev0c395372014-12-20 15:54:02 -080022#include "ndn-link-control-helper.hpp"
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -070023
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"
30
Alexander Afanasyev0c395372014-12-20 15:54:02 -080031#include "ns3/ndn-l3-protocol.hpp"
32#include "ns3/ndn-net-device-face.hpp"
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -070033
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080034NS_LOG_COMPONENT_DEFINE("ndn.LinkControlHelper");
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -070035
36namespace ns3 {
37namespace ndn {
38
39void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080040LinkControlHelper::FailLink(Ptr<Node> node1, Ptr<Node> node2)
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -070041{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080042 NS_LOG_FUNCTION(node1 << node2);
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -070043
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080044 NS_ASSERT(node1 != 0);
45 NS_ASSERT(node2 != 0);
46
47 Ptr<ndn::L3Protocol> ndn1 = node1->GetObject<ndn::L3Protocol>();
48 Ptr<ndn::L3Protocol> ndn2 = node2->GetObject<ndn::L3Protocol>();
49
50 NS_ASSERT(ndn1 != 0);
51 NS_ASSERT(ndn2 != 0);
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -070052
53 // iterate over all faces to find the right one
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080054 for (uint32_t faceId = 0; faceId < ndn1->GetNFaces(); faceId++) {
55 Ptr<ndn::NetDeviceFace> ndFace = ndn1->GetFace(faceId)->GetObject<ndn::NetDeviceFace>();
56 if (ndFace == 0)
57 continue;
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -070058
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080059 Ptr<PointToPointNetDevice> nd1 = ndFace->GetNetDevice()->GetObject<PointToPointNetDevice>();
60 if (nd1 == 0)
61 continue;
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -070062
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080063 Ptr<Channel> channel = nd1->GetChannel();
64 if (channel == 0)
65 continue;
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -070066
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080067 Ptr<PointToPointChannel> ppChannel = DynamicCast<PointToPointChannel>(channel);
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -070068
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080069 Ptr<NetDevice> nd2 = ppChannel->GetDevice(0);
70 if (nd2->GetNode() == node1)
71 nd2 = ppChannel->GetDevice(1);
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -070072
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080073 if (nd2->GetNode() == node2) {
74 Ptr<ndn::Face> face1 = ndn1->GetFaceByNetDevice(nd1);
75 Ptr<ndn::Face> face2 = ndn2->GetFaceByNetDevice(nd2);
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -070076
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080077 face1->SetUp(false);
78 face2->SetUp(false);
79 break;
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -070080 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080081 }
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -070082}
83void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080084LinkControlHelper::FailLinkByName(const std::string& node1, const std::string& node2)
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -070085{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080086 FailLink(Names::Find<Node>(node1), Names::Find<Node>(node2));
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -070087}
88
89void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080090LinkControlHelper::UpLink(Ptr<Node> node1, Ptr<Node> node2)
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -070091{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080092 NS_LOG_FUNCTION(node1 << node2);
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -070093
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080094 NS_ASSERT(node1 != 0);
95 NS_ASSERT(node2 != 0);
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -070096
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080097 Ptr<ndn::L3Protocol> ndn1 = node1->GetObject<ndn::L3Protocol>();
98 Ptr<ndn::L3Protocol> ndn2 = node2->GetObject<ndn::L3Protocol>();
99
100 NS_ASSERT(ndn1 != 0);
101 NS_ASSERT(ndn2 != 0);
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -0700102
103 // iterate over all faces to find the right one
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800104 for (uint32_t faceId = 0; faceId < ndn1->GetNFaces(); faceId++) {
105 Ptr<ndn::NetDeviceFace> ndFace = ndn1->GetFace(faceId)->GetObject<ndn::NetDeviceFace>();
106 if (ndFace == 0)
107 continue;
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -0700108
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800109 Ptr<PointToPointNetDevice> nd1 = ndFace->GetNetDevice()->GetObject<PointToPointNetDevice>();
110 if (nd1 == 0)
111 continue;
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -0700112
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800113 Ptr<Channel> channel = nd1->GetChannel();
114 if (channel == 0)
115 continue;
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -0700116
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800117 Ptr<PointToPointChannel> ppChannel = DynamicCast<PointToPointChannel>(channel);
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -0700118
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800119 Ptr<NetDevice> nd2 = ppChannel->GetDevice(0);
120 if (nd2->GetNode() == node1)
121 nd2 = ppChannel->GetDevice(1);
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -0700122
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800123 if (nd2->GetNode() == node2) {
124 Ptr<ndn::Face> face1 = ndn1->GetFaceByNetDevice(nd1);
125 Ptr<ndn::Face> face2 = ndn2->GetFaceByNetDevice(nd2);
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -0700126
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800127 face1->SetUp(true);
128 face2->SetUp(true);
129 break;
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -0700130 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800131 }
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -0700132}
133
134void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800135LinkControlHelper::UpLinkByName(const std::string& node1, const std::string& node2)
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -0700136{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800137 UpLink(Names::Find<Node>(node1), Names::Find<Node>(node2));
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -0700138}
Alexander Afanasyev0fb80b92013-07-20 08:20:50 -0700139}
140}