Hello everyone!
I want to use the trace system to track the reception and forwarding of data packets between the source and target nodes.
But my program encountered some errors and couldn't run.
My code is as follows
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/mobility-module.h"
#include "ns3/energy-module.h" //may not be needed here...
#include "ns3/aqua-sim-ng-module.h"
#include "ns3/applications-module.h"
#include "ns3/log.h"
#include "ns3/callback.h"
#include "ns3/netanim-module.h"
#include "ns3/application-container.h"
/*
* VBF Test
*/
using namespace ns3;
NS_LOG_COMPONENT_DEFINE("VBF");
void recvPackets (std::string context, Ptr<const Packet> packet, const Address &from);
void sendPackets ( Ptr<const Packet> packet);
int
main (int argc, char *argv[])
{
double simStop = 200; //seconds
int nodes = 200;
int sinks = 1;
uint32_t m_dataRate = 10000;
uint32_t m_packetSize = 40;
double range = 50;
//double range = 100;
//int m_maxBurst =10;
LogComponentEnable ("VBF", LOG_LEVEL_INFO);
LogComponentEnable ("AquaSimVBF", LOG_LEVEL_DEBUG);
//to change on the fly
CommandLine cmd;
cmd.AddValue ("simStop", "Length of simulation", simStop);
cmd.AddValue ("nodes", "Amount of regular underwater nodes", nodes);
cmd.AddValue ("sinks", "Amount of underwater sinks", sinks);
cmd.Parse(argc,argv);
std::cout << "-----------Initializing simulation-----------\n";
NodeContainer nodesCon;
NodeContainer sinksCon;
NodeContainer senderCon;
nodesCon.Create(nodes);
sinksCon.Create(sinks);
senderCon.Create(1);
PacketSocketHelper socketHelper;
socketHelper.Install(nodesCon);
socketHelper.Install(sinksCon);
socketHelper.Install(senderCon);
//establish layers using helper's pre-build settings
AquaSimChannelHelper channel = AquaSimChannelHelper::Default();
channel.SetPropagation("ns3::AquaSimRangePropagation");
AquaSimHelper asHelper = AquaSimHelper::Default();
//AquaSimEnergyHelper energy; //******this could instead be handled by node helper. ****/
asHelper.SetChannel(channel.Create());
asHelper.SetMac("ns3::AquaSimBroadcastMac");
//asHelper.SetRouting("ns3::AquaSimVBF", "Width", DoubleValue(100), "TargetPos", Vector3DValue(Vector(190,190,0)));
asHelper.SetRouting("ns3::AquaSimVBF", "Width", DoubleValue(100), "TargetPos", Vector3DValue(Vector(190,190,200)));
/*
* Preset up mobility model for nodes and sinks here
*/
MobilityHelper mobility;
MobilityHelper nodeMobility;
NetDeviceContainer devices;
Ptr<ListPositionAllocator> position = CreateObject<ListPositionAllocator> ();
std::cout << "Creating Nodes\n";
for (NodeContainer::Iterator i = nodesCon.Begin(); i != nodesCon.End(); i++)
{
Ptr<AquaSimNetDevice> newDevice = CreateObject<AquaSimNetDevice>();
devices.Add(asHelper.Create(*i, newDevice));
newDevice->GetPhy()->SetTransRange(range);
}
for (NodeContainer::Iterator i = sinksCon.Begin(); i != sinksCon.End(); i++)
{
Ptr<AquaSimNetDevice> newDevice = CreateObject<AquaSimNetDevice>();
//position->Add(Vector(190,190,0));
position->Add(Vector(190,190,200));
devices.Add(asHelper.Create(*i, newDevice));
newDevice->GetPhy()->SetTransRange(range);
}
Ptr<AquaSimNetDevice> newDevice = CreateObject<AquaSimNetDevice>();
position->Add(Vector(0,0,0));
devices.Add(asHelper.Create(senderCon.Get(0),newDevice));
newDevice->GetPhy()->SetTransRange(range);
//Set sink at origin and surround with uniform distribution of regular nodes.
mobility.SetPositionAllocator(position);
mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
//nodeMobility.SetPositionAllocator("ns3::UniformDiscPositionAllocator", "X", DoubleValue(100.0),"Y", DoubleValue(100.0), "rho", DoubleValue(100));
nodeMobility.SetPositionAllocator("ns3::RandomBoxPositionAllocator", "X", StringValue("ns3::UniformRandomVariable[Min=0|Max=200]"),
"Y", StringValue("ns3::UniformRandomVariable[Min=0|Max=200]"), "Z", StringValue("ns3::UniformRandomVariable[Min=0|Max=200]"));
nodeMobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
nodeMobility.Install(nodesCon);
mobility.Install(sinksCon);
mobility.Install(senderCon);
PacketSocketAddress socket;
socket.SetAllDevices();
// socket.SetSingleDevice (devices.Get(0)->GetIfIndex());
socket.SetPhysicalAddress (devices.Get(nodes)->GetAddress());
socket.SetProtocol (0);
//std::cout << devices.Get(nodes)->GetAddress() << " &&& " << devices.Get(0)->GetIfIndex() << "\n";
//std::cout << devices.Get(0)->GetAddress() << " &&& " << devices.Get(1)->GetIfIndex() << "\n";
OnOffHelper app ("ns3::PacketSocketFactory", Address (socket));
app.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=0.0066]"));
app.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0.9934]"));
// app.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=0.026]"));
// app.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0.974]"));
app.SetAttribute ("DataRate", DataRateValue (m_dataRate));
app.SetAttribute ("PacketSize", UintegerValue (m_packetSize));
ApplicationContainer apps = app.Install (senderCon);
apps.Start (Seconds (0.5));
apps.Stop (Seconds (simStop));
PacketSinkHelper sink ("ns3::PacketSocketFactory",
Address (socket));
ApplicationContainer sinkapps = sink.Install (sinksCon.Get(0));
apps.Start (Seconds (0.01));
apps.Stop (Seconds (simStop));
//Ptr<Node> sinkNode = sinksCon.Get(0);
//TypeId psfid = TypeId::LookupByName ("ns3::PacketSocketFactory");
//Ptr<Socket> sinkSocket = Socket::CreateSocket (sinkNode, psfid);
//sinkSocket->Bind (socket);
Config::ConnectWithoutContext("/NodeList/0/ApplicationList/*/$ns3::OnOffApplication/Tx", MakeCallback(&sendPackets));
Config::Connect("/NodeList/*/ApplicationList/*/$ns3::PacketSink/Rx", MakeCallback(&recvPackets));
Packet::EnablePrinting ();
std::cout << "-----------Running Simulation-----------\n";
Simulator::Stop(Seconds(simStop));
Simulator::Run();
asHelper.GetChannel()->PrintCounters();
Simulator::Destroy();
std::cout << "fin.\n";
return 0;
}
The error I encountered was.
/home/wqf/tarballs/ns-allinone-3.36.1/ns-3.36.1/src/aqua-sim-ng/examples/VBF.cc:176: undefined reference to `sendPackets(ns3::Ptr<ns3::Packet const>)'
/usr/bin/ld: /home/wqf/tarballs/ns-allinone-3.36.1/ns-3.36.1/src/aqua-sim-ng/examples/VBF.cc:177: undefined reference to `recvPackets(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, ns3::Ptr<ns3::Packet const>, ns3::Address const&)'
collect2: error: ld returned 1 exit status
How can I solve this problem?