How to configure the trace variable for OnOffApplication and PacketSink applications.

86 views
Skip to first unread message

项远辉

unread,
Mar 28, 2024, 9:21:00 AM3/28/24
to ns-3-users
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?

Tommaso Pecorella

unread,
Mar 28, 2024, 8:49:54 PM3/28/24
to ns-3-users
By writing the definitions of the functions "recvPackets" and "sendPackets" - you just declared them, see https://learn.microsoft.com/en-us/cpp/cpp/declarations-and-definitions-cpp?view=msvc-170

项远辉

unread,
Mar 29, 2024, 2:31:37 AM3/29/24
to ns-3-users
But after I changed the code like this, another error occurred.

#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 (Ptr<const Packet> packet);
void sendPackets (Ptr<const Packet> packet);


void recvPackets (Ptr<const Packet> packet)
{
  NS_LOG_UNCOND ("+"<<Simulator::Now().GetSeconds()<<""<<packet->GetSize()<<"");
}

void sendPackets (Ptr<const Packet> packet)
{
  NS_LOG_UNCOND ("+"<<Simulator::Now().GetSeconds()<<""<<packet->GetSize()<<"");
  Config::ConnectWithoutContext("/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. 

Creating Nodes
msg="Could not connect callback to /NodeList/0/ApplicationList/*/$ns3::OnOffApplication/Tx", +0.000000000s -1 file=../src/core/model/config.cc, line=906
terminate called without an active exception

Tommaso Pecorella

unread,
Mar 29, 2024, 8:30:06 PM3/29/24
to ns-3-users
You're aware that you didn't install OnOffApplication in node 0, right?

项远辉

unread,
Mar 29, 2024, 9:30:59 PM3/29/24
to ns-3-users
Thank you for your reply,I think I have installed the OnOffApplication on the source node(sendernode) and thePacketSink on the target node(sinksnode),
I made the following modifications to the code, but the output is the same result.

  int sender =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.AddValue ("sinks", "Amount of underwater sender", sender);

  cmd.Parse(argc,argv);

  std::cout << "-----------Initializing simulation-----------\n";  

  NodeContainer nodesCon; 
  NodeContainer sinksCon; 
  NodeContainer senderCon; 
  nodesCon.Create(nodes); 
  sinksCon.Create(sinks);
  senderCon.Create(sender); 
  for (NodeContainer::Iterator i = senderCon.Begin(); i != senderCon.End(); i++)

    {
      Ptr<AquaSimNetDevice> newDevice = CreateObject<AquaSimNetDevice>();
      position->Add(Vector(0,0,0));

   
      devices.Add(asHelper.Create(*i, 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.Get(0)); 
  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::ConnectWithoutContext("/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;
}

Tommaso Pecorella

unread,
Mar 30, 2024, 6:11:33 AM3/30/24
to ns-3-users
> Config::ConnectWithoutContext("/NodeList/0/ApplicationList/*/$ns3::OnOffApplication/Tx", MakeCallback(&sendPackets));
> Config::ConnectWithoutContext("/NodeList/*/ApplicationList/*/$ns3::PacketSink/Rx", MakeCallback(&recvPackets));

The NodeList (read the documentation) is the list of the nodes, ordered by their creation order (read the documentation), and in these lines:
 nodesCon.Create(nodes); 
> sinksCon.Create(sinks);
> senderCon.Create(sender); 

you're creating the sender as the LAST node.

... it's your code, not mine.
Reply all
Reply to author
Forward
0 new messages