|
|
|
|
1 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 |
/* |
| 3 |
* Copyright (c) 2013 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-send-app.h" |
| 33 |
#include <cstdlib> |
| 34 |
#include <cstdio> |
| 35 |
|
| 36 |
namespace ns3 { |
| 37 |
|
| 38 |
NS_LOG_COMPONENT_DEFINE ("SimpleSendApp") |
| 39 |
; |
| 40 |
NS_OBJECT_ENSURE_REGISTERED (SimpleSendApp) |
| 41 |
; |
| 42 |
|
| 43 |
TypeId |
| 44 |
SimpleSendApp::GetTypeId (void) |
| 45 |
{ |
| 46 |
static TypeId tid = TypeId ("ns3::SimpleSendApp") |
| 47 |
.SetParent<Application> () |
| 48 |
.AddConstructor<SimpleSendApp> () |
| 49 |
.AddAttribute ("MaxPackets", |
| 50 |
"The maximum number of packets the application will send", |
| 51 |
UintegerValue (100), |
| 52 |
MakeUintegerAccessor (&SimpleSendApp::m_count), |
| 53 |
MakeUintegerChecker<uint32_t> ()) |
| 54 |
.AddAttribute ("Interval", |
| 55 |
"The time to wait between packets", TimeValue (Seconds (1.0)), |
| 56 |
MakeTimeAccessor (&SimpleSendApp::m_interval), |
| 57 |
MakeTimeChecker ()) |
| 58 |
.AddAttribute ("RemoteAddress", |
| 59 |
"The destination Address of the outbound packets", |
| 60 |
AddressValue (), |
| 61 |
MakeAddressAccessor (&SimpleSendApp::m_peerAddress), |
| 62 |
MakeAddressChecker ()) |
| 63 |
.AddAttribute ("PacketSize", |
| 64 |
"Size of packets generated. The minimum packet size is 12 bytes which is the size of the header carrying the sequence number and the time stamp.", |
| 65 |
UintegerValue (1024), |
| 66 |
MakeUintegerAccessor (&SimpleSendApp::m_size), |
| 67 |
MakeUintegerChecker<uint32_t> (12,1500)) |
| 68 |
; |
| 69 |
return tid; |
| 70 |
} |
| 71 |
|
| 72 |
SimpleSendApp::SimpleSendApp () |
| 73 |
{ |
| 74 |
NS_LOG_FUNCTION (this); |
| 75 |
m_sent = 0; |
| 76 |
m_socket = 0; |
| 77 |
m_sendEvent = EventId (); |
| 78 |
} |
| 79 |
|
| 80 |
SimpleSendApp::~SimpleSendApp () |
| 81 |
{ |
| 82 |
NS_LOG_FUNCTION (this); |
| 83 |
} |
| 84 |
|
| 85 |
void |
| 86 |
SimpleSendApp::SetRemote (PacketSocketAddress addr) |
| 87 |
{ |
| 88 |
NS_LOG_FUNCTION (this << addr); |
| 89 |
m_peerAddress = Address(addr); |
| 90 |
} |
| 91 |
|
| 92 |
void |
| 93 |
SimpleSendApp::SetRemote (Address addr) |
| 94 |
{ |
| 95 |
NS_LOG_FUNCTION (this << addr); |
| 96 |
m_peerAddress = addr; |
| 97 |
} |
| 98 |
|
| 99 |
void |
| 100 |
SimpleSendApp::DoDispose (void) |
| 101 |
{ |
| 102 |
NS_LOG_FUNCTION (this); |
| 103 |
Application::DoDispose (); |
| 104 |
} |
| 105 |
|
| 106 |
void |
| 107 |
SimpleSendApp::StartApplication (void) |
| 108 |
{ |
| 109 |
NS_LOG_FUNCTION (this); |
| 110 |
|
| 111 |
if (m_socket == 0) |
| 112 |
{ |
| 113 |
TypeId tid = TypeId::LookupByName ("ns3::PacketSocketFactory"); |
| 114 |
m_socket = Socket::CreateSocket (GetNode (), tid); |
| 115 |
if (PacketSocketAddress::IsMatchingType(m_peerAddress) == true) |
| 116 |
{ |
| 117 |
m_socket->Bind (); |
| 118 |
m_socket->Connect (m_peerAddress); |
| 119 |
} |
| 120 |
else |
| 121 |
{ |
| 122 |
NS_ABORT_MSG ("Peer address type not matching PacketSocketAddress"); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
m_socket->SetRecvCallback (MakeNullCallback<void, Ptr<Socket> > ()); |
| 127 |
m_sendEvent = Simulator::Schedule (Seconds (0.0), &SimpleSendApp::Send, this); |
| 128 |
} |
| 129 |
|
| 130 |
void |
| 131 |
SimpleSendApp::StopApplication (void) |
| 132 |
{ |
| 133 |
NS_LOG_FUNCTION (this); |
| 134 |
Simulator::Cancel (m_sendEvent); |
| 135 |
m_socket->Close (); |
| 136 |
} |
| 137 |
|
| 138 |
void |
| 139 |
SimpleSendApp::Send (void) |
| 140 |
{ |
| 141 |
NS_LOG_FUNCTION (this); |
| 142 |
NS_ASSERT (m_sendEvent.IsExpired ()); |
| 143 |
|
| 144 |
Ptr<Packet> p = Create<Packet> (m_size); |
| 145 |
|
| 146 |
std::stringstream peerAddressStringStream; |
| 147 |
peerAddressStringStream << PacketSocketAddress::ConvertFrom (m_peerAddress); |
| 148 |
|
| 149 |
if ((m_socket->Send (p)) >= 0) |
| 150 |
{ |
| 151 |
++m_sent; |
| 152 |
NS_LOG_INFO ("TraceDelay TX " << m_size << " bytes to " |
| 153 |
<< peerAddressStringStream.str () << " Uid: " |
| 154 |
<< p->GetUid () << " Time: " |
| 155 |
<< (Simulator::Now ()).GetSeconds ()); |
| 156 |
} |
| 157 |
else |
| 158 |
{ |
| 159 |
NS_LOG_INFO ("Error while sending " << m_size << " bytes to " |
| 160 |
<< peerAddressStringStream.str ()); |
| 161 |
} |
| 162 |
|
| 163 |
if (m_sent < m_count) |
| 164 |
{ |
| 165 |
m_sendEvent = Simulator::Schedule (m_interval, &SimpleSendApp::Send, this); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
} // Namespace ns3 |