blob: cf2b99d384b7fd7660aa79961bae313edd24cf86 [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
22#include "ndn-link-control-helper.h"
23
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
31#include "ns3/ndn-l3-protocol.h"
32#include "ns3/ndn-net-device-face.h"
33
34NS_LOG_COMPONENT_DEFINE ("ndn.LinkControlHelper");
35
36namespace ns3 {
37namespace ndn {
38
39void
40LinkControlHelper::FailLink (Ptr<Node> node1, Ptr<Node> node2)
41{
42 NS_LOG_FUNCTION (node1 << node2);
43
44 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);
52
53 // iterate over all faces to find the right one
54 for (uint32_t faceId = 0; faceId < ndn1->GetNFaces (); faceId++)
55 {
56 Ptr<ndn::NetDeviceFace> ndFace = ndn1->GetFace (faceId)->GetObject<ndn::NetDeviceFace> ();
57 if (ndFace == 0) continue;
58
59 Ptr<PointToPointNetDevice> nd1 = ndFace->GetNetDevice ()->GetObject<PointToPointNetDevice> ();
60 if (nd1 == 0) continue;
61
62 Ptr<Channel> channel = nd1->GetChannel ();
63 if (channel == 0) continue;
64
65 Ptr<PointToPointChannel> ppChannel = DynamicCast<PointToPointChannel> (channel);
66
67 Ptr<NetDevice> nd2 = ppChannel->GetDevice (0);
68 if (nd2->GetNode () == node1)
69 nd2 = ppChannel->GetDevice (1);
70
71 if (nd2->GetNode () == node2)
72 {
73 Ptr<ndn::Face> face1 = ndn1->GetFaceByNetDevice (nd1);
74 Ptr<ndn::Face> face2 = ndn2->GetFaceByNetDevice (nd2);
75
76 face1->SetUp (false);
77 face2->SetUp (false);
78 break;
79 }
80 }
81}
82void
83LinkControlHelper::FailLinkByName (const std::string &node1, const std::string &node2)
84{
85 FailLink (Names::Find<Node> (node1), Names::Find<Node> (node2));
86}
87
88void
89LinkControlHelper::UpLink (Ptr<Node> node1, Ptr<Node> node2)
90{
91 NS_LOG_FUNCTION (node1 << node2);
92
93 NS_ASSERT (node1 != 0);
94 NS_ASSERT (node2 != 0);
95
96 Ptr<ndn::L3Protocol> ndn1 = node1->GetObject<ndn::L3Protocol> ();
97 Ptr<ndn::L3Protocol> ndn2 = node2->GetObject<ndn::L3Protocol> ();
98
99 NS_ASSERT (ndn1 != 0);
100 NS_ASSERT (ndn2 != 0);
101
102 // iterate over all faces to find the right one
103 for (uint32_t faceId = 0; faceId < ndn1->GetNFaces (); faceId++)
104 {
105 Ptr<ndn::NetDeviceFace> ndFace = ndn1->GetFace (faceId)->GetObject<ndn::NetDeviceFace> ();
106 if (ndFace == 0) continue;
107
108 Ptr<PointToPointNetDevice> nd1 = ndFace->GetNetDevice ()->GetObject<PointToPointNetDevice> ();
109 if (nd1 == 0) continue;
110
111 Ptr<Channel> channel = nd1->GetChannel ();
112 if (channel == 0) continue;
113
114 Ptr<PointToPointChannel> ppChannel = DynamicCast<PointToPointChannel> (channel);
115
116 Ptr<NetDevice> nd2 = ppChannel->GetDevice (0);
117 if (nd2->GetNode () == node1)
118 nd2 = ppChannel->GetDevice (1);
119
120 if (nd2->GetNode () == node2)
121 {
122 Ptr<ndn::Face> face1 = ndn1->GetFaceByNetDevice (nd1);
123 Ptr<ndn::Face> face2 = ndn2->GetFaceByNetDevice (nd2);
124
125 face1->SetUp (true);
126 face2->SetUp (true);
127 break;
128 }
129 }
130}
131
132void
133LinkControlHelper::UpLinkByName (const std::string &node1, const std::string &node2)
134{
135 UpLink (Names::Find<Node> (node1), Names::Find<Node> (node2));
136}
137
138}
139}