ns-3-leo/examples/leo-delay-tracing-example.cc
2020-08-27 17:59:58 +02:00

205 lines
6.5 KiB
C++

/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
#include "ns3/core-module.h"
#include "ns3/mobility-module.h"
#include "ns3/leo-module.h"
#include "ns3/network-module.h"
#include "ns3/aodv-module.h"
#include "ns3/udp-server.h"
#include "ns3/epidemic-routing-module.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("LeoDelayTracingExample");
static void
EchoRx (std::string context, Ptr<const Packet> packet)
{
SeqTsHeader seqTs;
Ptr<Packet> p = packet->Copy ();
p->RemoveHeader (seqTs);
// seqnr, timestamp, delay
std::cout << context << "," << seqTs.GetSeq () << "," << seqTs.GetTs () << "," << Simulator::Now () - seqTs.GetTs () << std::endl;
}
static void
EchoTx (std::string context, Ptr<const Packet> packet)
{
SeqTsHeader seqTs;
Ptr<Packet> p = packet->Copy ();
p->RemoveHeader (seqTs);
// seqnr, timestamp, delay
std::cout << context << "," << seqTs.GetSeq () << "," << seqTs.GetTs () << "," << Simulator::Now () - seqTs.GetTs () << std::endl;
}
static void
MacTxDrop (std::string context, Ptr<const Packet> packet)
{
Ptr<Packet> p = packet->Copy ();
std::cout << context << ",MacTxDrop," << p << std::endl;
}
static void
MacRxDrop (std::string context, Ptr<const Packet> packet)
{
Ptr<Packet> p = packet->Copy ();
std::cout << context << ",MacRxDrop," << p << std::endl;
}
static void
PhyTxDrop (std::string context, Ptr<const Packet> packet)
{
Ptr<Packet> p = packet->Copy ();
std::cout << context << ",PhyTxDrop," << p << std::endl;
}
static void
PhyRxDrop (std::string context, Ptr<const Packet> packet)
{
Ptr<Packet> p = packet->Copy ();
std::cout << context << ",PhyRxDrop," << p << std::endl;
}
int main (int argc, char *argv[])
{
CommandLine cmd;
std::string orbitFile;
std::string traceFile;
LeoLatLong source;
LeoLatLong destination;
std::string islRate;
std::string constellation;
uint32_t latGws = 20;
uint32_t lonGws = 20;
double interval;
double duration;
bool islEnable = false;
bool traceDrops = false;
std::string routingProto = "aodv";
cmd.AddValue("orbitFile", "CSV file with orbit parameters", orbitFile);
cmd.AddValue("traceFile", "CSV file to store mobility trace in", traceFile);
cmd.AddValue("precision", "ns3::LeoCircularOrbitMobilityModel::Precision");
cmd.AddValue("duration", "Duration of the simulation in seconds", duration);
cmd.AddValue("source", "Traffic source", source);
cmd.AddValue("destination", "Traffic destination", destination);
cmd.AddValue("islRate", "ns3::MockNetDevice::DataRate");
cmd.AddValue("constellation", "LEO constellation link settings name", constellation);
cmd.AddValue("interval", "Echo interval", interval);
cmd.AddValue("routing", "Routing protocol", routingProto);
cmd.AddValue("ttlThresh", "ns3::aodv::RoutingProtocol::TtlThreshold");
cmd.AddValue("routeTimeout", "ns3::aodv::RoutingProtocol::ActiveRouteTimeout");
cmd.AddValue("islEnable", "Enable inter-satellite links", islEnable);
cmd.AddValue("traceDrops", "Enable tracing of PHY and MAC drops", traceDrops);
cmd.AddValue("latGws", "Latitudal rows of gateways", latGws);
cmd.AddValue("latGws", "Longitudinal rows of gateways", lonGws);
cmd.Parse (argc, argv);
std::streambuf *coutbuf = std::cout.rdbuf();
// redirect cout if traceFile
std::ofstream out;
out.open (traceFile);
if (out.is_open ())
{
std::cout.rdbuf(out.rdbuf());
}
LeoOrbitNodeHelper orbit;
NodeContainer satellites = orbit.Install (orbitFile);
LeoGndNodeHelper ground;
NodeContainer stations = ground.Install (latGws, lonGws);
NodeContainer users = ground.Install (source, destination);
stations.Add (users);
LeoChannelHelper utCh;
utCh.SetConstellation (constellation);
NetDeviceContainer utNet = utCh.Install (satellites, stations);
InternetStackHelper stack;
if (routingProto == "epidemic")
{
EpidemicHelper epidemic;
stack.SetRoutingHelper (epidemic);
}
else
{
// Install internet stack on nodes
AodvHelper aodv;
aodv.Set ("EnableHello", BooleanValue (false));
aodv.Set ("RreqRateLimit", UintegerValue (1));
aodv.Set ("RerrRateLimit", UintegerValue (1));
stack.SetRoutingHelper (aodv);
}
stack.Install (satellites);
stack.Install (stations);
Ipv4AddressHelper ipv4;
ipv4.SetBase ("10.1.0.0", "255.255.0.0");
ipv4.Assign (utNet);
if (islEnable)
{
IslHelper islCh;
NetDeviceContainer islNet = islCh.Install (satellites);
ipv4.SetBase ("10.2.0.0", "255.255.0.0");
ipv4.Assign (islNet);
}
Ptr<Node> client = users.Get (0);
Ptr<Node> server = users.Get (1);
// we want to ping terminals
UdpServerHelper echoServer (9);
ApplicationContainer serverApps = echoServer.Install (server);
// install a client on one of the terminals
ApplicationContainer clientApps;
Address remote = server->GetObject<Ipv4> ()->GetAddress (1, 0).GetLocal ();
UdpClientHelper echoClient (remote, 9);
echoClient.SetAttribute ("MaxPackets", UintegerValue (duration / interval));
echoClient.SetAttribute ("Interval", TimeValue (Seconds (interval)));
echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
clientApps.Add (echoClient.Install (client));
Config::Connect ("/NodeList/*/ApplicationList/*/$ns3::UdpServer/Rx",
MakeCallback (&EchoRx));
Config::Connect ("/NodeList/*/ApplicationList/*/$ns3::UdpServer/Tx",
MakeCallback (&EchoTx));
if (traceDrops)
{
Config::Connect ("/NodeList/*/DeviceList/*/$ns3::MockNetDevice/MacTxDrop",
MakeCallback (&MacTxDrop));
Config::Connect ("/NodeList/*/DeviceList/*/$ns3::MockNetDevice/PhyTxDrop",
MakeCallback (&PhyTxDrop));
Config::Connect ("/NodeList/*/DeviceList/*/$ns3::MockNetDevice/MacRxDrop",
MakeCallback (&MacRxDrop));
Config::Connect ("/NodeList/*/DeviceList/*/$ns3::MockNetDevice/PhyRxDrop",
MakeCallback (&PhyRxDrop));
}
std::cerr << "LOCAL =" << client->GetId () << std::endl;
std::cerr << "REMOTE=" << server->GetId () << ",addr=" << Ipv4Address::ConvertFrom (remote) << std::endl;
std::cout << "Context,Sequence Number,Timestamp,Delay" << std::endl;
//serverApps.Start (Seconds (1));
//clientApps.Start (Seconds (1));
Simulator::Stop (Seconds (duration));
Simulator::Run ();
Simulator::Destroy ();
Ptr<UdpServer> result = StaticCast<UdpServer> (serverApps.Get (0));
std::cout << "Received,Lost" << std::endl
<< result->GetReceived () << "," << result->GetLost () << std::endl;
out.close ();
std::cout.rdbuf(coutbuf);
return 0;
}