NS-3 based Named Data Networking (NDN) simulator
ndnSIM: NDN, CCN, CCNx, content centric networks
API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
ndn-ip-faces-helper.cc
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2 /*
3  * Copyright (c) 2013 University of California, Los Angeles
4  * Alexander Afanasyev
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation;
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
20  */
21 
22 #include "ndn-ip-faces-helper.h"
23 #include "ndn-ip-face-stack.h"
24 
25 #include "ns3/ndn-stack-helper.h"
26 #include "ns3/node-container.h"
27 #include "ns3/log.h"
28 #include "ns3/simulator.h"
29 #include "ndn-tcp-face.h"
30 #include "ndn-udp-face.h"
31 
32 NS_LOG_COMPONENT_DEFINE ("ndn.IpFacesHelper");
33 
34 using namespace std;
35 
36 namespace ns3 {
37 namespace ndn {
38 
39 void
40 IpFacesHelper::Install (Ptr<Node> node)
41 {
42  Ptr<IpFaceStack> stack = CreateObject<IpFaceStack> ();
43  node->AggregateObject (stack);
44 }
45 
46 void
47 IpFacesHelper::Install (const NodeContainer &nodes)
48 {
49  for (NodeContainer::Iterator node = nodes.Begin ();
50  node != nodes.End ();
51  node ++)
52  {
53  Install (*node);
54  }
55 }
56 
57 void
58 IpFacesHelper::InstallAll ()
59 {
60  Install (NodeContainer::GetGlobal ());
61 }
62 
63 
64 struct TcpPrefixRegistrator : SimpleRefCount<TcpPrefixRegistrator>
65 {
66  TcpPrefixRegistrator (Ptr<Node> node, const std::string &prefix, int16_t metric)
67  : m_node (node)
68  , m_prefix (prefix)
69  , m_metric (metric)
70  {
71  }
72 
73  void
74  Run (Ptr<Face> face)
75  {
76  ndn::StackHelper::AddRoute (m_node, m_prefix, face, m_metric);
77  }
78 private:
79  Ptr<Node> m_node;
80  std::string m_prefix;
81  int16_t m_metric;
82 };
83 
84 static void
85 ScheduledCreateTcp (Ptr<Node> node, Ipv4Address address, const std::string &prefix, int16_t metric)
86 {
87  Ptr<IpFaceStack> stack = node->GetObject<IpFaceStack> ();
88  NS_ASSERT_MSG (stack != 0, "ndn::IpFaceStack needs to be installed on the node");
89 
90  Ptr<Face> face = stack->GetTcpFaceByAddress (address);
91  if (face == 0)
92  {
93  Ptr<TcpPrefixRegistrator> registrator = Create<TcpPrefixRegistrator> (node, prefix, metric);
94  stack->CreateOrGetTcpFace (address, MakeCallback (&TcpPrefixRegistrator::Run, registrator));
95  }
96  else
97  {
98  ndn::StackHelper::AddRoute (node, prefix, face, metric);
99  }
100 }
101 
102 void
103 IpFacesHelper::CreateTcpFace (const Time &when, Ptr<Node> node, Ipv4Address address, const std::string &prefix, int16_t metric/* = 1*/)
104 {
105  Simulator::ScheduleWithContext (node->GetId (), when, ScheduledCreateTcp, node, address, prefix, metric);
106 }
107 
108 static void
109 ScheduledCreateUdp (Ptr<Node> node, Ipv4Address address, const std::string &prefix, int16_t metric)
110 {
111  Ptr<IpFaceStack> stack = node->GetObject<IpFaceStack> ();
112  NS_ASSERT_MSG (stack != 0, "ndn::IpFaceStack needs to be installed on the node");
113 
114  Ptr<Face> face = stack->CreateOrGetUdpFace (address);
115  ndn::StackHelper::AddRoute (node, prefix, face, metric);
116 }
117 
118 void
119 IpFacesHelper::CreateUdpFace (const Time &when, Ptr<Node> node, Ipv4Address address, const std::string &prefix, int16_t metric/* = 1*/)
120 {
121  Simulator::ScheduleWithContext (node->GetId (), when, ScheduledCreateUdp, node, address, prefix, metric);
122 }
123 
124 } // namespace ndn
125 } // namespace ns3
Application that provides functionality of creating IP-based faces on NDN nodes.