Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
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 "simple-recv-app.h" | |
33 #include <cstdlib> | |
34 #include <cstdio> | |
35 | |
36 namespace ns3 { | |
37 | |
38 NS_LOG_COMPONENT_DEFINE ("SimpleRecvApp") | |
39 ; | |
40 NS_OBJECT_ENSURE_REGISTERED (SimpleRecvApp) | |
41 ; | |
42 | |
43 TypeId | |
44 SimpleRecvApp::GetTypeId (void) | |
45 { | |
46 static TypeId tid = TypeId ("ns3::SimpleRecvApp") | |
47 .SetParent<Application> () | |
48 .AddConstructor<SimpleRecvApp> () | |
49 .AddTraceSource ("Rx", "A packet has been received", | |
50 MakeTraceSourceAccessor (&SimpleRecvApp::m_rxTrace)) | |
51 ; | |
52 return tid; | |
53 } | |
54 | |
55 SimpleRecvApp::SimpleRecvApp () | |
56 { | |
57 NS_LOG_FUNCTION (this); | |
58 m_pktRx = 0; | |
59 m_bytesRx = 0; | |
60 m_socket = 0; | |
61 } | |
62 | |
63 SimpleRecvApp::~SimpleRecvApp () | |
64 { | |
65 NS_LOG_FUNCTION (this); | |
66 } | |
67 | |
68 void | |
69 SimpleRecvApp::DoDispose (void) | |
70 { | |
71 NS_LOG_FUNCTION (this); | |
72 Application::DoDispose (); | |
73 } | |
74 | |
75 void | |
76 SimpleRecvApp::StartApplication (void) | |
77 { | |
78 NS_LOG_FUNCTION (this); | |
79 | |
80 if (m_socket == 0) | |
81 { | |
82 TypeId tid = TypeId::LookupByName ("ns3::PacketSocketFactory"); | |
83 m_socket = Socket::CreateSocket (GetNode (), tid); | |
84 m_socket->Bind (); | |
85 } | |
86 | |
87 m_socket->SetRecvCallback (MakeCallback (&SimpleRecvApp::HandleRead, this)); | |
88 } | |
89 | |
90 void | |
91 SimpleRecvApp::StopApplication (void) | |
92 { | |
93 NS_LOG_FUNCTION (this); | |
94 m_socket->SetRecvCallback (MakeNullCallback<void, Ptr<Socket> > ()); | |
95 m_socket->Close (); | |
96 } | |
97 | |
98 void | |
99 SimpleRecvApp::HandleRead (Ptr<Socket> socket) | |
100 { | |
101 NS_LOG_FUNCTION (this << socket); | |
102 Ptr<Packet> packet; | |
103 Address from; | |
104 while ((packet = socket->RecvFrom (from))) | |
105 { | |
106 if (packet->GetSize () == 0) | |
107 { //EOF | |
Tom Henderson
2014/04/11 13:51:29
add debug log statement here
| |
108 break; | |
109 } | |
110 m_pktRx ++; | |
111 m_bytesRx += packet->GetSize (); | |
112 if (PacketSocketAddress::IsMatchingType (from)) | |
Tom Henderson
2014/04/11 13:51:29
You are incrementing the counters and firing the t
| |
113 { | |
114 NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () | |
115 << "s packet sink received " | |
116 << packet->GetSize () << " bytes from " | |
117 << PacketSocketAddress::ConvertFrom(from) | |
118 << " total Rx " << m_pktRx << " packets" | |
119 << " and " << m_bytesRx << " bytes"); | |
120 } | |
121 m_rxTrace (packet, from); | |
122 } | |
123 } | |
124 | |
125 } // Namespace ns3 | |
OLD | NEW |