|
1 |
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ |
2 |
// |
3 |
// Copyright (c) 2006 Georgia Tech Research Corporation |
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 |
#include "ns3/address.h" |
19 |
#include "ns3/inet-socket-address.h" |
20 |
#include "ns3/node.h" |
21 |
#include "ns3/socket.h" |
22 |
#include "ns3/simulator.h" |
23 |
#include "ns3/socket-factory.h" |
24 |
#include "ns3/packet.h" |
25 |
#include "packet-sink.h" |
26 |
|
27 |
using namespace std; |
28 |
|
29 |
namespace ns3 { |
30 |
|
31 |
// Constructors |
32 |
|
33 |
PacketSink::PacketSink (Ptr<Node> n, |
34 |
const Address &local, |
35 |
std::string iid) |
36 |
: Application(n) |
37 |
{ |
38 |
Construct (n, local, iid); |
39 |
} |
40 |
|
41 |
void |
42 |
PacketSink::Construct (Ptr<Node> n, |
43 |
const Address &local, |
44 |
std::string iid) |
45 |
{ |
46 |
m_socket = 0; |
47 |
m_local = local; |
48 |
m_iid = iid; |
49 |
} |
50 |
|
51 |
PacketSink::~PacketSink() |
52 |
{} |
53 |
|
54 |
void |
55 |
PacketSink::DoDispose (void) |
56 |
{ |
57 |
m_socket = 0; |
58 |
|
59 |
// chain up |
60 |
Application::DoDispose (); |
61 |
} |
62 |
|
63 |
|
64 |
// Application Methods |
65 |
void PacketSink::StartApplication() // Called at time specified by Start |
66 |
{ |
67 |
// Create the socket if not already |
68 |
if (!m_socket) |
69 |
{ |
70 |
InterfaceId iid = InterfaceId::LookupByName (m_iid); |
71 |
Ptr<SocketFactory> socketFactory = |
72 |
GetNode ()->QueryInterface<SocketFactory> (iid); |
73 |
m_socket = socketFactory->CreateSocket (); |
74 |
m_socket->Bind (m_local); |
75 |
} |
76 |
m_socket->SetRecvCallback((Callback<void, Ptr<Socket>, const Packet &, |
77 |
const Address &>) MakeCallback(&PacketSink::Receive, this)); |
78 |
} |
79 |
|
80 |
void PacketSink::StopApplication() // Called at time specified by Stop |
81 |
{ |
82 |
if (!m_socket) |
83 |
{ |
84 |
m_socket->SetRecvCallback((Callback<void, Ptr<Socket>, const Packet &, |
85 |
const Address &>) NULL); |
86 |
|
87 |
} |
88 |
} |
89 |
|
90 |
// This callback body suggested by Joe Kopena's wiki |
91 |
void PacketSink::Receive(Ptr<Socket> socket, const Packet &packet, |
92 |
const Address &from) |
93 |
{ |
94 |
InetSocketAddress address = InetSocketAddress::ConvertFrom (from); |
95 |
cout << __PRETTY_FUNCTION__ << ": Received " << packet.GetSize() << |
96 |
" bytes from " << address.GetIpv4() << " [" << address << "]---'" << |
97 |
packet.PeekData() << "'" << endl; |
98 |
} |
99 |
|
100 |
} // Namespace ns3 |