/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014 Universita' di Firenze
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
*/
#include "ns3/test.h"
#include "ns3/simulator.h"
#include "ns3/uinteger.h"
#include "ns3/traced-callback.h"
#include "ns3/packet.h"
#include "ns3/packet-socket-helper.h"
#include "ns3/packet-socket-client.h"
#include "ns3/packet-socket-server.h"
#include "ns3/simple-net-device.h"
#include "ns3/simple-channel.h"
#include "ns3/netanim-module.h"
#include "ns3/csma-module.h"
#include <string>
using namespace ns3;
using namespace std;
class PacketSocketAppsTest : public TestCase
{
uint32_t m_receivedPacketSize;
uint32_t m_receivedPacketNumber;
public:
virtual void DoRun (void);
PacketSocketAppsTest ();
void ReceivePkt (Ptr<const Packet> packet, const Address &from);
};
PacketSocketAppsTest::PacketSocketAppsTest ()
: TestCase ("Packet Socket Apps test")
{
m_receivedPacketSize = 0;
m_receivedPacketNumber = 0;
}
void PacketSocketAppsTest::ReceivePkt (Ptr<const Packet> packet, const Address &from)
{
if (packet)
{
m_receivedPacketSize = packet->GetSize ();
m_receivedPacketNumber++;
}
}
void
PacketSocketAppsTest::DoRun (void)
{
ns3::PacketMetadata::Enable () ;
// Create topology
NodeContainer nodes;
nodes.Create (2);
// ns3::PacketMetadata::Enable();
PacketSocketHelper packetSocket;
// give packet socket powers to nodes.
packetSocket.Install (nodes);
/* Ptr<SimpleNetDevice> txDev;
txDev = CreateObject<SimpleNetDevice> ();
nodes.Get (0)->AddDevice (txDev);
Ptr<SimpleNetDevice> rxDev;
rxDev = CreateObject<SimpleNetDevice> ();
nodes.Get (1)->AddDevice (rxDev);
Ptr<SimpleChannel> channel = CreateObject<SimpleChannel> ();
txDev->SetChannel (channel);
rxDev->SetChannel (channel);
txDev->SetNode (nodes.Get (0));
rxDev->SetNode (nodes.Get (1));
PacketSocketAddress socketAddr;
socketAddr.SetSingleDevice (txDev->GetIfIndex ());
socketAddr.SetPhysicalAddress (rxDev->GetAddress ());
socketAddr.SetProtocol (1);
*/
//modify
CsmaHelper csma;
csma.SetChannelAttribute ("DataRate", DataRateValue (DataRate (5000000)));
csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
NetDeviceContainer devs = csma.Install (nodes);
PacketSocketAddress socketAddr;
socketAddr.SetSingleDevice (devs.Get(0) ->GetIfIndex ());
socketAddr.SetPhysicalAddress (devs.Get(1)->GetAddress ());
// Arbitrary protocol type.
// Note: PacketSocket doesn't have any L4 multiplexing or demultiplexing
// The only mux/demux is based on the protocol field
socketAddr.SetProtocol (1);
// Ptr<PacketSocketClient> client = CreateObject<PacketSocketClient> ();
// client->SetRemote (socketAddr);
// nodes.Get (0)->AddApplication (client);
// Ptr<PacketSocketServer> server = CreateObject<PacketSocketServer> ();
// server->SetLocal (socketAddr);
// nodes.Get (1)->AddApplication (server);
// client->StartApplication();
Ptr<PacketSocketClient> client = CreateObject<PacketSocketClient> ();
client->SetRemote (socketAddr);
client->SetAttribute ("PacketSize", UintegerValue (1000));
client->SetAttribute ("MaxPackets", UintegerValue (3));
nodes.Get (0)->AddApplication (client);
Ptr<PacketSocketServer> server = CreateObject<PacketSocketServer> ();
server->TraceConnectWithoutContext ("Rx", MakeCallback (&PacketSocketAppsTest::ReceivePkt, this));
server->SetLocal (socketAddr);
nodes.Get (1)->AddApplication (server);
//add......................................................
string str="hello ns3 !";
uint8_t buffer[255] ;
uint32_t len = str.length();
for(uint32_t i=0;i<len;i++)
{
buffer[i]=str[i];//char 与 uint_8逐个赋值
}
buffer[len]='\0';
Ptr<const Packet> packet = Create<Packet>(buffer,sizeof(buffer));//把buffer写入到包内
//send packet
// client->Send(packet);
PacketSocketAppsTest::ReceivePkt(packet,socketAddr);
AsciiTraceHelper ascii;
csma.EnableAsciiAll (ascii.CreateFileStream ("806packet-socket-apps-test-suite.pcap"));
csma.EnablePcapAll ("806packet-socket-apps-test-suite");
AnimationInterface anim("806packet-socket-apps-test-suite.xml");
Simulator::Run ();
Simulator::Destroy ();
NS_TEST_EXPECT_MSG_EQ (m_receivedPacketNumber, 3, "Number of packet received");
NS_TEST_EXPECT_MSG_EQ (m_receivedPacketSize, 1000, "Size of packet received");
}
class PacketSocketAppsTestSuite : public TestSuite
{
public:
PacketSocketAppsTestSuite () : TestSuite ("packet-socket-apps", UNIT)
{
AddTestCase (new PacketSocketAppsTest, TestCase::QUICK);
}
};
//new.............................................................
int main (int argc, char *argv[]){
PacketSocketAppsTest packetSocketAppsCase;
packetSocketAppsCase.DoRun();
return 0;
}
static PacketSocketAppsTestSuite g_packetSocketAppsTestSuite;
806PacketSocketTest
最新推荐文章于 2024-07-17 15:00:16 发布