From 3f4ba0a8c111170ae8d11e270bb838691a538ac6 Mon Sep 17 00:00:00 2001
From: Tim Schubert <tim.schubert@tu-bs.de>
Date: Sat, 29 Aug 2020 21:11:55 +0200
Subject: [PATCH] Make examples runable as tests

---
 .../leo-circular-orbit-tracing-example.cc     | 13 ++++-
 examples/leo-delay-tracing-example.cc         | 31 +++++++----
 helper/leo-orbit-node-helper.cc               | 52 ++++++++++++++-----
 helper/leo-orbit-node-helper.h                | 15 ++++++
 test/examples-to-run.py                       | 22 ++++++++
 5 files changed, 107 insertions(+), 26 deletions(-)
 create mode 100644 test/examples-to-run.py

diff --git a/examples/leo-circular-orbit-tracing-example.cc b/examples/leo-circular-orbit-tracing-example.cc
index 315f39d..06a41c4 100644
--- a/examples/leo-circular-orbit-tracing-example.cc
+++ b/examples/leo-circular-orbit-tracing-example.cc
@@ -22,7 +22,7 @@ int main(int argc, char *argv[])
   CommandLine cmd;
   std::string orbitFile;
   std::string traceFile;
-  double duration;
+  double duration = 60;
   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");
@@ -30,7 +30,16 @@ int main(int argc, char *argv[])
   cmd.Parse (argc, argv);
 
   LeoOrbitNodeHelper orbit;
-  NodeContainer satellites = orbit.Install (orbitFile);
+  NodeContainer satellites;
+  if (orbitFile.empty())
+    {
+      satellites = orbit.Install (orbitFile);
+    }
+  else
+    {
+      satellites = orbit.Install ({ LeoOrbit (1200, 20, 32, 16),
+      				  LeoOrbit (1180, 30, 12, 10) });
+    }
 
   Config::Connect ("/NodeList/*/$ns3::MobilityModel/CourseChange",
                    MakeCallback (&CourseChange));
diff --git a/examples/leo-delay-tracing-example.cc b/examples/leo-delay-tracing-example.cc
index a58fb84..0c70894 100644
--- a/examples/leo-delay-tracing-example.cc
+++ b/examples/leo-delay-tracing-example.cc
@@ -74,18 +74,18 @@ int main (int argc, char *argv[])
   CommandLine cmd;
   std::string orbitFile;
   std::string traceFile;
-  LeoLatLong source;
-  LeoLatLong destination;
-  std::string islRate;
-  std::string constellation;
+  LeoLatLong source (51.84, 10.28);
+  LeoLatLong destination (40.76, -73.96);
+  std::string islRate = "2Gbps";
+  std::string constellation = "TelesatGateway";
   uint32_t latGws = 20;
   uint32_t lonGws = 20;
-  double interval;
-  double duration;
-  bool islEnabled = false;
-  bool traceDrops = false;
-  bool traceTxRx = false;
-  bool traceFwd = false;
+  double interval = 1;
+  double duration = 100;
+  bool islEnabled = true;
+  bool traceDrops = true;
+  bool traceTxRx = true;
+  bool traceFwd = true;
   std::string routingProto = "aodv";
   cmd.AddValue("orbitFile", "CSV file with orbit parameters", orbitFile);
   cmd.AddValue("traceFile", "CSV file to store mobility trace in", traceFile);
@@ -117,7 +117,16 @@ int main (int argc, char *argv[])
     }
 
   LeoOrbitNodeHelper orbit;
-  NodeContainer satellites = orbit.Install (orbitFile);
+  NodeContainer satellites;
+  if (orbitFile.empty())
+    {
+      satellites = orbit.Install (orbitFile);
+    }
+  else
+    {
+      satellites = orbit.Install ({ LeoOrbit (1200, 20, 32, 16),
+      				  LeoOrbit (1180, 30, 12, 10) });
+    }
 
   LeoGndNodeHelper ground;
   NodeContainer stations = ground.Install (latGws, lonGws);
diff --git a/helper/leo-orbit-node-helper.cc b/helper/leo-orbit-node-helper.cc
index 6a364f8..2d07e01 100644
--- a/helper/leo-orbit-node-helper.cc
+++ b/helper/leo-orbit-node-helper.cc
@@ -1,6 +1,7 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 
 #include <fstream>
+#include <vector>
 
 #include "ns3/log.h"
 #include "ns3/config.h"
@@ -10,7 +11,6 @@
 #include "ns3/integer.h"
 
 #include "leo-orbit-node-helper.h"
-#include "../model/leo-orbit.h"
 
 using namespace std;
 
@@ -33,6 +33,26 @@ LeoOrbitNodeHelper::SetAttribute (string name, const AttributeValue &value)
   m_nodeFactory.Set (name, value);
 }
 
+NodeContainer
+LeoOrbitNodeHelper::Install (const LeoOrbit &orbit)
+{
+  NS_LOG_FUNCTION (this << orbit);
+
+  MobilityHelper mobility;
+  mobility.SetPositionAllocator ("ns3::LeoCircularOrbitPostionAllocator",
+                                 "NumOrbits", IntegerValue (orbit.planes),
+                                 "NumSatellites", IntegerValue (orbit.sats));
+  mobility.SetMobilityModel ("ns3::LeoCircularOrbitMobilityModel",
+  			     "Altitude", DoubleValue (orbit.alt),
+  			     "Inclination", DoubleValue (orbit.inc));
+
+  NodeContainer c;
+  c.Create (orbit.sats*orbit.planes);
+  mobility.Install (c);
+
+  return c;
+}
+
 NodeContainer
 LeoOrbitNodeHelper::Install (const std::string &orbitFile)
 {
@@ -44,18 +64,7 @@ LeoOrbitNodeHelper::Install (const std::string &orbitFile)
   LeoOrbit orbit;
   while ((orbits >> orbit))
     {
-      MobilityHelper mobility;
-      mobility.SetPositionAllocator ("ns3::LeoCircularOrbitPostionAllocator",
-                                     "NumOrbits", IntegerValue (orbit.planes),
-                                     "NumSatellites", IntegerValue (orbit.sats));
-      mobility.SetMobilityModel ("ns3::LeoCircularOrbitMobilityModel",
-  			     	 "Altitude", DoubleValue (orbit.alt),
-  			     	 "Inclination", DoubleValue (orbit.inc));
-
-      NodeContainer c;
-      c.Create (orbit.sats*orbit.planes);
-      mobility.Install (c);
-      nodes.Add (c);
+      nodes.Add (Install (orbit));
       NS_LOG_DEBUG ("Added orbit plane");
     }
   orbits.close ();
@@ -65,4 +74,21 @@ LeoOrbitNodeHelper::Install (const std::string &orbitFile)
   return nodes;
 }
 
+NodeContainer
+LeoOrbitNodeHelper::Install (const vector<LeoOrbit> &orbits)
+{
+  NS_LOG_FUNCTION (this << orbits);
+
+  NodeContainer nodes;
+  for (uint64_t i = 0; i < orbits.size(); i++)
+    {
+      nodes.Add (Install (orbits[i]));
+      NS_LOG_DEBUG ("Added orbit plane");
+    }
+
+  NS_LOG_DEBUG ("Added " << nodes.GetN () << " nodes");
+
+  return nodes;
+}
+
 }; // namespace ns3
diff --git a/helper/leo-orbit-node-helper.h b/helper/leo-orbit-node-helper.h
index b0cef97..cf32568 100644
--- a/helper/leo-orbit-node-helper.h
+++ b/helper/leo-orbit-node-helper.h
@@ -9,6 +9,7 @@
 #include "ns3/node-container.h"
 #include "ns3/leo-circular-orbit-mobility-model.h"
 #include "ns3/leo-circular-orbit-position-allocator.h"
+#include "ns3/leo-orbit.h"
 
 /**
  * \brief Builds a node container of nodes with LEO positions using a list of
@@ -33,6 +34,20 @@ public:
    */
   NodeContainer Install (const std::string &orbitFile);
 
+  /**
+   *
+   * \param orbits orbit definitions
+   * \returns a node container containing nodes using the specified attributes
+   */
+  NodeContainer Install (const std::vector<LeoOrbit> &orbits);
+
+  /**
+   *
+   * \param orbit orbit definition
+   * \returns a node container containing nodes using the specified attributes
+   */
+  NodeContainer Install (const LeoOrbit &orbit);
+
   /**
    * Set an attribute for each node
    *
diff --git a/test/examples-to-run.py b/test/examples-to-run.py
new file mode 100644
index 0000000..c0758f4
--- /dev/null
+++ b/test/examples-to-run.py
@@ -0,0 +1,22 @@
+#! /usr/bin/env python3
+## -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
+
+# A list of C++ examples to run in order to ensure that they remain
+# buildable and runnable over time.  Each tuple in the list contains
+#
+#     (example_name, do_run, do_valgrind_run).
+#
+# See test.py for more information.
+cpp_examples = [
+    ("leo-orbit", "True", "True"),
+    ("leo-delay", "True", "True"),
+    ("leo-bulk-send", "True", "True"),
+]
+
+# A list of Python examples to run in order to ensure that they remain
+# runnable over time.  Each tuple in the list contains
+#
+#     (example_name, do_run).
+#
+# See test.py for more information.
+python_examples = []