Skip to content

Instantly share code, notes, and snippets.

@SzymonSzott
Created December 4, 2018 12:39
Show Gist options
  • Select an option

  • Save SzymonSzott/2be510d82e947a605e4dec041deeacdc to your computer and use it in GitHub Desktop.

Select an option

Save SzymonSzott/2be510d82e947a605e4dec041deeacdc to your computer and use it in GitHub Desktop.
Generic traffic generator installer for ns-3
void installTrafficGenerator(Ptr<ns3::Node> fromNode, Ptr<ns3::Node> toNode, int port, std::string offeredLoad, int packetSize, int simulationTime, int warmupTime ) {
Ptr<Ipv4> ipv4 = toNode->GetObject<Ipv4> (); // Get Ipv4 instance of the node
Ipv4Address addr = ipv4->GetAddress (1, 0).GetLocal (); // Get Ipv4InterfaceAddress of xth interface.
ApplicationContainer sourceApplications, sinkApplications;
uint8_t tosValue = 0x70; //AC_BE
//Add random fuzz to app start time
double min = 0.0;
double max = 1.0;
Ptr<UniformRandomVariable> fuzz = CreateObject<UniformRandomVariable> ();
fuzz->SetAttribute ("Min", DoubleValue (min));
fuzz->SetAttribute ("Max", DoubleValue (max));
InetSocketAddress sinkSocket (addr, port);
sinkSocket.SetTos (tosValue);
OnOffHelper onOffHelper ("ns3::UdpSocketFactory", sinkSocket);
onOffHelper.SetConstantRate (DataRate (offeredLoad + "Mbps"), packetSize);
sourceApplications.Add (onOffHelper.Install (fromNode)); //fromNode
PacketSinkHelper packetSinkHelper ("ns3::UdpSocketFactory", sinkSocket);
sinkApplications.Add (packetSinkHelper.Install (toNode)); //toNode
sinkApplications.Start (Seconds (warmupTime));
sinkApplications.Stop (Seconds (simulationTime));
sourceApplications.Start (Seconds (warmupTime+fuzz->GetValue ()));
sourceApplications.Stop (Seconds (simulationTime));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment