LEFT | RIGHT |
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ | 1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
2 /* | 2 /* |
3 * Copyright (c) 2014 Universita' di Firenze | 3 * Copyright (c) 2014 Universita' di Firenze |
4 * | 4 * |
5 * This program is free software; you can redistribute it and/or modify | 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 | 6 * it under the terms of the GNU General Public License version 2 as |
7 * published by the Free Software Foundation; | 7 * published by the Free Software Foundation; |
8 * | 8 * |
9 * This program is distributed in the hope that it will be useful, | 9 * This program is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 ; | 49 ; |
50 return tid; | 50 return tid; |
51 } | 51 } |
52 | 52 |
53 PacketSocketServer::PacketSocketServer () | 53 PacketSocketServer::PacketSocketServer () |
54 { | 54 { |
55 NS_LOG_FUNCTION (this); | 55 NS_LOG_FUNCTION (this); |
56 m_pktRx = 0; | 56 m_pktRx = 0; |
57 m_bytesRx = 0; | 57 m_bytesRx = 0; |
58 m_socket = 0; | 58 m_socket = 0; |
| 59 m_localAddressSet = false; |
59 } | 60 } |
60 | 61 |
61 PacketSocketServer::~PacketSocketServer () | 62 PacketSocketServer::~PacketSocketServer () |
62 { | 63 { |
63 NS_LOG_FUNCTION (this); | 64 NS_LOG_FUNCTION (this); |
64 } | 65 } |
65 | 66 |
66 void | 67 void |
67 PacketSocketServer::DoDispose (void) | 68 PacketSocketServer::DoDispose (void) |
68 { | 69 { |
69 NS_LOG_FUNCTION (this); | 70 NS_LOG_FUNCTION (this); |
70 Application::DoDispose (); | 71 Application::DoDispose (); |
71 } | 72 } |
72 | 73 |
73 void | 74 void |
74 PacketSocketServer::StartApplication (void) | 75 PacketSocketServer::StartApplication (void) |
75 { | 76 { |
76 NS_LOG_FUNCTION (this); | 77 NS_LOG_FUNCTION (this); |
| 78 NS_ASSERT_MSG (m_localAddressSet, "Local address not set"); |
77 | 79 |
78 if (m_socket == 0) | 80 if (m_socket == 0) |
79 { | 81 { |
80 TypeId tid = TypeId::LookupByName ("ns3::PacketSocketFactory"); | 82 TypeId tid = TypeId::LookupByName ("ns3::PacketSocketFactory"); |
81 m_socket = Socket::CreateSocket (GetNode (), tid); | 83 m_socket = Socket::CreateSocket (GetNode (), tid); |
82 m_socket->Bind (); | 84 m_socket->Bind (m_localAddress); |
83 } | 85 } |
84 | 86 |
85 m_socket->SetRecvCallback (MakeCallback (&PacketSocketServer::HandleRead, this
)); | 87 m_socket->SetRecvCallback (MakeCallback (&PacketSocketServer::HandleRead, this
)); |
86 } | 88 } |
87 | 89 |
88 void | 90 void |
89 PacketSocketServer::StopApplication (void) | 91 PacketSocketServer::StopApplication (void) |
90 { | 92 { |
91 NS_LOG_FUNCTION (this); | 93 NS_LOG_FUNCTION (this); |
92 m_socket->SetRecvCallback (MakeNullCallback<void, Ptr<Socket> > ()); | 94 m_socket->SetRecvCallback (MakeNullCallback<void, Ptr<Socket> > ()); |
93 m_socket->Close (); | 95 m_socket->Close (); |
| 96 } |
| 97 |
| 98 void |
| 99 PacketSocketServer::SetLocal (PacketSocketAddress addr) |
| 100 { |
| 101 NS_LOG_FUNCTION (this << addr); |
| 102 m_localAddress = addr; |
| 103 m_localAddressSet = true; |
94 } | 104 } |
95 | 105 |
96 void | 106 void |
97 PacketSocketServer::HandleRead (Ptr<Socket> socket) | 107 PacketSocketServer::HandleRead (Ptr<Socket> socket) |
98 { | 108 { |
99 NS_LOG_FUNCTION (this << socket); | 109 NS_LOG_FUNCTION (this << socket); |
100 Ptr<Packet> packet; | 110 Ptr<Packet> packet; |
101 Address from; | 111 Address from; |
102 while ((packet = socket->RecvFrom (from))) | 112 while ((packet = socket->RecvFrom (from))) |
103 { | 113 { |
104 if (PacketSocketAddress::IsMatchingType (from)) | 114 if (PacketSocketAddress::IsMatchingType (from)) |
105 { | 115 { |
106 m_pktRx ++; | 116 m_pktRx ++; |
107 m_bytesRx += packet->GetSize (); | 117 m_bytesRx += packet->GetSize (); |
108 NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () | 118 NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () |
109 << "s packet sink received " | 119 << "s packet sink received " |
110 << packet->GetSize () << " bytes from " | 120 << packet->GetSize () << " bytes from " |
111 << PacketSocketAddress::ConvertFrom (from) | 121 << PacketSocketAddress::ConvertFrom (from) |
112 << " total Rx " << m_pktRx << " packets" | 122 << " total Rx " << m_pktRx << " packets" |
113 << " and " << m_bytesRx << " bytes"); | 123 << " and " << m_bytesRx << " bytes"); |
114 m_rxTrace (packet, from); | 124 m_rxTrace (packet, from); |
115 } | 125 } |
116 } | 126 } |
117 } | 127 } |
118 | 128 |
119 } // Namespace ns3 | 129 } // Namespace ns3 |
LEFT | RIGHT |