Add wrapper for waypoints file

This commit is contained in:
Tim Schubert 2020-07-06 17:25:22 +02:00
parent 8ab0bc023e
commit 9ead83462b
5 changed files with 202 additions and 0 deletions

0
data/empty Normal file
View file

View file

@ -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<LeoWaypointInputFileStreamContainer> container = CreateObject<LeoWaypointInputFileStreamContainer> ();
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;

View file

@ -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<Object> ()
.AddConstructor<LeoWaypointInputFileStreamContainer> ()
.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;
}
};

View file

@ -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 <fstream>
#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

View file

@ -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: