a topology class to read ns2's mobility files and a mobility generator.
This commit is contained in:
@@ -257,6 +257,7 @@ node.add_sources ([
|
||||
'random-walk-mobility-model.cc',
|
||||
'random-direction-mobility-model.cc',
|
||||
'hierarchical-mobility-model.cc',
|
||||
'ns2-mobility-file-topology.cc',
|
||||
])
|
||||
node.add_inst_headers ([
|
||||
'node.h',
|
||||
@@ -283,6 +284,7 @@ node.add_inst_headers ([
|
||||
'random-walk-mobility-model.h',
|
||||
'random-direction-mobility-model.h',
|
||||
'hierarchical-mobility-model.h',
|
||||
'ns2-mobility-file-topology.h',
|
||||
])
|
||||
|
||||
applications = build.Ns3Module ('applications', 'src/applications')
|
||||
@@ -378,6 +380,12 @@ p2p.add_inst_headers ([
|
||||
|
||||
|
||||
# utils
|
||||
mobgen = build.Ns3Module ('mobility-generator', 'utils')
|
||||
ns3.add (mobgen)
|
||||
mobgen.set_executable ()
|
||||
mobgen.add_deps (['simulator', 'node'])
|
||||
mobgen.add_source ('mobility-generator.cc')
|
||||
|
||||
run_tests = build.Ns3Module('run-tests', 'utils')
|
||||
ns3.add(run_tests)
|
||||
run_tests.set_executable()
|
||||
|
||||
139
src/node/ns2-mobility-file-topology.cc
Normal file
139
src/node/ns2-mobility-file-topology.cc
Normal file
@@ -0,0 +1,139 @@
|
||||
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
|
||||
/*
|
||||
* Copyright (c) 2007 INRIA
|
||||
* All rights reserved.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License version 2 as
|
||||
* published by the Free Software Foundation;
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*
|
||||
* Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
|
||||
*/
|
||||
#include "ns3/debug.h"
|
||||
#include "ns3/simulator.h"
|
||||
#include "ns2-mobile-file-topology.h"
|
||||
#include "node-list.h"
|
||||
#include "node.h"
|
||||
#include "static-speed-mobility-model.h"
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
|
||||
NS_DEBUG_COMPONENT_DEFINE ("Ns2MobileFileTopology");
|
||||
|
||||
namespace ns3 {
|
||||
|
||||
|
||||
Ns2MobileFileTopology::Ns2MobileFileTopology (std::string filename)
|
||||
: m_filename (filename)
|
||||
{}
|
||||
|
||||
|
||||
Ptr<StaticSpeedMobilityModel>
|
||||
Ns2MobileFileTopology::GetMobilityModel (std::string idString, const ObjectStore &store) const
|
||||
{
|
||||
std::istringstream iss;
|
||||
iss.str (idString);
|
||||
uint32_t id;
|
||||
iss >> id;
|
||||
Ptr<Object> object = store.Get (id);
|
||||
if (object == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
Ptr<StaticSpeedMobilityModel> model =
|
||||
object->QueryInterface<StaticSpeedMobilityModel> (StaticSpeedMobilityModel::iid);
|
||||
if (model == 0)
|
||||
{
|
||||
model = Create<StaticSpeedMobilityModel> ();
|
||||
object->AddInterface (model);
|
||||
}
|
||||
return model;
|
||||
}
|
||||
|
||||
double
|
||||
Ns2MobileFileTopology::ReadDouble (std::string valueString) const
|
||||
{
|
||||
std::istringstream iss;
|
||||
iss.str (valueString);
|
||||
double value;
|
||||
iss >> value;
|
||||
return value;
|
||||
}
|
||||
|
||||
void
|
||||
Ns2MobileFileTopology::LayoutObjectStore (const ObjectStore &store) const
|
||||
{
|
||||
std::ifstream file (m_filename.c_str (), std::ios::in);
|
||||
if (file.is_open())
|
||||
{
|
||||
while (!file.eof() )
|
||||
{
|
||||
std::string line;
|
||||
getline (file, line);
|
||||
std::string::size_type startNodeId = line.find_first_of ("(");
|
||||
std::string::size_type endNodeId = line.find_first_of (")");
|
||||
if (startNodeId == std::string::npos ||
|
||||
endNodeId == std::string::npos)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
Ptr<StaticSpeedMobilityModel> model = GetMobilityModel (line.substr (startNodeId + 1,
|
||||
endNodeId - startNodeId),
|
||||
store);
|
||||
if (model == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (startNodeId == 6)
|
||||
{
|
||||
double value = ReadDouble (line.substr (endNodeId + 9, std::string::npos));
|
||||
std::string coordinate = line.substr (endNodeId + 6, 1);
|
||||
if (coordinate == "X")
|
||||
{
|
||||
model->SetX (value);
|
||||
NS_DEBUG ("X=" << value);
|
||||
}
|
||||
else if (coordinate == "Y")
|
||||
{
|
||||
model->SetY (value);
|
||||
NS_DEBUG ("Y=" << value);
|
||||
}
|
||||
else if (coordinate == "Z")
|
||||
{
|
||||
model->SetZ (value);
|
||||
NS_DEBUG ("Z=" << value);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
double at = ReadDouble (line.substr (8, startNodeId - 17));
|
||||
std::string::size_type xSpeedEnd = line.find_first_of (" ", endNodeId + 10);
|
||||
std::string::size_type ySpeedEnd = line.find_first_of (" ", xSpeedEnd + 1);
|
||||
double xSpeed = ReadDouble (line.substr (endNodeId + 10, xSpeedEnd - endNodeId - 10));
|
||||
double ySpeed = ReadDouble (line.substr (xSpeedEnd + 1, ySpeedEnd - xSpeedEnd - 1));
|
||||
double zSpeed = ReadDouble (line.substr (ySpeedEnd + 1, std::string::npos));
|
||||
NS_DEBUG ("at=" << at << "xSpeed=" << xSpeed << ", ySpeed=" << ySpeed << ", zSpeed=" << zSpeed);
|
||||
Simulator::Schedule (Seconds (at), &StaticSpeedMobilityModel::SetSpeed, model,
|
||||
xSpeed, ySpeed, zSpeed);
|
||||
}
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
Ns2MobileFileTopology::Layout (void) const
|
||||
{
|
||||
Layout (NodeList::Begin (), NodeList::End ());
|
||||
}
|
||||
|
||||
} // namespace ns3
|
||||
87
src/node/ns2-mobility-file-topology.h
Normal file
87
src/node/ns2-mobility-file-topology.h
Normal file
@@ -0,0 +1,87 @@
|
||||
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
|
||||
/*
|
||||
* Copyright (c) 2007 INRIA
|
||||
* All rights reserved.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License version 2 as
|
||||
* published by the Free Software Foundation;
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*
|
||||
* Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
|
||||
*/
|
||||
#ifndef NS2_MOBILE_FILE_TOPOLOGY_H
|
||||
#define NS2_MOBILE_FILE_TOPOLOGY_H
|
||||
|
||||
#include <string>
|
||||
#include <stdint.h>
|
||||
#include "ns3/ptr.h"
|
||||
#include "ns3/object.h"
|
||||
#include "static-speed-mobility-model.h"
|
||||
|
||||
namespace ns3 {
|
||||
|
||||
class Ns2MobileFileTopology
|
||||
{
|
||||
public:
|
||||
Ns2MobileFileTopology (std::string filename);
|
||||
|
||||
void Layout (void) const;
|
||||
template <typename T>
|
||||
void Layout (T begin, T end) const;
|
||||
private:
|
||||
class ObjectStore
|
||||
{
|
||||
public:
|
||||
virtual ~ObjectStore () {}
|
||||
virtual Ptr<Object> Get (uint32_t i) const = 0;
|
||||
};
|
||||
void LayoutObjectStore (const ObjectStore &store) const;
|
||||
Ptr<StaticSpeedMobilityModel> GetMobilityModel (std::string idString, const ObjectStore &store) const;
|
||||
double ReadDouble (std::string valueString) const;
|
||||
std::string m_filename;
|
||||
};
|
||||
|
||||
} // namespace ns3
|
||||
|
||||
namespace ns3 {
|
||||
|
||||
template <typename T>
|
||||
void
|
||||
Ns2MobileFileTopology::Layout (T begin, T end) const
|
||||
{
|
||||
class MyObjectStore : public ObjectStore
|
||||
{
|
||||
public:
|
||||
MyObjectStore (T begin, T end)
|
||||
: m_begin (begin),
|
||||
m_end (end)
|
||||
{}
|
||||
virtual Ptr<Object> Get (uint32_t i) const {
|
||||
T iterator = m_begin;
|
||||
iterator += i;
|
||||
if (iterator >= m_end)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return *iterator;
|
||||
}
|
||||
private:
|
||||
T m_begin;
|
||||
T m_end;
|
||||
};
|
||||
LayoutObjectStore (MyObjectStore (begin, end));
|
||||
}
|
||||
|
||||
|
||||
} // namespace ns3
|
||||
|
||||
#endif /* NS2_MOBILE_FILE_TOPOLOGY_H */
|
||||
67
utils/mobility-generator.cc
Normal file
67
utils/mobility-generator.cc
Normal file
@@ -0,0 +1,67 @@
|
||||
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
|
||||
/*
|
||||
* Copyright (c) 2007 INRIA
|
||||
* All rights reserved.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License version 2 as
|
||||
* published by the Free Software Foundation;
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*
|
||||
* Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
|
||||
*/
|
||||
|
||||
#include "ns3/ns2-mobile-file-topology.h"
|
||||
#include "ns3/object.h"
|
||||
#include "ns3/simulator.h"
|
||||
#include "ns3/mobility-model-notifier.h"
|
||||
#include <iostream>
|
||||
|
||||
using namespace ns3;
|
||||
|
||||
static void
|
||||
CourseChange (Ptr<const MobilityModel> position)
|
||||
{
|
||||
Position pos = position->Get ();
|
||||
std::cout << Simulator::Now () << ", pos=" << position << ", x=" << pos.x << ", y=" << pos.y
|
||||
<< ", z=" << pos.z << std::endl;
|
||||
}
|
||||
|
||||
int main (int argc, char *argv[])
|
||||
{
|
||||
std::vector<Ptr<Object> > objects;
|
||||
while (argc > 0)
|
||||
{
|
||||
if (strncmp (*argv, "--n=", strlen ("--n=")) == 0)
|
||||
{
|
||||
uint32_t n = atoi (*argv + strlen ("--n="));
|
||||
for (uint32_t i = 0; i < n; i++)
|
||||
{
|
||||
Ptr<MobilityModelNotifier> notifier = Create<MobilityModelNotifier> ();
|
||||
notifier->RegisterListener (MakeCallback (&CourseChange));
|
||||
objects.push_back (notifier);
|
||||
}
|
||||
}
|
||||
else if (strncmp (*argv, "--ns2-topology=",
|
||||
strlen ("--ns2-topology=")) == 0)
|
||||
{
|
||||
const char *filename = *argv + strlen ("--ns2-topology=");
|
||||
Ns2MobileFileTopology topology (filename);
|
||||
topology.Layout (objects.begin (), objects.end ());
|
||||
}
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
Simulator::Run ();
|
||||
Simulator::Destroy ();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user