21 #include "ndn-ip-face-stack.h"
22 #include "ndn-tcp-face.h"
23 #include "ndn-udp-face.h"
25 #include "ns3/ndn-l3-protocol.h"
28 #include "ns3/assert.h"
29 #include "ns3/packet.h"
30 #include "ns3/boolean.h"
32 #include "ns3/socket.h"
33 #include "ns3/tcp-socket-factory.h"
34 #include "ns3/udp-socket-factory.h"
35 #include "ns3/simulator.h"
37 NS_LOG_COMPONENT_DEFINE (
"ndn.IpFaceStack");
42 NS_OBJECT_ENSURE_REGISTERED (IpFaceStack);
44 const Callback< void, Ptr<Face> > IpFaceStack::NULL_CREATE_CALLBACK = MakeNullCallback< void, Ptr<Face> > ();
47 IpFaceStack::GetTypeId (
void)
49 static TypeId tid = TypeId (
"ns3::ndn::IpFaceStack")
52 .AddConstructor<IpFaceStack> ()
54 .AddAttribute (
"EnableTCP",
"Enable ability to create TCP faces",
56 MakeBooleanAccessor (&IpFaceStack::m_enableTcp),
57 MakeBooleanChecker ())
59 .AddAttribute (
"EnableUDP",
"Enable ability to create UDP faces",
61 MakeBooleanAccessor (&IpFaceStack::m_enableUdp),
62 MakeBooleanChecker ())
71 IpFaceStack::~IpFaceStack ()
76 IpFaceStack::NotifyNewAggregate ()
80 m_node = GetObject<Node> ();
83 Simulator::ScheduleWithContext (m_node->GetId (), Seconds (0.1), &IpFaceStack::StartServer,
this);
90 IpFaceStack::StartServer ()
92 NS_LOG_FUNCTION (
this);
96 m_tcpServer = Socket::CreateSocket (m_node, TcpSocketFactory::GetTypeId ());
99 m_tcpServer->Listen ();
101 m_tcpServer->SetAcceptCallback (MakeCallback (&IpFaceStack::OnTcpConnectionRequest,
this),
102 MakeCallback (&IpFaceStack::OnTcpConnectionAccept,
this));
107 m_udpServer = Socket::CreateSocket (m_node, UdpSocketFactory::GetTypeId ());
110 m_udpServer->SetRecvCallback (MakeCallback (&IpFaceStack::OnUdpPacket,
this));
115 IpFaceStack::OnTcpConnectionRequest (Ptr< Socket > sock,
const Address &addr)
117 NS_LOG_FUNCTION (
this << sock << InetSocketAddress::ConvertFrom (addr));
122 IpFaceStack::OnTcpConnectionAccept (Ptr<Socket> socket,
const Address &addr)
124 NS_LOG_FUNCTION (
this << socket << InetSocketAddress::ConvertFrom (addr));
126 Ptr<L3Protocol> ndn = m_node->GetObject<L3Protocol> ();
127 Ptr<TcpFace> face = CreateObject<TcpFace> (m_node, socket, InetSocketAddress::ConvertFrom (addr).GetIpv4 ());
132 socket->SetCloseCallbacks (MakeCallback (&TcpFace::OnTcpConnectionClosed, face),
133 MakeCallback (&TcpFace::OnTcpConnectionClosed, face));
137 IpFaceStack::OnUdpPacket (Ptr< Socket > socket)
139 NS_LOG_FUNCTION (
this << socket);
143 while ((packet = socket->RecvFrom (from)))
145 Ptr<UdpFace> face =
CreateOrGetUdpFace (InetSocketAddress::ConvertFrom (from).GetIpv4 ());
146 face->ReceiveFromUdp (packet);
153 TcpFaceMap::iterator i = m_tcpFaceMap.find (address);
154 if (i != m_tcpFaceMap.end ())
163 m_tcpFaceMap.erase (face->GetAddress ());
169 UdpFaceMap::iterator i = m_udpFaceMap.find (address);
170 if (i != m_udpFaceMap.end ())
179 NS_LOG_FUNCTION (address);
181 TcpFaceMap::iterator i = m_tcpFaceMap.find (address);
182 if (i != m_tcpFaceMap.end ())
185 Ptr<Socket> socket = Socket::CreateSocket (m_node, TcpSocketFactory::GetTypeId ());
186 Ptr<TcpFace> face = CreateObject<TcpFace> (m_node, socket, address);
188 face->SetCreateCallback (onCreate);
190 socket->SetConnectCallback (MakeCallback (&TcpFace::OnConnect, face),
191 MakeNullCallback<
void, Ptr< Socket > > ());
194 m_tcpFaceMap.insert (std::make_pair (address, face));
202 NS_LOG_FUNCTION (address);
204 UdpFaceMap::iterator i = m_udpFaceMap.find (address);
205 if (i != m_udpFaceMap.end ())
208 Ptr<Socket> socket = Socket::CreateSocket (m_node, UdpSocketFactory::GetTypeId ());
213 Ptr<UdpFace> face = CreateObject<UdpFace> (m_node, socket, address);
214 Ptr<L3Protocol> ndn = m_node->GetObject<
L3Protocol> ();
219 m_udpFaceMap.insert (std::make_pair (address, face));
virtual uint32_t AddFace(const Ptr< Face > &face)
Add face to Ndn stack.
Ptr< UdpFace > GetUdpFaceByAddress(const Ipv4Address &addr)
Lookup UdpFace for a given address.
Implementation network-layer of NDN stack.
Ptr< TcpFace > GetTcpFaceByAddress(const Ipv4Address &addr)
Lookup TcpFace for a given address.
void DestroyTcpFace(Ptr< TcpFace > face)
Destroy TcpFace, e.g., after TCP connection got dropped.
static const uint16_t IP_STACK_PORT
TCP/UDP port for NDN stack.
Ptr< TcpFace > CreateOrGetTcpFace(Ipv4Address address, Callback< void, Ptr< Face > > onCreate=NULL_CREATE_CALLBACK)
Method allowing creation and lookup of faces.
IpFaceStack()
Default constructor.
Ptr< UdpFace > CreateOrGetUdpFace(Ipv4Address address)
Method allowing creation and lookup of faces.