#include "ns3/simulator.h" #include "ns3/random-variable.h" #include "ns3/random-variable-default-value.h" #include "ns3/component-manager.h" #include "random-waypoint-mobility-model.h" #include "random-position.h" namespace ns3 { static RandomVariableDefaultValue g_speed ("RandomWaypointSpeed", "A random variable used to pick the speed of a random waypoint model.", "Uniform:0.3:0.7"); static RandomVariableDefaultValue g_pause ("RandomWaypointPause", "A random variable used to pick the pause of a random waypoint model.", "Constant:2"); static ClassIdDefaultValue g_position ("RandomWaypointPosition", "A random position model used to pick the next waypoint position.", RandomPosition::iid, "RandomPositionRectangle"); const ClassId RandomWaypointMobilityModel::cid = MakeClassId ("RandomWaypointMobilityModel", MobilityModel::iid); RandomWaypointMobilityModelParameters::RandomWaypointMobilityModelParameters () : m_speed (g_speed.GetCopy ()), m_pause (g_pause.GetCopy ()) { m_position = ComponentManager::Create (g_position.GetValue (), RandomPosition::iid); } RandomWaypointMobilityModelParameters::RandomWaypointMobilityModelParameters (Ptr randomPosition, const RandomVariable &speed, const RandomVariable &pause) : m_speed (speed.Copy ()), m_pause (pause.Copy ()), m_position (randomPosition) {} void RandomWaypointMobilityModelParameters::SetWaypointPositionModel (Ptr randomPosition) { m_position = randomPosition; } void RandomWaypointMobilityModelParameters::SetSpeed (const RandomVariable &speed) { delete m_speed; m_speed = speed.Copy (); } void RandomWaypointMobilityModelParameters::SetPause (const RandomVariable &pause) { delete m_pause; m_pause = pause.Copy (); } void RandomWaypointMobilityModelParameters::DoDispose (void) { m_position = 0; delete m_pause; delete m_speed; m_pause = 0; m_speed = 0; } Ptr RandomWaypointMobilityModelParameters::GetCurrent (void) { static Ptr parameters = 0; if (parameters == 0 || g_position.IsDirty () || g_pause.IsDirty () || g_speed.IsDirty ()) { parameters = Create (); } return parameters; } RandomWaypointMobilityModel::RandomWaypointMobilityModel () : m_parameters (RandomWaypointMobilityModelParameters::GetCurrent ()) { Simulator::ScheduleNow (&RandomWaypointMobilityModel::Start, this); } RandomWaypointMobilityModel::RandomWaypointMobilityModel (Ptr parameters) : m_parameters (parameters) { Simulator::ScheduleNow (&RandomWaypointMobilityModel::Start, this); } void RandomWaypointMobilityModel::Start (void) { Position m_current = m_helper.GetCurrentPosition (); Position destination = m_parameters->m_position->Get (); double speed = m_parameters->m_speed->GetValue (); Time pause = Seconds (m_parameters->m_pause->GetValue ()); double dx = (destination.x - m_current.x) * speed; double dy = (destination.y - m_current.y) * speed; double dz = (destination.z - m_current.z) * speed; m_helper.Reset (Speed (dx,dy,dz), pause); Time travelDelay = Seconds (CalculateDistance (destination, m_current) / speed); m_event = Simulator::Schedule (travelDelay + pause, &RandomWaypointMobilityModel::Start, this); } Position RandomWaypointMobilityModel::DoGet (void) const { return m_helper.GetCurrentPosition (); } void RandomWaypointMobilityModel::DoSet (const Position &position) { m_helper.InitializePosition (position); Simulator::Remove (m_event); Simulator::ScheduleNow (&RandomWaypointMobilityModel::Start, this); } Speed RandomWaypointMobilityModel::DoGetSpeed (void) const { return m_helper.GetSpeed (); } } // namespace ns3