From 97de8c9d24a1e03c318be63b6dc7e066fd42cc6e Mon Sep 17 00:00:00 2001
From: Tim Schubert <tim.schubert@tu-bs.de>
Date: Mon, 20 Jul 2020 15:48:24 +0200
Subject: [PATCH] refactor stuff

---
 helper/arp-cache-helper.cc               |  8 +++
 helper/arp-cache-helper.h                |  2 +-
 helper/isl-helper.cc                     |  3 +
 helper/leo-channel-helper.cc             | 10 +++
 helper/leo-helper.cc                     | 20 +++---
 helper/nd-cache-helper.cc                |  7 ++
 helper/satellite-node-helper.cc          |  2 +-
 model/isl-mock-channel.cc                |  1 +
 model/leo-mock-channel.cc                | 45 ++++++-------
 model/mock-channel.cc                    | 32 +++------
 test/ground-node-helper-test-suite.cc    |  2 -
 test/isl-test-suite.cc                   |  2 +-
 test/leo-test-suite.cc                   | 85 +++++++++++++++++-------
 test/satellite-node-helper-test-suite.cc |  2 -
 wscript                                  |  2 -
 15 files changed, 135 insertions(+), 88 deletions(-)

diff --git a/helper/arp-cache-helper.cc b/helper/arp-cache-helper.cc
index c1bb392..f957561 100644
--- a/helper/arp-cache-helper.cc
+++ b/helper/arp-cache-helper.cc
@@ -3,6 +3,7 @@
 #include "ns3/core-module.h"
 #include "ns3/network-module.h"
 #include "ns3/internet-module.h"
+#include "ns3/log.h"
 #include "../model/leo-mock-net-device.h"
 
 #include "arp-cache-helper.h"
@@ -10,13 +11,18 @@
 namespace ns3
 {
 
+NS_LOG_COMPONENT_DEFINE ("ArpCacheHelper");
+
 void
 ArpCacheHelper::Install (NetDeviceContainer &devices, Ipv4InterfaceContainer &interfaces) const
 {
+  NS_LOG_FUNCTION (this);
+
   for (size_t i = 0; i < devices.GetN (); i ++)
     {
       Ptr<NetDevice> dev = devices.Get (i);
       Ptr<Node> node = dev->GetNode ();
+      NS_LOG_INFO ("Preparing ARP cache of " << node);
       Ptr<Ipv4L3Protocol> ipv4 = node->GetObject<Ipv4L3Protocol> ();
       int32_t ifIndex = ipv4->GetInterfaceForDevice (dev);
       Ptr<Ipv4Interface> interface = ipv4->GetInterface (ifIndex);
@@ -47,6 +53,8 @@ ArpCacheHelper::Install (NetDeviceContainer &devices, Ipv4InterfaceContainer &in
               entry = cache->Add (ipaddr);
             }
           entry->SetMacAddress (address);
+
+	  NS_LOG_DEBUG ("Added entry for " << address);
       	}
     }
 }
diff --git a/helper/arp-cache-helper.h b/helper/arp-cache-helper.h
index 7349c9f..2d03b74 100644
--- a/helper/arp-cache-helper.h
+++ b/helper/arp-cache-helper.h
@@ -20,4 +20,4 @@ public:
 
 }; /* namespace ns3 */
 
-#endif /* ARP_CACHE_HELPER */
+#endif /* ARP_CACHE_HELPER_H */
diff --git a/helper/isl-helper.cc b/helper/isl-helper.cc
index fdd89e5..26bd92e 100644
--- a/helper/isl-helper.cc
+++ b/helper/isl-helper.cc
@@ -224,12 +224,15 @@ IslHelper::Install (NodeContainer c)
 NetDeviceContainer
 IslHelper::Install (std::vector<Ptr<Node> > &nodes)
 {
+  NS_LOG_FUNCTION (this);
+
   Ptr<MockChannel> channel = m_channelFactory.Create<MockChannel> ();
 
   NetDeviceContainer container;
 
   for (Ptr<Node> node: nodes)
   {
+    NS_LOG_DEBUG ("Adding device for node " << node->GetId ());
     Ptr<MockNetDevice> dev = m_deviceFactory.Create<MockNetDevice> ();
     dev->SetAddress (Mac48Address::Allocate ());
     node->AddDevice (dev);
diff --git a/helper/leo-channel-helper.cc b/helper/leo-channel-helper.cc
index 19a419b..70eaff4 100644
--- a/helper/leo-channel-helper.cc
+++ b/helper/leo-channel-helper.cc
@@ -230,6 +230,8 @@ LeoChannelHelper::EnableAsciiInternal (
 NetDeviceContainer
 LeoChannelHelper::Install (std::vector<Ptr<Node> > &satellites, std::vector<Ptr<Node> > &stations)
 {
+  NS_LOG_FUNCTION (this);
+
   Ptr<LeoMockChannel> channel = m_channelFactory.Create<LeoMockChannel> ();
 
   NetDeviceContainer container;
@@ -243,6 +245,8 @@ LeoChannelHelper::Install (std::vector<Ptr<Node> > &satellites, std::vector<Ptr<
     dev->SetQueue (queue);
     dev->Attach (channel);
     container.Add (dev);
+
+    NS_LOG_DEBUG ("Added device for node " << node->GetId ());
   }
 
   for (Ptr<Node> node : stations)
@@ -254,6 +258,8 @@ LeoChannelHelper::Install (std::vector<Ptr<Node> > &satellites, std::vector<Ptr<
     dev->SetQueue (queue);
     dev->Attach (channel);
     container.Add (dev);
+
+    NS_LOG_DEBUG ("Added device for node " << node->GetId ());
   }
 
   return container;
@@ -270,15 +276,19 @@ LeoChannelHelper::Install (NodeContainer &satellites, NodeContainer &stations)
 NetDeviceContainer
 LeoChannelHelper::Install (std::vector<std::string> &satellites, std::vector<std::string> &stations)
 {
+  NS_LOG_FUNCTION (this);
+
   std::vector<Ptr<Node> > sats;
   std::vector<Ptr<Node> > stats;
   for (std::string name : satellites)
   {
+    NS_LOG_DEBUG ("Adding node " << name);
     Ptr<Node> node = Names::Find<Node>(name);
     sats.push_back (node);
   }
   for (std::string name : stations)
   {
+    NS_LOG_DEBUG ("Adding node " << name);
     Ptr<Node> node = Names::Find<Node>(name);
     stats.push_back (node);
   }
diff --git a/helper/leo-helper.cc b/helper/leo-helper.cc
index aa584de..5fe2cc7 100644
--- a/helper/leo-helper.cc
+++ b/helper/leo-helper.cc
@@ -29,17 +29,17 @@ LeoHelper::Install (NodeContainer &satellites, NodeContainer &gateways, NodeCont
   m_stackHelper.Install (gateways);
   m_stackHelper.Install (terminals);
 
-  // Make all networks addressable
-  Ipv6AddressHelper address;
-  Ipv6InterfaceContainer islAddrs = address.Assign (islNet);
-  Ipv6InterfaceContainer gwAddrs = address.Assign (gwNet);
-  Ipv6InterfaceContainer utAddrs = address.Assign (utNet);
+  //// Make all networks addressable
+  //Ipv6AddressHelper address;
+  //Ipv6InterfaceContainer islAddrs = address.Assign (islNet);
+  //Ipv6InterfaceContainer gwAddrs = address.Assign (gwNet);
+  //Ipv6InterfaceContainer utAddrs = address.Assign (utNet);
 
-  // Pre-fill the ND caches of networks
-  NdCacheHelper ndCache;
-  ndCache.Install (islNet, islAddrs);
-  ndCache.Install (gwNet, gwAddrs);
-  ndCache.Install (utNet, utAddrs);
+  //// Pre-fill the ND caches of networks
+  //NdCacheHelper ndCache;
+  //ndCache.Install (islNet, islAddrs);
+  //ndCache.Install (gwNet, gwAddrs);
+  //ndCache.Install (utNet, utAddrs);
 
   // Make all networks addressable for legacy protocol
   Ipv4AddressHelper legacy;
diff --git a/helper/nd-cache-helper.cc b/helper/nd-cache-helper.cc
index 882db2f..96198e4 100644
--- a/helper/nd-cache-helper.cc
+++ b/helper/nd-cache-helper.cc
@@ -3,6 +3,7 @@
 #include "ns3/core-module.h"
 #include "ns3/network-module.h"
 #include "ns3/internet-module.h"
+#include "ns3/log.h"
 #include "../model/leo-mock-net-device.h"
 
 #include "nd-cache-helper.h"
@@ -10,9 +11,13 @@
 namespace ns3
 {
 
+NS_LOG_COMPONENT_DEFINE ("NdCacheHelper");
+
 void
 NdCacheHelper::Install (NetDeviceContainer &devices, Ipv6InterfaceContainer &interfaces) const
 {
+  NS_LOG_FUNCTION (this);
+
   // prepare NDS cache
   for (uint32_t i = 0; i < devices.GetN (); i++)
     {
@@ -47,6 +52,8 @@ NdCacheHelper::Install (NetDeviceContainer &devices, Ipv6InterfaceContainer &int
               entry = cache->Add (ipaddr);
             }
           entry->SetMacAddress (address);
+
+	  NS_LOG_DEBUG ("Added entry for " << address);
         }
     }
 }
diff --git a/helper/satellite-node-helper.cc b/helper/satellite-node-helper.cc
index 16a6f96..8cdd4b0 100644
--- a/helper/satellite-node-helper.cc
+++ b/helper/satellite-node-helper.cc
@@ -28,7 +28,7 @@ LeoSatNodeHelper::SetAttribute (string name, const AttributeValue &value)
 NodeContainer
 LeoSatNodeHelper::Install (vector<string> &wpFiles)
 {
-  NS_LOG_FUNCTION (wpFiles);
+  NS_LOG_FUNCTION (this);
 
   NodeContainer nodes;
   for (size_t i = 0; i < wpFiles.size (); i ++)
diff --git a/model/isl-mock-channel.cc b/model/isl-mock-channel.cc
index 76fd592..1bc86b1 100644
--- a/model/isl-mock-channel.cc
+++ b/model/isl-mock-channel.cc
@@ -74,6 +74,7 @@ IslMockChannel::TransmitStart (
           {
             if (i == srcId) continue;
             dst = DynamicCast<MockNetDevice> (GetDevice (i));
+            // TODO check LOS using mobility model
             Deliver (p, src, dst, txTime);
           }
         return true;
diff --git a/model/leo-mock-channel.cc b/model/leo-mock-channel.cc
index 004ab6c..75d4f9d 100644
--- a/model/leo-mock-channel.cc
+++ b/model/leo-mock-channel.cc
@@ -41,6 +41,8 @@ LeoMockChannel::TransmitStart (Ptr<const Packet> p,
                           Address dst,
                           Time txTime)
 {
+  NS_LOG_FUNCTION (this << p << devId << dst << txTime);
+
   // Find devices joined to channel
   Ptr<MockNetDevice> srcDev = DynamicCast<MockNetDevice> (GetDevice (devId));
   if (srcDev == 0)
@@ -50,36 +52,29 @@ LeoMockChannel::TransmitStart (Ptr<const Packet> p,
     }
 
   bool fromGround = m_groundDevices.find (srcDev->GetAddress ()) != m_groundDevices.end ();
+  bool fromSpace = m_satelliteDevices.find (srcDev->GetAddress ()) != m_satelliteDevices.end ();
 
+  NS_ASSERT_MSG (!(fromGround && fromSpace), "Source device can not be both on ground and in space");
+
+  DeviceIndex *dests;
   if (fromGround)
     {
-      // Satellites can always directly be addresses
-      // Assume beams are narrow enough to not also receive the signal at other
-      // satellites for performance reasons.
-      DeviceIndex::iterator it = m_satelliteDevices.find (dst);
-      if (it == m_satelliteDevices.end ())
-        {
-          NS_LOG_ERROR ("unable to find satellite with address " << dst);
-          return false;
-        }
-      return Deliver (p, srcDev, it->second, txTime);
+      dests = &m_satelliteDevices;
     }
-  else if (srcDev->IsBroadcast () || srcDev->IsMulticast ())
-    // space to ground delivers to everything within the beam
+  else if (fromSpace)
     {
-      DeviceIndex::iterator it = m_groundDevices.find (dst);
-      if (it == m_groundDevices.end ())
-        {
-          NS_LOG_ERROR ("unable to find satellite with address " << dst);
-          return false;
-        }
-      for (DeviceIndex::iterator it = m_groundDevices.begin ();
-           it != m_groundDevices.end ();
-           it++)
-        {
-          // TODO deliver only to devices in the same beam
-          Deliver (p, srcDev, it->second, txTime);
-        }
+      dests = &m_groundDevices;
+    }
+  else
+    {
+      NS_LOG_ERROR ("unable to find satellite with address " << dst);
+      return false;
+    }
+
+  for (DeviceIndex::iterator it = dests->begin (); it != dests->end (); it ++)
+    {
+      // TODO deliver only to devices in the same beam
+      Deliver (p, srcDev, it->second, txTime);
     }
 
   return true;
diff --git a/model/mock-channel.cc b/model/mock-channel.cc
index 2e20166..4dde7e9 100644
--- a/model/mock-channel.cc
+++ b/model/mock-channel.cc
@@ -115,7 +115,7 @@ MockChannel::GetDevice (std::size_t i) const
 Time
 MockChannel::GetDelay (Ptr<const MockNetDevice> src, Ptr<const MockNetDevice> dst, Time txTime) const
 {
-  NS_LOG_DEBUG ("Get delay from " << src << " to " << dst);
+  NS_LOG_FUNCTION (this << src << dst << txTime);
 
   Ptr<MobilityModel> modSrc = src->GetNode ()->GetObject<MobilityModel> ();
   Ptr<MobilityModel> modDst = dst->GetNode ()->GetObject<MobilityModel> ();
@@ -160,29 +160,19 @@ MockChannel::Deliver (
     Time txTime)
 {
   NS_LOG_FUNCTION (this << p << src->GetAddress () << dst->GetAddress () << txTime);
+
   Time delay = GetDelay (src, dst, txTime);
 
-  /* Check if there is LOS between the source and destination */
-  if (GetPropagationLoss ()->CalcRxPower(1, src->GetNode ()->GetObject<MobilityModel> (), dst->GetNode ()->GetObject<MobilityModel> ()) > 0)
-  {
-    Simulator::ScheduleWithContext (dst->GetNode ()->GetId (),
-        delay,
-        &MockNetDevice::Receive,
-        dst,
-        p->Copy (),
-        src);
+  Simulator::ScheduleWithContext (dst->GetNode ()->GetId (),
+        			  delay,
+        			  &MockNetDevice::Receive,
+        			  dst,
+        			  p->Copy (),
+        			  src);
 
-    // Call the tx anim callback on the net device
-    m_txrxMock (p, src, dst, txTime, delay);
-    return true;
-  }
-  else
-  {
-    NS_LOG_LOGIC (dst << " unreachable from " << src);
-
-    return false;
-  }
+  // Call the tx anim callback on the net device
+  m_txrxMock (p, src, dst, txTime, delay);
+  return true;
 }
 
-
 } // namespace ns3
diff --git a/test/ground-node-helper-test-suite.cc b/test/ground-node-helper-test-suite.cc
index bf9e4f2..8ae1b3f 100644
--- a/test/ground-node-helper-test-suite.cc
+++ b/test/ground-node-helper-test-suite.cc
@@ -30,7 +30,6 @@ EmptyGndNodeHelperTestCase::EmptyGndNodeHelperTestCase ()
 
 EmptyGndNodeHelperTestCase::~EmptyGndNodeHelperTestCase ()
 {
-  Simulator::Destroy ();
 }
 
 void
@@ -63,7 +62,6 @@ SomeGndNodeHelperTestCase::SomeGndNodeHelperTestCase ()
 
 SomeGndNodeHelperTestCase::~SomeGndNodeHelperTestCase ()
 {
-  Simulator::Destroy ();
 }
 
 void
diff --git a/test/isl-test-suite.cc b/test/isl-test-suite.cc
index 4df4be6..1d10439 100644
--- a/test/isl-test-suite.cc
+++ b/test/isl-test-suite.cc
@@ -28,7 +28,6 @@ IslIcmpTestCase::IslIcmpTestCase ()
 
 IslIcmpTestCase::~IslIcmpTestCase ()
 {
-  Simulator::Destroy ();
 }
 
 void
@@ -80,6 +79,7 @@ IslIcmpTestCase::DoRun (void)
   serverApps.Stop (Seconds (10.0));
 
   Simulator::Run ();
+  Simulator::Destroy ();
 }
 
 class IslTestSuite : public TestSuite
diff --git a/test/leo-test-suite.cc b/test/leo-test-suite.cc
index 7789f6f..5ff244e 100644
--- a/test/leo-test-suite.cc
+++ b/test/leo-test-suite.cc
@@ -30,13 +30,12 @@ LeoTestCase1::LeoTestCase1 ()
 
 LeoTestCase1::~LeoTestCase1 ()
 {
-  Simulator::Destroy ();
 }
 
 void
 LeoTestCase1::DoRun (void)
 {
-  Time::SetResolution (Time::NS);
+  Time::SetResolution (Time::MS);
 
   std::vector<std::string> satWps =
     {
@@ -52,38 +51,78 @@ LeoTestCase1::DoRun (void)
   NodeContainer gateways = gndHelper.Install ("contrib/leo/data/test/ground-stations.txt");
   NodeContainer terminals = gndHelper.Install ("contrib/leo/data/test/ground-stations.txt");
 
-  LeoHelper leo;
-  leo.SetDeviceAttribute ("DataRate", StringValue ("10Mbps"));
-  leo.SetChannelAttribute ("PropagationDelay", StringValue ("ns3::ConstantSpeedPropagationDelayModel"));
-  AodvHelper aodv;
-  leo.SetRoutingHelper (aodv);
+  NetDeviceContainer islNet, gwNet, utNet;
 
-  NetDeviceContainer allDevices = leo.Install (satellites, gateways, terminals);
+  IslHelper islCh;
+  islCh.SetDeviceAttribute ("DataRate", StringValue ("10Mbps"));
+  islCh.SetDeviceAttribute ("ReceiveErrorModel", StringValue ("ns3::BurstErrorModel"));
+  islCh.SetChannelAttribute ("PropagationDelay", StringValue ("ns3::ConstantSpeedPropagationDelayModel"));
+  //// TODO propagation loss from mobility model
+  islCh.SetChannelAttribute ("PropagationLoss", StringValue ("ns3::RangePropagationLossModel"));
+  islNet = islCh.Install (satellites);
+
+  LeoChannelHelper gwCh;
+  gwCh.SetGndDeviceAttribute ("DataRate", StringValue ("10Mbps"));
+  gwCh.SetGndDeviceAttribute ("ReceiveErrorModel", StringValue ("ns3::BurstErrorModel"));
+  gwCh.SetSatDeviceAttribute ("DataRate", StringValue ("10Mbps"));
+  gwCh.SetSatDeviceAttribute ("ReceiveErrorModel", StringValue ("ns3::BurstErrorModel"));
+  gwCh.SetChannelAttribute ("PropagationDelay", StringValue ("ns3::ConstantSpeedPropagationDelayModel"));
+  // TODO propagation loss from mobility model
+  gwCh.SetChannelAttribute ("PropagationLoss", StringValue ("ns3::RangePropagationLossModel"));
+  gwNet = gwCh.Install (satellites, gateways);
+
+  LeoChannelHelper utCh;
+  utCh.SetGndDeviceAttribute ("DataRate", StringValue ("10Mbps"));
+  utCh.SetGndDeviceAttribute ("ReceiveErrorModel", StringValue ("ns3::BurstErrorModel"));
+  utCh.SetSatDeviceAttribute ("DataRate", StringValue ("10Mbps"));
+  utCh.SetSatDeviceAttribute ("ReceiveErrorModel", StringValue ("ns3::BurstErrorModel"));
+  utCh.SetChannelAttribute ("PropagationDelay", StringValue ("ns3::ConstantSpeedPropagationDelayModel"));
+  // TODO propagation loss from mobility model
+  utCh.SetChannelAttribute ("PropagationLoss", StringValue ("ns3::RangePropagationLossModel"));
+  utNet = utCh.Install (satellites, terminals);
+
+  // Install internet stack on nodes
+  InternetStackHelper stack;
+  AodvHelper aodv;
+  stack.SetRoutingHelper (aodv);
+  stack.Install (satellites);
+  stack.Install (gateways);
+  stack.Install (terminals);
+
+  // Make all networks addressable for legacy protocol
+  Ipv4AddressHelper ipv4;
+  ipv4.SetBase ("10.1.0.0", "255.255.0.0");
+  Ipv4InterfaceContainer islIp = ipv4.Assign (islNet);
+  ipv4.SetBase ("10.2.0.0", "255.255.0.0");
+  Ipv4InterfaceContainer gwIp = ipv4.Assign (gwNet);
+  ipv4.SetBase ("10.3.0.0", "255.255.0.0");
+  Ipv4InterfaceContainer utIp = ipv4.Assign (utNet);
+
+  ArpCacheHelper arpCache;
+  arpCache.Install (islNet, islIp);
+  arpCache.Install (gwNet, gwIp);
+  arpCache.Install (utNet, utIp);
 
   // we want to ping terminals
   UdpEchoServerHelper echoServer (9);
   ApplicationContainer serverApps = echoServer.Install (terminals);
 
-  // install a client on each of the terminals
+  // install a client on one of the terminals
   ApplicationContainer clientApps;
-  for (uint32_t i = 1; i < terminals.GetN (); i++)
-    {
-      Address remote = terminals.Get (i)->GetObject<Ipv4> ()->GetAddress (1, 0).GetLocal ();
-      UdpEchoClientHelper echoClient (remote, 9);
-      echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
-      echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
-      echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
-
-      clientApps.Add (echoClient.Install (terminals.Get (i-1)));
-    }
-
-  clientApps.Start (Seconds (2.0));
-  clientApps.Stop (Seconds (10.0));
+  Address remote = utIp.GetAddress (1, 0);
+  UdpEchoClientHelper echoClient (remote, 9);
+  echoClient.SetAttribute ("MaxPackets", UintegerValue (3));
+  echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
+  echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
+  clientApps.Add (echoClient.Install (terminals.Get (0)));
 
   serverApps.Start (Seconds (1.0));
-  serverApps.Stop (Seconds (10.0));
+  clientApps.Start (Seconds (2.0));
+  clientApps.Stop (Seconds (9.0));
+  serverApps.Stop (Seconds (10));
 
   Simulator::Run ();
+  Simulator::Destroy ();
 }
 
 // The TestSuite class names the TestSuite, identifies what type of TestSuite,
diff --git a/test/satellite-node-helper-test-suite.cc b/test/satellite-node-helper-test-suite.cc
index 506da25..51bcc0c 100644
--- a/test/satellite-node-helper-test-suite.cc
+++ b/test/satellite-node-helper-test-suite.cc
@@ -29,7 +29,6 @@ EmptySatNodeHelperTestCase::EmptySatNodeHelperTestCase ()
 
 EmptySatNodeHelperTestCase::~EmptySatNodeHelperTestCase ()
 {
-  Simulator::Destroy ();
 }
 
 void
@@ -62,7 +61,6 @@ SingleSatNodeHelperTestCase::SingleSatNodeHelperTestCase ()
 
 SingleSatNodeHelperTestCase::~SingleSatNodeHelperTestCase ()
 {
-  Simulator::Destroy ();
 }
 
 void
diff --git a/wscript b/wscript
index 2229324..4030384 100644
--- a/wscript
+++ b/wscript
@@ -12,7 +12,6 @@ def build(bld):
         'helper/arp-cache-helper.cc',
         'helper/isl-helper.cc',
         'helper/leo-channel-helper.cc',
-        'helper/leo-helper.cc',
         'helper/nd-cache-helper.cc',
         'helper/ground-node-helper.cc',
         'helper/satellite-node-helper.cc',
@@ -45,7 +44,6 @@ def build(bld):
         'helper/arp-cache-helper.h',
         'helper/isl-helper.h',
         'helper/leo-channel-helper.h',
-        'helper/leo-helper.h',
         'helper/nd-cache-helper.h',
         'helper/ground-node-helper.h',
         'helper/satellite-node-helper.h',