From 9ead83462b5e5d9e4039f1a4da41144a28099ba6 Mon Sep 17 00:00:00 2001 From: Tim Schubert Date: Mon, 6 Jul 2020 17:25:22 +0200 Subject: [PATCH] Add wrapper for waypoints file --- data/empty | 0 .../leo-input-fstream-container-test-suite.cc | 54 +++++++++ utils/leo-input-fstream-container.cc | 103 ++++++++++++++++++ utils/leo-input-fstream-container.h | 42 +++++++ wscript | 3 + 5 files changed, 202 insertions(+) create mode 100644 data/empty create mode 100644 test/leo-input-fstream-container-test-suite.cc create mode 100644 utils/leo-input-fstream-container.cc create mode 100644 utils/leo-input-fstream-container.h diff --git a/data/empty b/data/empty new file mode 100644 index 0000000..e69de29 diff --git a/test/leo-input-fstream-container-test-suite.cc b/test/leo-input-fstream-container-test-suite.cc new file mode 100644 index 0000000..11533a8 --- /dev/null +++ b/test/leo-input-fstream-container-test-suite.cc @@ -0,0 +1,54 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ + +#include "ns3/leo-module.h" +#include "ns3/test.h" + +using namespace ns3; + +class LeoFileInputTestCase : public TestCase +{ +public: + LeoFileInputTestCase (); + virtual ~LeoFileInputTestCase (); + +private: + virtual void DoRun (void); +}; + +LeoFileInputTestCase::LeoFileInputTestCase () + : TestCase ("Test reading from empty file") +{ +} + +LeoFileInputTestCase::~LeoFileInputTestCase () +{ +} + +void +LeoFileInputTestCase::DoRun (void) +{ + Ptr container = CreateObject (); + container->SetAttribute("File", StringValue ("contrib/leo/data/empty")); + container->SetAttribute("LastTime", TimeValue (Time (1))); + Waypoint wp; + + bool res = container->GetNextSample(wp); + + NS_TEST_ASSERT_MSG_EQ (res, false, "Reading from empty stream fails"); +} + +class LeoWaypointsTestSuite : public TestSuite +{ +public: + LeoWaypointsTestSuite (); +}; + +LeoWaypointsTestSuite::LeoWaypointsTestSuite () + : TestSuite ("leo-waypoints", UNIT) +{ + // TestDuration for TestCase can be QUICK, EXTENSIVE or TAKES_FOREVER + AddTestCase (new LeoFileInputTestCase, TestCase::QUICK); +} + +// Do not forget to allocate an instance of this TestSuite +static LeoWaypointsTestSuite leoWaypointsTestSuite; diff --git a/utils/leo-input-fstream-container.cc b/utils/leo-input-fstream-container.cc new file mode 100644 index 0000000..8c4365b --- /dev/null +++ b/utils/leo-input-fstream-container.cc @@ -0,0 +1,103 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ + +#include "ns3/string.h" +#include "ns3/time-data-calculators.h" +#include "leo-input-fstream-container.h" + +namespace ns3 +{ + +TypeId +LeoWaypointInputFileStreamContainer::GetTypeId (void) +{ + static TypeId tid = TypeId ("ns3::LeoWaypointInputFileStreamContainer") + .SetGroupName ("Leo") + .SetParent () + .AddConstructor () + .AddAttribute ("File", + "The path to the file opened in the stream", + StringValue (), + MakeStringAccessor (&LeoWaypointInputFileStreamContainer::m_filePath), + MakeStringChecker ()) + .AddAttribute ("LastTime", + "The minimum point in time for returned samples", + TimeValue (), + MakeTimeAccessor (&LeoWaypointInputFileStreamContainer::m_lastTime), + MakeTimeChecker ()) + ; + return tid; +} + +LeoWaypointInputFileStreamContainer::LeoWaypointInputFileStreamContainer () : + m_filePath (), + m_input () +{ +} + +LeoWaypointInputFileStreamContainer::LeoWaypointInputFileStreamContainer (string filePath, Time lastTime) : + m_filePath (filePath), + m_lastTime (lastTime) +{ +} + +LeoWaypointInputFileStreamContainer::~LeoWaypointInputFileStreamContainer () +{ + m_input.close (); +} + +bool +LeoWaypointInputFileStreamContainer::GetNextSample (Waypoint &sample) +{ + if (!m_input.is_open ()) + { + m_input.open (m_filePath); + } + + if (!m_input.is_open ()) + { + NS_ABORT_MSG ("Input stream is not open"); + } + + if (m_input.bad ()) + { + NS_ABORT_MSG ("Input stream is bad"); + } + + while (!m_input.eof () && sample.time < m_lastTime) + { + m_input >> sample; + } + m_lastTime = sample.time; + + return m_input.good (); +} + +void +LeoWaypointInputFileStreamContainer::SetFile (const string path) +{ + m_input.close (); + m_filePath = path; + m_input.open (m_filePath); +} + +string +LeoWaypointInputFileStreamContainer::GetFile () const +{ + return m_filePath; +} + +void +LeoWaypointInputFileStreamContainer::SetLastTime (const Time lastTime) +{ + m_input.clear (); + m_input.seekg (0, std::ios::beg); + m_lastTime = lastTime; +} + +Time +LeoWaypointInputFileStreamContainer::GetLastTime () const +{ + return m_lastTime; +} + +}; diff --git a/utils/leo-input-fstream-container.h b/utils/leo-input-fstream-container.h new file mode 100644 index 0000000..175f82c --- /dev/null +++ b/utils/leo-input-fstream-container.h @@ -0,0 +1,42 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ + +#ifndef LEO_INPUT_FSTREAM_CONTAINER +#define LEO_INPUT_FSTREAM_CONTAINER + +#include +#include "ns3/object.h" +#include "ns3/waypoint.h" + +using namespace std; + +namespace ns3 +{ + +class LeoWaypointInputFileStreamContainer : public Object +{ +public: + static TypeId GetTypeId (void); + + LeoWaypointInputFileStreamContainer (); + virtual ~LeoWaypointInputFileStreamContainer (); + + LeoWaypointInputFileStreamContainer (string filePath, Time lastTime); + + bool GetNextSample (Waypoint &sample); + + void SetFile (const string path); + string GetFile () const; + + void SetLastTime (const Time lastTime); + Time GetLastTime () const; + +private: + string m_filePath; + Time m_lastTime; + ifstream m_input; + +}; + +}; + +#endif diff --git a/wscript b/wscript index e5231a7..8bc4582 100644 --- a/wscript +++ b/wscript @@ -21,6 +21,7 @@ def build(bld): 'model/mock-net-device.cc', 'model/mock-channel.cc', 'helper/nd-cache-helper.cc', + 'utils/leo-input-fstream-container.cc', ] module_test = bld.create_ns3_module_test_library('leo') @@ -29,6 +30,7 @@ def build(bld): 'test/isl-test-suite.cc', 'test/isl-mock-channel-test-suite.cc', 'test/leo-mock-channel-test-suite.cc', + 'test/leo-input-fstream-container-test-suite.cc', ] headers = bld(features='ns3header') @@ -46,6 +48,7 @@ def build(bld): 'model/mock-net-device.h', 'model/mock-channel.h', 'helper/nd-cache-helper.h', + 'utils/leo-input-fstream-container.h', ] if bld.env.ENABLE_EXAMPLES: