author | Tom Henderson <tomh@tomh.org> |
Sat, 17 May 2008 12:08:20 -0700 | |
changeset 3125 | d2d8a36cfd23 |
parent 3120 | 774b2637845e |
child 3137 | fbd18f80a77a |
permissions | -rw-r--r-- |
1497 | 1 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
2 |
/* |
|
3 |
* Copyright 2007 University of Washington |
|
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 |
*/ |
|
1500
895ed42278d3
finish up basic echo apps
Craig Dowell <craigdo@ee.washington.edu>
parents:
1497
diff
changeset
|
18 |
|
1504
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1500
diff
changeset
|
19 |
#include "ns3/log.h" |
1497 | 20 |
#include "ns3/ipv4-address.h" |
21 |
#include "ns3/nstime.h" |
|
22 |
#include "ns3/inet-socket-address.h" |
|
23 |
#include "ns3/socket.h" |
|
24 |
#include "ns3/simulator.h" |
|
25 |
#include "ns3/socket-factory.h" |
|
26 |
#include "ns3/packet.h" |
|
2494
1c69ea12779c
port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2257
diff
changeset
|
27 |
#include "ns3/uinteger.h" |
1497 | 28 |
|
29 |
#include "udp-echo-server.h" |
|
30 |
||
31 |
namespace ns3 { |
|
32 |
||
1504
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1500
diff
changeset
|
33 |
NS_LOG_COMPONENT_DEFINE ("UdpEchoServerApplication"); |
2494
1c69ea12779c
port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2257
diff
changeset
|
34 |
NS_OBJECT_ENSURE_REGISTERED (UdpEchoServer); |
1497 | 35 |
|
2494
1c69ea12779c
port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2257
diff
changeset
|
36 |
TypeId |
1c69ea12779c
port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2257
diff
changeset
|
37 |
UdpEchoServer::GetTypeId (void) |
1c69ea12779c
port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2257
diff
changeset
|
38 |
{ |
2602
d9262bff6df2
add back support for introspected doxygen.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2494
diff
changeset
|
39 |
static TypeId tid = TypeId ("ns3::UdpEchoServer") |
2494
1c69ea12779c
port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2257
diff
changeset
|
40 |
.SetParent<Application> () |
1c69ea12779c
port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2257
diff
changeset
|
41 |
.AddConstructor<UdpEchoServer> () |
2765
7bb9c55cfb49
improve attribute help string
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
42 |
.AddAttribute ("Port", "Port on which we listen for incoming packets.", |
2965
4b28e9740e3b
get rid of Attribute class. Use AttributeValue subclasses directly.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2765
diff
changeset
|
43 |
UintegerValue (9), |
2494
1c69ea12779c
port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2257
diff
changeset
|
44 |
MakeUintegerAccessor (&UdpEchoServer::m_port), |
1c69ea12779c
port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2257
diff
changeset
|
45 |
MakeUintegerChecker<uint16_t> ()) |
1c69ea12779c
port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2257
diff
changeset
|
46 |
; |
1c69ea12779c
port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2257
diff
changeset
|
47 |
return tid; |
1c69ea12779c
port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2257
diff
changeset
|
48 |
} |
1c69ea12779c
port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2257
diff
changeset
|
49 |
|
1c69ea12779c
port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2257
diff
changeset
|
50 |
UdpEchoServer::UdpEchoServer () |
1497 | 51 |
{ |
2983
e3a416fe9dd5
NS_LOG_FUNCTION -> NS_LOG_FUNCTION_NOARGS and NS_LOG_PARAMS -> NS_LOG_FUNCTION
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2765
diff
changeset
|
52 |
NS_LOG_FUNCTION_NOARGS (); |
1497 | 53 |
} |
54 |
||
55 |
UdpEchoServer::~UdpEchoServer() |
|
56 |
{ |
|
2983
e3a416fe9dd5
NS_LOG_FUNCTION -> NS_LOG_FUNCTION_NOARGS and NS_LOG_PARAMS -> NS_LOG_FUNCTION
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2765
diff
changeset
|
57 |
NS_LOG_FUNCTION_NOARGS (); |
1497 | 58 |
} |
59 |
||
60 |
void |
|
61 |
UdpEchoServer::DoDispose (void) |
|
62 |
{ |
|
2983
e3a416fe9dd5
NS_LOG_FUNCTION -> NS_LOG_FUNCTION_NOARGS and NS_LOG_PARAMS -> NS_LOG_FUNCTION
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2765
diff
changeset
|
63 |
NS_LOG_FUNCTION_NOARGS (); |
1497 | 64 |
Application::DoDispose (); |
65 |
} |
|
66 |
||
67 |
void |
|
68 |
UdpEchoServer::StartApplication (void) |
|
69 |
{ |
|
2983
e3a416fe9dd5
NS_LOG_FUNCTION -> NS_LOG_FUNCTION_NOARGS and NS_LOG_PARAMS -> NS_LOG_FUNCTION
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2765
diff
changeset
|
70 |
NS_LOG_FUNCTION_NOARGS (); |
1497 | 71 |
|
72 |
if (!m_socket) |
|
73 |
{ |
|
3125
d2d8a36cfd23
s/ns3::Udp/ns3::UdpSocketFactory
Tom Henderson <tomh@tomh.org>
parents:
3120
diff
changeset
|
74 |
TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory"); |
3116
c33b6d2775b7
Move API for socket factory to a Socket::CreateSocket () factory method
Tom Henderson <tomh@tomh.org>
parents:
3103
diff
changeset
|
75 |
m_socket = Socket::CreateSocket (GetNode(), tid); |
2494
1c69ea12779c
port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2257
diff
changeset
|
76 |
InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), m_port); |
1c69ea12779c
port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2257
diff
changeset
|
77 |
m_socket->Bind (local); |
1497 | 78 |
} |
79 |
||
3102 | 80 |
m_socket->SetRecvCallback(MakeCallback(&UdpEchoServer::HandleRead, this)); |
1497 | 81 |
} |
82 |
||
83 |
void |
|
84 |
UdpEchoServer::StopApplication () |
|
85 |
{ |
|
2983
e3a416fe9dd5
NS_LOG_FUNCTION -> NS_LOG_FUNCTION_NOARGS and NS_LOG_PARAMS -> NS_LOG_FUNCTION
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2765
diff
changeset
|
86 |
NS_LOG_FUNCTION_NOARGS (); |
1504
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1500
diff
changeset
|
87 |
|
1497 | 88 |
if (!m_socket) |
89 |
{ |
|
3102 | 90 |
m_socket->SetRecvCallback(MakeNullCallback<void, Ptr<Socket> > ()); |
1497 | 91 |
} |
92 |
} |
|
93 |
||
3098
d384d52f8f6e
Cut over UDP applications to use the new receive API
Tom Henderson <tomh@tomh.org>
parents:
2989
diff
changeset
|
94 |
void |
d384d52f8f6e
Cut over UDP applications to use the new receive API
Tom Henderson <tomh@tomh.org>
parents:
2989
diff
changeset
|
95 |
UdpEchoServer::HandleRead (Ptr<Socket> socket) |
d384d52f8f6e
Cut over UDP applications to use the new receive API
Tom Henderson <tomh@tomh.org>
parents:
2989
diff
changeset
|
96 |
{ |
d384d52f8f6e
Cut over UDP applications to use the new receive API
Tom Henderson <tomh@tomh.org>
parents:
2989
diff
changeset
|
97 |
Ptr<Packet> packet; |
3103
91c6fce46125
overloaded Recv() method suggested by Gustavo
Tom Henderson <tomh@tomh.org>
parents:
3102
diff
changeset
|
98 |
while (packet = socket->Recv ()) |
3098
d384d52f8f6e
Cut over UDP applications to use the new receive API
Tom Henderson <tomh@tomh.org>
parents:
2989
diff
changeset
|
99 |
{ |
d384d52f8f6e
Cut over UDP applications to use the new receive API
Tom Henderson <tomh@tomh.org>
parents:
2989
diff
changeset
|
100 |
SocketRxAddressTag tag; |
d384d52f8f6e
Cut over UDP applications to use the new receive API
Tom Henderson <tomh@tomh.org>
parents:
2989
diff
changeset
|
101 |
bool found = packet->PeekTag (tag); |
d384d52f8f6e
Cut over UDP applications to use the new receive API
Tom Henderson <tomh@tomh.org>
parents:
2989
diff
changeset
|
102 |
NS_ASSERT (found); |
d384d52f8f6e
Cut over UDP applications to use the new receive API
Tom Henderson <tomh@tomh.org>
parents:
2989
diff
changeset
|
103 |
Address from = tag.GetAddress (); |
d384d52f8f6e
Cut over UDP applications to use the new receive API
Tom Henderson <tomh@tomh.org>
parents:
2989
diff
changeset
|
104 |
packet->RemoveTag (tag); |
d384d52f8f6e
Cut over UDP applications to use the new receive API
Tom Henderson <tomh@tomh.org>
parents:
2989
diff
changeset
|
105 |
if (InetSocketAddress::IsMatchingType (from)) |
d384d52f8f6e
Cut over UDP applications to use the new receive API
Tom Henderson <tomh@tomh.org>
parents:
2989
diff
changeset
|
106 |
{ |
d384d52f8f6e
Cut over UDP applications to use the new receive API
Tom Henderson <tomh@tomh.org>
parents:
2989
diff
changeset
|
107 |
InetSocketAddress address = InetSocketAddress::ConvertFrom (from); |
d384d52f8f6e
Cut over UDP applications to use the new receive API
Tom Henderson <tomh@tomh.org>
parents:
2989
diff
changeset
|
108 |
NS_LOG_INFO ("Received " << packet->GetSize() << " bytes from " << |
d384d52f8f6e
Cut over UDP applications to use the new receive API
Tom Henderson <tomh@tomh.org>
parents:
2989
diff
changeset
|
109 |
address.GetIpv4()); |
d384d52f8f6e
Cut over UDP applications to use the new receive API
Tom Henderson <tomh@tomh.org>
parents:
2989
diff
changeset
|
110 |
|
d384d52f8f6e
Cut over UDP applications to use the new receive API
Tom Henderson <tomh@tomh.org>
parents:
2989
diff
changeset
|
111 |
NS_LOG_LOGIC ("Echoing packet"); |
3120 | 112 |
socket->SendTo (packet, from); |
3098
d384d52f8f6e
Cut over UDP applications to use the new receive API
Tom Henderson <tomh@tomh.org>
parents:
2989
diff
changeset
|
113 |
} |
d384d52f8f6e
Cut over UDP applications to use the new receive API
Tom Henderson <tomh@tomh.org>
parents:
2989
diff
changeset
|
114 |
} |
d384d52f8f6e
Cut over UDP applications to use the new receive API
Tom Henderson <tomh@tomh.org>
parents:
2989
diff
changeset
|
115 |
} |
d384d52f8f6e
Cut over UDP applications to use the new receive API
Tom Henderson <tomh@tomh.org>
parents:
2989
diff
changeset
|
116 |
|
1497 | 117 |
} // Namespace ns3 |