|
|
| 1 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 |
/* |
| 3 |
* This program is free software; you can redistribute it and/or modify |
| 4 |
* it under the terms of the GNU General Public License version 2 as |
| 5 |
* published by the Free Software Foundation; |
| 6 |
* |
| 7 |
* This program is distributed in the hope that it will be useful, |
| 8 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 |
* GNU General Public License for more details. |
| 11 |
* |
| 12 |
* You should have received a copy of the GNU General Public License |
| 13 |
* along with this program; if not, write to the Free Software |
| 14 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 15 |
* |
| 16 |
* Author: Hossam Khader <hossamkhader@gmail.com> |
| 17 |
*/ |
| 18 |
#include "uan-application.h" |
| 19 |
#include "ns3/log.h" |
| 20 |
#include "ns3/socket-factory.h" |
| 21 |
#include "ns3/packet-socket-address.h" |
| 22 |
#include "ns3/packet-socket.h" |
| 23 |
|
| 24 |
namespace ns3 { |
| 25 |
|
| 26 |
NS_LOG_COMPONENT_DEFINE ("UanApplication"); |
| 27 |
|
| 28 |
NS_OBJECT_ENSURE_REGISTERED (UanApplication); |
| 29 |
|
| 30 |
TypeId |
| 31 |
UanApplication::GetTypeId (void) |
| 32 |
{ |
| 33 |
static TypeId tid = TypeId ("ns3::UanApplication") |
| 34 |
.SetParent<Application> () |
| 35 |
.SetGroupName ("Applications") |
| 36 |
.AddConstructor<UanApplication> () |
| 37 |
; |
| 38 |
return tid; |
| 39 |
} |
| 40 |
|
| 41 |
UanApplication::UanApplication () |
| 42 |
{ |
| 43 |
NS_LOG_FUNCTION (this); |
| 44 |
m_socket = NULL; |
| 45 |
m_packetReceivedCallback.Nullify (); |
| 46 |
} |
| 47 |
UanApplication::~UanApplication () |
| 48 |
{ |
| 49 |
NS_LOG_FUNCTION (this); |
| 50 |
} |
| 51 |
|
| 52 |
void |
| 53 |
UanApplication::DoDispose (void) |
| 54 |
{ |
| 55 |
NS_LOG_FUNCTION (this); |
| 56 |
m_socket = NULL; |
| 57 |
m_packetReceivedCallback.Nullify (); |
| 58 |
Application::DoDispose (); |
| 59 |
} |
| 60 |
|
| 61 |
uint32_t |
| 62 |
UanApplication::GetApplicationId (void) const |
| 63 |
{ |
| 64 |
NS_LOG_FUNCTION (this); |
| 65 |
Ptr<Node> node = GetNode (); |
| 66 |
for (uint32_t i = 0; i < node->GetNApplications (); ++i) |
| 67 |
{ |
| 68 |
if (node->GetApplication (i) == this) |
| 69 |
{ |
| 70 |
return i; |
| 71 |
} |
| 72 |
} |
| 73 |
NS_ASSERT_MSG (false, "forgot to add application to node"); |
| 74 |
return 0; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
void |
| 79 |
UanApplication::Send (UanAddress dst, const Buffer dataBuffer) |
| 80 |
{ |
| 81 |
NS_LOG_FUNCTION (this); |
| 82 |
Address address = GetNode ()->GetDevice (0)->GetAddress (); |
| 83 |
uint8_t *address_buffer = new uint8_t[address.GetLength ()]; |
| 84 |
address.CopyTo (address_buffer); |
| 85 |
UanAddress local_address = UanAddress (address_buffer[address.GetLength () -1]); |
| 86 |
delete [] address_buffer; |
| 87 |
|
| 88 |
const uint8_t * data = dataBuffer.PeekData (); |
| 89 |
|
| 90 |
if(dst.GetAsInt () != local_address.GetAsInt ()) |
| 91 |
{ |
| 92 |
PacketSocketAddress socketAddress; |
| 93 |
socketAddress.SetSingleDevice (GetNode ()->GetDevice (0)->GetIfIndex ()); |
| 94 |
socketAddress.SetPhysicalAddress (dst); |
| 95 |
socketAddress.SetProtocol (0); |
| 96 |
Ptr<Socket> socket = Socket::CreateSocket (GetNode (), TypeId::LookupByName ("ns3::PacketSocketFactory")); |
| 97 |
Ptr<Packet> p = Create<Packet> (data, dataBuffer.GetSize () + 1); |
| 98 |
NS_ASSERT (socket != NULL); |
| 99 |
socket->Bind (); |
| 100 |
socket->Connect (socketAddress); |
| 101 |
socket->Send (p); |
| 102 |
socket->Close (); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
void |
| 107 |
UanApplication::Receive (Ptr<Socket> socket) |
| 108 |
{ |
| 109 |
NS_LOG_FUNCTION (this << socket); |
| 110 |
NS_ASSERT (m_socket != NULL); |
| 111 |
while (m_socket->GetRxAvailable () > 0) |
| 112 |
{ |
| 113 |
Ptr<Packet> p = m_socket->Recv (); |
| 114 |
uint8_t *buffer = new uint8_t[p->GetSize ()]; |
| 115 |
p->CopyData (buffer, p->GetSize ()); |
| 116 |
UanAddress src_address; |
| 117 |
PacketTagIterator i = p->GetPacketTagIterator (); |
| 118 |
while (i.HasNext ()) |
| 119 |
{ |
| 120 |
PacketTagIterator::Item item = i.Next (); |
| 121 |
if (item.GetTypeId () == TypeId::LookupByName ("ns3::SocketAddressTag")) |
| 122 |
{ |
| 123 |
SocketAddressTag tag; |
| 124 |
item.GetTag (tag); |
| 125 |
Address address = tag.GetAddress (); |
| 126 |
uint8_t *address_buffer = new uint8_t[address.GetLength ()]; |
| 127 |
address.CopyTo (address_buffer); |
| 128 |
src_address = UanAddress (address_buffer[address.GetLength () -1]); |
| 129 |
delete [] address_buffer; |
| 130 |
} |
| 131 |
} |
| 132 |
if(!m_packetReceivedCallback.IsNull ()) |
| 133 |
{ |
| 134 |
Buffer dataBuffer; |
| 135 |
Buffer::Iterator iterator = dataBuffer.Begin (); |
| 136 |
iterator.Write (buffer, p->GetSize ()); |
| 137 |
m_packetReceivedCallback (src_address, dataBuffer); |
| 138 |
} |
| 139 |
delete[] buffer; |
| 140 |
} |
| 141 |
|
| 142 |
} |
| 143 |
|
| 144 |
void |
| 145 |
UanApplication::StartApplication (void) |
| 146 |
{ |
| 147 |
NS_LOG_FUNCTION (this); |
| 148 |
PacketSocketAddress socketAddress; |
| 149 |
socketAddress.SetSingleDevice (GetNode ()->GetDevice (0)->GetIfIndex ()); |
| 150 |
socketAddress.SetPhysicalAddress (UanAddress (UanAddress::GetBroadcast ())); |
| 151 |
socketAddress.SetProtocol (0); |
| 152 |
if (!m_socket) |
| 153 |
{ |
| 154 |
m_socket = Socket::CreateSocket (GetNode (), TypeId::LookupByName ("ns3::PacketSocketFactory")); |
| 155 |
} |
| 156 |
m_socket->Bind (); |
| 157 |
m_socket->Connect (socketAddress); |
| 158 |
m_socket->SetRecvCallback (MakeCallback (&UanApplication::Receive, this)); |
| 159 |
} |
| 160 |
|
| 161 |
void |
| 162 |
UanApplication::StopApplication (void) |
| 163 |
{ |
| 164 |
NS_LOG_FUNCTION (this); |
| 165 |
if(m_socket != NULL) |
| 166 |
{ |
| 167 |
m_socket->Close (); |
| 168 |
} |
| 169 |
else |
| 170 |
{ |
| 171 |
NS_LOG_WARN ("UanApplication found null socket to close in StopApplication"); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
void |
| 176 |
UanApplication::SetPacketReceivedCallback (PacketReceivedCallback callback) |
| 177 |
{ |
| 178 |
NS_LOG_FUNCTION (this); |
| 179 |
if (callback.IsNull ()) |
| 180 |
{ |
| 181 |
NS_LOG_DEBUG ("AuvWaypointMobilityModel:Setting NULL packet received callback!"); |
| 182 |
} |
| 183 |
m_packetReceivedCallback = callback; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
} // namespace ns3 |