/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ /* * Copyright (c) 2008 INRIA * * 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/mobility-helper.h" #include "ns3/mobility-model.h" #include "ns3/position-allocator.h" #include "ns3/hierarchical-mobility-model.h" #include "ns3/log.h" #include "ns3/pointer.h" #include "ns3/config.h" #include "ns3/simulator.h" #include "ns3/names.h" #include "ns3/string.h" #include namespace ns3 { NS_LOG_COMPONENT_DEFINE ("MobilityHelper"); MobilityHelper::MobilityHelper () { m_position = CreateObjectWithAttributes ("X", StringValue ("ns3::ConstantRandomVariable[Constant=0.0]"), "Y", StringValue ("ns3::ConstantRandomVariable[Constant=0.0]")); m_mobility.SetTypeId ("ns3::ConstantPositionMobilityModel"); } MobilityHelper::~MobilityHelper () { } void MobilityHelper::SetPositionAllocator (Ptr allocator) { m_position = allocator; } void MobilityHelper::SetPositionAllocator (std::string type, std::string n1, const AttributeValue &v1, std::string n2, const AttributeValue &v2, std::string n3, const AttributeValue &v3, std::string n4, const AttributeValue &v4, std::string n5, const AttributeValue &v5, std::string n6, const AttributeValue &v6, std::string n7, const AttributeValue &v7, std::string n8, const AttributeValue &v8, std::string n9, const AttributeValue &v9) { ObjectFactory pos; pos.SetTypeId (type); pos.Set (n1, v1); pos.Set (n2, v2); pos.Set (n3, v3); pos.Set (n4, v4); pos.Set (n5, v5); pos.Set (n6, v6); pos.Set (n7, v7); pos.Set (n8, v8); pos.Set (n9, v9); m_position = pos.Create ()->GetObject (); } void MobilityHelper::SetMobilityModel (std::string type, std::string n1, const AttributeValue &v1, std::string n2, const AttributeValue &v2, std::string n3, const AttributeValue &v3, std::string n4, const AttributeValue &v4, std::string n5, const AttributeValue &v5, std::string n6, const AttributeValue &v6, std::string n7, const AttributeValue &v7, std::string n8, const AttributeValue &v8, std::string n9, const AttributeValue &v9) { m_mobility.SetTypeId (type); m_mobility.Set (n1, v1); m_mobility.Set (n2, v2); m_mobility.Set (n3, v3); m_mobility.Set (n4, v4); m_mobility.Set (n5, v5); m_mobility.Set (n6, v6); m_mobility.Set (n7, v7); m_mobility.Set (n8, v8); m_mobility.Set (n9, v9); } void MobilityHelper::PushReferenceMobilityModel (Ptr reference) { Ptr mobility = reference->GetObject (); m_mobilityStack.push_back (mobility); } void MobilityHelper::PushReferenceMobilityModel (std::string referenceName) { Ptr mobility = Names::Find (referenceName); m_mobilityStack.push_back (mobility); } void MobilityHelper::PopReferenceMobilityModel (void) { m_mobilityStack.pop_back (); } std::string MobilityHelper::GetMobilityModelType (void) const { return m_mobility.GetTypeId ().GetName (); } void MobilityHelper::Install (Ptr node) const { Ptr object = node; Ptr model = object->GetObject (); if (model == 0) { model = m_mobility.Create ()->GetObject (); if (model == 0) { NS_FATAL_ERROR ("The requested mobility model is not a mobility model: \""<< m_mobility.GetTypeId ().GetName ()<<"\""); } if (m_mobilityStack.empty ()) { NS_LOG_DEBUG ("node="<AggregateObject (hierarchical); NS_LOG_DEBUG ("node="<flags (saved_flags); os->precision (saved_precision); } void MobilityHelper::EnableAscii (Ptr stream, uint32_t nodeid) { std::ostringstream oss; oss << "/NodeList/" << nodeid << "/$ns3::MobilityModel/CourseChange"; Config::ConnectWithoutContextFailSafe (oss.str (), MakeBoundCallback (&MobilityHelper::CourseChanged, stream)); } void MobilityHelper::EnableAscii (Ptr stream, NodeContainer n) { for (NodeContainer::Iterator i = n.Begin (); i != n.End (); ++i) { EnableAscii (stream, (*i)->GetId ()); } } void MobilityHelper::EnableAsciiAll (Ptr stream) { EnableAscii (stream, NodeContainer::GetGlobal ()); } int64_t MobilityHelper::AssignStreams (NodeContainer c, int64_t stream) { int64_t currentStream = stream; Ptr node; Ptr mobility; for (NodeContainer::Iterator i = c.Begin (); i != c.End (); ++i) { node = (*i); mobility = node->GetObject (); if (mobility) { currentStream += mobility->AssignStreams (currentStream); } } return (currentStream - stream); } double MobilityHelper::GetDistanceSquaredBetween (Ptr n1, Ptr n2) { NS_LOG_FUNCTION_NOARGS (); double distSq = 0.0; Ptr rxPosition = n1->GetObject (); NS_ASSERT (rxPosition != 0); Ptr txPosition = n2->GetObject (); NS_ASSERT (txPosition != 0); double dist = rxPosition -> GetDistanceFrom (txPosition); distSq = dist * dist; return distSq; } } // namespace ns3