1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3 * Copyright (c) 2014 Universita' di Firenze
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: Tommaso Pecorella <tommaso.pecorella@unifi.it>
19 */
20
21 #include "ns3/log.h"
22 #include "ns3/nstime.h"
23 #include "ns3/packet-socket-address.h"
24 #include "ns3/packet-socket.h"
25 #include "ns3/packet-socket-factory.h"
26 #include "ns3/socket.h"
27 #include "ns3/simulator.h"
28 #include "ns3/socket-factory.h"
29 #include "ns3/packet.h"
30 #include "ns3/uinteger.h"
31 #include "ns3/abort.h"
32 #include "packet-socket-server.h"
33 #include <cstdlib>
34 #include <cstdio>
35
36 namespace ns3 {
37
38 NS_LOG_COMPONENT_DEFINE ("PacketSocketServer");
39
40 NS_OBJECT_ENSURE_REGISTERED (PacketSocketServer);
41
42 TypeId
GetTypeId(void)43 PacketSocketServer::GetTypeId (void)
44 {
45 static TypeId tid = TypeId ("ns3::PacketSocketServer")
46 .SetParent<Application> ()
47 .SetGroupName("Network")
48 .AddConstructor<PacketSocketServer> ()
49 .AddTraceSource ("Rx", "A packet has been received",
50 MakeTraceSourceAccessor (&PacketSocketServer::m_rxTrace),
51 "ns3::Packet::AddressTracedCallback")
52 ;
53 return tid;
54 }
55
PacketSocketServer()56 PacketSocketServer::PacketSocketServer ()
57 {
58 NS_LOG_FUNCTION (this);
59 m_pktRx = 0;
60 m_bytesRx = 0;
61 m_socket = 0;
62 m_localAddressSet = false;
63 }
64
~PacketSocketServer()65 PacketSocketServer::~PacketSocketServer ()
66 {
67 NS_LOG_FUNCTION (this);
68 }
69
70 void
DoDispose(void)71 PacketSocketServer::DoDispose (void)
72 {
73 NS_LOG_FUNCTION (this);
74 Application::DoDispose ();
75 }
76
77 void
StartApplication(void)78 PacketSocketServer::StartApplication (void)
79 {
80 NS_LOG_FUNCTION (this);
81 NS_ASSERT_MSG (m_localAddressSet, "Local address not set");
82
83 if (m_socket == 0)
84 {
85 TypeId tid = TypeId::LookupByName ("ns3::PacketSocketFactory");
86 m_socket = Socket::CreateSocket (GetNode (), tid);
87 m_socket->Bind (m_localAddress);
88 }
89
90 m_socket->SetRecvCallback (MakeCallback (&PacketSocketServer::HandleRead, this));
91 }
92
93 void
StopApplication(void)94 PacketSocketServer::StopApplication (void)
95 {
96 NS_LOG_FUNCTION (this);
97 m_socket->SetRecvCallback (MakeNullCallback<void, Ptr<Socket> > ());
98 m_socket->Close ();
99 }
100
101 void
SetLocal(PacketSocketAddress addr)102 PacketSocketServer::SetLocal (PacketSocketAddress addr)
103 {
104 NS_LOG_FUNCTION (this << addr);
105 m_localAddress = addr;
106 m_localAddressSet = true;
107 }
108
109 void
HandleRead(Ptr<Socket> socket)110 PacketSocketServer::HandleRead (Ptr<Socket> socket)
111 {
112 NS_LOG_FUNCTION (this << socket);
113 Ptr<Packet> packet;
114 Address from;
115 while ((packet = socket->RecvFrom (from)))
116 {
117 if (PacketSocketAddress::IsMatchingType (from))
118 {
119 m_pktRx ++;
120 m_bytesRx += packet->GetSize ();
121 NS_LOG_INFO ("At time " << Simulator::Now ().As (Time::S)
122 << " packet sink received "
123 << packet->GetSize () << " bytes from "
124 << PacketSocketAddress::ConvertFrom (from)
125 << " total Rx " << m_pktRx << " packets"
126 << " and " << m_bytesRx << " bytes");
127 m_rxTrace (packet, from);
128 }
129 }
130 }
131
132 } // Namespace ns3
133