From 24458b4bf11937b461c103df2cec3b09f649a04a Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Tue, 17 Jul 2007 10:47:25 +0200 Subject: [PATCH] a topology class to read ns2's mobility files and a mobility generator. --- SConstruct | 8 ++ src/node/ns2-mobility-file-topology.cc | 139 +++++++++++++++++++++++++ src/node/ns2-mobility-file-topology.h | 87 ++++++++++++++++ utils/mobility-generator.cc | 67 ++++++++++++ 4 files changed, 301 insertions(+) create mode 100644 src/node/ns2-mobility-file-topology.cc create mode 100644 src/node/ns2-mobility-file-topology.h create mode 100644 utils/mobility-generator.cc diff --git a/SConstruct b/SConstruct index db7883eff..5cb86153a 100644 --- a/SConstruct +++ b/SConstruct @@ -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() diff --git a/src/node/ns2-mobility-file-topology.cc b/src/node/ns2-mobility-file-topology.cc new file mode 100644 index 000000000..cc0b135eb --- /dev/null +++ b/src/node/ns2-mobility-file-topology.cc @@ -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 + */ +#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 +#include + +NS_DEBUG_COMPONENT_DEFINE ("Ns2MobileFileTopology"); + +namespace ns3 { + + +Ns2MobileFileTopology::Ns2MobileFileTopology (std::string filename) + : m_filename (filename) +{} + + +Ptr +Ns2MobileFileTopology::GetMobilityModel (std::string idString, const ObjectStore &store) const +{ + std::istringstream iss; + iss.str (idString); + uint32_t id; + iss >> id; + Ptr object = store.Get (id); + if (object == 0) + { + return 0; + } + Ptr model = + object->QueryInterface (StaticSpeedMobilityModel::iid); + if (model == 0) + { + model = Create (); + 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 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 diff --git a/src/node/ns2-mobility-file-topology.h b/src/node/ns2-mobility-file-topology.h new file mode 100644 index 000000000..7d08bad26 --- /dev/null +++ b/src/node/ns2-mobility-file-topology.h @@ -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 + */ +#ifndef NS2_MOBILE_FILE_TOPOLOGY_H +#define NS2_MOBILE_FILE_TOPOLOGY_H + +#include +#include +#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 + void Layout (T begin, T end) const; +private: + class ObjectStore + { + public: + virtual ~ObjectStore () {} + virtual Ptr Get (uint32_t i) const = 0; + }; + void LayoutObjectStore (const ObjectStore &store) const; + Ptr GetMobilityModel (std::string idString, const ObjectStore &store) const; + double ReadDouble (std::string valueString) const; + std::string m_filename; +}; + +} // namespace ns3 + +namespace ns3 { + +template +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 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 */ diff --git a/utils/mobility-generator.cc b/utils/mobility-generator.cc new file mode 100644 index 000000000..2eb4a9a2d --- /dev/null +++ b/utils/mobility-generator.cc @@ -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 + */ + +#include "ns3/ns2-mobile-file-topology.h" +#include "ns3/object.h" +#include "ns3/simulator.h" +#include "ns3/mobility-model-notifier.h" +#include + +using namespace ns3; + +static void +CourseChange (Ptr 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 > 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 notifier = Create (); + 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; +}