blob: 94faea4eac4d776936b552b30ffb6b06de0f85e1 [file] [log] [blame]
Alexander Afanasyevf4e24522013-06-24 14:11:57 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2011 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 */
20
21#include "ndn-ip-face-stack.h"
22#include "ndn-tcp-face.h"
23
24#include "ns3/ndn-l3-protocol.h"
25
26#include "ns3/log.h"
27#include "ns3/assert.h"
28#include "ns3/packet.h"
29
30#include "ns3/socket.h"
31#include "ns3/tcp-socket-factory.h"
32#include "ns3/simulator.h"
33
34NS_LOG_COMPONENT_DEFINE ("ndn.IpFaceStack");
35
36namespace ns3 {
37namespace ndn {
38
39NS_OBJECT_ENSURE_REGISTERED (IpFaceStack);
40
41const uint16_t NDN_IP_STACK_PORT = 9695;
42
43TypeId
44IpFaceStack::GetTypeId (void)
45{
46 static TypeId tid = TypeId ("ns3::ndn::IpFaceStack")
47 .SetGroupName ("Ndn")
48 .SetParent<Object> ()
49 .AddConstructor<IpFaceStack> ()
50
51 .AddAttribute ("EnableTCP", "Enable ability to create TCP faces",
52 BooleanValue (true),
53 MakeBooleanAccessor (&IpFaceStack::m_enableTcp),
54 MakeBooleanChecker ())
55
56 // .AddAttribute ("EnableUDP", "Enable ability to create UDP faces",
57 // BooleanValue (true),
58 // MakeBooleanAccessor (&IpFaceStack::m_enableUdp),
59 // MakeBooleanChecker ())
60 ;
61 return tid;
62}
63
64IpFaceStack::IpFaceStack ()
65{
66}
67
68IpFaceStack::~IpFaceStack ()
69{
70}
71
72void
73IpFaceStack::NotifyNewAggregate ()
74{
75 if (m_node == 0)
76 {
77 m_node = GetObject<Node> ();
78 if (m_node != 0)
79 {
80 Simulator::ScheduleWithContext (m_node->GetId (), Seconds (0.1), &IpFaceStack::StartServer, this);
81 }
82 }
83}
84
85// Application Methods
86void
87IpFaceStack::StartServer () // Called at time specified by Start
88{
89 NS_LOG_FUNCTION (this);
90
91 if (m_enableTcp)
92 {
93 m_tcpServer = Socket::CreateSocket (m_node, TcpSocketFactory::GetTypeId ());
94
95 m_tcpServer->Bind (InetSocketAddress (Ipv4Address::GetAny (), NDN_IP_STACK_PORT));
96 m_tcpServer->Listen ();
97
98 m_tcpServer->SetAcceptCallback (MakeCallback (&IpFaceStack::OnTcpConnectionRequest, this),
99 MakeCallback (&IpFaceStack::OnTcpConnectionAccept, this));
100
101 }
102}
103
104bool
105IpFaceStack::OnTcpConnectionRequest (Ptr< Socket > sock, const Address &addr)
106{
107 NS_LOG_FUNCTION (this << sock << InetSocketAddress::ConvertFrom (addr));
108 return true; // accept all connections from anybody
109}
110
111void
112IpFaceStack::OnTcpConnectionAccept (Ptr<Socket> socket, const Address &addr)
113{
114 NS_LOG_FUNCTION (this << socket << InetSocketAddress::ConvertFrom (addr));
115
116 Ptr<L3Protocol> ndn = m_node->GetObject<L3Protocol> ();
117 Ptr<TcpFace> face = CreateObject<TcpFace> (m_node, socket, InetSocketAddress::ConvertFrom (addr).GetIpv4 ());
118
119 ndn->AddFace (face);
120 face->SetUp (true);
121
122 socket->SetCloseCallbacks (MakeCallback (&TcpFace::OnTcpConnectionClosed, face),
123 MakeCallback (&TcpFace::OnTcpConnectionClosed, face));
124}
125
126} // namespace ndn
127} // namespace ns3