From 6ec6318d5fc654382a870482e8f028ddae793737 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 29 Jun 2007 09:41:37 +0200 Subject: [PATCH 001/148] position models --- SConstruct | 8 +++ src/node/notify-static-position.cc | 67 ++++++++++++++++++ src/node/notify-static-position.h | 74 ++++++++++++++++++++ src/node/position.cc | 71 +++++++++++++++++++ src/node/position.h | 92 ++++++++++++++++++++++++ src/node/static-position.cc | 109 +++++++++++++++++++++++++++++ src/node/static-position.h | 95 +++++++++++++++++++++++++ src/node/static-speed-position.cc | 98 ++++++++++++++++++++++++++ src/node/static-speed-position.h | 98 ++++++++++++++++++++++++++ 9 files changed, 712 insertions(+) create mode 100644 src/node/notify-static-position.cc create mode 100644 src/node/notify-static-position.h create mode 100644 src/node/position.cc create mode 100644 src/node/position.h create mode 100644 src/node/static-position.cc create mode 100644 src/node/static-position.h create mode 100644 src/node/static-speed-position.cc create mode 100644 src/node/static-speed-position.h diff --git a/SConstruct b/SConstruct index 44f168c6b..84e9595ba 100644 --- a/SConstruct +++ b/SConstruct @@ -242,6 +242,10 @@ node.add_sources ([ 'udp.cc', 'ipv4.cc', 'application.cc', + 'position.cc', + 'static-position.cc', + 'notify-static-position.cc', + 'static-speed-position.cc', ]) node.add_inst_headers ([ 'node.h', @@ -259,6 +263,10 @@ node.add_inst_headers ([ 'udp.h', 'ipv4.h', 'application.h', + 'position.h', + 'static-position.h', + 'notify-static-position.h', + 'static-speed-position.h', ]) applications = build.Ns3Module ('applications', 'src/applications') diff --git a/src/node/notify-static-position.cc b/src/node/notify-static-position.cc new file mode 100644 index 000000000..76875f137 --- /dev/null +++ b/src/node/notify-static-position.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 "notify-static-position.h" + +namespace ns3 { + +const InterfaceId NotifyStaticPosition::iid = MakeInterfaceId ("NotifyStaticPosition", StaticPosition::iid); + +NotifyStaticPosition::NotifyStaticPosition () + : StaticPosition () +{} +NotifyStaticPosition::NotifyStaticPosition (double x, double y, double z) + : StaticPosition (x, y, z) +{} + +void +NotifyStaticPosition::RegisterListener (Listener listener) +{ + m_listeners.push_back (listener); +} +void +NotifyStaticPosition::UnregisterListener (Listener callback) +{ + for (std::list::iterator i = m_listeners.begin (); + i != m_listeners.end ();) + { + Listener listener = *i; + if (listener.IsEqual (callback)) + { + i = m_listeners.erase (i); + } + else + { + i++; + } + } +} +void +NotifyStaticPosition::NotifyPositionChange (void) const +{ + for (std::list::const_iterator i = m_listeners.begin (); + i != m_listeners.end (); i++) + { + Listener listener = *i; + listener (*this); + } +} + +} // namespace ns3 diff --git a/src/node/notify-static-position.h b/src/node/notify-static-position.h new file mode 100644 index 000000000..ab917e58a --- /dev/null +++ b/src/node/notify-static-position.h @@ -0,0 +1,74 @@ +/* -*- 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 NOTIFY_STATIC_POSITION_H +#define NOTIFY_STATIC_POSITION_H + +#include "ns3/callback.h" +#include "static-position.h" +#include + +namespace ns3 { + +/** + * \brief notify listeners of position changes. + */ +class NotifyStaticPosition : public StaticPosition +{ +public: + static const InterfaceId iid; + typedef Callback Listener; + /** + * Create a position located at coordinates (0,0,0) + */ + NotifyStaticPosition (); + /** + * \param x x coordinate + * \param y y coordinate + * \param z z coordinate + * + * Create a position located at coordinates (x,y,z) + * Unit is meters + */ + NotifyStaticPosition (double x, double y, double z); + + /** + * \param listener listener to add + * + * The listener will be notified upon every position change. + */ + void RegisterListener (Listener listener); + /** + * \param listener listener to remove + * + * The listener will not be notified anymore upon every + * position change. It is not an error to try to unregister + * a non-registered liste + */ + void UnregisterListener (Listener listener); +private: + virtual void NotifyPositionChange (void) const; + std::list m_listeners; +}; + +} // namespace ns3 + + +#endif /* NOTIFY_STATIC_POSITION_H */ diff --git a/src/node/position.cc b/src/node/position.cc new file mode 100644 index 000000000..db7db979e --- /dev/null +++ b/src/node/position.cc @@ -0,0 +1,71 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2006,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 "position.h" +#include + +namespace ns3 { + +const InterfaceId Position::iid = MakeInterfaceId ("Position", Object::iid); + +Position::~Position () +{} + +void +Position::Get (double &x, double &y, double &z) const +{ + DoGet (x,y,z); +} +double +Position::GetX (void) const +{ + double x,y,z; + DoGet (x,y,z); + return x; +} +double +Position::GetY (void) const +{ + double x, y, z; + DoGet (x,y,z); + return y; +} +double +Position::GetZ (void) const +{ + double x, y, z; + DoGet (x,y,z); + return z; +} + +double +Position::GetDistanceFrom (const Position &position) const +{ + double ox,oy,oz; + double x,y,z; + position.DoGet (ox,oy,oz); + DoGet (x,y,z); + double dx = ox - x; + double dy = oy - y; + double dz = oz - z; + return sqrt (dx*dx+dy*dy+dz*dz); +} + +}; // namespace ns3 diff --git a/src/node/position.h b/src/node/position.h new file mode 100644 index 000000000..cf4209c7f --- /dev/null +++ b/src/node/position.h @@ -0,0 +1,92 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2006,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 POSITION_H +#define POSITION_H + +#include "ns3/object.h" + +namespace ns3 { + +/** + * \brief keep track of the current position of an object + * + * All space coordinates in this class and its subclasses are + * understood to be meters or meters/s. i.e., they are all + * metric international units. + */ +class Position : public Object +{ +public: + static const InterfaceId iid; + virtual ~Position () = 0; + + /** + * \param x reference to floating-point variable for x coordinate. + * \param y reference to floating-point variable for y coordinate. + * \param z reference to floating-point variable for z coordinate. + * + * Store in the x, y, and z variables the current coordinates + * managed by this position object. + * Unit is meters + */ + void Get (double &x, double &y, double &z) const; + /** + * \returns the current x coordinate + * + * Unit is meters + */ + double GetX (void) const; + /** + * \returns the current y coordinate + * + * Unit is meters + */ + double GetY (void) const; + /** + * \returns the current z coordinate + * + * Unit is meters + */ + double GetZ (void) const; + /** + * \param position a reference to another position object instance + * \returns the distance between the two objects. + * + * Unit is meters + */ + double GetDistanceFrom (const Position &position) const; +private: + /** + * \param x reference to floating-point variable for x coordinate. + * \param y reference to floating-point variable for y coordinate. + * \param z reference to floating-point variable for z coordinate. + * + * Store in the x, y, and z variables the current coordinates + * managed by this position object. Concrete subclasses of this + * base class must implement this method. + * Unit is meters + */ + virtual void DoGet (double &x, double &y, double &z) const = 0; +}; + +}; // namespace ns3 + +#endif /* POSITION_H */ diff --git a/src/node/static-position.cc b/src/node/static-position.cc new file mode 100644 index 000000000..0081780cd --- /dev/null +++ b/src/node/static-position.cc @@ -0,0 +1,109 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2006 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 "static-position.h" + +namespace ns3 { + +const InterfaceId StaticPosition::iid = MakeInterfaceId ("StaticPosition", Position::iid); + +StaticPosition::StaticPosition () + : m_x (0.0), m_y (0.0), m_z (0.0) +{} +StaticPosition::StaticPosition (double x, double y, double z) + : m_x (x), m_y (y), m_z (z) +{} +StaticPosition::~StaticPosition () +{} + +void +StaticPosition::Set (double x, double y, double z) +{ + bool mustNotify = false; + if (x != m_x || + y != m_y || + z != m_z) + { + mustNotify = true; + } + m_x = x; + m_y = y; + m_z = z; + if (mustNotify) + { + NotifyPositionChange (); + } +} +void +StaticPosition::SetX (double x) +{ + bool mustNotify = false; + if (x != m_x) + { + mustNotify = true; + } + m_x = x; + if (mustNotify) + { + NotifyPositionChange (); + } +} +void +StaticPosition::SetY (double y) +{ + bool mustNotify = false; + if (y != m_y) + { + mustNotify = true; + } + m_y = y; + if (mustNotify) + { + NotifyPositionChange (); + } +} +void +StaticPosition::SetZ (double z) +{ + bool mustNotify = false; + if (z != m_z) + { + mustNotify = true; + } + m_z = z; + if (mustNotify) + { + NotifyPositionChange (); + } +} + +void +StaticPosition::NotifyPositionChange (void) const +{} + +void +StaticPosition::DoGet (double &x, double &y, double &z) const +{ + x = m_x; + y = m_y; + z = m_z; +} + +}; // namespace ns3 diff --git a/src/node/static-position.h b/src/node/static-position.h new file mode 100644 index 000000000..098101e2e --- /dev/null +++ b/src/node/static-position.h @@ -0,0 +1,95 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2006,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 STATIC_POSITION_H +#define STATIC_POSITION_H + +#include "position.h" + +namespace ns3 { + +/** + * \brief a position model for which the current position does not + * change once it has been set and until it is set again + * explicitely to a new value. + */ +class StaticPosition : public Position +{ +public: + static const InterfaceId iid; + /** + * Create a position located at coordinates (0,0,0) + */ + StaticPosition (); + /** + * \param x x coordinate + * \param y y coordinate + * \param z z coordinate + * + * Create a position located at coordinates (x,y,z). + * Unit is meters + */ + StaticPosition (double x, double y, double z); + virtual ~StaticPosition (); + + /** + * \param x x coordinate + * \param y y coordinate + * \param z z coordinate + * + * Set all 3 coordinates at the same time. + * Unit is meters + */ + void Set (double x, double y, double z); + /** + * \param x x coordinate + * + * Set x coordinate. Unit is meters + */ + void SetX (double x); + /** + * \param y y coordinate + * + * Set y coordinate. Unit is meters + */ + void SetY (double y); + /** + * \param z z coordinate + * + * Set z coordinate. Unit is meters + */ + void SetZ (double z); +private: + /** + * Subclasses must override this virtual method to be notified + * of a call to one of the Set methods which changes the current + * position. The default implementation does nothing. This method + * is invoked _after_ the current position has been updated. + */ + virtual void NotifyPositionChange (void) const; + virtual void DoGet (double &x, double &y, double &z) const; + double m_x; + double m_y; + double m_z; +}; + +}; // namespace ns3 + +#endif /* STATIC_POSITION_H */ diff --git a/src/node/static-speed-position.cc b/src/node/static-speed-position.cc new file mode 100644 index 000000000..78f8f9441 --- /dev/null +++ b/src/node/static-speed-position.cc @@ -0,0 +1,98 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2006, 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 "static-speed-position.h" +#include "ns3/simulator.h" + +namespace ns3 { + +const InterfaceId StaticSpeedPosition::iid = MakeInterfaceId ("StaticSpeedPosition", Position::iid); + +StaticSpeedPosition::StaticSpeedPosition () + : m_x (0.0), + m_y (0.0), + m_z (0.0), + m_dx (0.0), + m_dy (0.0), + m_dz (0.0), + m_prevTime (Simulator::Now ()) +{} +StaticSpeedPosition::StaticSpeedPosition (double x, double y, double z) + : m_x (x), + m_y (y), + m_z (z), + m_dx (0.0), + m_dy (0.0), + m_dz (0.0), + m_prevTime (Simulator::Now ()) +{} +StaticSpeedPosition::StaticSpeedPosition (double x, double y, double z, + double dx, double dy, double dz) + : m_x (x), + m_y (y), + m_z (z), + m_dx (dx), + m_dy (dy), + m_dz (dz), + m_prevTime (Simulator::Now ()) +{} + +StaticSpeedPosition::~StaticSpeedPosition () +{} + +void +StaticSpeedPosition::SetPosition (double x, double y, double z) +{ + Update (); + m_x = x; + m_y = y; + m_z = z; +} + +void +StaticSpeedPosition::SetSpeed (double dx, double dy, double dz) +{ + Update (); + m_dx = dx; + m_dy = dy; + m_dz = dz; +} + +void +StaticSpeedPosition::Update (void) const +{ + Time deltaTime = Simulator::Now () - m_prevTime; + m_prevTime = Simulator::Now (); + double deltaS = deltaTime.GetSeconds (); + m_x += m_dx * deltaS; + m_y += m_dy * deltaS; + m_z += m_dz * deltaS; +} + +void +StaticSpeedPosition::RealGet (double &x, double &y, double &z) const +{ + Update (); + x = m_x; + y = m_y; + z = m_z; +} + +}; // namespace ns3 diff --git a/src/node/static-speed-position.h b/src/node/static-speed-position.h new file mode 100644 index 000000000..a54b94aa3 --- /dev/null +++ b/src/node/static-speed-position.h @@ -0,0 +1,98 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2006, 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 STATIC_SPEED_POSITION_H +#define STATIC_SPEED_POSITION_H + +#include +#include "position.h" +#include "ns3/nstime.h" + +namespace ns3 { + +class StaticSpeedPosition : public Position +{ +public: + static const InterfaceId iid; + /** + * Create position located at coordinates (0,0,0) with + * speed (0,0,0). + */ + StaticSpeedPosition (); + /** + * \param x x coordinate + * \param y y coordinate + * \param z z coordinate + * + * Create a position located at coordinates (x,y,z) with + * speed (0,0,0). + * Unit is meters + */ + StaticSpeedPosition (double x, double y, double z); + /** + * \param x x coordinate + * \param y y coordinate + * \param z z coordinate + * \param dx x coordinate speed + * \param dy y coordinate speed + * \param dz z coordinate speed + * + * Create a position located at coordinates (x,y,z) with + * speed (dx,dy,dz). + * Unit is meters and meters/s + */ + StaticSpeedPosition (double x, double y, double z, + double dx, double dy, double dz); + virtual ~StaticSpeedPosition (); + + /** + * \param x x coordinate + * \param y y coordinate + * \param z z coordinate + * + * Set the current position now to (x,y,z) + * Unit is meters + */ + void SetPosition (double x, double y, double z); + + /* + * \param dx x coordinate speed + * \param dy y coordinate speed + * \param dz z coordinate speed + * + * Set the current speed now to (dx,dy,dz) + * Unit is meters/s + */ + void SetSpeed (double dx, double dy, double dz); +private: + virtual void RealGet (double &x, double &y, double &z) const; + void Update (void) const; + mutable double m_x; + mutable double m_y; + mutable double m_z; + double m_dx; + double m_dy; + double m_dz; + mutable Time m_prevTime; +}; + +}; // namespace ns3 + +#endif /* STATIC_SPEED_POSITION */ From 889452447424a0a72421e0d191c243be0beb71f1 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 2 Jul 2007 10:50:07 +0200 Subject: [PATCH 002/148] add floating point support to type names --- src/core/type-name.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/core/type-name.h b/src/core/type-name.h index 9390b9473..22ed0244f 100644 --- a/src/core/type-name.h +++ b/src/core/type-name.h @@ -26,6 +26,8 @@ DEF_TYPE (int8_t); DEF_TYPE (int16_t); DEF_TYPE (int32_t); DEF_TYPE (int64_t); +DEF_TYPE (float); +DEF_TYPE (double); #undef DEF_TYPE From 418adb52a4a87873eb53d2765da3d57bc414e05f Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 2 Jul 2007 10:50:18 +0200 Subject: [PATCH 003/148] add Position::DoSet et al. --- src/node/position.cc | 35 ++++++++++++++++ src/node/position.h | 7 ++++ src/node/static-position.cc | 66 ++++++------------------------- src/node/static-position.h | 28 +------------ src/node/static-speed-position.cc | 19 +++++---- src/node/static-speed-position.h | 13 +----- 6 files changed, 66 insertions(+), 102 deletions(-) diff --git a/src/node/position.cc b/src/node/position.cc index db7db979e..f60ee67a2 100644 --- a/src/node/position.cc +++ b/src/node/position.cc @@ -55,6 +55,41 @@ Position::GetZ (void) const return z; } +void +Position::Set (double x, double y, double z) +{ + DoSet (x, y, z); +} +void +Position::SetXY (double x, double y) +{ + double currentX, currentY, currentZ; + DoGet (currentX, currentY, currentZ); + DoSet (x, y, currentZ); +} +void +Position::SetX (double x) +{ + double currentX, currentY, currentZ; + DoGet (currentX, currentY, currentZ); + DoSet (x, currentY, currentZ); +} +void +Position::SetY (double y) +{ + double currentX, currentY, currentZ; + DoGet (currentX, currentY, currentZ); + DoSet (currentX, y, currentZ); +} +void +Position::SetZ (double z) +{ + double currentX, currentY, currentZ; + DoGet (currentX, currentY, currentZ); + DoSet (currentX, currentY, z); +} + + double Position::GetDistanceFrom (const Position &position) const { diff --git a/src/node/position.h b/src/node/position.h index cf4209c7f..dc1596744 100644 --- a/src/node/position.h +++ b/src/node/position.h @@ -66,6 +66,12 @@ public: * Unit is meters */ double GetZ (void) const; + + void Set (double x, double y, double z); + void SetXY (double x, double y); + void SetX (double x); + void SetY (double y); + void SetZ (double z); /** * \param position a reference to another position object instance * \returns the distance between the two objects. @@ -85,6 +91,7 @@ private: * Unit is meters */ virtual void DoGet (double &x, double &y, double &z) const = 0; + virtual void DoSet (double x, double y, double z) const = 0; }; }; // namespace ns3 diff --git a/src/node/static-position.cc b/src/node/static-position.cc index 0081780cd..9c6c06233 100644 --- a/src/node/static-position.cc +++ b/src/node/static-position.cc @@ -33,8 +33,19 @@ StaticPosition::StaticPosition (double x, double y, double z) StaticPosition::~StaticPosition () {} +void +StaticPosition::NotifyPositionChange (void) const +{} + void -StaticPosition::Set (double x, double y, double z) +StaticPosition::DoGet (double &x, double &y, double &z) const +{ + x = m_x; + y = m_y; + z = m_z; +} +void +StaticPosition::DoSet (double x, double y, double z) { bool mustNotify = false; if (x != m_x || @@ -51,59 +62,6 @@ StaticPosition::Set (double x, double y, double z) NotifyPositionChange (); } } -void -StaticPosition::SetX (double x) -{ - bool mustNotify = false; - if (x != m_x) - { - mustNotify = true; - } - m_x = x; - if (mustNotify) - { - NotifyPositionChange (); - } -} -void -StaticPosition::SetY (double y) -{ - bool mustNotify = false; - if (y != m_y) - { - mustNotify = true; - } - m_y = y; - if (mustNotify) - { - NotifyPositionChange (); - } -} -void -StaticPosition::SetZ (double z) -{ - bool mustNotify = false; - if (z != m_z) - { - mustNotify = true; - } - m_z = z; - if (mustNotify) - { - NotifyPositionChange (); - } -} -void -StaticPosition::NotifyPositionChange (void) const -{} - -void -StaticPosition::DoGet (double &x, double &y, double &z) const -{ - x = m_x; - y = m_y; - z = m_z; -} }; // namespace ns3 diff --git a/src/node/static-position.h b/src/node/static-position.h index 098101e2e..bf6ee6e78 100644 --- a/src/node/static-position.h +++ b/src/node/static-position.h @@ -49,33 +49,6 @@ public: StaticPosition (double x, double y, double z); virtual ~StaticPosition (); - /** - * \param x x coordinate - * \param y y coordinate - * \param z z coordinate - * - * Set all 3 coordinates at the same time. - * Unit is meters - */ - void Set (double x, double y, double z); - /** - * \param x x coordinate - * - * Set x coordinate. Unit is meters - */ - void SetX (double x); - /** - * \param y y coordinate - * - * Set y coordinate. Unit is meters - */ - void SetY (double y); - /** - * \param z z coordinate - * - * Set z coordinate. Unit is meters - */ - void SetZ (double z); private: /** * Subclasses must override this virtual method to be notified @@ -85,6 +58,7 @@ private: */ virtual void NotifyPositionChange (void) const; virtual void DoGet (double &x, double &y, double &z) const; + virtual void DoSet (double x, double y, double z); double m_x; double m_y; double m_z; diff --git a/src/node/static-speed-position.cc b/src/node/static-speed-position.cc index 78f8f9441..4fc077795 100644 --- a/src/node/static-speed-position.cc +++ b/src/node/static-speed-position.cc @@ -57,15 +57,6 @@ StaticSpeedPosition::StaticSpeedPosition (double x, double y, double z, StaticSpeedPosition::~StaticSpeedPosition () {} -void -StaticSpeedPosition::SetPosition (double x, double y, double z) -{ - Update (); - m_x = x; - m_y = y; - m_z = z; -} - void StaticSpeedPosition::SetSpeed (double dx, double dy, double dz) { @@ -87,12 +78,20 @@ StaticSpeedPosition::Update (void) const } void -StaticSpeedPosition::RealGet (double &x, double &y, double &z) const +StaticSpeedPosition::DoGet (double &x, double &y, double &z) const { Update (); x = m_x; y = m_y; z = m_z; } +void +StaticSpeedPosition::DoSet (double x, double y, double z) +{ + Update (); + m_x = x; + m_y = y; + m_z = z; +} }; // namespace ns3 diff --git a/src/node/static-speed-position.h b/src/node/static-speed-position.h index a54b94aa3..8771337f3 100644 --- a/src/node/static-speed-position.h +++ b/src/node/static-speed-position.h @@ -62,16 +62,6 @@ public: double dx, double dy, double dz); virtual ~StaticSpeedPosition (); - /** - * \param x x coordinate - * \param y y coordinate - * \param z z coordinate - * - * Set the current position now to (x,y,z) - * Unit is meters - */ - void SetPosition (double x, double y, double z); - /* * \param dx x coordinate speed * \param dy y coordinate speed @@ -82,7 +72,8 @@ public: */ void SetSpeed (double dx, double dy, double dz); private: - virtual void RealGet (double &x, double &y, double &z) const; + virtual void DoGet (double &x, double &y, double &z) const; + virtual void DoSet (double x, double y, double z); void Update (void) const; mutable double m_x; mutable double m_y; From 0197e59eb101e9e279e41dd592e00020fecec2b8 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 2 Jul 2007 11:29:27 +0200 Subject: [PATCH 004/148] DoSet should not be const --- src/node/position.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node/position.h b/src/node/position.h index dc1596744..46565716f 100644 --- a/src/node/position.h +++ b/src/node/position.h @@ -91,7 +91,7 @@ private: * Unit is meters */ virtual void DoGet (double &x, double &y, double &z) const = 0; - virtual void DoSet (double x, double y, double z) const = 0; + virtual void DoSet (double x, double y, double z) = 0; }; }; // namespace ns3 From a2ca638f471bfa43a8661a60bd9816e01a9cd4a1 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 2 Jul 2007 11:29:54 +0200 Subject: [PATCH 005/148] add cid support to position subclasses --- src/node/static-position.cc | 5 +++++ src/node/static-position.h | 10 ++++++++++ src/node/static-speed-position.cc | 13 +++++++++++++ src/node/static-speed-position.h | 11 +++++++++++ 4 files changed, 39 insertions(+) diff --git a/src/node/static-position.cc b/src/node/static-position.cc index 9c6c06233..024c5151e 100644 --- a/src/node/static-position.cc +++ b/src/node/static-position.cc @@ -23,10 +23,15 @@ namespace ns3 { const InterfaceId StaticPosition::iid = MakeInterfaceId ("StaticPosition", Position::iid); +const ClassId StaticPosition::cid = MakeClassId ("StaticPosition", + StaticPosition::iid); StaticPosition::StaticPosition () : m_x (0.0), m_y (0.0), m_z (0.0) {} +StaticPosition::StaticPosition (double x, double y) + : m_x (x), m_y (y), m_z (0.0) +{} StaticPosition::StaticPosition (double x, double y, double z) : m_x (x), m_y (y), m_z (z) {} diff --git a/src/node/static-position.h b/src/node/static-position.h index bf6ee6e78..67e6bd7fd 100644 --- a/src/node/static-position.h +++ b/src/node/static-position.h @@ -21,6 +21,7 @@ #ifndef STATIC_POSITION_H #define STATIC_POSITION_H +#include "ns3/component-manager.h" #include "position.h" namespace ns3 { @@ -34,10 +35,19 @@ class StaticPosition : public Position { public: static const InterfaceId iid; + static const ClassId cid; /** * Create a position located at coordinates (0,0,0) */ StaticPosition (); + /** + * \param x x coordinate + * \param y y coordinate + * + * Create a position located at coordinates (x,y,0). + * Unit is meters + */ + StaticPosition (double x, double y); /** * \param x x coordinate * \param y y coordinate diff --git a/src/node/static-speed-position.cc b/src/node/static-speed-position.cc index 4fc077795..fd2b45f15 100644 --- a/src/node/static-speed-position.cc +++ b/src/node/static-speed-position.cc @@ -24,6 +24,10 @@ namespace ns3 { const InterfaceId StaticSpeedPosition::iid = MakeInterfaceId ("StaticSpeedPosition", Position::iid); +const ClassId StaticSpeedPosition::cid = + MakeClassId ("StaticSpeedPosition", + StaticSpeedPosition::iid); + StaticSpeedPosition::StaticSpeedPosition () : m_x (0.0), @@ -43,6 +47,15 @@ StaticSpeedPosition::StaticSpeedPosition (double x, double y, double z) m_dz (0.0), m_prevTime (Simulator::Now ()) {} +StaticSpeedPosition::StaticSpeedPosition (double x, double y) + : m_x (x), + m_y (y), + m_z (0.0), + m_dx (0.0), + m_dy (0.0), + m_dz (0.0), + m_prevTime (Simulator::Now ()) +{} StaticSpeedPosition::StaticSpeedPosition (double x, double y, double z, double dx, double dy, double dz) : m_x (x), diff --git a/src/node/static-speed-position.h b/src/node/static-speed-position.h index 8771337f3..ed8d48e64 100644 --- a/src/node/static-speed-position.h +++ b/src/node/static-speed-position.h @@ -24,6 +24,7 @@ #include #include "position.h" #include "ns3/nstime.h" +#include "ns3/component-manager.h" namespace ns3 { @@ -31,11 +32,21 @@ class StaticSpeedPosition : public Position { public: static const InterfaceId iid; + static const ClassId cid; /** * Create position located at coordinates (0,0,0) with * speed (0,0,0). */ StaticSpeedPosition (); + /** + * \param x x coordinate + * \param y y coordinate + * + * Create a position located at coordinates (x,y,0) with + * speed (0,0,0). + * Unit is meters + */ + StaticSpeedPosition (double x, double y); /** * \param x x coordinate * \param y y coordinate From 965e14c3f9e5b55d75a4fe598d372606107fe29c Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 2 Jul 2007 12:14:12 +0200 Subject: [PATCH 006/148] add cid support --- SConstruct | 2 ++ src/node/notify-static-position.cc | 3 +++ src/node/notify-static-position.h | 11 +++++++++++ 3 files changed, 16 insertions(+) diff --git a/SConstruct b/SConstruct index 84e9595ba..aaf27f5cb 100644 --- a/SConstruct +++ b/SConstruct @@ -246,6 +246,7 @@ node.add_sources ([ 'static-position.cc', 'notify-static-position.cc', 'static-speed-position.cc', + 'grid-topology.cc', ]) node.add_inst_headers ([ 'node.h', @@ -267,6 +268,7 @@ node.add_inst_headers ([ 'static-position.h', 'notify-static-position.h', 'static-speed-position.h', + 'grid-topology.h', ]) applications = build.Ns3Module ('applications', 'src/applications') diff --git a/src/node/notify-static-position.cc b/src/node/notify-static-position.cc index 76875f137..551bb388d 100644 --- a/src/node/notify-static-position.cc +++ b/src/node/notify-static-position.cc @@ -23,6 +23,9 @@ namespace ns3 { const InterfaceId NotifyStaticPosition::iid = MakeInterfaceId ("NotifyStaticPosition", StaticPosition::iid); +const ClassId NotifyStaticPosition::cid = + MakeClassId ("NotifyStaticPosition", + NotifyStaticPosition::iid); NotifyStaticPosition::NotifyStaticPosition () : StaticPosition () diff --git a/src/node/notify-static-position.h b/src/node/notify-static-position.h index ab917e58a..9483287c2 100644 --- a/src/node/notify-static-position.h +++ b/src/node/notify-static-position.h @@ -22,6 +22,7 @@ #define NOTIFY_STATIC_POSITION_H #include "ns3/callback.h" +#include "ns3/component-manager.h" #include "static-position.h" #include @@ -34,11 +35,21 @@ class NotifyStaticPosition : public StaticPosition { public: static const InterfaceId iid; + static const ClassId cid; + typedef Callback Listener; /** * Create a position located at coordinates (0,0,0) */ NotifyStaticPosition (); + /** + * \param x x coordinate + * \param y y coordinate + * + * Create a position located at coordinates (x,y,0) + * Unit is meters + */ + NotifyStaticPosition (double x, double y); /** * \param x x coordinate * \param y y coordinate From e9ee638ae9e0c9123997f45c02a9850f4917bbd7 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 2 Jul 2007 12:14:24 +0200 Subject: [PATCH 007/148] grid topology --- src/node/grid-topology.cc | 65 +++++++++++++++++++++++++++++++++++++++ src/node/grid-topology.h | 54 ++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 src/node/grid-topology.cc create mode 100644 src/node/grid-topology.h diff --git a/src/node/grid-topology.cc b/src/node/grid-topology.cc new file mode 100644 index 000000000..1796fb233 --- /dev/null +++ b/src/node/grid-topology.cc @@ -0,0 +1,65 @@ +/* -*- 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 "grid-topology.h" +#include "static-position.h" + +namespace ns3 { + +GridTopology::GridTopology (double xMin, double yMin, uint32_t nCols, double deltaX, double deltaY) + : m_xMin (xMin), + m_yMin (yMin), + m_nCols (nCols), + m_deltaX (deltaX), + m_deltaY (deltaY), + m_positionClassId (StaticPosition::cid) +{} + +void +GridTopology::SetPositionModel (ClassId classId) +{ + m_positionClassId = classId; +} + +void +GridTopology::Create (std::vector > nodes) +{ + double x, y; + uint32_t col; + x = m_xMin; + y = m_yMin; + col = 0; + for (std::vector >::const_iterator i = nodes.begin (); + i != nodes.end (); i++) + { + Ptr node = *i; + node->AddInterface (ComponentManager::Create (m_positionClassId, x, y)); + x += m_deltaX; + col++; + if (col == m_nCols) + { + col = 0; + x = m_xMin; + y += m_deltaY; + } + } +} + +} // namespace ns3 diff --git a/src/node/grid-topology.h b/src/node/grid-topology.h new file mode 100644 index 000000000..f624af893 --- /dev/null +++ b/src/node/grid-topology.h @@ -0,0 +1,54 @@ +/* -*- 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 GRID_TOPOLOGY_H +#define GRID_TOPOLOGY_H + +#include +#include "ns3/component-manager.h" +#include "ns3/ptr.h" +#include "node.h" + +namespace ns3 { + +class GridTopology +{ + public: + GridTopology (double xMin, double yMin, uint32_t nCols, double deltaX, double deltaY); + + void SetPositionModel (ClassId classId); + + /** + * Add position to each node in vector. + */ + void Create (std::vector > nodes); + private: + GridTopology (); + double m_xMin; + double m_yMin; + uint32_t m_nCols; + double m_deltaX; + double m_deltaY; + ClassId m_positionClassId; +}; + +} // namespace ns3 + +#endif /* GRID_TOPOLOGY_H */ From 7a86a8a8e71c25af8dfcdee621251560167447d5 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 2 Jul 2007 14:01:49 +0200 Subject: [PATCH 008/148] make default constructor private to avoid potential problems --- src/core/default-value.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/core/default-value.h b/src/core/default-value.h index 6f9f98508..08dbae285 100644 --- a/src/core/default-value.h +++ b/src/core/default-value.h @@ -45,6 +45,8 @@ public: protected: DefaultValueBase (const std::string &name, const std::string &help); +private: + DefaultValueBase (); private: virtual bool DoParseValue (const std::string &value) = 0; virtual std::string DoGetType (void) const = 0; From 1271ada6e10e874d19b19ba1860eacc2c596b5fc Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 2 Jul 2007 14:02:02 +0200 Subject: [PATCH 009/148] TimeDefaultValue class --- src/simulator/time-default-value.cc | 106 ++++++++++++++++++++++++++++ src/simulator/time-default-value.h | 47 ++++++++++++ 2 files changed, 153 insertions(+) create mode 100644 src/simulator/time-default-value.cc create mode 100644 src/simulator/time-default-value.h diff --git a/src/simulator/time-default-value.cc b/src/simulator/time-default-value.cc new file mode 100644 index 000000000..46449f4c2 --- /dev/null +++ b/src/simulator/time-default-value.cc @@ -0,0 +1,106 @@ +/* -*- 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 "time-default-value.h" + +namespace ns3 { + +TimeDefaultValue::TimeDefaultValue (const std::string name, + const std::string help, + Time defaultValue) + : DefaultValueBase (name, help), + m_defaultValue (defaultValue), + m_value (defaultValue) +{ + DefaultValueList::Add (this); +} +Time +TimeDefaultValue::GetValue (void) const +{ + return m_value; +} +bool +TimeDefaultValue::DoParseValue (const std::string &value) +{ + std::string::size_type n = value.find_first_not_of("0123456789."); + if (n == std::string::npos) + { + return false; + } + std::string trailer = value.substr(n, std::string::npos); + std::istringstream iss; + iss.str (value.substr(0, n)); + + if (trailer == std::string("s")) + { + double v; + iss >> v; + m_value = Seconds (v); + return !iss.bad () && !iss.fail (); + } + uint64_t integer; + iss >> integer; + if (iss.bad () || iss.fail ()) + { + return false; + } + if (trailer == std::string("ms")) + { + m_value = MilliSeconds (integer); + return true; + } + if (trailer == std::string("us")) + { + m_value = MicroSeconds (integer); + return true; + } + if (trailer == std::string("ns")) + { + m_value = NanoSeconds (integer); + return true; + } + if (trailer == std::string("ps")) + { + m_value = PicoSeconds (integer); + return true; + } + if (trailer == std::string("fs")) + { + m_value = FemtoSeconds (integer); + return true; + } + return false; +} +std::string +TimeDefaultValue::DoGetType (void) const +{ + return "(s|ms|us|ns|ps|fs)"; +} +std::string +TimeDefaultValue::DoGetDefaultValue (void) const +{ + std::ostringstream oss; + oss << m_value.GetSeconds () << "s"; + return oss.str (); +} + + +} // namespace ns3 diff --git a/src/simulator/time-default-value.h b/src/simulator/time-default-value.h new file mode 100644 index 000000000..d417c59c0 --- /dev/null +++ b/src/simulator/time-default-value.h @@ -0,0 +1,47 @@ +/* -*- 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 TIME_DEFAULT_VALUE_H +#define TIME_DEFAULT_VALUE_H + +#include "ns3/default-value.h" +#include "ns3/nstime.h" + +namespace ns3 { + +class TimeDefaultValue : public DefaultValueBase +{ +public: + TimeDefaultValue (const std::string name, + const std::string help, + Time defaultValue); + Time GetValue (void) const; +private: + virtual bool DoParseValue (const std::string &value); + virtual std::string DoGetType (void) const; + virtual std::string DoGetDefaultValue (void) const; + + Time m_defaultValue; + Time m_value; +}; + +} // namespace ns3 + +#endif /* TIME_DEFAULT_VALUE_H */ From fb968dccdf0844a8bd09405f3515fe9cfd454427 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 2 Jul 2007 14:02:57 +0200 Subject: [PATCH 010/148] forgot to add missing constructor implementation --- src/node/notify-static-position.cc | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/node/notify-static-position.cc b/src/node/notify-static-position.cc index 551bb388d..9394ffa92 100644 --- a/src/node/notify-static-position.cc +++ b/src/node/notify-static-position.cc @@ -30,6 +30,9 @@ const ClassId NotifyStaticPosition::cid = NotifyStaticPosition::NotifyStaticPosition () : StaticPosition () {} +NotifyStaticPosition::NotifyStaticPosition (double x, double y) + : StaticPosition (x, y, 0.0) +{} NotifyStaticPosition::NotifyStaticPosition (double x, double y, double z) : StaticPosition (x, y, z) {} From 04ebac22795cce693bb1d0b7a5f9e0853299ea7b Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 2 Jul 2007 14:07:27 +0200 Subject: [PATCH 011/148] forgot to add TypeName implementation for float and double --- src/core/type-name.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/core/type-name.cc b/src/core/type-name.cc index 0bc6b9b46..3a135b12a 100644 --- a/src/core/type-name.cc +++ b/src/core/type-name.cc @@ -19,6 +19,7 @@ DEF_TYPE (int8_t); DEF_TYPE (int16_t); DEF_TYPE (int32_t); DEF_TYPE (int64_t); - +DEF_TYPE (float); +DEF_TYPE (double); }//namespace ns3 From fca6b52fbab9e100f50988d06bf8229f12bb9fa3 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 2 Jul 2007 14:07:51 +0200 Subject: [PATCH 012/148] random walk position model (untested) --- SConstruct | 4 + src/node/random-walk-position.cc | 153 +++++++++++++++++++++++++++++++ src/node/random-walk-position.h | 94 +++++++++++++++++++ 3 files changed, 251 insertions(+) create mode 100644 src/node/random-walk-position.cc create mode 100644 src/node/random-walk-position.h diff --git a/SConstruct b/SConstruct index aaf27f5cb..95f578c36 100644 --- a/SConstruct +++ b/SConstruct @@ -129,6 +129,7 @@ simu.add_sources([ 'scheduler-map.cc', 'event-impl.cc', 'simulator.cc', + 'time-default-value.cc', ]) simu.add_headers([ 'scheduler-heap.h', @@ -144,6 +145,7 @@ simu.add_inst_headers([ 'scheduler.h', 'scheduler-factory.h', 'simulation-singleton.h', + 'time-default-value.h', ]) high_precision_as_double = ARGUMENTS.get('high-precision-as-double', 'n') if high_precision_as_double == 'y': @@ -247,6 +249,7 @@ node.add_sources ([ 'notify-static-position.cc', 'static-speed-position.cc', 'grid-topology.cc', + 'random-walk-position.cc', ]) node.add_inst_headers ([ 'node.h', @@ -269,6 +272,7 @@ node.add_inst_headers ([ 'notify-static-position.h', 'static-speed-position.h', 'grid-topology.h', + 'random-walk-position.h', ]) applications = build.Ns3Module ('applications', 'src/applications') diff --git a/src/node/random-walk-position.cc b/src/node/random-walk-position.cc new file mode 100644 index 000000000..e545bc4e7 --- /dev/null +++ b/src/node/random-walk-position.cc @@ -0,0 +1,153 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2006,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 "random-walk-position.h" +#include "ns3/default-value.h" +#include "ns3/time-default-value.h" +#include "ns3/simulator.h" +#include + +namespace ns3 { + +static IntegerDefaultValue g_minSpeed ("RandomWalkMinSpeed", + "Minimum speed used during a random walk", + 0); +static IntegerDefaultValue g_maxSpeed ("RandomWalkMaxSpeed", + "Maximum speed used during a random walk", + 0); +static EnumDefaultValue g_mode + ("RandomWalkMode", + "The mode indicates the condition used to " + "change the current speed and direction", + RandomWalkPositionParameters::MODE_DISTANCE, "Distance", + RandomWalkPositionParameters::MODE_TIME, "Time", + 0, 0); +static IntegerDefaultValue g_modeDistance ("RandomWalkModeDistance", + "Distance to walk before changing direction and speed.", + 10); +static TimeDefaultValue g_modeTime ("RandomWalkModeTime", + "Time to walk before changing direction and speed.", + Seconds (1)); + +RandomWalkPositionParameters::RandomWalkPositionParameters () + : m_minSpeed (g_minSpeed.GetValue ()), + m_maxSpeed (g_maxSpeed.GetValue ()), + m_mode (g_mode.GetValue ()), + m_modeDistance (g_modeDistance.GetValue ()), + m_modeTime (g_modeDistance.GetValue ()) +{} +bool +RandomWalkPositionParameters::IsDefault (void) const +{ + if (m_minSpeed != g_minSpeed.GetValue () || + m_maxSpeed != g_maxSpeed.GetValue () || + m_mode != g_mode.GetValue () || + m_modeDistance != g_modeDistance.GetValue () || + m_modeTime != g_modeTime.GetValue ()) + { + return false; + } + return true; +} + +void +RandomWalkPositionParameters::SetSpeedBounds (double minSpeed, double maxSpeed) +{ + m_minSpeed = minSpeed; + m_maxSpeed = maxSpeed; +} + + +UniformVariable RandomWalkPosition::m_randomDirection (0.0, 2*3.141592); + +Ptr +RandomWalkPosition::GetDefaultParameters (void) +{ + static Ptr parameters = Create (); + if (!parameters->IsDefault ()) + { + parameters = Create (); + } + return parameters; +} + +RandomWalkPosition::RandomWalkPosition () + : m_x (0.0), + m_y (0.0), + m_dx (0.0), + m_dy (0.0), + m_prevTime (Simulator::Now ()), + m_parameters (RandomWalkPosition::GetDefaultParameters ()) +{ + Reset (); +} + +void +RandomWalkPosition::Reset (void) +{ + Update (); + double speed = UniformVariable::GetSingleValue (m_parameters->m_minSpeed, + m_parameters->m_maxSpeed); + double direction = m_randomDirection.GetValue (); + double dx = std::cos (direction); + double dy = std::sin (direction); + m_dx = dx * speed; + m_dy = dy * speed; + Time delay; + if (m_parameters->m_mode == RandomWalkPositionParameters::MODE_TIME) + { + delay = m_parameters->m_modeTime; + } + else + { + double distance = g_modeDistance.GetValue (); + delay = Seconds (distance / sqrt (m_dx * m_dx + m_dy * m_dy)); + } + Simulator::Schedule (delay, &RandomWalkPosition::Reset, this); +} + +void +RandomWalkPosition::Update (void) const +{ + Time deltaTime = Simulator::Now () - m_prevTime; + m_prevTime = Simulator::Now (); + double deltaS = deltaTime.GetSeconds (); + m_x += m_dx * deltaS; + m_y += m_dy * deltaS; +} + +void +RandomWalkPosition::DoGet (double &x, double &y, double &z) const +{ + Update (); + x = m_x; + y = m_y; + z = 0; +} +void +RandomWalkPosition::DoSet (double x, double y, double z) +{ + m_x = x; + m_y = y; + m_prevTime = Simulator::Now (); +} + + +} // namespace ns3 diff --git a/src/node/random-walk-position.h b/src/node/random-walk-position.h new file mode 100644 index 000000000..c4b814b79 --- /dev/null +++ b/src/node/random-walk-position.h @@ -0,0 +1,94 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2006,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 RANDOM_WALK_POSITION_H +#define RANDOM_WALK_POSITION_H + +#include "ns3/object.h" +#include "ns3/position.h" +#include "ns3/nstime.h" +#include "ns3/random-variable.h" + +namespace ns3 { + +class RandomWalkPositionParameters : public Object +{ + public: + enum Mode { + MODE_DISTANCE, + MODE_TIME + }; + RandomWalkPositionParameters (); + bool IsDefault (void) const; + void SetSpeedBounds (double minSpeed, double maxSpeed); + private: + friend class RandomWalkPosition; + double m_xMin; + double m_xMax; + double m_yMin; + double m_yMax; + double m_minSpeed; + double m_maxSpeed; + enum Mode m_mode; + double m_modeDistance; + Time m_modeTime; +}; + +/** + * \brief a random walk position model + * + * Each instance moves with a speed and direction choosen at random + * in the intervals [minspeed,maxspeed] and [0,2pi] until + * either a fixed distance has been walked or until a fixed period + * of time. + */ +class RandomWalkPosition : public Position +{ + public: + /** + * Create a new position object located at a random + * position within the default random walk area. + */ + RandomWalkPosition (); + RandomWalkPosition (double x, double y); + RandomWalkPosition (Ptr parameters); + RandomWalkPosition (Ptr parameters, + double x, double y); + private: + virtual void DoGet (double &x, double &y, double &z) const; + virtual void DoSet (double x, double y, double z); + + void Reset (void); + void Update (void) const; + static Ptr GetDefaultParameters (void); + static UniformVariable m_randomDirection; + + mutable double m_x; + mutable double m_y; + double m_dx; + double m_dy; + mutable Time m_prevTime; + Ptr m_parameters; +}; + + +} // namespace ns3 + +#endif /* RANDOM_WALK_POSITION_H */ From d27798e7970fcb9125e33d1688064ca70d56d5c9 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 2 Jul 2007 14:57:19 +0200 Subject: [PATCH 013/148] rework the notification mechanism to allow its optional use with every position model --- SConstruct | 4 +- samples/main-random-walk.cc | 15 +++++++ ...c-position.cc => position-set-notifier.cc} | 27 +++++------- ...tic-position.h => position-set-notifier.h} | 41 +++++++------------ src/node/position.cc | 16 +++++++- src/node/position.h | 8 ++++ src/node/random-walk-position.cc | 18 ++++++-- src/node/static-position.cc | 16 +++----- src/node/static-position.h | 7 ---- src/node/static-speed-position.cc | 18 ++++++++ 10 files changed, 100 insertions(+), 70 deletions(-) create mode 100644 samples/main-random-walk.cc rename src/node/{notify-static-position.cc => position-set-notifier.cc} (62%) rename src/node/{notify-static-position.h => position-set-notifier.h} (66%) diff --git a/SConstruct b/SConstruct index 95f578c36..320bd5c3f 100644 --- a/SConstruct +++ b/SConstruct @@ -245,8 +245,8 @@ node.add_sources ([ 'ipv4.cc', 'application.cc', 'position.cc', + 'position-set-notifier.cc', 'static-position.cc', - 'notify-static-position.cc', 'static-speed-position.cc', 'grid-topology.cc', 'random-walk-position.cc', @@ -268,8 +268,8 @@ node.add_inst_headers ([ 'ipv4.h', 'application.h', 'position.h', + 'position-set-notifier.h', 'static-position.h', - 'notify-static-position.h', 'static-speed-position.h', 'grid-topology.h', 'random-walk-position.h', diff --git a/samples/main-random-walk.cc b/samples/main-random-walk.cc new file mode 100644 index 000000000..2ca10fa5a --- /dev/null +++ b/samples/main-random-walk.cc @@ -0,0 +1,15 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ + + +int main (int argc, char *argv[]) +{ + Bind ("RandomWalkMinSpeed", "2"); + Bind ("RandomWalkMaxSpeed", "3"); + Bind ("RandomWalkMode", "Time"); + Bind ("RandomWalkModeDistance", "20"); + Bind ("RandomWalkModeTime", "2s"); + + RandomWalkPosition pos; + + return 0; +} diff --git a/src/node/notify-static-position.cc b/src/node/position-set-notifier.cc similarity index 62% rename from src/node/notify-static-position.cc rename to src/node/position-set-notifier.cc index 9394ffa92..9a90623be 100644 --- a/src/node/notify-static-position.cc +++ b/src/node/position-set-notifier.cc @@ -18,32 +18,25 @@ * * Author: Mathieu Lacage */ -#include "notify-static-position.h" +#include "position-set-notifier.h" namespace ns3 { -const InterfaceId NotifyStaticPosition::iid = MakeInterfaceId ("NotifyStaticPosition", StaticPosition::iid); -const ClassId NotifyStaticPosition::cid = - MakeClassId ("NotifyStaticPosition", - NotifyStaticPosition::iid); +const InterfaceId PositionSetNotifier::iid = MakeInterfaceId ("PositionSetNotifier", Object::iid); +const ClassId PositionSetNotifier::cid = + MakeClassId ("PositionSetNotifier", + PositionSetNotifier::iid); -NotifyStaticPosition::NotifyStaticPosition () - : StaticPosition () -{} -NotifyStaticPosition::NotifyStaticPosition (double x, double y) - : StaticPosition (x, y, 0.0) -{} -NotifyStaticPosition::NotifyStaticPosition (double x, double y, double z) - : StaticPosition (x, y, z) +PositionSetNotifier::PositionSetNotifier () {} void -NotifyStaticPosition::RegisterListener (Listener listener) +PositionSetNotifier::RegisterListener (Listener listener) { m_listeners.push_back (listener); } void -NotifyStaticPosition::UnregisterListener (Listener callback) +PositionSetNotifier::UnregisterListener (Listener callback) { for (std::list::iterator i = m_listeners.begin (); i != m_listeners.end ();) @@ -60,13 +53,13 @@ NotifyStaticPosition::UnregisterListener (Listener callback) } } void -NotifyStaticPosition::NotifyPositionChange (void) const +PositionSetNotifier::Notify (Ptr position) const { for (std::list::const_iterator i = m_listeners.begin (); i != m_listeners.end (); i++) { Listener listener = *i; - listener (*this); + listener (position); } } diff --git a/src/node/notify-static-position.h b/src/node/position-set-notifier.h similarity index 66% rename from src/node/notify-static-position.h rename to src/node/position-set-notifier.h index 9483287c2..4affdfe14 100644 --- a/src/node/notify-static-position.h +++ b/src/node/position-set-notifier.h @@ -18,47 +18,36 @@ * * Author: Mathieu Lacage */ -#ifndef NOTIFY_STATIC_POSITION_H -#define NOTIFY_STATIC_POSITION_H +#ifndef POSITION_SET_NOTIFIER_H +#define POSITION_SET_NOTIFIER_H -#include "ns3/callback.h" +#include "ns3/object.h" #include "ns3/component-manager.h" -#include "static-position.h" -#include +#include "ns3/callback.h" +#include "position.h" namespace ns3 { /** * \brief notify listeners of position changes. */ -class NotifyStaticPosition : public StaticPosition +class PositionSetNotifier : public Object { public: static const InterfaceId iid; static const ClassId cid; - typedef Callback Listener; + typedef Callback > Listener; + /** - * Create a position located at coordinates (0,0,0) + * Create a new position notifier */ - NotifyStaticPosition (); + PositionSetNotifier (); + /** - * \param x x coordinate - * \param y y coordinate - * - * Create a position located at coordinates (x,y,0) - * Unit is meters + * \param position the position which just changed. */ - NotifyStaticPosition (double x, double y); - /** - * \param x x coordinate - * \param y y coordinate - * \param z z coordinate - * - * Create a position located at coordinates (x,y,z) - * Unit is meters - */ - NotifyStaticPosition (double x, double y, double z); + void Notify (Ptr position) const; /** * \param listener listener to add @@ -75,11 +64,9 @@ public: */ void UnregisterListener (Listener listener); private: - virtual void NotifyPositionChange (void) const; std::list m_listeners; }; } // namespace ns3 - -#endif /* NOTIFY_STATIC_POSITION_H */ +#endif /* POSITION_SET_NOTIFIER_H */ diff --git a/src/node/position.cc b/src/node/position.cc index f60ee67a2..a63acbbd9 100644 --- a/src/node/position.cc +++ b/src/node/position.cc @@ -19,6 +19,7 @@ * Author: Mathieu Lacage */ #include "position.h" +#include "position-set-notifier.h" #include namespace ns3 { @@ -66,7 +67,7 @@ Position::SetXY (double x, double y) double currentX, currentY, currentZ; DoGet (currentX, currentY, currentZ); DoSet (x, y, currentZ); -} + } void Position::SetX (double x) { @@ -103,4 +104,15 @@ Position::GetDistanceFrom (const Position &position) const return sqrt (dx*dx+dy*dy+dz*dz); } -}; // namespace ns3 +void +Position::NotifyCourseChange (void) const +{ + Ptr notifier = + QueryInterface (PositionSetNotifier::iid); + if (notifier != 0) + { + notifier->Notify (this); + } +} + +} // namespace ns3 diff --git a/src/node/position.h b/src/node/position.h index 46565716f..5ac383d47 100644 --- a/src/node/position.h +++ b/src/node/position.h @@ -67,6 +67,8 @@ public: */ double GetZ (void) const; + void Add (double dx, double dy, double dz); + void Set (double x, double y, double z); void SetXY (double x, double y); void SetX (double x); @@ -79,6 +81,12 @@ public: * Unit is meters */ double GetDistanceFrom (const Position &position) const; +protected: + /** + * Must be invoked by subclasses when the course of the + * position changes to notify course change listeners. + */ + void NotifyCourseChange (void) const; private: /** * \param x reference to floating-point variable for x coordinate. diff --git a/src/node/random-walk-position.cc b/src/node/random-walk-position.cc index e545bc4e7..4a01545c2 100644 --- a/src/node/random-walk-position.cc +++ b/src/node/random-walk-position.cc @@ -106,10 +106,10 @@ RandomWalkPosition::Reset (void) double speed = UniformVariable::GetSingleValue (m_parameters->m_minSpeed, m_parameters->m_maxSpeed); double direction = m_randomDirection.GetValue (); - double dx = std::cos (direction); - double dy = std::sin (direction); - m_dx = dx * speed; - m_dy = dy * speed; + double dx = std::cos (direction) * speed; + double dy = std::sin (direction) * speed; + m_dx = dx; + m_dy = dy; Time delay; if (m_parameters->m_mode == RandomWalkPositionParameters::MODE_TIME) { @@ -120,6 +120,7 @@ RandomWalkPosition::Reset (void) double distance = g_modeDistance.GetValue (); delay = Seconds (distance / sqrt (m_dx * m_dx + m_dy * m_dy)); } + NotifyCourseChange (); Simulator::Schedule (delay, &RandomWalkPosition::Reset, this); } @@ -144,9 +145,18 @@ RandomWalkPosition::DoGet (double &x, double &y, double &z) const void RandomWalkPosition::DoSet (double x, double y, double z) { + bool changed = false; + if (m_x != x || m_y != y) + { + changed = true; + } m_x = x; m_y = y; m_prevTime = Simulator::Now (); + if (changed) + { + NotifyCourseChange (); + } } diff --git a/src/node/static-position.cc b/src/node/static-position.cc index 024c5151e..f4de7f914 100644 --- a/src/node/static-position.cc +++ b/src/node/static-position.cc @@ -38,10 +38,6 @@ StaticPosition::StaticPosition (double x, double y, double z) StaticPosition::~StaticPosition () {} -void -StaticPosition::NotifyPositionChange (void) const -{} - void StaticPosition::DoGet (double &x, double &y, double &z) const { @@ -52,19 +48,17 @@ StaticPosition::DoGet (double &x, double &y, double &z) const void StaticPosition::DoSet (double x, double y, double z) { - bool mustNotify = false; - if (x != m_x || - y != m_y || - z != m_z) + bool changed = false; + if (m_x != x || m_y != y || m_z != z) { - mustNotify = true; + changed = true; } m_x = x; m_y = y; m_z = z; - if (mustNotify) + if (changed) { - NotifyPositionChange (); + NotifyCourseChange (); } } diff --git a/src/node/static-position.h b/src/node/static-position.h index 67e6bd7fd..5bfd2f650 100644 --- a/src/node/static-position.h +++ b/src/node/static-position.h @@ -60,13 +60,6 @@ public: virtual ~StaticPosition (); private: - /** - * Subclasses must override this virtual method to be notified - * of a call to one of the Set methods which changes the current - * position. The default implementation does nothing. This method - * is invoked _after_ the current position has been updated. - */ - virtual void NotifyPositionChange (void) const; virtual void DoGet (double &x, double &y, double &z) const; virtual void DoSet (double x, double y, double z); double m_x; diff --git a/src/node/static-speed-position.cc b/src/node/static-speed-position.cc index fd2b45f15..4defb8e58 100644 --- a/src/node/static-speed-position.cc +++ b/src/node/static-speed-position.cc @@ -73,10 +73,19 @@ StaticSpeedPosition::~StaticSpeedPosition () void StaticSpeedPosition::SetSpeed (double dx, double dy, double dz) { + bool changed = false; Update (); + if (m_dx != dx || m_dy != dy || m_dz != dz) + { + changed = true; + } m_dx = dx; m_dy = dy; m_dz = dz; + if (changed) + { + NotifyCourseChange (); + } } void @@ -101,10 +110,19 @@ StaticSpeedPosition::DoGet (double &x, double &y, double &z) const void StaticSpeedPosition::DoSet (double x, double y, double z) { + bool changed = false; Update (); + if (m_x != x || m_y != y || m_z != z) + { + changed = true; + } m_x = x; m_y = y; m_z = z; + if (changed) + { + NotifyCourseChange (); + } } }; // namespace ns3 From 77eb4ec65ac371e83d4dbe34efbafb84a1295453 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 2 Jul 2007 15:08:10 +0200 Subject: [PATCH 014/148] improve sample code, make it build --- SConstruct | 6 ++++++ samples/main-random-walk.cc | 29 ++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/SConstruct b/SConstruct index 320bd5c3f..d77098af2 100644 --- a/SConstruct +++ b/SConstruct @@ -413,6 +413,12 @@ ns3.add(sample_callback) sample_callback.add_dep('core') sample_callback.add_source('main-callback.cc') +sample_random_walk = build.Ns3Module('sample-random-walk', 'samples') +sample_random_walk.set_executable() +ns3.add(sample_random_walk) +sample_random_walk.add_deps(['core', 'node']) +sample_random_walk.add_source('main-random-walk.cc') + sample_ptr = build.Ns3Module('sample-ptr', 'samples') sample_ptr.set_executable() ns3.add(sample_ptr) diff --git a/samples/main-random-walk.cc b/samples/main-random-walk.cc index 2ca10fa5a..59ade2f0a 100644 --- a/samples/main-random-walk.cc +++ b/samples/main-random-walk.cc @@ -1,5 +1,23 @@ /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +#include "ns3/ptr.h" +#include "ns3/position.h" +#include "ns3/position-set-notifier.h" +#include "ns3/random-walk-position.h" +#include "ns3/default-value.h" +#include "ns3/command-line.h" +#include "ns3/simulator.h" +#include "ns3/nstime.h" + +using namespace ns3; + +static void +CourseChange (Ptr position) +{ + double x, y, z; + position->Get (x, y, z); + std::cout << "pos=" << position << ", x=" << x << ", y=" << y << ", z=" << z << std::endl; +} int main (int argc, char *argv[]) { @@ -9,7 +27,16 @@ int main (int argc, char *argv[]) Bind ("RandomWalkModeDistance", "20"); Bind ("RandomWalkModeTime", "2s"); - RandomWalkPosition pos; + CommandLine::Parse (argc, argv); + + Ptr notifier = Create (); + Ptr position = Create (); + position->AddInterface (notifier); + notifier->RegisterListener (MakeCallback (&CourseChange)); + + Simulator::StopAt (Seconds (10.0)); + + Simulator::Run (); return 0; } From e4d291802b47914504f5acac31793a489e6869b1 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 2 Jul 2007 15:15:25 +0200 Subject: [PATCH 015/148] make the sample code actually do something --- samples/main-random-walk.cc | 2 +- src/node/position-set-notifier.cc | 4 +++- src/node/position.cc | 5 +++++ src/node/position.h | 1 + src/node/random-walk-position.cc | 1 + src/node/static-position.cc | 12 +++++++++--- src/node/static-speed-position.cc | 16 ++++++++++++---- 7 files changed, 32 insertions(+), 9 deletions(-) diff --git a/samples/main-random-walk.cc b/samples/main-random-walk.cc index 59ade2f0a..04228546a 100644 --- a/samples/main-random-walk.cc +++ b/samples/main-random-walk.cc @@ -16,7 +16,7 @@ CourseChange (Ptr position) { double x, y, z; position->Get (x, y, z); - std::cout << "pos=" << position << ", x=" << x << ", y=" << y << ", z=" << z << std::endl; + std::cout << Simulator::Now () << ", pos=" << position << ", x=" << x << ", y=" << y << ", z=" << z << std::endl; } int main (int argc, char *argv[]) diff --git a/src/node/position-set-notifier.cc b/src/node/position-set-notifier.cc index 9a90623be..ffbdde559 100644 --- a/src/node/position-set-notifier.cc +++ b/src/node/position-set-notifier.cc @@ -28,7 +28,9 @@ const ClassId PositionSetNotifier::cid = PositionSetNotifier::iid); PositionSetNotifier::PositionSetNotifier () -{} +{ + SetInterfaceId (PositionSetNotifier::iid); +} void PositionSetNotifier::RegisterListener (Listener listener) diff --git a/src/node/position.cc b/src/node/position.cc index a63acbbd9..9390ea020 100644 --- a/src/node/position.cc +++ b/src/node/position.cc @@ -26,6 +26,11 @@ namespace ns3 { const InterfaceId Position::iid = MakeInterfaceId ("Position", Object::iid); +Position::Position () +{ + SetInterfaceId (Position::iid); +} + Position::~Position () {} diff --git a/src/node/position.h b/src/node/position.h index 5ac383d47..f86f4aab3 100644 --- a/src/node/position.h +++ b/src/node/position.h @@ -36,6 +36,7 @@ class Position : public Object { public: static const InterfaceId iid; + Position (); virtual ~Position () = 0; /** diff --git a/src/node/random-walk-position.cc b/src/node/random-walk-position.cc index 4a01545c2..ffb412326 100644 --- a/src/node/random-walk-position.cc +++ b/src/node/random-walk-position.cc @@ -96,6 +96,7 @@ RandomWalkPosition::RandomWalkPosition () m_prevTime (Simulator::Now ()), m_parameters (RandomWalkPosition::GetDefaultParameters ()) { + SetInterfaceId (RandomWalkPosition::iid); Reset (); } diff --git a/src/node/static-position.cc b/src/node/static-position.cc index f4de7f914..b13e1b1ab 100644 --- a/src/node/static-position.cc +++ b/src/node/static-position.cc @@ -28,13 +28,19 @@ const ClassId StaticPosition::cid = MakeClassId ( StaticPosition::StaticPosition () : m_x (0.0), m_y (0.0), m_z (0.0) -{} +{ + SetInterfaceId (StaticPosition::iid); +} StaticPosition::StaticPosition (double x, double y) : m_x (x), m_y (y), m_z (0.0) -{} +{ + SetInterfaceId (StaticPosition::iid); +} StaticPosition::StaticPosition (double x, double y, double z) : m_x (x), m_y (y), m_z (z) -{} +{ + SetInterfaceId (StaticPosition::iid); +} StaticPosition::~StaticPosition () {} diff --git a/src/node/static-speed-position.cc b/src/node/static-speed-position.cc index 4defb8e58..7676a22fe 100644 --- a/src/node/static-speed-position.cc +++ b/src/node/static-speed-position.cc @@ -37,7 +37,9 @@ StaticSpeedPosition::StaticSpeedPosition () m_dy (0.0), m_dz (0.0), m_prevTime (Simulator::Now ()) -{} +{ + SetInterfaceId (StaticSpeedPosition::iid); +} StaticSpeedPosition::StaticSpeedPosition (double x, double y, double z) : m_x (x), m_y (y), @@ -46,7 +48,9 @@ StaticSpeedPosition::StaticSpeedPosition (double x, double y, double z) m_dy (0.0), m_dz (0.0), m_prevTime (Simulator::Now ()) -{} +{ + SetInterfaceId (StaticSpeedPosition::iid); +} StaticSpeedPosition::StaticSpeedPosition (double x, double y) : m_x (x), m_y (y), @@ -55,7 +59,9 @@ StaticSpeedPosition::StaticSpeedPosition (double x, double y) m_dy (0.0), m_dz (0.0), m_prevTime (Simulator::Now ()) -{} +{ + SetInterfaceId (StaticSpeedPosition::iid); +} StaticSpeedPosition::StaticSpeedPosition (double x, double y, double z, double dx, double dy, double dz) : m_x (x), @@ -65,7 +71,9 @@ StaticSpeedPosition::StaticSpeedPosition (double x, double y, double z, m_dy (dy), m_dz (dz), m_prevTime (Simulator::Now ()) -{} +{ + SetInterfaceId (StaticSpeedPosition::iid); +} StaticSpeedPosition::~StaticSpeedPosition () {} From e57869a38e3c77c07e26d01a2f6e7cdc125d07e5 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 2 Jul 2007 15:27:53 +0200 Subject: [PATCH 016/148] add some debugging output. initialize the time argument correctly. --- src/node/random-walk-position.cc | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/node/random-walk-position.cc b/src/node/random-walk-position.cc index ffb412326..0cdb61998 100644 --- a/src/node/random-walk-position.cc +++ b/src/node/random-walk-position.cc @@ -22,8 +22,11 @@ #include "ns3/default-value.h" #include "ns3/time-default-value.h" #include "ns3/simulator.h" +#include "ns3/debug.h" #include +NS_DEBUG_COMPONENT_DEFINE ("RandomWalk"); + namespace ns3 { static IntegerDefaultValue g_minSpeed ("RandomWalkMinSpeed", @@ -51,7 +54,7 @@ RandomWalkPositionParameters::RandomWalkPositionParameters () m_maxSpeed (g_maxSpeed.GetValue ()), m_mode (g_mode.GetValue ()), m_modeDistance (g_modeDistance.GetValue ()), - m_modeTime (g_modeDistance.GetValue ()) + m_modeTime (g_modeTime.GetValue ()) {} bool RandomWalkPositionParameters::IsDefault (void) const @@ -122,6 +125,7 @@ RandomWalkPosition::Reset (void) delay = Seconds (distance / sqrt (m_dx * m_dx + m_dy * m_dy)); } NotifyCourseChange (); + NS_DEBUG ("change speed at " << Simulator::Now () << " in " << delay); Simulator::Schedule (delay, &RandomWalkPosition::Reset, this); } From 2f8fdae0db48ab3c8927198194178b5607f198a7 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 2 Jul 2007 15:28:28 +0200 Subject: [PATCH 017/148] increase sample simulation run --- samples/main-random-walk.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/main-random-walk.cc b/samples/main-random-walk.cc index 04228546a..c0f77bd4b 100644 --- a/samples/main-random-walk.cc +++ b/samples/main-random-walk.cc @@ -34,7 +34,7 @@ int main (int argc, char *argv[]) position->AddInterface (notifier); notifier->RegisterListener (MakeCallback (&CourseChange)); - Simulator::StopAt (Seconds (10.0)); + Simulator::StopAt (Seconds (20.0)); Simulator::Run (); From 57b5993f8dd56ee3bcb3795cbda8955189d90db0 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 2 Jul 2007 18:12:27 +0200 Subject: [PATCH 018/148] add dox documentation --- src/node/random-walk-position.h | 51 +++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 9 deletions(-) diff --git a/src/node/random-walk-position.h b/src/node/random-walk-position.h index c4b814b79..3a7575d8e 100644 --- a/src/node/random-walk-position.h +++ b/src/node/random-walk-position.h @@ -28,6 +28,9 @@ namespace ns3 { +/** + * \brief parameters to control a random walk model + */ class RandomWalkPositionParameters : public Object { public: @@ -35,15 +38,31 @@ class RandomWalkPositionParameters : public Object MODE_DISTANCE, MODE_TIME }; + /** + * Instantiate a set of RandomWalk parameters initialized + * with the Bind default values. + */ RandomWalkPositionParameters (); - bool IsDefault (void) const; + /** + * \param minSpeed the minimum speed + * \param maxSpeed the maximum speed + * + * The speed of any node is chosen such that minSpeed <= speed <= maxSpeed + */ void SetSpeedBounds (double minSpeed, double maxSpeed); + /** + * \param distance the distance before a direction change + * + * Unit is meters + */ + void SetModeDistance (double distance); + /** + * \param time the delay before a direction change. + */ + void SetModeTime (Time time); private: + bool IsDefault (void) const; friend class RandomWalkPosition; - double m_xMin; - double m_xMax; - double m_yMin; - double m_yMax; double m_minSpeed; double m_maxSpeed; enum Mode m_mode; @@ -52,23 +71,37 @@ class RandomWalkPositionParameters : public Object }; /** - * \brief a random walk position model + * \brief an unbounded 2D random walk position model * * Each instance moves with a speed and direction choosen at random * in the intervals [minspeed,maxspeed] and [0,2pi] until - * either a fixed distance has been walked or until a fixed period + * either a fixed distance has been walked or until a fixed amount * of time. + * + * The parameters of the model can be specified either with the ns3::Bind + * function and the variables "RandomWalkMinSpeed", "RandomWalkMaxSpeed", + * "RandomWalkMode", "RandomWalkModeDistance", and, "RandomWalkModeTime" or + * with an instance of the RandomWalkPositionParameters class which + * must be fed to the RandomWalkPosition constructors. */ class RandomWalkPosition : public Position { public: /** - * Create a new position object located at a random - * position within the default random walk area. + * Create a new position object located at position (0,0,0) */ RandomWalkPosition (); + /** + * Create a new position object located at position (x,y,0) + */ RandomWalkPosition (double x, double y); + /** + * Create a new position object located at position (0,0,0) + */ RandomWalkPosition (Ptr parameters); + /** + * Create a new position object located at position (x,y,0) + */ RandomWalkPosition (Ptr parameters, double x, double y); private: From c14499b211479a44a6c1f648ffb3ca3441ebded5 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 2 Jul 2007 18:14:20 +0200 Subject: [PATCH 019/148] add dox documentation --- src/simulator/time-default-value.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/simulator/time-default-value.h b/src/simulator/time-default-value.h index d417c59c0..87d0c7fcb 100644 --- a/src/simulator/time-default-value.h +++ b/src/simulator/time-default-value.h @@ -26,12 +26,35 @@ namespace ns3 { +/** + * \ingroup config + * \brief a ns3::Time variable for ns3::Bind + * + * Every instance of this type is automatically + * registered in the variable pool which is used + * by ns3::Bind. + */ class TimeDefaultValue : public DefaultValueBase { public: + /** + * \param name name of variable + * \param help help text which explains the purpose + * and the semantics of this variable + * \param defaultValue the default value to assign + * to this variable. + * + * Unless the user invokes ns3::Bind with the right arguments, + * the GetValue method will return the default value. Otherwise, + * it will return the user-specified value. + */ TimeDefaultValue (const std::string name, const std::string help, Time defaultValue); + /** + * \returns the default value for this variable or a + * user-provided overriden variable. + */ Time GetValue (void) const; private: virtual bool DoParseValue (const std::string &value); From 2f29b73496832447de593fed1f96f5d738b75f4c Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 2 Jul 2007 20:12:01 +0200 Subject: [PATCH 020/148] improve grid topology sample code and add dox documentation --- SConstruct | 6 +++++ samples/main-grid-topology.cc | 48 +++++++++++++++++++++++++++++++++++ src/node/grid-topology.cc | 10 ++++---- src/node/grid-topology.h | 26 ++++++++++++++++--- 4 files changed, 82 insertions(+), 8 deletions(-) create mode 100644 samples/main-grid-topology.cc diff --git a/SConstruct b/SConstruct index d77098af2..7342503d2 100644 --- a/SConstruct +++ b/SConstruct @@ -419,6 +419,12 @@ ns3.add(sample_random_walk) sample_random_walk.add_deps(['core', 'node']) sample_random_walk.add_source('main-random-walk.cc') +sample_grid_topology = build.Ns3Module('sample-grid-topology', 'samples') +sample_grid_topology.set_executable() +ns3.add(sample_grid_topology) +sample_grid_topology.add_deps(['core', 'internet-node']) +sample_grid_topology.add_source('main-grid-topology.cc') + sample_ptr = build.Ns3Module('sample-ptr', 'samples') sample_ptr.set_executable() ns3.add(sample_ptr) diff --git a/samples/main-grid-topology.cc b/samples/main-grid-topology.cc new file mode 100644 index 000000000..41ca3c638 --- /dev/null +++ b/samples/main-grid-topology.cc @@ -0,0 +1,48 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ + +#include "ns3/ptr.h" +#include "ns3/grid-topology.h" +#include "ns3/static-position.h" +#include "ns3/internet-node.h" + +using namespace ns3; + +int main (int argc, char *argv[]) +{ + std::vector > nodes; + + // create an array of empty nodes for testing purposes + for (uint32_t i = 0; i < 120; i++) + { + nodes.push_back (Create ()); + } + + // setup the grid itself: objects are layed out + // started from (-100,-100) with 20 objects per row, + // the x interval between each object is 5 meters + // and the y interval between each object is 20 meters + GridTopology grid (-100, -100, 20, 5, 20); + + // each object will be attached a static position. + grid.SetPositionModel (StaticPosition::cid); + + // finalize the setup by attaching to each object + // in the input array a position and initializing + // this position with the calculated coordinates. + grid.Create (nodes); + + + // iterate our nodes and print their position. + for (std::vector >::const_iterator j = nodes.begin (); + j != nodes.end (); j++) + { + Ptr object = *j; + Ptr position = object->QueryInterface (Position::iid); + double x, y, z; + position->Get (x,y,z); + std::cout << "x=" << x << ", y=" << y << ", z=" << z << std::endl; + } + + + return 0; +} diff --git a/src/node/grid-topology.cc b/src/node/grid-topology.cc index 1796fb233..3e809e324 100644 --- a/src/node/grid-topology.cc +++ b/src/node/grid-topology.cc @@ -39,18 +39,18 @@ GridTopology::SetPositionModel (ClassId classId) } void -GridTopology::Create (std::vector > nodes) +GridTopology::Create (std::vector > objects) { double x, y; uint32_t col; x = m_xMin; y = m_yMin; col = 0; - for (std::vector >::const_iterator i = nodes.begin (); - i != nodes.end (); i++) + for (std::vector >::const_iterator i = objects.begin (); + i != objects.end (); i++) { - Ptr node = *i; - node->AddInterface (ComponentManager::Create (m_positionClassId, x, y)); + Ptr object = *i; + object->AddInterface (ComponentManager::Create (m_positionClassId, x, y)); x += m_deltaX; col++; if (col == m_nCols) diff --git a/src/node/grid-topology.h b/src/node/grid-topology.h index f624af893..2ff50031b 100644 --- a/src/node/grid-topology.h +++ b/src/node/grid-topology.h @@ -24,21 +24,41 @@ #include #include "ns3/component-manager.h" #include "ns3/ptr.h" -#include "node.h" namespace ns3 { +/** + * \brief a 2D grid of objects + */ class GridTopology { public: + /** + * \param xMin the left boundary where the objects will start being arranged. + * \param yMin the lower boundary where the objects will start being arranged. + * \param nCols number of objects for each row + * \param deltaX distance separating two adjacent objects along the x axis. + * \param deltaY distance separating two adjacent objects along the y axis. + * + * The first object is positioned at (xMin,yMin). + */ GridTopology (double xMin, double yMin, uint32_t nCols, double deltaX, double deltaY); + /** + * \param classId the classId of the position object to attach to each + * input object. + */ void SetPositionModel (ClassId classId); /** - * Add position to each node in vector. + * \param objects a vector of objects + * + * Attach a position (the type of position is specified through + * the ClassId given to SetPositionModel) to each object present + * in the input vector and configure its initial location with a set + * of coordinates arranged according to a regular rectangular grid. */ - void Create (std::vector > nodes); + void Create (std::vector > objects); private: GridTopology (); double m_xMin; From af6777397c2b700546070d0ced2eb9a4405ae39b Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 2 Jul 2007 20:17:06 +0200 Subject: [PATCH 021/148] rename GridTopology::Create to GridTopology::ArrangeHorizontally --- samples/main-grid-topology.cc | 2 +- src/node/grid-topology.cc | 32 ++++++++++++++++++++++++++++---- src/node/grid-topology.h | 20 ++++++++++++++++---- 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/samples/main-grid-topology.cc b/samples/main-grid-topology.cc index 41ca3c638..f2837d4cf 100644 --- a/samples/main-grid-topology.cc +++ b/samples/main-grid-topology.cc @@ -29,7 +29,7 @@ int main (int argc, char *argv[]) // finalize the setup by attaching to each object // in the input array a position and initializing // this position with the calculated coordinates. - grid.Create (nodes); + grid.ArrangeHorizontally (nodes); // iterate our nodes and print their position. diff --git a/src/node/grid-topology.cc b/src/node/grid-topology.cc index 3e809e324..d4e637f95 100644 --- a/src/node/grid-topology.cc +++ b/src/node/grid-topology.cc @@ -23,10 +23,10 @@ namespace ns3 { -GridTopology::GridTopology (double xMin, double yMin, uint32_t nCols, double deltaX, double deltaY) +GridTopology::GridTopology (double xMin, double yMin, uint32_t n, double deltaX, double deltaY) : m_xMin (xMin), m_yMin (yMin), - m_nCols (nCols), + m_n (n), m_deltaX (deltaX), m_deltaY (deltaY), m_positionClassId (StaticPosition::cid) @@ -39,7 +39,7 @@ GridTopology::SetPositionModel (ClassId classId) } void -GridTopology::Create (std::vector > objects) +GridTopology::ArrangeHorizontally (std::vector > objects) { double x, y; uint32_t col; @@ -53,7 +53,7 @@ GridTopology::Create (std::vector > objects) object->AddInterface (ComponentManager::Create (m_positionClassId, x, y)); x += m_deltaX; col++; - if (col == m_nCols) + if (col == m_n) { col = 0; x = m_xMin; @@ -62,4 +62,28 @@ GridTopology::Create (std::vector > objects) } } +void +GridTopology::ArrangeVertically (std::vector > objects) +{ + double x, y; + uint32_t row; + x = m_xMin; + y = m_yMin; + row = 0; + for (std::vector >::const_iterator i = objects.begin (); + i != objects.end (); i++) + { + Ptr object = *i; + object->AddInterface (ComponentManager::Create (m_positionClassId, x, y)); + y += m_deltaY; + row++; + if (row == m_n) + { + row = 0; + y = m_yMin; + x += m_deltaX; + } + } +} + } // namespace ns3 diff --git a/src/node/grid-topology.h b/src/node/grid-topology.h index 2ff50031b..822024038 100644 --- a/src/node/grid-topology.h +++ b/src/node/grid-topology.h @@ -36,7 +36,7 @@ class GridTopology /** * \param xMin the left boundary where the objects will start being arranged. * \param yMin the lower boundary where the objects will start being arranged. - * \param nCols number of objects for each row + * \param n number of objects for each row or column * \param deltaX distance separating two adjacent objects along the x axis. * \param deltaY distance separating two adjacent objects along the y axis. * @@ -56,14 +56,26 @@ class GridTopology * Attach a position (the type of position is specified through * the ClassId given to SetPositionModel) to each object present * in the input vector and configure its initial location with a set - * of coordinates arranged according to a regular rectangular grid. + * of coordinates arranged according to a regular rectangular grid, + * one row after the other. */ - void Create (std::vector > objects); + void ArrangeHorizontally (std::vector > objects); + + /** + * \param objects a vector of objects + * + * Attach a position (the type of position is specified through + * the ClassId given to SetPositionModel) to each object present + * in the input vector and configure its initial location with a set + * of coordinates arranged according to a regular rectangular grid, + * one column after the other. + */ + void ArrangeVertically (std::vector > objects); private: GridTopology (); double m_xMin; double m_yMin; - uint32_t m_nCols; + uint32_t m_n; double m_deltaX; double m_deltaY; ClassId m_positionClassId; From 3f87b85886dd6beac73da02a4894feaa68e0cf61 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 2 Jul 2007 20:25:25 +0200 Subject: [PATCH 022/148] add cid/iid support to RandomWalk --- samples/main-grid-topology.cc | 3 ++- src/node/random-walk-position.cc | 18 ++++++++++++++++++ src/node/random-walk-position.h | 3 +++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/samples/main-grid-topology.cc b/samples/main-grid-topology.cc index f2837d4cf..58c831465 100644 --- a/samples/main-grid-topology.cc +++ b/samples/main-grid-topology.cc @@ -2,6 +2,7 @@ #include "ns3/ptr.h" #include "ns3/grid-topology.h" +#include "ns3/random-walk-position.h" #include "ns3/static-position.h" #include "ns3/internet-node.h" @@ -25,6 +26,7 @@ int main (int argc, char *argv[]) // each object will be attached a static position. grid.SetPositionModel (StaticPosition::cid); + //grid.SetPositionModel (RandomWalkPosition::cid); // finalize the setup by attaching to each object // in the input array a position and initializing @@ -43,6 +45,5 @@ int main (int argc, char *argv[]) std::cout << "x=" << x << ", y=" << y << ", z=" << z << std::endl; } - return 0; } diff --git a/src/node/random-walk-position.cc b/src/node/random-walk-position.cc index 0cdb61998..5c6280da4 100644 --- a/src/node/random-walk-position.cc +++ b/src/node/random-walk-position.cc @@ -29,6 +29,12 @@ NS_DEBUG_COMPONENT_DEFINE ("RandomWalk"); namespace ns3 { +const InterfaceId RandomWalkPosition::iid = + MakeInterfaceId ("RandomWalkPosition", Object::iid); +const ClassId RandomWalkPosition::cid = + MakeClassId ("RandomWalkPosition", RandomWalkPosition::iid); + + static IntegerDefaultValue g_minSpeed ("RandomWalkMinSpeed", "Minimum speed used during a random walk", 0); @@ -103,6 +109,18 @@ RandomWalkPosition::RandomWalkPosition () Reset (); } +RandomWalkPosition::RandomWalkPosition (double x, double y) + : m_x (x), + m_y (y), + m_dx (0.0), + m_dy (0.0), + m_prevTime (Simulator::Now ()), + m_parameters (RandomWalkPosition::GetDefaultParameters ()) +{ + SetInterfaceId (RandomWalkPosition::iid); + Reset (); +} + void RandomWalkPosition::Reset (void) { diff --git a/src/node/random-walk-position.h b/src/node/random-walk-position.h index 3a7575d8e..87258c310 100644 --- a/src/node/random-walk-position.h +++ b/src/node/random-walk-position.h @@ -25,6 +25,7 @@ #include "ns3/position.h" #include "ns3/nstime.h" #include "ns3/random-variable.h" +#include "ns3/component-manager.h" namespace ns3 { @@ -87,6 +88,8 @@ class RandomWalkPositionParameters : public Object class RandomWalkPosition : public Position { public: + static const InterfaceId iid; + static const ClassId cid; /** * Create a new position object located at position (0,0,0) */ From 5fdde955b33451daa0c7ff12ad8114aeac62ddfb Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Tue, 3 Jul 2007 08:54:37 +0200 Subject: [PATCH 023/148] change default values and add some debugging --- src/node/random-walk-position.cc | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/node/random-walk-position.cc b/src/node/random-walk-position.cc index 5c6280da4..700b6c82b 100644 --- a/src/node/random-walk-position.cc +++ b/src/node/random-walk-position.cc @@ -37,10 +37,10 @@ const ClassId RandomWalkPosition::cid = static IntegerDefaultValue g_minSpeed ("RandomWalkMinSpeed", "Minimum speed used during a random walk", - 0); + 0.1); static IntegerDefaultValue g_maxSpeed ("RandomWalkMaxSpeed", "Maximum speed used during a random walk", - 0); + 0.5); static EnumDefaultValue g_mode ("RandomWalkMode", "The mode indicates the condition used to " @@ -127,6 +127,8 @@ RandomWalkPosition::Reset (void) Update (); double speed = UniformVariable::GetSingleValue (m_parameters->m_minSpeed, m_parameters->m_maxSpeed); + NS_DEBUG ("min="<< m_parameters->m_minSpeed << ", max=" << m_parameters->m_maxSpeed << + ", speed=" << speed); double direction = m_randomDirection.GetValue (); double dx = std::cos (direction) * speed; double dy = std::sin (direction) * speed; @@ -139,7 +141,7 @@ RandomWalkPosition::Reset (void) } else { - double distance = g_modeDistance.GetValue (); + double distance = m_parameters->m_modeDistance; delay = Seconds (distance / sqrt (m_dx * m_dx + m_dy * m_dy)); } NotifyCourseChange (); From 872df4e54e12164213ce8f1a27b0c7e65e7a03a4 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Tue, 3 Jul 2007 08:56:40 +0200 Subject: [PATCH 024/148] RandomWalk is a Position --- src/node/random-walk-position.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node/random-walk-position.cc b/src/node/random-walk-position.cc index 700b6c82b..59deb7e37 100644 --- a/src/node/random-walk-position.cc +++ b/src/node/random-walk-position.cc @@ -30,7 +30,7 @@ NS_DEBUG_COMPONENT_DEFINE ("RandomWalk"); namespace ns3 { const InterfaceId RandomWalkPosition::iid = - MakeInterfaceId ("RandomWalkPosition", Object::iid); + MakeInterfaceId ("RandomWalkPosition", Position::iid); const ClassId RandomWalkPosition::cid = MakeClassId ("RandomWalkPosition", RandomWalkPosition::iid); From 94c1f03300cd897ee61873b8b4c5dc590fde8507 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Tue, 3 Jul 2007 09:15:20 +0200 Subject: [PATCH 025/148] test random walk position model with grid topology --- samples/main-grid-topology.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/samples/main-grid-topology.cc b/samples/main-grid-topology.cc index 58c831465..0da0c0bd0 100644 --- a/samples/main-grid-topology.cc +++ b/samples/main-grid-topology.cc @@ -40,6 +40,7 @@ int main (int argc, char *argv[]) { Ptr object = *j; Ptr position = object->QueryInterface (Position::iid); + NS_ASSERT (position != 0); double x, y, z; position->Get (x,y,z); std::cout << "x=" << x << ", y=" << y << ", z=" << z << std::endl; From 9a89cf1978342759e49d2ce2bdaf46f848160580 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Tue, 3 Jul 2007 10:04:16 +0200 Subject: [PATCH 026/148] change GridTopology API and add RandomRectangle topology with similar API. --- SConstruct | 2 + samples/main-grid-topology.cc | 3 +- src/node/grid-topology.cc | 47 ++++-------------- src/node/grid-topology.h | 36 +++++++++++++- src/node/random-rectangle-topology.cc | 60 +++++++++++++++++++++++ src/node/random-rectangle-topology.h | 69 +++++++++++++++++++++++++++ 6 files changed, 175 insertions(+), 42 deletions(-) create mode 100644 src/node/random-rectangle-topology.cc create mode 100644 src/node/random-rectangle-topology.h diff --git a/SConstruct b/SConstruct index 7342503d2..9a18bbf93 100644 --- a/SConstruct +++ b/SConstruct @@ -249,6 +249,7 @@ node.add_sources ([ 'static-position.cc', 'static-speed-position.cc', 'grid-topology.cc', + 'random-rectangle-topology.cc', 'random-walk-position.cc', ]) node.add_inst_headers ([ @@ -272,6 +273,7 @@ node.add_inst_headers ([ 'static-position.h', 'static-speed-position.h', 'grid-topology.h', + 'random-rectangle-topology.h', 'random-walk-position.h', ]) diff --git a/samples/main-grid-topology.cc b/samples/main-grid-topology.cc index 0da0c0bd0..20ce064d9 100644 --- a/samples/main-grid-topology.cc +++ b/samples/main-grid-topology.cc @@ -31,8 +31,7 @@ int main (int argc, char *argv[]) // finalize the setup by attaching to each object // in the input array a position and initializing // this position with the calculated coordinates. - grid.ArrangeHorizontally (nodes); - + grid.LayoutRowFirst (nodes.begin (), nodes.end ()); // iterate our nodes and print their position. for (std::vector >::const_iterator j = nodes.begin (); diff --git a/src/node/grid-topology.cc b/src/node/grid-topology.cc index d4e637f95..a621c7105 100644 --- a/src/node/grid-topology.cc +++ b/src/node/grid-topology.cc @@ -39,51 +39,22 @@ GridTopology::SetPositionModel (ClassId classId) } void -GridTopology::ArrangeHorizontally (std::vector > objects) +GridTopology::LayoutOneRowFirst (Ptr object, uint32_t i) { double x, y; - uint32_t col; - x = m_xMin; - y = m_yMin; - col = 0; - for (std::vector >::const_iterator i = objects.begin (); - i != objects.end (); i++) - { - Ptr object = *i; - object->AddInterface (ComponentManager::Create (m_positionClassId, x, y)); - x += m_deltaX; - col++; - if (col == m_n) - { - col = 0; - x = m_xMin; - y += m_deltaY; - } - } + x = m_xMin + m_deltaX * (i % m_n); + y = m_yMin + m_deltaY * (i / m_n); + object->AddInterface (ComponentManager::Create (m_positionClassId, x, y)); } void -GridTopology::ArrangeVertically (std::vector > objects) +GridTopology::LayoutOneColumnFirst (Ptr object, uint32_t i) { double x, y; - uint32_t row; - x = m_xMin; - y = m_yMin; - row = 0; - for (std::vector >::const_iterator i = objects.begin (); - i != objects.end (); i++) - { - Ptr object = *i; - object->AddInterface (ComponentManager::Create (m_positionClassId, x, y)); - y += m_deltaY; - row++; - if (row == m_n) - { - row = 0; - y = m_yMin; - x += m_deltaX; - } - } + x = m_xMin + m_deltaX * (i / m_n); + y = m_yMin + m_deltaY * (i % m_n); + object->AddInterface (ComponentManager::Create (m_positionClassId, x, y)); } + } // namespace ns3 diff --git a/src/node/grid-topology.h b/src/node/grid-topology.h index 822024038..f2cc724a1 100644 --- a/src/node/grid-topology.h +++ b/src/node/grid-topology.h @@ -59,7 +59,8 @@ class GridTopology * of coordinates arranged according to a regular rectangular grid, * one row after the other. */ - void ArrangeHorizontally (std::vector > objects); + template + void LayoutRowFirst (const T &begin, const T &end); /** * \param objects a vector of objects @@ -70,9 +71,12 @@ class GridTopology * of coordinates arranged according to a regular rectangular grid, * one column after the other. */ - void ArrangeVertically (std::vector > objects); + template + void LayoutColumnFirst (const T &begin, const T &end); private: GridTopology (); + void LayoutOneRowFirst (Ptr object, uint32_t i); + void LayoutOneColumnFirst (Ptr object, uint32_t i); double m_xMin; double m_yMin; uint32_t m_n; @@ -83,4 +87,32 @@ class GridTopology } // namespace ns3 +namespace ns3 { + +template +void +GridTopology::LayoutRowFirst (const T &begin, const T &end) +{ + uint32_t j = 0; + for (T i = begin; i != end; i++) + { + j++; + LayoutOneRowFirst (*i, j); + } +} + +template +void +GridTopology::LayoutColumnFirst (const T &begin, const T &end) +{ + uint32_t j = 0; + for (T i = begin; i != end; i++) + { + j++; + LayoutOneColumnFirst (*i, j); + } +} + +} // namespace ns3 + #endif /* GRID_TOPOLOGY_H */ diff --git a/src/node/random-rectangle-topology.cc b/src/node/random-rectangle-topology.cc new file mode 100644 index 000000000..b35122b1f --- /dev/null +++ b/src/node/random-rectangle-topology.cc @@ -0,0 +1,60 @@ +/* -*- 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 "random-rectangle-topology.h" +#include "static-position.h" + +namespace ns3 { + +RandomRectangleTopology::RandomRectangleTopology (double xMin, double xMax, double yMin, double yMax) + : m_xVariable (new UniformVariable (xMin, xMax)), + m_yVariable (new UniformVariable (yMin, yMax)), + m_positionModel (StaticPosition::cid) +{} +RandomRectangleTopology::RandomRectangleTopology (const RandomVariable &xVariable, + const RandomVariable &yVariable) + : m_xVariable (xVariable.Copy ()), + m_yVariable (yVariable.Copy ()), + m_positionModel (StaticPosition::cid) +{} +RandomRectangleTopology::~RandomRectangleTopology () +{ + delete m_xVariable; + delete m_yVariable; + m_xVariable = 0; + m_yVariable = 0; +} + +void +RandomRectangleTopology::SetPositionModel (ClassId classId) +{ + m_positionModel = classId; +} + +void +RandomRectangleTopology::LayoutOne (Ptr object) +{ + double x = m_xVariable->GetValue (); + double y = m_yVariable->GetValue (); + object->AddInterface (ComponentManager::Create (m_positionModel, x, y)); +} + + +} // namespace ns3 diff --git a/src/node/random-rectangle-topology.h b/src/node/random-rectangle-topology.h new file mode 100644 index 000000000..6568a993e --- /dev/null +++ b/src/node/random-rectangle-topology.h @@ -0,0 +1,69 @@ +/* -*- 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 RANDOM_RECTANGLE_TOPOLOGY_H +#define RANDOM_RECTANGLE_TOPOLOGY_H + +#include "ns3/random-variable.h" +#include "ns3/ptr.h" +#include "ns3/object.h" +#include "ns3/component-manager.h" + +namespace ns3 { + +class RandomRectangleTopology +{ + public: + + RandomRectangleTopology (double xMin, double xMax, double yMin, double yMax); + RandomRectangleTopology (const RandomVariable &xVariable, const RandomVariable &yVariable); + + ~RandomRectangleTopology (); + + void SetPositionModel (ClassId classId); + + void LayoutOne (Ptr object); + + template + void Layout (const T &begin, const T &end); + private: + RandomVariable *m_xVariable; + RandomVariable *m_yVariable; + ClassId m_positionModel; +}; + +} // namespace ns3 + +namespace ns3 { + +template +void +RandomRectangleTopology::Layout (const T &begin, const T &end) +{ + for (T i = begin; i != end; i++) + { + LayoutOne (*i); + } +} + + +} // namespace ns3 + +#endif /* RANDOM_RECTANGLE_TOPOLOGY_H */ From 5e975db36e1cb9aef0155876e780233004b0c710 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Tue, 3 Jul 2007 12:34:26 +0200 Subject: [PATCH 027/148] a bunch of new DefaultValue subclasses --- src/core/random-variable-default-value.cc | 119 ++++++++++++++++++++++ src/core/random-variable-default-value.h | 50 +++++++++ src/core/rectangle-default-value.cc | 106 +++++++++++++++++++ src/core/rectangle-default-value.h | 58 +++++++++++ 4 files changed, 333 insertions(+) create mode 100644 src/core/random-variable-default-value.cc create mode 100644 src/core/random-variable-default-value.h create mode 100644 src/core/rectangle-default-value.cc create mode 100644 src/core/rectangle-default-value.h diff --git a/src/core/random-variable-default-value.cc b/src/core/random-variable-default-value.cc new file mode 100644 index 000000000..df36d02ba --- /dev/null +++ b/src/core/random-variable-default-value.cc @@ -0,0 +1,119 @@ +/* -*- 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 "random-variable-default-value.h" + +namespace ns3 { + +RandomVariableDefaultValue::RandomVariableDefaultValue (std::string name, + std::string help, + std::string defaultValue) + : DefaultValueBase (name, help), + m_defaultValue (defaultValue), + m_value (defaultValue) +{ + if (!Parse (defaultValue, false, 0)) + { + NS_FATAL_ERROR ("Invalid Random Variable specification: " << defaultValue); + } + DefaultValueList::Add (this); +} + +RandomVariable * +RandomVariableDefaultValue::GetCopy (void) +{ + RandomVariable *variable; + bool ok = Parse (m_value, true, &variable); + NS_ASSERT (ok); + return variable; +} +double +RandomVariableDefaultValue::ReadAsDouble (std::string value, bool &ok) +{ + double v; + std::istringstream iss; + iss.str (value); + iss >> v; + ok = !iss.bad () && !iss.fail (); + return v; +} +bool +RandomVariableDefaultValue::Parse (const std::string &value, + bool mustCreate, RandomVariable **pVariable) +{ + std::string::size_type pos = value.find_first_of(":"); + if (pos == std::string::npos) + { + return false; + } + bool ok; + std::string type = value.substr (0, pos); + std::string v = value.substr (pos+1, std::string::npos); + if (type == "Constant") + { + double constant = ReadAsDouble (v, ok); + if (mustCreate) + { + *pVariable = new ConstantVariable (constant); + } + return ok; + } + else if (type == "Uniform") + { + std::string::size_type maxPos = v.find_first_of(":"); + if (maxPos == std::string::npos) + { + return false; + } + std::string min = v.substr (0, maxPos); + std::string max = v.substr (maxPos + 1, std::string::npos); + double minVal; + double maxVal; + minVal = ReadAsDouble (min, ok); + maxVal = ReadAsDouble (max, ok); + if (mustCreate) + { + *pVariable = new UniformVariable (minVal, maxVal); + } + return ok; + } + else + { + // XXX parse other types of distributions. + return false; + } +} +bool +RandomVariableDefaultValue::DoParseValue (const std::string &value) +{ + return Parse (value, false, 0); +} +std::string +RandomVariableDefaultValue::DoGetType (void) const +{ + return "(Uniform:min:max|Constant:cst)"; +} +std::string +RandomVariableDefaultValue::DoGetDefaultValue (void) const +{ + return m_defaultValue; +} + +} // namespace ns3 diff --git a/src/core/random-variable-default-value.h b/src/core/random-variable-default-value.h new file mode 100644 index 000000000..782c62331 --- /dev/null +++ b/src/core/random-variable-default-value.h @@ -0,0 +1,50 @@ +/* -*- 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 RANDOM_VARIABLE_DEFAULT_VALUE_H +#define RANDOM_VARIABLE_DEFAULT_VALUE_H + +#include "random-variable.h" +#include "default-value.h" + +namespace ns3 { + +class RandomVariableDefaultValue : public DefaultValueBase +{ + public: + RandomVariableDefaultValue (std::string name, + std::string help, + std::string defaultValue); + + RandomVariable *GetCopy (void); +private: + bool Parse (const std::string &value, bool mustCreate, RandomVariable **pVariable); + double ReadAsDouble (const std::string value, bool &ok); + virtual bool DoParseValue (const std::string &value); + virtual std::string DoGetType (void) const; + virtual std::string DoGetDefaultValue (void) const; + + std::string m_defaultValue; + std::string m_value; +}; + +} // namespace ns3 + +#endif /* RANDOM_VARIABLE_DEFAULT_VALUE_H */ diff --git a/src/core/rectangle-default-value.cc b/src/core/rectangle-default-value.cc new file mode 100644 index 000000000..96fa63be7 --- /dev/null +++ b/src/core/rectangle-default-value.cc @@ -0,0 +1,106 @@ +/* -*- 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 "rectangle-default-value.h" + +namespace ns3 { + +RectangleDefaultValue::RectangleDefaultValue (std::string name, + std::string help, + double xMin, double xMax, + double yMin, double yMax) + : DefaultValueBase (name, help), + m_xMinDefault (xMin), + m_xMaxDefault (xMax), + m_yMinDefault (yMin), + m_yMaxDefault (yMax), + m_xMin (xMin), + m_xMax (xMax), + m_yMin (yMin), + m_yMax (yMax) +{ + DefaultValueList::Add (this); +} + +double +RectangleDefaultValue::GetMinX (void) const +{ + return m_xMin; +} +double +RectangleDefaultValue::GetMinY (void) const +{ + return m_yMin; +} +double +RectangleDefaultValue::GetMaxX (void) const +{ + return m_xMax; +} +double +RectangleDefaultValue::GetMaxY (void) const +{ + return m_yMax; +} +bool +RectangleDefaultValue::DoParseValue (const std::string &value) +{ + std::string::size_type xMinStart = 0; + std::string::size_type xMinEnd = value.find_first_of(":", xMinStart); + std::string::size_type xMaxStart = xMinEnd + 1; + std::string::size_type xMaxEnd = value.find_first_of(":", xMaxStart); + std::string::size_type yMinStart = xMaxEnd + 1; + std::string::size_type yMinEnd = value.find_first_of(":", yMinStart); + std::string::size_type yMaxStart = yMinEnd + 1; + std::string::size_type yMaxEnd = std::string::npos; + + std::string xMinString = value.substr (xMinStart, xMinEnd); + std::string xMaxString = value.substr (xMaxStart, xMaxEnd); + std::string yMinString = value.substr (yMinStart, yMinEnd); + std::string yMaxString = value.substr (yMaxStart, yMaxEnd); + + std::istringstream iss; + iss.str (xMinString); + iss >> m_xMin; + iss.str (xMaxString); + iss >> m_xMax; + iss.str (yMinString); + iss >> m_yMin; + iss.str (yMaxString); + iss >> m_yMax; + + return !iss.bad () && !iss.fail (); +} +std::string +RectangleDefaultValue::DoGetType (void) const +{ + return "(xMin:xMax:yMin:yMax)"; +} +std::string +RectangleDefaultValue::DoGetDefaultValue (void) const +{ + std::ostringstream oss; + oss << m_xMinDefault << ":" << m_xMaxDefault << ":" << m_yMinDefault << ":" << m_yMaxDefault; + return oss.str (); +} + + +} // namespace ns3 diff --git a/src/core/rectangle-default-value.h b/src/core/rectangle-default-value.h new file mode 100644 index 000000000..f81388780 --- /dev/null +++ b/src/core/rectangle-default-value.h @@ -0,0 +1,58 @@ +/* -*- 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 RECTANGLE_DEFAULT_VALUE_H +#define RECTANGLE_DEFAULT_VALUE_H + +#include +#include "default-value.h" + +namespace ns3 { + +class RectangleDefaultValue : public DefaultValueBase +{ + public: + RectangleDefaultValue (std::string name, + std::string help, + double xMin, double xMax, + double yMin, double yMax); + + double GetMinX (void) const; + double GetMinY (void) const; + double GetMaxX (void) const; + double GetMaxY (void) const; + private: + virtual bool DoParseValue (const std::string &value); + virtual std::string DoGetType (void) const; + virtual std::string DoGetDefaultValue (void) const; + + double m_xMinDefault; + double m_xMaxDefault; + double m_yMinDefault; + double m_yMaxDefault; + double m_xMin; + double m_xMax; + double m_yMin; + double m_yMax; +}; + +} // namespace ns3 + +#endif /* RECTANGLE_DEFAULT_VALUE_H */ From 184d929521291545433af208b8840dfe4cd189bf Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Tue, 3 Jul 2007 12:36:01 +0200 Subject: [PATCH 028/148] parse command-line arguments --- samples/main-grid-topology.cc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/samples/main-grid-topology.cc b/samples/main-grid-topology.cc index 20ce064d9..a803229cb 100644 --- a/samples/main-grid-topology.cc +++ b/samples/main-grid-topology.cc @@ -2,14 +2,16 @@ #include "ns3/ptr.h" #include "ns3/grid-topology.h" -#include "ns3/random-walk-position.h" #include "ns3/static-position.h" #include "ns3/internet-node.h" +#include "ns3/command-line.h" using namespace ns3; int main (int argc, char *argv[]) { + CommandLine::Parse (argc, argv); + std::vector > nodes; // create an array of empty nodes for testing purposes @@ -26,7 +28,6 @@ int main (int argc, char *argv[]) // each object will be attached a static position. grid.SetPositionModel (StaticPosition::cid); - //grid.SetPositionModel (RandomWalkPosition::cid); // finalize the setup by attaching to each object // in the input array a position and initializing From 71d559e1f2781109ac67d0f1558bfdfcc5b448b2 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Tue, 3 Jul 2007 13:19:09 +0200 Subject: [PATCH 029/148] add dirtyness tracking to DefaultValueBase --- src/core/default-value.cc | 20 ++++++++++++++++++-- src/core/default-value.h | 14 ++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/core/default-value.cc b/src/core/default-value.cc index fa0a2406d..7072205e1 100644 --- a/src/core/default-value.cc +++ b/src/core/default-value.cc @@ -26,7 +26,8 @@ namespace ns3 { DefaultValueBase::DefaultValueBase (const std::string &name, const std::string &help) : m_name (name), - m_help (help) + m_help (help), + m_dirty (false) {} DefaultValueBase::~DefaultValueBase () {} @@ -41,9 +42,24 @@ DefaultValueBase::GetHelp (void) const return m_help; } bool +DefaultValueBase::IsDirty (void) const +{ + return m_dirty; +} +void +DefaultValueBase::ClearDirtyFlag (void) +{ + m_dirty = false; +} +bool DefaultValueBase::ParseValue (const std::string &value) { - return DoParseValue (value); + bool ok = DoParseValue (value); + if (ok) + { + m_dirty = true; + } + return ok; } std::string DefaultValueBase::GetType (void) const diff --git a/src/core/default-value.h b/src/core/default-value.h index 08dbae285..91cf70dae 100644 --- a/src/core/default-value.h +++ b/src/core/default-value.h @@ -37,6 +37,19 @@ public: virtual ~DefaultValueBase (); std::string GetName (void) const; std::string GetHelp (void) const; + + /** + * \returns true if this value is dirty, false otherwise. + * + * A value becomes dirty when ParseValue is invoked + * and it successfully completes. Dirtyness indicates + * that the state of the value was changed by a user. + */ + bool IsDirty (void) const; + /** + * Clear the dirty state. + */ + void ClearDirtyFlag (void); // parse a matching parameter // return true in case of success, false otherwise. bool ParseValue (const std::string &value); @@ -53,6 +66,7 @@ private: virtual std::string DoGetDefaultValue (void) const = 0; std::string m_name; std::string m_help; + bool m_dirty; }; class DefaultValueList From 038bc809cbe09a6ab3142ac54d2a951c7a11ce4b Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Tue, 3 Jul 2007 16:11:40 +0200 Subject: [PATCH 030/148] add operator << (std::ostream)for Ptr --- src/core/ptr.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/core/ptr.h b/src/core/ptr.h index dd664be86..c612a0211 100644 --- a/src/core/ptr.h +++ b/src/core/ptr.h @@ -143,6 +143,9 @@ T * PeekPointer (const Ptr &p); template T * GetPointer (const Ptr &p); +template +std::ostream &operator << (std::ostream &, const Ptr &p); + // allow if (sp == 0) template @@ -290,6 +293,13 @@ T * GetPointer (const Ptr &p) return p.m_ptr; } +template +std::ostream &operator << (std::ostream &os, const Ptr &p) +{ + os << PeekPointer (p); + return os; +} + template bool operator == (Ptr const &lhs, T2 const *rhs) From 96698c5783a94fa65b60629e7c2fa8b8fcb18a2d Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Tue, 3 Jul 2007 17:05:23 +0200 Subject: [PATCH 031/148] In some cases, when an event is scheduled against a subclass of Object, and if no one owns a reference directly to this object, the object is alive, has a refcount of zero and the method ran when the event expires runs against the raw pointer which means that we are manipulating an object with a refcount of zero. So, we must disable this check. This is really evil but I see no way to work around this. --- src/core/object.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/src/core/object.cc b/src/core/object.cc index 9b4812c42..e70762cf1 100644 --- a/src/core/object.cc +++ b/src/core/object.cc @@ -118,7 +118,6 @@ Object::~Object () Ptr Object::DoQueryInterface (InterfaceId iid) const { - NS_ASSERT (Check ()); const Object *currentObject = this; do { NS_ASSERT (currentObject != 0); From 0c77aa5a6987bb7e964f54042dba9abf45791ac1 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Tue, 3 Jul 2007 17:11:53 +0200 Subject: [PATCH 032/148] implement Dispose --- src/node/random-walk-position.cc | 7 +++++++ src/node/random-walk-position.h | 1 + 2 files changed, 8 insertions(+) diff --git a/src/node/random-walk-position.cc b/src/node/random-walk-position.cc index 59deb7e37..b4c9da3eb 100644 --- a/src/node/random-walk-position.cc +++ b/src/node/random-walk-position.cc @@ -159,6 +159,13 @@ RandomWalkPosition::Update (void) const m_y += m_dy * deltaS; } +void +RandomWalkPosition::DoDispose (void) +{ + m_parameters = 0; + // chain up + Position::DoDispose (); +} void RandomWalkPosition::DoGet (double &x, double &y, double &z) const { diff --git a/src/node/random-walk-position.h b/src/node/random-walk-position.h index 87258c310..cc65c273e 100644 --- a/src/node/random-walk-position.h +++ b/src/node/random-walk-position.h @@ -108,6 +108,7 @@ class RandomWalkPosition : public Position RandomWalkPosition (Ptr parameters, double x, double y); private: + virtual void DoDispose (void); virtual void DoGet (double &x, double &y, double &z) const; virtual void DoSet (double x, double y, double z); From c0420dfa2abc14d799416b61eb6743ce39741b86 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Tue, 3 Jul 2007 17:12:28 +0200 Subject: [PATCH 033/148] add a few constructors --- src/node/random-rectangle-topology.cc | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/node/random-rectangle-topology.cc b/src/node/random-rectangle-topology.cc index b35122b1f..ef6034320 100644 --- a/src/node/random-rectangle-topology.cc +++ b/src/node/random-rectangle-topology.cc @@ -20,9 +20,25 @@ */ #include "random-rectangle-topology.h" #include "static-position.h" +#include "ns3/random-variable-default-value.h" namespace ns3 { +static RandomVariableDefaultValue +g_xVariable ("RandomRectangleTopologyX", + "Random variable to choose the x coordinate of the elements of a RandomRectangleTopology.", + "Uniform:-100:100"); +static RandomVariableDefaultValue +g_yVariable ("RandomRectangleTopologyY", + "Random variable to choose the y coordinate of the elements of a RandomRectangleTopology.", + "Uniform:-100:100"); + +RandomRectangleTopology::RandomRectangleTopology () + : m_xVariable (g_xVariable.GetCopy ()), + m_yVariable (g_yVariable.GetCopy ()), + m_positionModel (StaticPosition::cid) +{} + RandomRectangleTopology::RandomRectangleTopology (double xMin, double xMax, double yMin, double yMax) : m_xVariable (new UniformVariable (xMin, xMax)), m_yVariable (new UniformVariable (yMin, yMax)), From ce6536c1d8e6eb15e8c63cf96e80f93f6a59fa94 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Tue, 3 Jul 2007 17:12:44 +0200 Subject: [PATCH 034/148] RandomDirection model --- src/node/random-direction-position.cc | 339 ++++++++++++++++++++++++++ src/node/random-direction-position.h | 103 ++++++++ 2 files changed, 442 insertions(+) create mode 100644 src/node/random-direction-position.cc create mode 100644 src/node/random-direction-position.h diff --git a/src/node/random-direction-position.cc b/src/node/random-direction-position.cc new file mode 100644 index 000000000..b64e31f8f --- /dev/null +++ b/src/node/random-direction-position.cc @@ -0,0 +1,339 @@ +/* -*- 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/random-variable-default-value.h" +#include "ns3/rectangle-default-value.h" +#include "ns3/simulator.h" +#include +#include +#include "random-direction-position.h" + +namespace ns3 { + +const double RandomDirectionPosition::PI = 3.1415; +const InterfaceId RandomDirectionPosition::iid = + MakeInterfaceId ("RandomDirectionPosition", Position::iid); +const ClassId RandomDirectionPosition::cid = + MakeClassId ("RandomDirectionPosition", + RandomDirectionPosition::iid); + + +static RandomVariableDefaultValue + g_speedVariable ("RandomDirectionSpeed", + "A random variable to control the speed of a RandomDirection mobility model.", + "Uniform:1:2"); + +static RandomVariableDefaultValue + g_pauseVariable ("RandomDirectionPause", + "A random variable to control the duration of of the pause of a RandomDiretion mobility model.", + "Constant:2"); + +static RectangleDefaultValue + g_rectangle ("RandomDirectionArea", + "The bounding area for the RandomDirection model.", + -100, 100, -100, 100); + + +RandomDirectionParameters::RandomDirectionParameters () + : m_xMin (g_rectangle.GetMinX ()), + m_xMax (g_rectangle.GetMaxX ()), + m_yMin (g_rectangle.GetMinY ()), + m_yMax (g_rectangle.GetMaxY ()), + m_speedVariable (g_speedVariable.GetCopy ()), + m_pauseVariable (g_pauseVariable.GetCopy ()) +{} +RandomDirectionParameters::RandomDirectionParameters (double xMin, double xMax, double yMin, double yMax) + : m_xMin (xMin), + m_xMax (xMax), + m_yMin (yMin), + m_yMax (yMax), + m_speedVariable (g_speedVariable.GetCopy ()), + m_pauseVariable (g_pauseVariable.GetCopy ()) +{} +RandomDirectionParameters::RandomDirectionParameters (double xMin, double xMax, double yMin, double yMax, + const RandomVariable &speedVariable, + const RandomVariable &pauseVariable) + : m_xMin (xMin), + m_xMax (xMax), + m_yMin (yMin), + m_yMax (yMax), + m_speedVariable (speedVariable.Copy ()), + m_pauseVariable (pauseVariable.Copy ()) +{} + +RandomDirectionParameters::~RandomDirectionParameters () +{ + delete m_speedVariable; + delete m_pauseVariable; + m_speedVariable = 0; + m_pauseVariable = 0; +} + +void +RandomDirectionParameters::SetSpeed (const RandomVariable &speedVariable) +{ + delete m_speedVariable; + m_speedVariable = speedVariable.Copy (); +} +void +RandomDirectionParameters::SetPause (const RandomVariable &pauseVariable) +{ + delete m_pauseVariable; + m_pauseVariable = pauseVariable.Copy (); +} +void +RandomDirectionParameters::SetBounds (double xMin, double xMax, double yMin, double yMax) +{ + m_xMin = xMin; + m_yMin = yMin; + m_xMax = xMax; + m_yMax = yMax; +} + +Ptr +RandomDirectionPosition::GetDefaultParameters (void) +{ + static Ptr parameters = Create (); + if (parameters->m_xMin != g_rectangle.GetMinX () || + parameters->m_yMin != g_rectangle.GetMinY () || + parameters->m_xMax != g_rectangle.GetMaxX () || + parameters->m_yMax != g_rectangle.GetMaxY () || + g_speedVariable.IsDirty () || + g_pauseVariable.IsDirty ()) + { + parameters = Create (); + g_speedVariable.ClearDirtyFlag (); + g_pauseVariable.ClearDirtyFlag (); + } + return parameters; +} + + +RandomDirectionPosition::RandomDirectionPosition () + : m_parameters (GetDefaultParameters ()), + m_x (0.0), + m_y (0.0), + m_dx (0.0), + m_dy (0.0), + m_prevTime (Simulator::Now ()), + m_pauseStart (Simulator::Now ()) +{ + SetInterfaceId (RandomDirectionPosition::iid); + InitializeDirectionAndSpeed (); +} +bool +RandomDirectionPosition::CheckPosition (void) const +{ + return + m_x <= m_parameters->m_xMax && + m_x >= m_parameters->m_xMin && + m_y <= m_parameters->m_yMax && + m_y >= m_parameters->m_yMin; +} + +RandomDirectionPosition::RandomDirectionPosition (double x, double y) + : m_parameters (GetDefaultParameters ()), + m_x (x), + m_y (y), + m_dx (0.0), + m_dy (0.0), + m_prevTime (Simulator::Now ()), + m_pauseStart (Simulator::Now ()) +{ + SetInterfaceId (RandomDirectionPosition::iid); + NS_ASSERT (CheckPosition ()); + InitializeDirectionAndSpeed (); +} +RandomDirectionPosition::RandomDirectionPosition (Ptr parameters) + : m_parameters (parameters), + m_x (0.0), + m_y (0.0), + m_dx (0.0), + m_dy (0.0), + m_prevTime (Simulator::Now ()), + m_pauseStart (Simulator::Now ()) +{ + SetInterfaceId (RandomDirectionPosition::iid); + InitializeDirectionAndSpeed (); + NS_ASSERT (CheckPosition ()); +} +RandomDirectionPosition::RandomDirectionPosition (Ptr parameters, + double x, double y) + : m_parameters (parameters), + m_x (x), + m_y (y), + m_dx (0.0), + m_dy (0.0), + m_prevTime (Simulator::Now ()), + m_pauseStart (Simulator::Now ()) +{ + SetInterfaceId (RandomDirectionPosition::iid); + InitializeDirectionAndSpeed (); + NS_ASSERT (CheckPosition ()); +} +void +RandomDirectionPosition::DoDispose (void) +{ + m_parameters = 0; + // chain up. + Position::DoDispose (); +} +enum RandomDirectionPosition::Side +RandomDirectionPosition::CalculateIntersection (double &x, double &y) +{ + double xMin = m_parameters->m_xMin; + double xMax = m_parameters->m_xMax; + double yMin = m_parameters->m_yMin; + double yMax = m_parameters->m_yMax; + double xMaxY = m_y + (xMax - m_x) / m_dx * m_dy; + double xMinY = m_y + (xMin - m_x) / m_dx * m_dy; + double yMaxX = m_x + (yMax - m_y) / m_dy * m_dx; + double yMinX = m_x + (yMin - m_y) / m_dy * m_dx; + bool xMaxOk = xMaxY <= yMax && xMaxY >= yMin; + bool xMinOk = xMinY <= yMax && xMinY >= yMin; + bool yMaxOk = yMaxX <= xMax && yMaxX >= xMin; + bool yMinOk = yMinX <= xMax && yMinX >= xMin; + if (xMaxOk && m_dx >= 0) + { + x = xMax; + y = xMaxY; + return RandomDirectionPosition::RIGHT; + } + else if (xMinOk && m_dx <= 0) + { + x = xMin; + y = xMinY; + return RandomDirectionPosition::LEFT; + } + else if (yMaxOk && m_dy >= 0) + { + x = yMaxX; + y = yMax; + return RandomDirectionPosition::TOP; + } + else if (yMinOk && m_dy <= 0) + { + x = yMinX; + y = yMin; + return RandomDirectionPosition::BOTTOM; + } + else + { + NS_ASSERT (false); + // quiet compiler + return RandomDirectionPosition::RIGHT; + } +} +void +RandomDirectionPosition::InitializeDirectionAndSpeed (void) +{ + double direction = UniformVariable::GetSingleValue (0, 2 * PI); + SetDirectionAndSpeed (direction); +} +void +RandomDirectionPosition::SetDirectionAndSpeed (double direction) +{ + double speed = m_parameters->m_speedVariable->GetValue (); + m_dx = std::cos (direction) * speed; + m_dy = std::sin (direction) * speed; + double x, y; + m_side = CalculateIntersection (x, y); + double deltaX = x - m_x; + double deltaY = y - m_y; + double distance = sqrt (deltaX * deltaX + deltaY * deltaY); + double seconds = distance / speed; + double pause = m_parameters->m_pauseVariable->GetValue (); + m_pauseStart = Simulator::Now () + Seconds (seconds); + m_prevTime = Simulator::Now (); + m_event = Simulator::Schedule (Seconds (seconds + pause), + &RandomDirectionPosition::ResetDirectionAndSpeed, this); +} +void +RandomDirectionPosition::ResetDirectionAndSpeed (void) +{ + Update (); + double direction = UniformVariable::GetSingleValue (0, PI); + switch (m_side) + { + case RandomDirectionPosition::RIGHT: + direction += PI / 2; + break; + case RandomDirectionPosition::LEFT: + direction += - PI / 2; + break; + case RandomDirectionPosition::TOP: + direction += PI; + break; + case RandomDirectionPosition::BOTTOM: + direction += 0.0; + break; + } + SetDirectionAndSpeed (direction); + NotifyCourseChange (); +} +void +RandomDirectionPosition::Update (void) const +{ + Time end = std::min (Simulator::Now (), m_pauseStart); + if (m_prevTime >= end) + { + return; + } + Time deltaTime = end - m_prevTime; + m_prevTime = Simulator::Now (); + double deltaS = deltaTime.GetSeconds (); + NS_ASSERT (CheckPosition ()); + m_x += m_dx * deltaS; + m_y += m_dy * deltaS; + // round to closest boundaries. + m_x = std::min (m_x, m_parameters->m_xMax); + m_x = std::max (m_x, m_parameters->m_xMin); + m_y = std::min (m_y, m_parameters->m_yMax); + m_y = std::max (m_y, m_parameters->m_yMin); + NS_ASSERT (CheckPosition ()); +} +void +RandomDirectionPosition::DoGet (double &x, double &y, double &z) const +{ + Update (); + x = m_x; + y = m_y; + z = 0; +} +void +RandomDirectionPosition::DoSet (double x, double y, double z) +{ + bool changed = false; + if (m_x != x || m_y != y) + { + changed = true; + } + m_x = x; + m_y = y; + m_prevTime = Simulator::Now (); + m_pauseStart = Simulator::Now (); + Simulator::Remove (m_event); + InitializeDirectionAndSpeed (); + NotifyCourseChange (); +} + + + +} // namespace ns3 diff --git a/src/node/random-direction-position.h b/src/node/random-direction-position.h new file mode 100644 index 000000000..eda7d8c6c --- /dev/null +++ b/src/node/random-direction-position.h @@ -0,0 +1,103 @@ +/* -*- 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 RANDOM_DIRECTION_POSITION_H +#define RANDOM_DIRECTION_POSITION_H + +#include "ns3/object.h" +#include "ns3/ptr.h" +#include "ns3/nstime.h" +#include "ns3/event-id.h" +#include "ns3/component-manager.h" +#include "position.h" + +namespace ns3 { + +class RandomVariable; + +class RandomDirectionParameters : public Object +{ + public: + RandomDirectionParameters (); + RandomDirectionParameters (double xMin, double xMax, double yMin, double yMax); + RandomDirectionParameters (double xMin, double xMax, double yMin, double yMax, + const RandomVariable &speedVariable, + const RandomVariable &pauseVariable); + virtual ~RandomDirectionParameters (); + + void SetSpeed (const RandomVariable &speedVariable); + void SetPause (const RandomVariable &pauseVariable); + void SetBounds (double xMin, double xMax, double yMin, double yMax); + private: + friend class RandomDirectionPosition; + double m_xMin; + double m_xMax; + double m_yMin; + double m_yMax; + RandomVariable *m_speedVariable; + RandomVariable *m_pauseVariable; + std::string m_speedVariableValue; + std::string m_pauseVariableValue; +}; + +class RandomDirectionPosition : public Position +{ + public: + static const InterfaceId iid; + static const ClassId cid; + + RandomDirectionPosition (); + RandomDirectionPosition (double x, double y); + RandomDirectionPosition (Ptr parameters); + RandomDirectionPosition (Ptr parameters, + double x, double y); + private: + enum Side { + TOP, + BOTTOM, + LEFT, + RIGHT + }; + static Ptr GetDefaultParameters (void); + void ResetDirectionAndSpeed (void); + void SetDirectionAndSpeed (double direction); + void InitializeDirectionAndSpeed (void); + void Update (void) const; + bool CheckPosition (void) const; + enum RandomDirectionPosition::Side CalculateIntersection (double &x, double &y); + virtual void DoDispose (void); + virtual void DoGet (double &x, double &y, double &z) const; + virtual void DoSet (double x, double y, double z); + + static const double PI; + Ptr m_parameters; + mutable double m_x; + mutable double m_y; + double m_dx; + double m_dy; + mutable Time m_prevTime; + Time m_pauseStart; + EventId m_event; + enum Side m_side; +}; + +} // namespace ns3 + +#endif /* RANDOM_DIRECTION_POSITION_H */ From f27587f391637759f37f8183083a138f71eff5f9 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Tue, 3 Jul 2007 17:14:09 +0200 Subject: [PATCH 035/148] build the new code --- SConstruct | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/SConstruct b/SConstruct index 9a18bbf93..86180c806 100644 --- a/SConstruct +++ b/SConstruct @@ -62,6 +62,8 @@ core.add_sources([ 'rng-stream.cc', 'uid-manager.cc', 'default-value.cc', + 'random-variable-default-value.cc', + 'rectangle-default-value.cc', 'command-line.cc', 'type-name.cc', 'component-manager.cc', @@ -93,6 +95,8 @@ core.add_inst_headers([ 'random-variable.h', 'rng-stream.h', 'default-value.h', + 'random-variable-default-value.h', + 'rectangle-default-value.h', 'command-line.h', 'type-name.h', 'component-manager.h', @@ -251,6 +255,7 @@ node.add_sources ([ 'grid-topology.cc', 'random-rectangle-topology.cc', 'random-walk-position.cc', + 'random-direction-position.cc', ]) node.add_inst_headers ([ 'node.h', @@ -275,6 +280,7 @@ node.add_inst_headers ([ 'grid-topology.h', 'random-rectangle-topology.h', 'random-walk-position.h', + 'random-direction-position.h', ]) applications = build.Ns3Module ('applications', 'src/applications') From 9111697a0b1837fcb0cc3a7c4e2cab018ed06487 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 4 Jul 2007 09:48:49 +0200 Subject: [PATCH 036/148] add missing constructor declaration --- src/node/random-rectangle-topology.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/node/random-rectangle-topology.h b/src/node/random-rectangle-topology.h index 6568a993e..038de83d8 100644 --- a/src/node/random-rectangle-topology.h +++ b/src/node/random-rectangle-topology.h @@ -31,7 +31,8 @@ namespace ns3 { class RandomRectangleTopology { public: - + + RandomRectangleTopology (); RandomRectangleTopology (double xMin, double xMax, double yMin, double yMax); RandomRectangleTopology (const RandomVariable &xVariable, const RandomVariable &yVariable); From bda20507bdb4130d202f6bc6998d9623b06fe5eb Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 4 Jul 2007 09:56:22 +0200 Subject: [PATCH 037/148] PositionSetNotifier -> MobilityModelNotifier --- SConstruct | 4 ++-- samples/main-random-walk.cc | 4 ++-- ...notifier.cc => mobility-model-notifier.cc} | 20 +++++++++---------- ...t-notifier.h => mobility-model-notifier.h} | 10 +++++----- src/node/position.cc | 6 +++--- 5 files changed, 22 insertions(+), 22 deletions(-) rename src/node/{position-set-notifier.cc => mobility-model-notifier.cc} (70%) rename src/node/{position-set-notifier.h => mobility-model-notifier.h} (90%) diff --git a/SConstruct b/SConstruct index 86180c806..4b3f0a0cd 100644 --- a/SConstruct +++ b/SConstruct @@ -249,7 +249,7 @@ node.add_sources ([ 'ipv4.cc', 'application.cc', 'position.cc', - 'position-set-notifier.cc', + 'mobility-model-notifier.cc', 'static-position.cc', 'static-speed-position.cc', 'grid-topology.cc', @@ -274,7 +274,7 @@ node.add_inst_headers ([ 'ipv4.h', 'application.h', 'position.h', - 'position-set-notifier.h', + 'mobility-model-notifier.h', 'static-position.h', 'static-speed-position.h', 'grid-topology.h', diff --git a/samples/main-random-walk.cc b/samples/main-random-walk.cc index c0f77bd4b..218ff51e0 100644 --- a/samples/main-random-walk.cc +++ b/samples/main-random-walk.cc @@ -2,7 +2,7 @@ #include "ns3/ptr.h" #include "ns3/position.h" -#include "ns3/position-set-notifier.h" +#include "ns3/mobility-model-notifier.h" #include "ns3/random-walk-position.h" #include "ns3/default-value.h" #include "ns3/command-line.h" @@ -29,7 +29,7 @@ int main (int argc, char *argv[]) CommandLine::Parse (argc, argv); - Ptr notifier = Create (); + Ptr notifier = Create (); Ptr position = Create (); position->AddInterface (notifier); notifier->RegisterListener (MakeCallback (&CourseChange)); diff --git a/src/node/position-set-notifier.cc b/src/node/mobility-model-notifier.cc similarity index 70% rename from src/node/position-set-notifier.cc rename to src/node/mobility-model-notifier.cc index ffbdde559..ab5fd6a6f 100644 --- a/src/node/position-set-notifier.cc +++ b/src/node/mobility-model-notifier.cc @@ -18,27 +18,27 @@ * * Author: Mathieu Lacage */ -#include "position-set-notifier.h" +#include "mobility-model-notifier.h" namespace ns3 { -const InterfaceId PositionSetNotifier::iid = MakeInterfaceId ("PositionSetNotifier", Object::iid); -const ClassId PositionSetNotifier::cid = - MakeClassId ("PositionSetNotifier", - PositionSetNotifier::iid); +const InterfaceId MobilityModelNotifier::iid = MakeInterfaceId ("MobilityModelNotifier", Object::iid); +const ClassId MobilityModelNotifier::cid = + MakeClassId ("MobilityModelNotifier", + MobilityModelNotifier::iid); -PositionSetNotifier::PositionSetNotifier () +MobilityModelNotifier::MobilityModelNotifier () { - SetInterfaceId (PositionSetNotifier::iid); + SetInterfaceId (MobilityModelNotifier::iid); } void -PositionSetNotifier::RegisterListener (Listener listener) +MobilityModelNotifier::RegisterListener (Listener listener) { m_listeners.push_back (listener); } void -PositionSetNotifier::UnregisterListener (Listener callback) +MobilityModelNotifier::UnregisterListener (Listener callback) { for (std::list::iterator i = m_listeners.begin (); i != m_listeners.end ();) @@ -55,7 +55,7 @@ PositionSetNotifier::UnregisterListener (Listener callback) } } void -PositionSetNotifier::Notify (Ptr position) const +MobilityModelNotifier::Notify (Ptr position) const { for (std::list::const_iterator i = m_listeners.begin (); i != m_listeners.end (); i++) diff --git a/src/node/position-set-notifier.h b/src/node/mobility-model-notifier.h similarity index 90% rename from src/node/position-set-notifier.h rename to src/node/mobility-model-notifier.h index 4affdfe14..3b575a742 100644 --- a/src/node/position-set-notifier.h +++ b/src/node/mobility-model-notifier.h @@ -18,8 +18,8 @@ * * Author: Mathieu Lacage */ -#ifndef POSITION_SET_NOTIFIER_H -#define POSITION_SET_NOTIFIER_H +#ifndef MOBILITY_MODEL_NOTIFIER_H +#define MOBILITY_MODEL_NOTIFIER_H #include "ns3/object.h" #include "ns3/component-manager.h" @@ -31,7 +31,7 @@ namespace ns3 { /** * \brief notify listeners of position changes. */ -class PositionSetNotifier : public Object +class MobilityModelNotifier : public Object { public: static const InterfaceId iid; @@ -42,7 +42,7 @@ public: /** * Create a new position notifier */ - PositionSetNotifier (); + MobilityModelNotifier (); /** * \param position the position which just changed. @@ -69,4 +69,4 @@ private: } // namespace ns3 -#endif /* POSITION_SET_NOTIFIER_H */ +#endif /* MOBILITY_MODEL_NOTIFIER_H */ diff --git a/src/node/position.cc b/src/node/position.cc index 9390ea020..bfa3642ff 100644 --- a/src/node/position.cc +++ b/src/node/position.cc @@ -19,7 +19,7 @@ * Author: Mathieu Lacage */ #include "position.h" -#include "position-set-notifier.h" +#include "mobility-model-notifier.h" #include namespace ns3 { @@ -112,8 +112,8 @@ Position::GetDistanceFrom (const Position &position) const void Position::NotifyCourseChange (void) const { - Ptr notifier = - QueryInterface (PositionSetNotifier::iid); + Ptr notifier = + QueryInterface (MobilityModelNotifier::iid); if (notifier != 0) { notifier->Notify (this); From 3e22f4762476e3d6133241a01cd8ea380160672c Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 4 Jul 2007 10:04:08 +0200 Subject: [PATCH 038/148] Position -> MobilityModel --- samples/main-grid-topology.cc | 4 +- samples/main-random-walk.cc | 4 +- src/node/grid-topology.cc | 4 +- src/node/grid-topology.h | 6 +- src/node/mobility-model-notifier.cc | 2 +- src/node/mobility-model-notifier.h | 4 +- src/node/position.cc | 30 +++++----- src/node/position.h | 8 +-- src/node/random-direction-position.cc | 82 +++++++++++++-------------- src/node/random-direction-position.h | 16 +++--- src/node/random-rectangle-topology.cc | 8 +-- src/node/random-rectangle-topology.h | 2 +- src/node/random-walk-position.cc | 58 +++++++++---------- src/node/random-walk-position.h | 24 ++++---- src/node/static-position.cc | 24 ++++---- src/node/static-position.h | 10 ++-- src/node/static-speed-position.cc | 34 +++++------ src/node/static-speed-position.h | 12 ++-- 18 files changed, 166 insertions(+), 166 deletions(-) diff --git a/samples/main-grid-topology.cc b/samples/main-grid-topology.cc index a803229cb..46b7d28c6 100644 --- a/samples/main-grid-topology.cc +++ b/samples/main-grid-topology.cc @@ -27,7 +27,7 @@ int main (int argc, char *argv[]) GridTopology grid (-100, -100, 20, 5, 20); // each object will be attached a static position. - grid.SetPositionModel (StaticPosition::cid); + grid.SetMobilityModelModel (StaticMobilityModel::cid); // finalize the setup by attaching to each object // in the input array a position and initializing @@ -39,7 +39,7 @@ int main (int argc, char *argv[]) j != nodes.end (); j++) { Ptr object = *j; - Ptr position = object->QueryInterface (Position::iid); + Ptr position = object->QueryInterface (MobilityModel::iid); NS_ASSERT (position != 0); double x, y, z; position->Get (x,y,z); diff --git a/samples/main-random-walk.cc b/samples/main-random-walk.cc index 218ff51e0..9c80e7551 100644 --- a/samples/main-random-walk.cc +++ b/samples/main-random-walk.cc @@ -12,7 +12,7 @@ using namespace ns3; static void -CourseChange (Ptr position) +CourseChange (Ptr position) { double x, y, z; position->Get (x, y, z); @@ -30,7 +30,7 @@ int main (int argc, char *argv[]) CommandLine::Parse (argc, argv); Ptr notifier = Create (); - Ptr position = Create (); + Ptr position = Create (); position->AddInterface (notifier); notifier->RegisterListener (MakeCallback (&CourseChange)); diff --git a/src/node/grid-topology.cc b/src/node/grid-topology.cc index a621c7105..d3fe2c588 100644 --- a/src/node/grid-topology.cc +++ b/src/node/grid-topology.cc @@ -29,11 +29,11 @@ GridTopology::GridTopology (double xMin, double yMin, uint32_t n, double deltaX, m_n (n), m_deltaX (deltaX), m_deltaY (deltaY), - m_positionClassId (StaticPosition::cid) + m_positionClassId (StaticMobilityModel::cid) {} void -GridTopology::SetPositionModel (ClassId classId) +GridTopology::SetMobilityModelModel (ClassId classId) { m_positionClassId = classId; } diff --git a/src/node/grid-topology.h b/src/node/grid-topology.h index f2cc724a1..825900870 100644 --- a/src/node/grid-topology.h +++ b/src/node/grid-topology.h @@ -48,13 +48,13 @@ class GridTopology * \param classId the classId of the position object to attach to each * input object. */ - void SetPositionModel (ClassId classId); + void SetMobilityModelModel (ClassId classId); /** * \param objects a vector of objects * * Attach a position (the type of position is specified through - * the ClassId given to SetPositionModel) to each object present + * the ClassId given to SetMobilityModelModel) to each object present * in the input vector and configure its initial location with a set * of coordinates arranged according to a regular rectangular grid, * one row after the other. @@ -66,7 +66,7 @@ class GridTopology * \param objects a vector of objects * * Attach a position (the type of position is specified through - * the ClassId given to SetPositionModel) to each object present + * the ClassId given to SetMobilityModelModel) to each object present * in the input vector and configure its initial location with a set * of coordinates arranged according to a regular rectangular grid, * one column after the other. diff --git a/src/node/mobility-model-notifier.cc b/src/node/mobility-model-notifier.cc index ab5fd6a6f..abb476c20 100644 --- a/src/node/mobility-model-notifier.cc +++ b/src/node/mobility-model-notifier.cc @@ -55,7 +55,7 @@ MobilityModelNotifier::UnregisterListener (Listener callback) } } void -MobilityModelNotifier::Notify (Ptr position) const +MobilityModelNotifier::Notify (Ptr position) const { for (std::list::const_iterator i = m_listeners.begin (); i != m_listeners.end (); i++) diff --git a/src/node/mobility-model-notifier.h b/src/node/mobility-model-notifier.h index 3b575a742..4a687e6a0 100644 --- a/src/node/mobility-model-notifier.h +++ b/src/node/mobility-model-notifier.h @@ -37,7 +37,7 @@ public: static const InterfaceId iid; static const ClassId cid; - typedef Callback > Listener; + typedef Callback > Listener; /** * Create a new position notifier @@ -47,7 +47,7 @@ public: /** * \param position the position which just changed. */ - void Notify (Ptr position) const; + void Notify (Ptr position) const; /** * \param listener listener to add diff --git a/src/node/position.cc b/src/node/position.cc index bfa3642ff..8095cc466 100644 --- a/src/node/position.cc +++ b/src/node/position.cc @@ -24,37 +24,37 @@ namespace ns3 { -const InterfaceId Position::iid = MakeInterfaceId ("Position", Object::iid); +const InterfaceId MobilityModel::iid = MakeInterfaceId ("MobilityModel", Object::iid); -Position::Position () +MobilityModel::MobilityModel () { - SetInterfaceId (Position::iid); + SetInterfaceId (MobilityModel::iid); } -Position::~Position () +MobilityModel::~MobilityModel () {} void -Position::Get (double &x, double &y, double &z) const +MobilityModel::Get (double &x, double &y, double &z) const { DoGet (x,y,z); } double -Position::GetX (void) const +MobilityModel::GetX (void) const { double x,y,z; DoGet (x,y,z); return x; } double -Position::GetY (void) const +MobilityModel::GetY (void) const { double x, y, z; DoGet (x,y,z); return y; } double -Position::GetZ (void) const +MobilityModel::GetZ (void) const { double x, y, z; DoGet (x,y,z); @@ -62,33 +62,33 @@ Position::GetZ (void) const } void -Position::Set (double x, double y, double z) +MobilityModel::Set (double x, double y, double z) { DoSet (x, y, z); } void -Position::SetXY (double x, double y) +MobilityModel::SetXY (double x, double y) { double currentX, currentY, currentZ; DoGet (currentX, currentY, currentZ); DoSet (x, y, currentZ); } void -Position::SetX (double x) +MobilityModel::SetX (double x) { double currentX, currentY, currentZ; DoGet (currentX, currentY, currentZ); DoSet (x, currentY, currentZ); } void -Position::SetY (double y) +MobilityModel::SetY (double y) { double currentX, currentY, currentZ; DoGet (currentX, currentY, currentZ); DoSet (currentX, y, currentZ); } void -Position::SetZ (double z) +MobilityModel::SetZ (double z) { double currentX, currentY, currentZ; DoGet (currentX, currentY, currentZ); @@ -97,7 +97,7 @@ Position::SetZ (double z) double -Position::GetDistanceFrom (const Position &position) const +MobilityModel::GetDistanceFrom (const MobilityModel &position) const { double ox,oy,oz; double x,y,z; @@ -110,7 +110,7 @@ Position::GetDistanceFrom (const Position &position) const } void -Position::NotifyCourseChange (void) const +MobilityModel::NotifyCourseChange (void) const { Ptr notifier = QueryInterface (MobilityModelNotifier::iid); diff --git a/src/node/position.h b/src/node/position.h index f86f4aab3..db1ca2567 100644 --- a/src/node/position.h +++ b/src/node/position.h @@ -32,12 +32,12 @@ namespace ns3 { * understood to be meters or meters/s. i.e., they are all * metric international units. */ -class Position : public Object +class MobilityModel : public Object { public: static const InterfaceId iid; - Position (); - virtual ~Position () = 0; + MobilityModel (); + virtual ~MobilityModel () = 0; /** * \param x reference to floating-point variable for x coordinate. @@ -81,7 +81,7 @@ public: * * Unit is meters */ - double GetDistanceFrom (const Position &position) const; + double GetDistanceFrom (const MobilityModel &position) const; protected: /** * Must be invoked by subclasses when the course of the diff --git a/src/node/random-direction-position.cc b/src/node/random-direction-position.cc index b64e31f8f..7da45cad8 100644 --- a/src/node/random-direction-position.cc +++ b/src/node/random-direction-position.cc @@ -27,12 +27,12 @@ namespace ns3 { -const double RandomDirectionPosition::PI = 3.1415; -const InterfaceId RandomDirectionPosition::iid = - MakeInterfaceId ("RandomDirectionPosition", Position::iid); -const ClassId RandomDirectionPosition::cid = - MakeClassId ("RandomDirectionPosition", - RandomDirectionPosition::iid); +const double RandomDirectionMobilityModel::PI = 3.1415; +const InterfaceId RandomDirectionMobilityModel::iid = + MakeInterfaceId ("RandomDirectionMobilityModel", MobilityModel::iid); +const ClassId RandomDirectionMobilityModel::cid = + MakeClassId ("RandomDirectionMobilityModel", + RandomDirectionMobilityModel::iid); static RandomVariableDefaultValue @@ -108,7 +108,7 @@ RandomDirectionParameters::SetBounds (double xMin, double xMax, double yMin, dou } Ptr -RandomDirectionPosition::GetDefaultParameters (void) +RandomDirectionMobilityModel::GetDefaultParameters (void) { static Ptr parameters = Create (); if (parameters->m_xMin != g_rectangle.GetMinX () || @@ -126,7 +126,7 @@ RandomDirectionPosition::GetDefaultParameters (void) } -RandomDirectionPosition::RandomDirectionPosition () +RandomDirectionMobilityModel::RandomDirectionMobilityModel () : m_parameters (GetDefaultParameters ()), m_x (0.0), m_y (0.0), @@ -135,11 +135,11 @@ RandomDirectionPosition::RandomDirectionPosition () m_prevTime (Simulator::Now ()), m_pauseStart (Simulator::Now ()) { - SetInterfaceId (RandomDirectionPosition::iid); + SetInterfaceId (RandomDirectionMobilityModel::iid); InitializeDirectionAndSpeed (); } bool -RandomDirectionPosition::CheckPosition (void) const +RandomDirectionMobilityModel::CheckMobilityModel (void) const { return m_x <= m_parameters->m_xMax && @@ -148,7 +148,7 @@ RandomDirectionPosition::CheckPosition (void) const m_y >= m_parameters->m_yMin; } -RandomDirectionPosition::RandomDirectionPosition (double x, double y) +RandomDirectionMobilityModel::RandomDirectionMobilityModel (double x, double y) : m_parameters (GetDefaultParameters ()), m_x (x), m_y (y), @@ -157,11 +157,11 @@ RandomDirectionPosition::RandomDirectionPosition (double x, double y) m_prevTime (Simulator::Now ()), m_pauseStart (Simulator::Now ()) { - SetInterfaceId (RandomDirectionPosition::iid); - NS_ASSERT (CheckPosition ()); + SetInterfaceId (RandomDirectionMobilityModel::iid); + NS_ASSERT (CheckMobilityModel ()); InitializeDirectionAndSpeed (); } -RandomDirectionPosition::RandomDirectionPosition (Ptr parameters) +RandomDirectionMobilityModel::RandomDirectionMobilityModel (Ptr parameters) : m_parameters (parameters), m_x (0.0), m_y (0.0), @@ -170,11 +170,11 @@ RandomDirectionPosition::RandomDirectionPosition (Ptr m_prevTime (Simulator::Now ()), m_pauseStart (Simulator::Now ()) { - SetInterfaceId (RandomDirectionPosition::iid); + SetInterfaceId (RandomDirectionMobilityModel::iid); InitializeDirectionAndSpeed (); - NS_ASSERT (CheckPosition ()); + NS_ASSERT (CheckMobilityModel ()); } -RandomDirectionPosition::RandomDirectionPosition (Ptr parameters, +RandomDirectionMobilityModel::RandomDirectionMobilityModel (Ptr parameters, double x, double y) : m_parameters (parameters), m_x (x), @@ -184,19 +184,19 @@ RandomDirectionPosition::RandomDirectionPosition (Ptr m_prevTime (Simulator::Now ()), m_pauseStart (Simulator::Now ()) { - SetInterfaceId (RandomDirectionPosition::iid); + SetInterfaceId (RandomDirectionMobilityModel::iid); InitializeDirectionAndSpeed (); - NS_ASSERT (CheckPosition ()); + NS_ASSERT (CheckMobilityModel ()); } void -RandomDirectionPosition::DoDispose (void) +RandomDirectionMobilityModel::DoDispose (void) { m_parameters = 0; // chain up. - Position::DoDispose (); + MobilityModel::DoDispose (); } -enum RandomDirectionPosition::Side -RandomDirectionPosition::CalculateIntersection (double &x, double &y) +enum RandomDirectionMobilityModel::Side +RandomDirectionMobilityModel::CalculateIntersection (double &x, double &y) { double xMin = m_parameters->m_xMin; double xMax = m_parameters->m_xMax; @@ -214,41 +214,41 @@ RandomDirectionPosition::CalculateIntersection (double &x, double &y) { x = xMax; y = xMaxY; - return RandomDirectionPosition::RIGHT; + return RandomDirectionMobilityModel::RIGHT; } else if (xMinOk && m_dx <= 0) { x = xMin; y = xMinY; - return RandomDirectionPosition::LEFT; + return RandomDirectionMobilityModel::LEFT; } else if (yMaxOk && m_dy >= 0) { x = yMaxX; y = yMax; - return RandomDirectionPosition::TOP; + return RandomDirectionMobilityModel::TOP; } else if (yMinOk && m_dy <= 0) { x = yMinX; y = yMin; - return RandomDirectionPosition::BOTTOM; + return RandomDirectionMobilityModel::BOTTOM; } else { NS_ASSERT (false); // quiet compiler - return RandomDirectionPosition::RIGHT; + return RandomDirectionMobilityModel::RIGHT; } } void -RandomDirectionPosition::InitializeDirectionAndSpeed (void) +RandomDirectionMobilityModel::InitializeDirectionAndSpeed (void) { double direction = UniformVariable::GetSingleValue (0, 2 * PI); SetDirectionAndSpeed (direction); } void -RandomDirectionPosition::SetDirectionAndSpeed (double direction) +RandomDirectionMobilityModel::SetDirectionAndSpeed (double direction) { double speed = m_parameters->m_speedVariable->GetValue (); m_dx = std::cos (direction) * speed; @@ -263,25 +263,25 @@ RandomDirectionPosition::SetDirectionAndSpeed (double direction) m_pauseStart = Simulator::Now () + Seconds (seconds); m_prevTime = Simulator::Now (); m_event = Simulator::Schedule (Seconds (seconds + pause), - &RandomDirectionPosition::ResetDirectionAndSpeed, this); + &RandomDirectionMobilityModel::ResetDirectionAndSpeed, this); } void -RandomDirectionPosition::ResetDirectionAndSpeed (void) +RandomDirectionMobilityModel::ResetDirectionAndSpeed (void) { Update (); double direction = UniformVariable::GetSingleValue (0, PI); switch (m_side) { - case RandomDirectionPosition::RIGHT: + case RandomDirectionMobilityModel::RIGHT: direction += PI / 2; break; - case RandomDirectionPosition::LEFT: + case RandomDirectionMobilityModel::LEFT: direction += - PI / 2; break; - case RandomDirectionPosition::TOP: + case RandomDirectionMobilityModel::TOP: direction += PI; break; - case RandomDirectionPosition::BOTTOM: + case RandomDirectionMobilityModel::BOTTOM: direction += 0.0; break; } @@ -289,7 +289,7 @@ RandomDirectionPosition::ResetDirectionAndSpeed (void) NotifyCourseChange (); } void -RandomDirectionPosition::Update (void) const +RandomDirectionMobilityModel::Update (void) const { Time end = std::min (Simulator::Now (), m_pauseStart); if (m_prevTime >= end) @@ -299,7 +299,7 @@ RandomDirectionPosition::Update (void) const Time deltaTime = end - m_prevTime; m_prevTime = Simulator::Now (); double deltaS = deltaTime.GetSeconds (); - NS_ASSERT (CheckPosition ()); + NS_ASSERT (CheckMobilityModel ()); m_x += m_dx * deltaS; m_y += m_dy * deltaS; // round to closest boundaries. @@ -307,10 +307,10 @@ RandomDirectionPosition::Update (void) const m_x = std::max (m_x, m_parameters->m_xMin); m_y = std::min (m_y, m_parameters->m_yMax); m_y = std::max (m_y, m_parameters->m_yMin); - NS_ASSERT (CheckPosition ()); + NS_ASSERT (CheckMobilityModel ()); } void -RandomDirectionPosition::DoGet (double &x, double &y, double &z) const +RandomDirectionMobilityModel::DoGet (double &x, double &y, double &z) const { Update (); x = m_x; @@ -318,7 +318,7 @@ RandomDirectionPosition::DoGet (double &x, double &y, double &z) const z = 0; } void -RandomDirectionPosition::DoSet (double x, double y, double z) +RandomDirectionMobilityModel::DoSet (double x, double y, double z) { bool changed = false; if (m_x != x || m_y != y) diff --git a/src/node/random-direction-position.h b/src/node/random-direction-position.h index eda7d8c6c..5bfe9fbbb 100644 --- a/src/node/random-direction-position.h +++ b/src/node/random-direction-position.h @@ -46,7 +46,7 @@ class RandomDirectionParameters : public Object void SetPause (const RandomVariable &pauseVariable); void SetBounds (double xMin, double xMax, double yMin, double yMax); private: - friend class RandomDirectionPosition; + friend class RandomDirectionMobilityModel; double m_xMin; double m_xMax; double m_yMin; @@ -57,16 +57,16 @@ class RandomDirectionParameters : public Object std::string m_pauseVariableValue; }; -class RandomDirectionPosition : public Position +class RandomDirectionMobilityModel : public MobilityModel { public: static const InterfaceId iid; static const ClassId cid; - RandomDirectionPosition (); - RandomDirectionPosition (double x, double y); - RandomDirectionPosition (Ptr parameters); - RandomDirectionPosition (Ptr parameters, + RandomDirectionMobilityModel (); + RandomDirectionMobilityModel (double x, double y); + RandomDirectionMobilityModel (Ptr parameters); + RandomDirectionMobilityModel (Ptr parameters, double x, double y); private: enum Side { @@ -80,8 +80,8 @@ class RandomDirectionPosition : public Position void SetDirectionAndSpeed (double direction); void InitializeDirectionAndSpeed (void); void Update (void) const; - bool CheckPosition (void) const; - enum RandomDirectionPosition::Side CalculateIntersection (double &x, double &y); + bool CheckMobilityModel (void) const; + enum RandomDirectionMobilityModel::Side CalculateIntersection (double &x, double &y); virtual void DoDispose (void); virtual void DoGet (double &x, double &y, double &z) const; virtual void DoSet (double x, double y, double z); diff --git a/src/node/random-rectangle-topology.cc b/src/node/random-rectangle-topology.cc index ef6034320..cf317905a 100644 --- a/src/node/random-rectangle-topology.cc +++ b/src/node/random-rectangle-topology.cc @@ -36,19 +36,19 @@ g_yVariable ("RandomRectangleTopologyY", RandomRectangleTopology::RandomRectangleTopology () : m_xVariable (g_xVariable.GetCopy ()), m_yVariable (g_yVariable.GetCopy ()), - m_positionModel (StaticPosition::cid) + m_positionModel (StaticMobilityModel::cid) {} RandomRectangleTopology::RandomRectangleTopology (double xMin, double xMax, double yMin, double yMax) : m_xVariable (new UniformVariable (xMin, xMax)), m_yVariable (new UniformVariable (yMin, yMax)), - m_positionModel (StaticPosition::cid) + m_positionModel (StaticMobilityModel::cid) {} RandomRectangleTopology::RandomRectangleTopology (const RandomVariable &xVariable, const RandomVariable &yVariable) : m_xVariable (xVariable.Copy ()), m_yVariable (yVariable.Copy ()), - m_positionModel (StaticPosition::cid) + m_positionModel (StaticMobilityModel::cid) {} RandomRectangleTopology::~RandomRectangleTopology () { @@ -59,7 +59,7 @@ RandomRectangleTopology::~RandomRectangleTopology () } void -RandomRectangleTopology::SetPositionModel (ClassId classId) +RandomRectangleTopology::SetMobilityModelModel (ClassId classId) { m_positionModel = classId; } diff --git a/src/node/random-rectangle-topology.h b/src/node/random-rectangle-topology.h index 038de83d8..7aa557ca2 100644 --- a/src/node/random-rectangle-topology.h +++ b/src/node/random-rectangle-topology.h @@ -38,7 +38,7 @@ class RandomRectangleTopology ~RandomRectangleTopology (); - void SetPositionModel (ClassId classId); + void SetMobilityModelModel (ClassId classId); void LayoutOne (Ptr object); diff --git a/src/node/random-walk-position.cc b/src/node/random-walk-position.cc index b4c9da3eb..bfab0ece8 100644 --- a/src/node/random-walk-position.cc +++ b/src/node/random-walk-position.cc @@ -29,10 +29,10 @@ NS_DEBUG_COMPONENT_DEFINE ("RandomWalk"); namespace ns3 { -const InterfaceId RandomWalkPosition::iid = - MakeInterfaceId ("RandomWalkPosition", Position::iid); -const ClassId RandomWalkPosition::cid = - MakeClassId ("RandomWalkPosition", RandomWalkPosition::iid); +const InterfaceId RandomWalkMobilityModel::iid = + MakeInterfaceId ("RandomWalkMobilityModel", MobilityModel::iid); +const ClassId RandomWalkMobilityModel::cid = + MakeClassId ("RandomWalkMobilityModel", RandomWalkMobilityModel::iid); static IntegerDefaultValue g_minSpeed ("RandomWalkMinSpeed", @@ -41,12 +41,12 @@ static IntegerDefaultValue g_minSpeed ("RandomWalkMinSpeed", static IntegerDefaultValue g_maxSpeed ("RandomWalkMaxSpeed", "Maximum speed used during a random walk", 0.5); -static EnumDefaultValue g_mode +static EnumDefaultValue g_mode ("RandomWalkMode", "The mode indicates the condition used to " "change the current speed and direction", - RandomWalkPositionParameters::MODE_DISTANCE, "Distance", - RandomWalkPositionParameters::MODE_TIME, "Time", + RandomWalkMobilityModelParameters::MODE_DISTANCE, "Distance", + RandomWalkMobilityModelParameters::MODE_TIME, "Time", 0, 0); static IntegerDefaultValue g_modeDistance ("RandomWalkModeDistance", "Distance to walk before changing direction and speed.", @@ -55,7 +55,7 @@ static TimeDefaultValue g_modeTime ("RandomWalkModeTime", "Time to walk before changing direction and speed.", Seconds (1)); -RandomWalkPositionParameters::RandomWalkPositionParameters () +RandomWalkMobilityModelParameters::RandomWalkMobilityModelParameters () : m_minSpeed (g_minSpeed.GetValue ()), m_maxSpeed (g_maxSpeed.GetValue ()), m_mode (g_mode.GetValue ()), @@ -63,7 +63,7 @@ RandomWalkPositionParameters::RandomWalkPositionParameters () m_modeTime (g_modeTime.GetValue ()) {} bool -RandomWalkPositionParameters::IsDefault (void) const +RandomWalkMobilityModelParameters::IsDefault (void) const { if (m_minSpeed != g_minSpeed.GetValue () || m_maxSpeed != g_maxSpeed.GetValue () || @@ -77,52 +77,52 @@ RandomWalkPositionParameters::IsDefault (void) const } void -RandomWalkPositionParameters::SetSpeedBounds (double minSpeed, double maxSpeed) +RandomWalkMobilityModelParameters::SetSpeedBounds (double minSpeed, double maxSpeed) { m_minSpeed = minSpeed; m_maxSpeed = maxSpeed; } -UniformVariable RandomWalkPosition::m_randomDirection (0.0, 2*3.141592); +UniformVariable RandomWalkMobilityModel::m_randomDirection (0.0, 2*3.141592); -Ptr -RandomWalkPosition::GetDefaultParameters (void) +Ptr +RandomWalkMobilityModel::GetDefaultParameters (void) { - static Ptr parameters = Create (); + static Ptr parameters = Create (); if (!parameters->IsDefault ()) { - parameters = Create (); + parameters = Create (); } return parameters; } -RandomWalkPosition::RandomWalkPosition () +RandomWalkMobilityModel::RandomWalkMobilityModel () : m_x (0.0), m_y (0.0), m_dx (0.0), m_dy (0.0), m_prevTime (Simulator::Now ()), - m_parameters (RandomWalkPosition::GetDefaultParameters ()) + m_parameters (RandomWalkMobilityModel::GetDefaultParameters ()) { - SetInterfaceId (RandomWalkPosition::iid); + SetInterfaceId (RandomWalkMobilityModel::iid); Reset (); } -RandomWalkPosition::RandomWalkPosition (double x, double y) +RandomWalkMobilityModel::RandomWalkMobilityModel (double x, double y) : m_x (x), m_y (y), m_dx (0.0), m_dy (0.0), m_prevTime (Simulator::Now ()), - m_parameters (RandomWalkPosition::GetDefaultParameters ()) + m_parameters (RandomWalkMobilityModel::GetDefaultParameters ()) { - SetInterfaceId (RandomWalkPosition::iid); + SetInterfaceId (RandomWalkMobilityModel::iid); Reset (); } void -RandomWalkPosition::Reset (void) +RandomWalkMobilityModel::Reset (void) { Update (); double speed = UniformVariable::GetSingleValue (m_parameters->m_minSpeed, @@ -135,7 +135,7 @@ RandomWalkPosition::Reset (void) m_dx = dx; m_dy = dy; Time delay; - if (m_parameters->m_mode == RandomWalkPositionParameters::MODE_TIME) + if (m_parameters->m_mode == RandomWalkMobilityModelParameters::MODE_TIME) { delay = m_parameters->m_modeTime; } @@ -146,11 +146,11 @@ RandomWalkPosition::Reset (void) } NotifyCourseChange (); NS_DEBUG ("change speed at " << Simulator::Now () << " in " << delay); - Simulator::Schedule (delay, &RandomWalkPosition::Reset, this); + Simulator::Schedule (delay, &RandomWalkMobilityModel::Reset, this); } void -RandomWalkPosition::Update (void) const +RandomWalkMobilityModel::Update (void) const { Time deltaTime = Simulator::Now () - m_prevTime; m_prevTime = Simulator::Now (); @@ -160,14 +160,14 @@ RandomWalkPosition::Update (void) const } void -RandomWalkPosition::DoDispose (void) +RandomWalkMobilityModel::DoDispose (void) { m_parameters = 0; // chain up - Position::DoDispose (); + MobilityModel::DoDispose (); } void -RandomWalkPosition::DoGet (double &x, double &y, double &z) const +RandomWalkMobilityModel::DoGet (double &x, double &y, double &z) const { Update (); x = m_x; @@ -175,7 +175,7 @@ RandomWalkPosition::DoGet (double &x, double &y, double &z) const z = 0; } void -RandomWalkPosition::DoSet (double x, double y, double z) +RandomWalkMobilityModel::DoSet (double x, double y, double z) { bool changed = false; if (m_x != x || m_y != y) diff --git a/src/node/random-walk-position.h b/src/node/random-walk-position.h index cc65c273e..440aaf1d3 100644 --- a/src/node/random-walk-position.h +++ b/src/node/random-walk-position.h @@ -32,7 +32,7 @@ namespace ns3 { /** * \brief parameters to control a random walk model */ -class RandomWalkPositionParameters : public Object +class RandomWalkMobilityModelParameters : public Object { public: enum Mode { @@ -43,7 +43,7 @@ class RandomWalkPositionParameters : public Object * Instantiate a set of RandomWalk parameters initialized * with the Bind default values. */ - RandomWalkPositionParameters (); + RandomWalkMobilityModelParameters (); /** * \param minSpeed the minimum speed * \param maxSpeed the maximum speed @@ -63,7 +63,7 @@ class RandomWalkPositionParameters : public Object void SetModeTime (Time time); private: bool IsDefault (void) const; - friend class RandomWalkPosition; + friend class RandomWalkMobilityModel; double m_minSpeed; double m_maxSpeed; enum Mode m_mode; @@ -82,10 +82,10 @@ class RandomWalkPositionParameters : public Object * The parameters of the model can be specified either with the ns3::Bind * function and the variables "RandomWalkMinSpeed", "RandomWalkMaxSpeed", * "RandomWalkMode", "RandomWalkModeDistance", and, "RandomWalkModeTime" or - * with an instance of the RandomWalkPositionParameters class which - * must be fed to the RandomWalkPosition constructors. + * with an instance of the RandomWalkMobilityModelParameters class which + * must be fed to the RandomWalkMobilityModel constructors. */ -class RandomWalkPosition : public Position +class RandomWalkMobilityModel : public MobilityModel { public: static const InterfaceId iid; @@ -93,19 +93,19 @@ class RandomWalkPosition : public Position /** * Create a new position object located at position (0,0,0) */ - RandomWalkPosition (); + RandomWalkMobilityModel (); /** * Create a new position object located at position (x,y,0) */ - RandomWalkPosition (double x, double y); + RandomWalkMobilityModel (double x, double y); /** * Create a new position object located at position (0,0,0) */ - RandomWalkPosition (Ptr parameters); + RandomWalkMobilityModel (Ptr parameters); /** * Create a new position object located at position (x,y,0) */ - RandomWalkPosition (Ptr parameters, + RandomWalkMobilityModel (Ptr parameters, double x, double y); private: virtual void DoDispose (void); @@ -114,7 +114,7 @@ class RandomWalkPosition : public Position void Reset (void); void Update (void) const; - static Ptr GetDefaultParameters (void); + static Ptr GetDefaultParameters (void); static UniformVariable m_randomDirection; mutable double m_x; @@ -122,7 +122,7 @@ class RandomWalkPosition : public Position double m_dx; double m_dy; mutable Time m_prevTime; - Ptr m_parameters; + Ptr m_parameters; }; diff --git a/src/node/static-position.cc b/src/node/static-position.cc index b13e1b1ab..3667cf36f 100644 --- a/src/node/static-position.cc +++ b/src/node/static-position.cc @@ -22,37 +22,37 @@ namespace ns3 { -const InterfaceId StaticPosition::iid = MakeInterfaceId ("StaticPosition", Position::iid); -const ClassId StaticPosition::cid = MakeClassId ("StaticPosition", - StaticPosition::iid); +const InterfaceId StaticMobilityModel::iid = MakeInterfaceId ("StaticMobilityModel", MobilityModel::iid); +const ClassId StaticMobilityModel::cid = MakeClassId ("StaticMobilityModel", + StaticMobilityModel::iid); -StaticPosition::StaticPosition () +StaticMobilityModel::StaticMobilityModel () : m_x (0.0), m_y (0.0), m_z (0.0) { - SetInterfaceId (StaticPosition::iid); + SetInterfaceId (StaticMobilityModel::iid); } -StaticPosition::StaticPosition (double x, double y) +StaticMobilityModel::StaticMobilityModel (double x, double y) : m_x (x), m_y (y), m_z (0.0) { - SetInterfaceId (StaticPosition::iid); + SetInterfaceId (StaticMobilityModel::iid); } -StaticPosition::StaticPosition (double x, double y, double z) +StaticMobilityModel::StaticMobilityModel (double x, double y, double z) : m_x (x), m_y (y), m_z (z) { - SetInterfaceId (StaticPosition::iid); + SetInterfaceId (StaticMobilityModel::iid); } -StaticPosition::~StaticPosition () +StaticMobilityModel::~StaticMobilityModel () {} void -StaticPosition::DoGet (double &x, double &y, double &z) const +StaticMobilityModel::DoGet (double &x, double &y, double &z) const { x = m_x; y = m_y; z = m_z; } void -StaticPosition::DoSet (double x, double y, double z) +StaticMobilityModel::DoSet (double x, double y, double z) { bool changed = false; if (m_x != x || m_y != y || m_z != z) diff --git a/src/node/static-position.h b/src/node/static-position.h index 5bfd2f650..939fa0fd9 100644 --- a/src/node/static-position.h +++ b/src/node/static-position.h @@ -31,7 +31,7 @@ namespace ns3 { * change once it has been set and until it is set again * explicitely to a new value. */ -class StaticPosition : public Position +class StaticMobilityModel : public MobilityModel { public: static const InterfaceId iid; @@ -39,7 +39,7 @@ public: /** * Create a position located at coordinates (0,0,0) */ - StaticPosition (); + StaticMobilityModel (); /** * \param x x coordinate * \param y y coordinate @@ -47,7 +47,7 @@ public: * Create a position located at coordinates (x,y,0). * Unit is meters */ - StaticPosition (double x, double y); + StaticMobilityModel (double x, double y); /** * \param x x coordinate * \param y y coordinate @@ -56,8 +56,8 @@ public: * Create a position located at coordinates (x,y,z). * Unit is meters */ - StaticPosition (double x, double y, double z); - virtual ~StaticPosition (); + StaticMobilityModel (double x, double y, double z); + virtual ~StaticMobilityModel (); private: virtual void DoGet (double &x, double &y, double &z) const; diff --git a/src/node/static-speed-position.cc b/src/node/static-speed-position.cc index 7676a22fe..2ffaabc07 100644 --- a/src/node/static-speed-position.cc +++ b/src/node/static-speed-position.cc @@ -23,13 +23,13 @@ namespace ns3 { -const InterfaceId StaticSpeedPosition::iid = MakeInterfaceId ("StaticSpeedPosition", Position::iid); -const ClassId StaticSpeedPosition::cid = - MakeClassId ("StaticSpeedPosition", - StaticSpeedPosition::iid); +const InterfaceId StaticSpeedMobilityModel::iid = MakeInterfaceId ("StaticSpeedMobilityModel", MobilityModel::iid); +const ClassId StaticSpeedMobilityModel::cid = + MakeClassId ("StaticSpeedMobilityModel", + StaticSpeedMobilityModel::iid); -StaticSpeedPosition::StaticSpeedPosition () +StaticSpeedMobilityModel::StaticSpeedMobilityModel () : m_x (0.0), m_y (0.0), m_z (0.0), @@ -38,9 +38,9 @@ StaticSpeedPosition::StaticSpeedPosition () m_dz (0.0), m_prevTime (Simulator::Now ()) { - SetInterfaceId (StaticSpeedPosition::iid); + SetInterfaceId (StaticSpeedMobilityModel::iid); } -StaticSpeedPosition::StaticSpeedPosition (double x, double y, double z) +StaticSpeedMobilityModel::StaticSpeedMobilityModel (double x, double y, double z) : m_x (x), m_y (y), m_z (z), @@ -49,9 +49,9 @@ StaticSpeedPosition::StaticSpeedPosition (double x, double y, double z) m_dz (0.0), m_prevTime (Simulator::Now ()) { - SetInterfaceId (StaticSpeedPosition::iid); + SetInterfaceId (StaticSpeedMobilityModel::iid); } -StaticSpeedPosition::StaticSpeedPosition (double x, double y) +StaticSpeedMobilityModel::StaticSpeedMobilityModel (double x, double y) : m_x (x), m_y (y), m_z (0.0), @@ -60,9 +60,9 @@ StaticSpeedPosition::StaticSpeedPosition (double x, double y) m_dz (0.0), m_prevTime (Simulator::Now ()) { - SetInterfaceId (StaticSpeedPosition::iid); + SetInterfaceId (StaticSpeedMobilityModel::iid); } -StaticSpeedPosition::StaticSpeedPosition (double x, double y, double z, +StaticSpeedMobilityModel::StaticSpeedMobilityModel (double x, double y, double z, double dx, double dy, double dz) : m_x (x), m_y (y), @@ -72,14 +72,14 @@ StaticSpeedPosition::StaticSpeedPosition (double x, double y, double z, m_dz (dz), m_prevTime (Simulator::Now ()) { - SetInterfaceId (StaticSpeedPosition::iid); + SetInterfaceId (StaticSpeedMobilityModel::iid); } -StaticSpeedPosition::~StaticSpeedPosition () +StaticSpeedMobilityModel::~StaticSpeedMobilityModel () {} void -StaticSpeedPosition::SetSpeed (double dx, double dy, double dz) +StaticSpeedMobilityModel::SetSpeed (double dx, double dy, double dz) { bool changed = false; Update (); @@ -97,7 +97,7 @@ StaticSpeedPosition::SetSpeed (double dx, double dy, double dz) } void -StaticSpeedPosition::Update (void) const +StaticSpeedMobilityModel::Update (void) const { Time deltaTime = Simulator::Now () - m_prevTime; m_prevTime = Simulator::Now (); @@ -108,7 +108,7 @@ StaticSpeedPosition::Update (void) const } void -StaticSpeedPosition::DoGet (double &x, double &y, double &z) const +StaticSpeedMobilityModel::DoGet (double &x, double &y, double &z) const { Update (); x = m_x; @@ -116,7 +116,7 @@ StaticSpeedPosition::DoGet (double &x, double &y, double &z) const z = m_z; } void -StaticSpeedPosition::DoSet (double x, double y, double z) +StaticSpeedMobilityModel::DoSet (double x, double y, double z) { bool changed = false; Update (); diff --git a/src/node/static-speed-position.h b/src/node/static-speed-position.h index ed8d48e64..2ee6b7875 100644 --- a/src/node/static-speed-position.h +++ b/src/node/static-speed-position.h @@ -28,7 +28,7 @@ namespace ns3 { -class StaticSpeedPosition : public Position +class StaticSpeedMobilityModel : public MobilityModel { public: static const InterfaceId iid; @@ -37,7 +37,7 @@ public: * Create position located at coordinates (0,0,0) with * speed (0,0,0). */ - StaticSpeedPosition (); + StaticSpeedMobilityModel (); /** * \param x x coordinate * \param y y coordinate @@ -46,7 +46,7 @@ public: * speed (0,0,0). * Unit is meters */ - StaticSpeedPosition (double x, double y); + StaticSpeedMobilityModel (double x, double y); /** * \param x x coordinate * \param y y coordinate @@ -56,7 +56,7 @@ public: * speed (0,0,0). * Unit is meters */ - StaticSpeedPosition (double x, double y, double z); + StaticSpeedMobilityModel (double x, double y, double z); /** * \param x x coordinate * \param y y coordinate @@ -69,9 +69,9 @@ public: * speed (dx,dy,dz). * Unit is meters and meters/s */ - StaticSpeedPosition (double x, double y, double z, + StaticSpeedMobilityModel (double x, double y, double z, double dx, double dy, double dz); - virtual ~StaticSpeedPosition (); + virtual ~StaticSpeedMobilityModel (); /* * \param dx x coordinate speed From dd0ff29f66c8e981de4fd5531ab252d73a30cc5e Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 4 Jul 2007 10:15:18 +0200 Subject: [PATCH 039/148] position.h -> mobility-model.h --- SConstruct | 20 +- samples/main-grid-topology.cc | 2 +- samples/main-random-walk.cc | 4 +- src/node/grid-topology.cc | 2 +- src/node/mobility-model-notifier.h | 2 +- src/node/{position.cc => mobility-model.cc} | 2 +- src/node/{position.h => mobility-model.h} | 0 ....cc => random-direction-mobility-model.cc} | 2 +- ...on.h => random-direction-mobility-model.h} | 2 +- ...k-position.cc => random-mobility-model.cc} | 2 +- ...alk-position.h => random-mobility-model.h} | 2 +- src/node/random-rectangle-topology.cc | 2 +- src/node/random-walk-mobility-model.cc | 195 ++++++++++++++++++ src/node/random-walk-mobility-model.h | 131 ++++++++++++ ...c-position.cc => static-mobility-model.cc} | 2 +- ...tic-position.h => static-mobility-model.h} | 2 +- ...tion.cc => static-speed-mobility-model.cc} | 2 +- ...sition.h => static-speed-mobility-model.h} | 2 +- 18 files changed, 351 insertions(+), 25 deletions(-) rename src/node/{position.cc => mobility-model.cc} (98%) rename src/node/{position.h => mobility-model.h} (100%) rename src/node/{random-direction-position.cc => random-direction-mobility-model.cc} (99%) rename src/node/{random-direction-position.h => random-direction-mobility-model.h} (99%) rename src/node/{random-walk-position.cc => random-mobility-model.cc} (99%) rename src/node/{random-walk-position.h => random-mobility-model.h} (99%) create mode 100644 src/node/random-walk-mobility-model.cc create mode 100644 src/node/random-walk-mobility-model.h rename src/node/{static-position.cc => static-mobility-model.cc} (98%) rename src/node/{static-position.h => static-mobility-model.h} (98%) rename src/node/{static-speed-position.cc => static-speed-mobility-model.cc} (98%) rename src/node/{static-speed-position.h => static-speed-mobility-model.h} (99%) diff --git a/SConstruct b/SConstruct index 4b3f0a0cd..b89015f4c 100644 --- a/SConstruct +++ b/SConstruct @@ -248,14 +248,14 @@ node.add_sources ([ 'udp.cc', 'ipv4.cc', 'application.cc', - 'position.cc', + 'mobility-model.cc', 'mobility-model-notifier.cc', - 'static-position.cc', - 'static-speed-position.cc', + 'static-mobility-model.cc', + 'static-speed-mobility-model.cc', 'grid-topology.cc', 'random-rectangle-topology.cc', - 'random-walk-position.cc', - 'random-direction-position.cc', + 'random-walk-mobility-model.cc', + 'random-direction-mobility-model.cc', ]) node.add_inst_headers ([ 'node.h', @@ -273,14 +273,14 @@ node.add_inst_headers ([ 'udp.h', 'ipv4.h', 'application.h', - 'position.h', + 'mobility-model.h', 'mobility-model-notifier.h', - 'static-position.h', - 'static-speed-position.h', + 'static-mobility-model.h', + 'static-speed-mobility-model.h', 'grid-topology.h', 'random-rectangle-topology.h', - 'random-walk-position.h', - 'random-direction-position.h', + 'random-walk-mobility-model.h', + 'random-direction-mobility-model.h', ]) applications = build.Ns3Module ('applications', 'src/applications') diff --git a/samples/main-grid-topology.cc b/samples/main-grid-topology.cc index 46b7d28c6..ae45ecf68 100644 --- a/samples/main-grid-topology.cc +++ b/samples/main-grid-topology.cc @@ -2,7 +2,7 @@ #include "ns3/ptr.h" #include "ns3/grid-topology.h" -#include "ns3/static-position.h" +#include "ns3/static-mobility-model.h" #include "ns3/internet-node.h" #include "ns3/command-line.h" diff --git a/samples/main-random-walk.cc b/samples/main-random-walk.cc index 9c80e7551..26d5a4569 100644 --- a/samples/main-random-walk.cc +++ b/samples/main-random-walk.cc @@ -1,9 +1,9 @@ /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ #include "ns3/ptr.h" -#include "ns3/position.h" +#include "ns3/mobility-model.h" #include "ns3/mobility-model-notifier.h" -#include "ns3/random-walk-position.h" +#include "ns3/random-walk-mobility-model.h" #include "ns3/default-value.h" #include "ns3/command-line.h" #include "ns3/simulator.h" diff --git a/src/node/grid-topology.cc b/src/node/grid-topology.cc index d3fe2c588..11911e8c7 100644 --- a/src/node/grid-topology.cc +++ b/src/node/grid-topology.cc @@ -19,7 +19,7 @@ * Author: Mathieu Lacage */ #include "grid-topology.h" -#include "static-position.h" +#include "static-mobility-model.h" namespace ns3 { diff --git a/src/node/mobility-model-notifier.h b/src/node/mobility-model-notifier.h index 4a687e6a0..8aec327de 100644 --- a/src/node/mobility-model-notifier.h +++ b/src/node/mobility-model-notifier.h @@ -24,7 +24,7 @@ #include "ns3/object.h" #include "ns3/component-manager.h" #include "ns3/callback.h" -#include "position.h" +#include "mobility-model.h" namespace ns3 { diff --git a/src/node/position.cc b/src/node/mobility-model.cc similarity index 98% rename from src/node/position.cc rename to src/node/mobility-model.cc index 8095cc466..30cc417c3 100644 --- a/src/node/position.cc +++ b/src/node/mobility-model.cc @@ -18,7 +18,7 @@ * * Author: Mathieu Lacage */ -#include "position.h" +#include "mobility-model.h" #include "mobility-model-notifier.h" #include diff --git a/src/node/position.h b/src/node/mobility-model.h similarity index 100% rename from src/node/position.h rename to src/node/mobility-model.h diff --git a/src/node/random-direction-position.cc b/src/node/random-direction-mobility-model.cc similarity index 99% rename from src/node/random-direction-position.cc rename to src/node/random-direction-mobility-model.cc index 7da45cad8..f73fe9b9a 100644 --- a/src/node/random-direction-position.cc +++ b/src/node/random-direction-mobility-model.cc @@ -23,7 +23,7 @@ #include "ns3/simulator.h" #include #include -#include "random-direction-position.h" +#include "random-direction-mobility-model.h" namespace ns3 { diff --git a/src/node/random-direction-position.h b/src/node/random-direction-mobility-model.h similarity index 99% rename from src/node/random-direction-position.h rename to src/node/random-direction-mobility-model.h index 5bfe9fbbb..50337a79c 100644 --- a/src/node/random-direction-position.h +++ b/src/node/random-direction-mobility-model.h @@ -26,7 +26,7 @@ #include "ns3/nstime.h" #include "ns3/event-id.h" #include "ns3/component-manager.h" -#include "position.h" +#include "mobility-model.h" namespace ns3 { diff --git a/src/node/random-walk-position.cc b/src/node/random-mobility-model.cc similarity index 99% rename from src/node/random-walk-position.cc rename to src/node/random-mobility-model.cc index bfab0ece8..12410f8d5 100644 --- a/src/node/random-walk-position.cc +++ b/src/node/random-mobility-model.cc @@ -18,7 +18,7 @@ * * Author: Mathieu Lacage */ -#include "random-walk-position.h" +#include "random-walk-mobility-model.h" #include "ns3/default-value.h" #include "ns3/time-default-value.h" #include "ns3/simulator.h" diff --git a/src/node/random-walk-position.h b/src/node/random-mobility-model.h similarity index 99% rename from src/node/random-walk-position.h rename to src/node/random-mobility-model.h index 440aaf1d3..8e7ecbc3b 100644 --- a/src/node/random-walk-position.h +++ b/src/node/random-mobility-model.h @@ -22,7 +22,7 @@ #define RANDOM_WALK_POSITION_H #include "ns3/object.h" -#include "ns3/position.h" +#include "ns3/mobility-model.h" #include "ns3/nstime.h" #include "ns3/random-variable.h" #include "ns3/component-manager.h" diff --git a/src/node/random-rectangle-topology.cc b/src/node/random-rectangle-topology.cc index cf317905a..05b3af74d 100644 --- a/src/node/random-rectangle-topology.cc +++ b/src/node/random-rectangle-topology.cc @@ -19,7 +19,7 @@ * Author: Mathieu Lacage */ #include "random-rectangle-topology.h" -#include "static-position.h" +#include "static-mobility-model.h" #include "ns3/random-variable-default-value.h" namespace ns3 { diff --git a/src/node/random-walk-mobility-model.cc b/src/node/random-walk-mobility-model.cc new file mode 100644 index 000000000..12410f8d5 --- /dev/null +++ b/src/node/random-walk-mobility-model.cc @@ -0,0 +1,195 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2006,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 "random-walk-mobility-model.h" +#include "ns3/default-value.h" +#include "ns3/time-default-value.h" +#include "ns3/simulator.h" +#include "ns3/debug.h" +#include + +NS_DEBUG_COMPONENT_DEFINE ("RandomWalk"); + +namespace ns3 { + +const InterfaceId RandomWalkMobilityModel::iid = + MakeInterfaceId ("RandomWalkMobilityModel", MobilityModel::iid); +const ClassId RandomWalkMobilityModel::cid = + MakeClassId ("RandomWalkMobilityModel", RandomWalkMobilityModel::iid); + + +static IntegerDefaultValue g_minSpeed ("RandomWalkMinSpeed", + "Minimum speed used during a random walk", + 0.1); +static IntegerDefaultValue g_maxSpeed ("RandomWalkMaxSpeed", + "Maximum speed used during a random walk", + 0.5); +static EnumDefaultValue g_mode + ("RandomWalkMode", + "The mode indicates the condition used to " + "change the current speed and direction", + RandomWalkMobilityModelParameters::MODE_DISTANCE, "Distance", + RandomWalkMobilityModelParameters::MODE_TIME, "Time", + 0, 0); +static IntegerDefaultValue g_modeDistance ("RandomWalkModeDistance", + "Distance to walk before changing direction and speed.", + 10); +static TimeDefaultValue g_modeTime ("RandomWalkModeTime", + "Time to walk before changing direction and speed.", + Seconds (1)); + +RandomWalkMobilityModelParameters::RandomWalkMobilityModelParameters () + : m_minSpeed (g_minSpeed.GetValue ()), + m_maxSpeed (g_maxSpeed.GetValue ()), + m_mode (g_mode.GetValue ()), + m_modeDistance (g_modeDistance.GetValue ()), + m_modeTime (g_modeTime.GetValue ()) +{} +bool +RandomWalkMobilityModelParameters::IsDefault (void) const +{ + if (m_minSpeed != g_minSpeed.GetValue () || + m_maxSpeed != g_maxSpeed.GetValue () || + m_mode != g_mode.GetValue () || + m_modeDistance != g_modeDistance.GetValue () || + m_modeTime != g_modeTime.GetValue ()) + { + return false; + } + return true; +} + +void +RandomWalkMobilityModelParameters::SetSpeedBounds (double minSpeed, double maxSpeed) +{ + m_minSpeed = minSpeed; + m_maxSpeed = maxSpeed; +} + + +UniformVariable RandomWalkMobilityModel::m_randomDirection (0.0, 2*3.141592); + +Ptr +RandomWalkMobilityModel::GetDefaultParameters (void) +{ + static Ptr parameters = Create (); + if (!parameters->IsDefault ()) + { + parameters = Create (); + } + return parameters; +} + +RandomWalkMobilityModel::RandomWalkMobilityModel () + : m_x (0.0), + m_y (0.0), + m_dx (0.0), + m_dy (0.0), + m_prevTime (Simulator::Now ()), + m_parameters (RandomWalkMobilityModel::GetDefaultParameters ()) +{ + SetInterfaceId (RandomWalkMobilityModel::iid); + Reset (); +} + +RandomWalkMobilityModel::RandomWalkMobilityModel (double x, double y) + : m_x (x), + m_y (y), + m_dx (0.0), + m_dy (0.0), + m_prevTime (Simulator::Now ()), + m_parameters (RandomWalkMobilityModel::GetDefaultParameters ()) +{ + SetInterfaceId (RandomWalkMobilityModel::iid); + Reset (); +} + +void +RandomWalkMobilityModel::Reset (void) +{ + Update (); + double speed = UniformVariable::GetSingleValue (m_parameters->m_minSpeed, + m_parameters->m_maxSpeed); + NS_DEBUG ("min="<< m_parameters->m_minSpeed << ", max=" << m_parameters->m_maxSpeed << + ", speed=" << speed); + double direction = m_randomDirection.GetValue (); + double dx = std::cos (direction) * speed; + double dy = std::sin (direction) * speed; + m_dx = dx; + m_dy = dy; + Time delay; + if (m_parameters->m_mode == RandomWalkMobilityModelParameters::MODE_TIME) + { + delay = m_parameters->m_modeTime; + } + else + { + double distance = m_parameters->m_modeDistance; + delay = Seconds (distance / sqrt (m_dx * m_dx + m_dy * m_dy)); + } + NotifyCourseChange (); + NS_DEBUG ("change speed at " << Simulator::Now () << " in " << delay); + Simulator::Schedule (delay, &RandomWalkMobilityModel::Reset, this); +} + +void +RandomWalkMobilityModel::Update (void) const +{ + Time deltaTime = Simulator::Now () - m_prevTime; + m_prevTime = Simulator::Now (); + double deltaS = deltaTime.GetSeconds (); + m_x += m_dx * deltaS; + m_y += m_dy * deltaS; +} + +void +RandomWalkMobilityModel::DoDispose (void) +{ + m_parameters = 0; + // chain up + MobilityModel::DoDispose (); +} +void +RandomWalkMobilityModel::DoGet (double &x, double &y, double &z) const +{ + Update (); + x = m_x; + y = m_y; + z = 0; +} +void +RandomWalkMobilityModel::DoSet (double x, double y, double z) +{ + bool changed = false; + if (m_x != x || m_y != y) + { + changed = true; + } + m_x = x; + m_y = y; + m_prevTime = Simulator::Now (); + if (changed) + { + NotifyCourseChange (); + } +} + + +} // namespace ns3 diff --git a/src/node/random-walk-mobility-model.h b/src/node/random-walk-mobility-model.h new file mode 100644 index 000000000..8e7ecbc3b --- /dev/null +++ b/src/node/random-walk-mobility-model.h @@ -0,0 +1,131 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2006,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 RANDOM_WALK_POSITION_H +#define RANDOM_WALK_POSITION_H + +#include "ns3/object.h" +#include "ns3/mobility-model.h" +#include "ns3/nstime.h" +#include "ns3/random-variable.h" +#include "ns3/component-manager.h" + +namespace ns3 { + +/** + * \brief parameters to control a random walk model + */ +class RandomWalkMobilityModelParameters : public Object +{ + public: + enum Mode { + MODE_DISTANCE, + MODE_TIME + }; + /** + * Instantiate a set of RandomWalk parameters initialized + * with the Bind default values. + */ + RandomWalkMobilityModelParameters (); + /** + * \param minSpeed the minimum speed + * \param maxSpeed the maximum speed + * + * The speed of any node is chosen such that minSpeed <= speed <= maxSpeed + */ + void SetSpeedBounds (double minSpeed, double maxSpeed); + /** + * \param distance the distance before a direction change + * + * Unit is meters + */ + void SetModeDistance (double distance); + /** + * \param time the delay before a direction change. + */ + void SetModeTime (Time time); + private: + bool IsDefault (void) const; + friend class RandomWalkMobilityModel; + double m_minSpeed; + double m_maxSpeed; + enum Mode m_mode; + double m_modeDistance; + Time m_modeTime; +}; + +/** + * \brief an unbounded 2D random walk position model + * + * Each instance moves with a speed and direction choosen at random + * in the intervals [minspeed,maxspeed] and [0,2pi] until + * either a fixed distance has been walked or until a fixed amount + * of time. + * + * The parameters of the model can be specified either with the ns3::Bind + * function and the variables "RandomWalkMinSpeed", "RandomWalkMaxSpeed", + * "RandomWalkMode", "RandomWalkModeDistance", and, "RandomWalkModeTime" or + * with an instance of the RandomWalkMobilityModelParameters class which + * must be fed to the RandomWalkMobilityModel constructors. + */ +class RandomWalkMobilityModel : public MobilityModel +{ + public: + static const InterfaceId iid; + static const ClassId cid; + /** + * Create a new position object located at position (0,0,0) + */ + RandomWalkMobilityModel (); + /** + * Create a new position object located at position (x,y,0) + */ + RandomWalkMobilityModel (double x, double y); + /** + * Create a new position object located at position (0,0,0) + */ + RandomWalkMobilityModel (Ptr parameters); + /** + * Create a new position object located at position (x,y,0) + */ + RandomWalkMobilityModel (Ptr parameters, + double x, double y); + private: + virtual void DoDispose (void); + virtual void DoGet (double &x, double &y, double &z) const; + virtual void DoSet (double x, double y, double z); + + void Reset (void); + void Update (void) const; + static Ptr GetDefaultParameters (void); + static UniformVariable m_randomDirection; + + mutable double m_x; + mutable double m_y; + double m_dx; + double m_dy; + mutable Time m_prevTime; + Ptr m_parameters; +}; + + +} // namespace ns3 + +#endif /* RANDOM_WALK_POSITION_H */ diff --git a/src/node/static-position.cc b/src/node/static-mobility-model.cc similarity index 98% rename from src/node/static-position.cc rename to src/node/static-mobility-model.cc index 3667cf36f..9356ee30f 100644 --- a/src/node/static-position.cc +++ b/src/node/static-mobility-model.cc @@ -18,7 +18,7 @@ * * Author: Mathieu Lacage */ -#include "static-position.h" +#include "static-mobility-model.h" namespace ns3 { diff --git a/src/node/static-position.h b/src/node/static-mobility-model.h similarity index 98% rename from src/node/static-position.h rename to src/node/static-mobility-model.h index 939fa0fd9..cb9da4c78 100644 --- a/src/node/static-position.h +++ b/src/node/static-mobility-model.h @@ -22,7 +22,7 @@ #define STATIC_POSITION_H #include "ns3/component-manager.h" -#include "position.h" +#include "mobility-model.h" namespace ns3 { diff --git a/src/node/static-speed-position.cc b/src/node/static-speed-mobility-model.cc similarity index 98% rename from src/node/static-speed-position.cc rename to src/node/static-speed-mobility-model.cc index 2ffaabc07..4684834de 100644 --- a/src/node/static-speed-position.cc +++ b/src/node/static-speed-mobility-model.cc @@ -18,7 +18,7 @@ * * Author: Mathieu Lacage */ -#include "static-speed-position.h" +#include "static-speed-mobility-model.h" #include "ns3/simulator.h" namespace ns3 { diff --git a/src/node/static-speed-position.h b/src/node/static-speed-mobility-model.h similarity index 99% rename from src/node/static-speed-position.h rename to src/node/static-speed-mobility-model.h index 2ee6b7875..7421cd31e 100644 --- a/src/node/static-speed-position.h +++ b/src/node/static-speed-mobility-model.h @@ -22,7 +22,7 @@ #define STATIC_SPEED_POSITION_H #include -#include "position.h" +#include "mobility-model.h" #include "ns3/nstime.h" #include "ns3/component-manager.h" From c416887542b1cf8286e40bbe49d69b2dfe2ec3a4 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 4 Jul 2007 10:18:14 +0200 Subject: [PATCH 040/148] POSITION_H -> MOBILITY_MODEL_H --- src/node/mobility-model.h | 6 +++--- src/node/random-direction-mobility-model.h | 6 +++--- src/node/random-mobility-model.h | 6 +++--- src/node/random-walk-mobility-model.h | 6 +++--- src/node/static-mobility-model.h | 6 +++--- src/node/static-speed-mobility-model.h | 4 ++-- 6 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/node/mobility-model.h b/src/node/mobility-model.h index db1ca2567..ab080274e 100644 --- a/src/node/mobility-model.h +++ b/src/node/mobility-model.h @@ -18,8 +18,8 @@ * * Author: Mathieu Lacage */ -#ifndef POSITION_H -#define POSITION_H +#ifndef MOBILITY_MODEL_H +#define MOBILITY_MODEL_H #include "ns3/object.h" @@ -105,4 +105,4 @@ private: }; // namespace ns3 -#endif /* POSITION_H */ +#endif /* MOBILITY_MODEL_H */ diff --git a/src/node/random-direction-mobility-model.h b/src/node/random-direction-mobility-model.h index 50337a79c..ac4810601 100644 --- a/src/node/random-direction-mobility-model.h +++ b/src/node/random-direction-mobility-model.h @@ -18,8 +18,8 @@ * * Author: Mathieu Lacage */ -#ifndef RANDOM_DIRECTION_POSITION_H -#define RANDOM_DIRECTION_POSITION_H +#ifndef RANDOM_DIRECTION_MOBILITY_MODEL_H +#define RANDOM_DIRECTION_MOBILITY_MODEL_H #include "ns3/object.h" #include "ns3/ptr.h" @@ -100,4 +100,4 @@ class RandomDirectionMobilityModel : public MobilityModel } // namespace ns3 -#endif /* RANDOM_DIRECTION_POSITION_H */ +#endif /* RANDOM_DIRECTION_MOBILITY_MODEL_H */ diff --git a/src/node/random-mobility-model.h b/src/node/random-mobility-model.h index 8e7ecbc3b..72448e175 100644 --- a/src/node/random-mobility-model.h +++ b/src/node/random-mobility-model.h @@ -18,8 +18,8 @@ * * Author: Mathieu Lacage */ -#ifndef RANDOM_WALK_POSITION_H -#define RANDOM_WALK_POSITION_H +#ifndef RANDOM_WALK_MOBILITY_MODEL_H +#define RANDOM_WALK_MOBILITY_MODEL_H #include "ns3/object.h" #include "ns3/mobility-model.h" @@ -128,4 +128,4 @@ class RandomWalkMobilityModel : public MobilityModel } // namespace ns3 -#endif /* RANDOM_WALK_POSITION_H */ +#endif /* RANDOM_WALK_MOBILITY_MODEL_H */ diff --git a/src/node/random-walk-mobility-model.h b/src/node/random-walk-mobility-model.h index 8e7ecbc3b..72448e175 100644 --- a/src/node/random-walk-mobility-model.h +++ b/src/node/random-walk-mobility-model.h @@ -18,8 +18,8 @@ * * Author: Mathieu Lacage */ -#ifndef RANDOM_WALK_POSITION_H -#define RANDOM_WALK_POSITION_H +#ifndef RANDOM_WALK_MOBILITY_MODEL_H +#define RANDOM_WALK_MOBILITY_MODEL_H #include "ns3/object.h" #include "ns3/mobility-model.h" @@ -128,4 +128,4 @@ class RandomWalkMobilityModel : public MobilityModel } // namespace ns3 -#endif /* RANDOM_WALK_POSITION_H */ +#endif /* RANDOM_WALK_MOBILITY_MODEL_H */ diff --git a/src/node/static-mobility-model.h b/src/node/static-mobility-model.h index cb9da4c78..215fd5872 100644 --- a/src/node/static-mobility-model.h +++ b/src/node/static-mobility-model.h @@ -18,8 +18,8 @@ * * Author: Mathieu Lacage */ -#ifndef STATIC_POSITION_H -#define STATIC_POSITION_H +#ifndef STATIC_MOBILITY_MODEL_H +#define STATIC_MOBILITY_MODEL_H #include "ns3/component-manager.h" #include "mobility-model.h" @@ -69,4 +69,4 @@ private: }; // namespace ns3 -#endif /* STATIC_POSITION_H */ +#endif /* STATIC_MOBILITY_MODEL_H */ diff --git a/src/node/static-speed-mobility-model.h b/src/node/static-speed-mobility-model.h index 7421cd31e..d1ff617fa 100644 --- a/src/node/static-speed-mobility-model.h +++ b/src/node/static-speed-mobility-model.h @@ -18,8 +18,8 @@ * * Author: Mathieu Lacage */ -#ifndef STATIC_SPEED_POSITION_H -#define STATIC_SPEED_POSITION_H +#ifndef STATIC_SPEED_MOBILITY_MODEL_H +#define STATIC_SPEED_MOBILITY_MODEL_H #include #include "mobility-model.h" From 99ec33418a40258ef746bfd0936dc27eea4e4c57 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 4 Jul 2007 10:24:45 +0200 Subject: [PATCH 041/148] add Position class and Get/Set methods on MobilityModel --- src/node/mobility-model.cc | 12 ++++++++++++ src/node/mobility-model.h | 24 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/node/mobility-model.cc b/src/node/mobility-model.cc index 30cc417c3..3cd675740 100644 --- a/src/node/mobility-model.cc +++ b/src/node/mobility-model.cc @@ -39,6 +39,13 @@ MobilityModel::Get (double &x, double &y, double &z) const { DoGet (x,y,z); } +Position +MobilityModel::Get (void) const +{ + Position position; + DoGet (position.x,position.y,position.z); + return position; +} double MobilityModel::GetX (void) const { @@ -67,6 +74,11 @@ MobilityModel::Set (double x, double y, double z) DoSet (x, y, z); } void +MobilityModel::Set (const Position &position) +{ + DoSet (position.x, position.y, position.z); +} +void MobilityModel::SetXY (double x, double y) { double currentX, currentY, currentZ; diff --git a/src/node/mobility-model.h b/src/node/mobility-model.h index ab080274e..6cf815f44 100644 --- a/src/node/mobility-model.h +++ b/src/node/mobility-model.h @@ -25,6 +25,28 @@ namespace ns3 { +class Vector3D +{ +public: + double x; + double y; + double z; +}; +class Position : public Vector3D +{}; +class Speed : public Vector3D +{}; +class Vector2D +{ +public: + double x; + double y; +}; +class Position2D : public Vector2D +{}; +class Speed2D : public Vector2D +{}; + /** * \brief keep track of the current position of an object * @@ -49,6 +71,7 @@ public: * Unit is meters */ void Get (double &x, double &y, double &z) const; + Position Get (void) const; /** * \returns the current x coordinate * @@ -71,6 +94,7 @@ public: void Add (double dx, double dy, double dz); void Set (double x, double y, double z); + void Set (const Position &position); void SetXY (double x, double y); void SetX (double x); void SetY (double y); From 2aca9086851848bca01e6f784d34737b9af390ee Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 4 Jul 2007 10:27:49 +0200 Subject: [PATCH 042/148] use new MobilityModel::Get and remove old one. --- samples/main-grid-topology.cc | 5 ++--- samples/main-random-walk.cc | 6 +++--- src/node/mobility-model.cc | 5 ----- src/node/mobility-model.h | 8 +------- 4 files changed, 6 insertions(+), 18 deletions(-) diff --git a/samples/main-grid-topology.cc b/samples/main-grid-topology.cc index ae45ecf68..d9f6f3d2a 100644 --- a/samples/main-grid-topology.cc +++ b/samples/main-grid-topology.cc @@ -41,9 +41,8 @@ int main (int argc, char *argv[]) Ptr object = *j; Ptr position = object->QueryInterface (MobilityModel::iid); NS_ASSERT (position != 0); - double x, y, z; - position->Get (x,y,z); - std::cout << "x=" << x << ", y=" << y << ", z=" << z << std::endl; + Position pos = position->Get (); + std::cout << "x=" << pos.x << ", y=" << pos.y << ", z=" << pos.z << std::endl; } return 0; diff --git a/samples/main-random-walk.cc b/samples/main-random-walk.cc index 26d5a4569..295a89c74 100644 --- a/samples/main-random-walk.cc +++ b/samples/main-random-walk.cc @@ -14,9 +14,9 @@ using namespace ns3; static void CourseChange (Ptr position) { - double x, y, z; - position->Get (x, y, z); - std::cout << Simulator::Now () << ", pos=" << position << ", x=" << x << ", y=" << y << ", z=" << z << std::endl; + 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[]) diff --git a/src/node/mobility-model.cc b/src/node/mobility-model.cc index 3cd675740..4f7430916 100644 --- a/src/node/mobility-model.cc +++ b/src/node/mobility-model.cc @@ -34,11 +34,6 @@ MobilityModel::MobilityModel () MobilityModel::~MobilityModel () {} -void -MobilityModel::Get (double &x, double &y, double &z) const -{ - DoGet (x,y,z); -} Position MobilityModel::Get (void) const { diff --git a/src/node/mobility-model.h b/src/node/mobility-model.h index 6cf815f44..2a3ed1d70 100644 --- a/src/node/mobility-model.h +++ b/src/node/mobility-model.h @@ -62,15 +62,9 @@ public: virtual ~MobilityModel () = 0; /** - * \param x reference to floating-point variable for x coordinate. - * \param y reference to floating-point variable for y coordinate. - * \param z reference to floating-point variable for z coordinate. - * - * Store in the x, y, and z variables the current coordinates - * managed by this position object. + * \returns the current position * Unit is meters */ - void Get (double &x, double &y, double &z) const; Position Get (void) const; /** * \returns the current x coordinate From be946aab508211225c02b6b9ba56911bf8f66762 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 4 Jul 2007 13:52:12 +0200 Subject: [PATCH 043/148] new version of DoGet and DoSet --- src/node/mobility-model.cc | 73 +++++++++++---------- src/node/mobility-model.h | 10 ++- src/node/random-direction-mobility-model.cc | 16 ++--- src/node/random-direction-mobility-model.h | 4 +- src/node/random-walk-mobility-model.cc | 16 ++--- src/node/random-walk-mobility-model.h | 4 +- src/node/static-mobility-model.cc | 18 +++-- src/node/static-mobility-model.h | 4 +- src/node/static-speed-mobility-model.cc | 18 +++-- src/node/static-speed-mobility-model.h | 4 +- 10 files changed, 84 insertions(+), 83 deletions(-) diff --git a/src/node/mobility-model.cc b/src/node/mobility-model.cc index 4f7430916..574a94e0b 100644 --- a/src/node/mobility-model.cc +++ b/src/node/mobility-model.cc @@ -26,6 +26,16 @@ namespace ns3 { const InterfaceId MobilityModel::iid = MakeInterfaceId ("MobilityModel", Object::iid); +Vector3D::Vector3D (double x, double y, double z) + : x (x), + y (y), + z (z) +{} + +Position::Position (double x, double y, double z) + : Vector3D (x, y, z) +{} + MobilityModel::MobilityModel () { SetInterfaceId (MobilityModel::iid); @@ -37,82 +47,77 @@ MobilityModel::~MobilityModel () Position MobilityModel::Get (void) const { - Position position; - DoGet (position.x,position.y,position.z); - return position; + return DoGet (); } double MobilityModel::GetX (void) const { - double x,y,z; - DoGet (x,y,z); - return x; + Position position = DoGet (); + return position.x; } double MobilityModel::GetY (void) const { - double x, y, z; - DoGet (x,y,z); - return y; + Position position = DoGet (); + return position.y; } double MobilityModel::GetZ (void) const { - double x, y, z; - DoGet (x,y,z); - return z; + Position position = DoGet (); + return position.z; } void MobilityModel::Set (double x, double y, double z) { - DoSet (x, y, z); + Position position (x, y, z); + DoSet (position); } void MobilityModel::Set (const Position &position) { - DoSet (position.x, position.y, position.z); + DoSet (position); } void MobilityModel::SetXY (double x, double y) { - double currentX, currentY, currentZ; - DoGet (currentX, currentY, currentZ); - DoSet (x, y, currentZ); + Position position = DoGet (); + position.x = x; + position.y = y; + DoSet (position); } void MobilityModel::SetX (double x) { - double currentX, currentY, currentZ; - DoGet (currentX, currentY, currentZ); - DoSet (x, currentY, currentZ); + Position position = DoGet (); + position.x = x; + DoSet (position); } void MobilityModel::SetY (double y) { - double currentX, currentY, currentZ; - DoGet (currentX, currentY, currentZ); - DoSet (currentX, y, currentZ); + Position position = DoGet (); + position.y = y; + DoSet (position); } void MobilityModel::SetZ (double z) { - double currentX, currentY, currentZ; - DoGet (currentX, currentY, currentZ); - DoSet (currentX, currentY, z); + Position position = DoGet (); + position.z = z; + DoSet (position); } double -MobilityModel::GetDistanceFrom (const MobilityModel &position) const +MobilityModel::GetDistanceFrom (const MobilityModel &other) const { - double ox,oy,oz; - double x,y,z; - position.DoGet (ox,oy,oz); - DoGet (x,y,z); - double dx = ox - x; - double dy = oy - y; - double dz = oz - z; + Position oPosition = other.DoGet (); + Position position = DoGet (); + double dx = oPosition.x - position.x; + double dy = oPosition.y - position.y; + double dz = oPosition.z - position.z; return sqrt (dx*dx+dy*dy+dz*dz); } diff --git a/src/node/mobility-model.h b/src/node/mobility-model.h index 2a3ed1d70..971c14b5b 100644 --- a/src/node/mobility-model.h +++ b/src/node/mobility-model.h @@ -28,12 +28,16 @@ namespace ns3 { class Vector3D { public: + Vector3D (double x, double y, double z); double x; double y; double z; }; class Position : public Vector3D -{}; +{ +public: + Position (double x, double y, double z); +}; class Speed : public Vector3D {}; class Vector2D @@ -117,8 +121,8 @@ private: * base class must implement this method. * Unit is meters */ - virtual void DoGet (double &x, double &y, double &z) const = 0; - virtual void DoSet (double x, double y, double z) = 0; + virtual Position DoGet (void) const = 0; + virtual void DoSet (const Position &position) = 0; }; }; // namespace ns3 diff --git a/src/node/random-direction-mobility-model.cc b/src/node/random-direction-mobility-model.cc index f73fe9b9a..f1b6c4c13 100644 --- a/src/node/random-direction-mobility-model.cc +++ b/src/node/random-direction-mobility-model.cc @@ -309,24 +309,22 @@ RandomDirectionMobilityModel::Update (void) const m_y = std::max (m_y, m_parameters->m_yMin); NS_ASSERT (CheckMobilityModel ()); } -void -RandomDirectionMobilityModel::DoGet (double &x, double &y, double &z) const +Position +RandomDirectionMobilityModel::DoGet (void) const { Update (); - x = m_x; - y = m_y; - z = 0; + return Position (m_x, m_y, 0.0); } void -RandomDirectionMobilityModel::DoSet (double x, double y, double z) +RandomDirectionMobilityModel::DoSet (const Position &position) { bool changed = false; - if (m_x != x || m_y != y) + if (m_x != position.x || m_y != position.y) { changed = true; } - m_x = x; - m_y = y; + m_x = position.x; + m_y = position.y; m_prevTime = Simulator::Now (); m_pauseStart = Simulator::Now (); Simulator::Remove (m_event); diff --git a/src/node/random-direction-mobility-model.h b/src/node/random-direction-mobility-model.h index ac4810601..1ab4d34ed 100644 --- a/src/node/random-direction-mobility-model.h +++ b/src/node/random-direction-mobility-model.h @@ -83,8 +83,8 @@ class RandomDirectionMobilityModel : public MobilityModel bool CheckMobilityModel (void) const; enum RandomDirectionMobilityModel::Side CalculateIntersection (double &x, double &y); virtual void DoDispose (void); - virtual void DoGet (double &x, double &y, double &z) const; - virtual void DoSet (double x, double y, double z); + virtual Position DoGet (void) const; + virtual void DoSet (const Position &position); static const double PI; Ptr m_parameters; diff --git a/src/node/random-walk-mobility-model.cc b/src/node/random-walk-mobility-model.cc index 12410f8d5..8e288a31c 100644 --- a/src/node/random-walk-mobility-model.cc +++ b/src/node/random-walk-mobility-model.cc @@ -166,24 +166,22 @@ RandomWalkMobilityModel::DoDispose (void) // chain up MobilityModel::DoDispose (); } -void -RandomWalkMobilityModel::DoGet (double &x, double &y, double &z) const +Position +RandomWalkMobilityModel::DoGet (void) const { Update (); - x = m_x; - y = m_y; - z = 0; + return Position (m_x, m_y, 0.0); } void -RandomWalkMobilityModel::DoSet (double x, double y, double z) +RandomWalkMobilityModel::DoSet (const Position &position) { bool changed = false; - if (m_x != x || m_y != y) + if (m_x != position.x || m_y != position.y) { changed = true; } - m_x = x; - m_y = y; + m_x = position.x; + m_y = position.y; m_prevTime = Simulator::Now (); if (changed) { diff --git a/src/node/random-walk-mobility-model.h b/src/node/random-walk-mobility-model.h index 72448e175..b2fef2953 100644 --- a/src/node/random-walk-mobility-model.h +++ b/src/node/random-walk-mobility-model.h @@ -109,8 +109,8 @@ class RandomWalkMobilityModel : public MobilityModel double x, double y); private: virtual void DoDispose (void); - virtual void DoGet (double &x, double &y, double &z) const; - virtual void DoSet (double x, double y, double z); + virtual Position DoGet (void) const; + virtual void DoSet (const Position &position); void Reset (void); void Update (void) const; diff --git a/src/node/static-mobility-model.cc b/src/node/static-mobility-model.cc index 9356ee30f..980692660 100644 --- a/src/node/static-mobility-model.cc +++ b/src/node/static-mobility-model.cc @@ -44,24 +44,22 @@ StaticMobilityModel::StaticMobilityModel (double x, double y, double z) StaticMobilityModel::~StaticMobilityModel () {} -void -StaticMobilityModel::DoGet (double &x, double &y, double &z) const +Position +StaticMobilityModel::DoGet (void) const { - x = m_x; - y = m_y; - z = m_z; + return Position (m_x, m_y, m_z); } void -StaticMobilityModel::DoSet (double x, double y, double z) +StaticMobilityModel::DoSet (const Position &position) { bool changed = false; - if (m_x != x || m_y != y || m_z != z) + if (m_x != position.x || m_y != position.y || m_z != position.z) { changed = true; } - m_x = x; - m_y = y; - m_z = z; + m_x = position.x; + m_y = position.y; + m_z = position.z; if (changed) { NotifyCourseChange (); diff --git a/src/node/static-mobility-model.h b/src/node/static-mobility-model.h index 215fd5872..d7600c1bd 100644 --- a/src/node/static-mobility-model.h +++ b/src/node/static-mobility-model.h @@ -60,8 +60,8 @@ public: virtual ~StaticMobilityModel (); private: - virtual void DoGet (double &x, double &y, double &z) const; - virtual void DoSet (double x, double y, double z); + virtual Position DoGet (void) const; + virtual void DoSet (const Position &position); double m_x; double m_y; double m_z; diff --git a/src/node/static-speed-mobility-model.cc b/src/node/static-speed-mobility-model.cc index 4684834de..217e0ac8d 100644 --- a/src/node/static-speed-mobility-model.cc +++ b/src/node/static-speed-mobility-model.cc @@ -107,26 +107,24 @@ StaticSpeedMobilityModel::Update (void) const m_z += m_dz * deltaS; } -void -StaticSpeedMobilityModel::DoGet (double &x, double &y, double &z) const +Position +StaticSpeedMobilityModel::DoGet (void) const { Update (); - x = m_x; - y = m_y; - z = m_z; + return Position (m_x, m_y, m_z); } void -StaticSpeedMobilityModel::DoSet (double x, double y, double z) +StaticSpeedMobilityModel::DoSet (const Position &position) { bool changed = false; Update (); - if (m_x != x || m_y != y || m_z != z) + if (m_x != position.x || m_y != position.y || m_z != position.z) { changed = true; } - m_x = x; - m_y = y; - m_z = z; + m_x = position.x; + m_y = position.y; + m_z = position.z; if (changed) { NotifyCourseChange (); diff --git a/src/node/static-speed-mobility-model.h b/src/node/static-speed-mobility-model.h index d1ff617fa..41a1e16bd 100644 --- a/src/node/static-speed-mobility-model.h +++ b/src/node/static-speed-mobility-model.h @@ -83,8 +83,8 @@ public: */ void SetSpeed (double dx, double dy, double dz); private: - virtual void DoGet (double &x, double &y, double &z) const; - virtual void DoSet (double x, double y, double z); + virtual Position DoGet (void) const; + virtual void DoSet (const Position &position); void Update (void) const; mutable double m_x; mutable double m_y; From b9c2580f22b3709cf097d54b89f9368efd6cfbed Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 16 Jul 2007 15:27:56 +0200 Subject: [PATCH 044/148] a hierarchical mobility model --- SConstruct | 2 + src/node/hierarchical-mobility-model.cc | 95 +++++++++++++++++++++++++ src/node/hierarchical-mobility-model.h | 52 ++++++++++++++ 3 files changed, 149 insertions(+) create mode 100644 src/node/hierarchical-mobility-model.cc create mode 100644 src/node/hierarchical-mobility-model.h diff --git a/SConstruct b/SConstruct index b89015f4c..db7883eff 100644 --- a/SConstruct +++ b/SConstruct @@ -256,6 +256,7 @@ node.add_sources ([ 'random-rectangle-topology.cc', 'random-walk-mobility-model.cc', 'random-direction-mobility-model.cc', + 'hierarchical-mobility-model.cc', ]) node.add_inst_headers ([ 'node.h', @@ -281,6 +282,7 @@ node.add_inst_headers ([ 'random-rectangle-topology.h', 'random-walk-mobility-model.h', 'random-direction-mobility-model.h', + 'hierarchical-mobility-model.h', ]) applications = build.Ns3Module ('applications', 'src/applications') diff --git a/src/node/hierarchical-mobility-model.cc b/src/node/hierarchical-mobility-model.cc new file mode 100644 index 000000000..2c2e84f21 --- /dev/null +++ b/src/node/hierarchical-mobility-model.cc @@ -0,0 +1,95 @@ +/* -*- 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 "hierarchical-mobility-model.h" +#include "mobility-model-notifier.h" + +namespace ns3 { + +HierarchicalMobilityModel::HierarchicalMobilityModel (Ptr child, Ptr parent) + : m_child (child), + m_parent (parent) +{ + Ptr childNotifier = + m_child->QueryInterface (MobilityModelNotifier::iid); + Ptr parentNotifier = + m_parent->QueryInterface (MobilityModelNotifier::iid); + if (childNotifier == 0) + { + childNotifier = Create (); + child->AddInterface (childNotifier); + } + if (parentNotifier == 0) + { + parentNotifier = Create (); + parent->AddInterface (parentNotifier); + } + childNotifier->RegisterListener (MakeCallback (&HierarchicalMobilityModel::ChildChanged, this)); + parentNotifier->RegisterListener (MakeCallback (&HierarchicalMobilityModel::ParentChanged, this)); +} + +Ptr +HierarchicalMobilityModel::GetChild (void) const +{ + return m_child; +} + +Ptr +HierarchicalMobilityModel::GetParent (void) const +{ + return m_parent; +} + +Position +HierarchicalMobilityModel::DoGet (void) const +{ + Position parentPosition = m_parent->Get (); + Position childPosition = m_child->Get (); + return Position (parentPosition.x + childPosition.x, + parentPosition.y + childPosition.y, + parentPosition.z + childPosition.z); +} +void +HierarchicalMobilityModel::DoSet (const Position &position) +{ + // This implementation of DoSet is really an arbitraty choice. + // anything else would have been ok. + Position parentPosition = m_parent->Get (); + Position childPosition (position.x - parentPosition.x, + position.y - parentPosition.y, + position.z - parentPosition.z); + m_child->Set (childPosition); +} + +void +HierarchicalMobilityModel::ParentChanged (Ptr model) +{ + MobilityModel::NotifyCourseChange (); +} + +void +HierarchicalMobilityModel::ChildChanged (Ptr model) +{ + MobilityModel::NotifyCourseChange (); +} + + + +} // namespace ns3 diff --git a/src/node/hierarchical-mobility-model.h b/src/node/hierarchical-mobility-model.h new file mode 100644 index 000000000..a1bb61713 --- /dev/null +++ b/src/node/hierarchical-mobility-model.h @@ -0,0 +1,52 @@ +/* -*- 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 HIERARCHICAL_MOBILITY_MODEL_H +#define HIERARCHICAL_MOBILITY_MODEL_H + +#include "mobility-model.h" + +namespace ns3 { + +class HierarchicalMobilityModel : public MobilityModel +{ +public: + static const InterfaceId iid; + + HierarchicalMobilityModel (Ptr child, Ptr parent); + + Ptr GetChild (void) const; + Ptr GetParent (void) const; + +private: + virtual Position DoGet (void) const; + virtual void DoSet (const Position &position); + + void ParentChanged (Ptr model); + void ChildChanged (Ptr model); + + Ptr m_child; + Ptr m_parent; +}; + + +} // namespace ns3 + +#endif /* HIERARCHICAL_MOBILITY_MODEL_H */ From 24458b4bf11937b461c103df2cec3b09f649a04a Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Tue, 17 Jul 2007 10:47:25 +0200 Subject: [PATCH 045/148] 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; +} From 2f011548474db0744ec29ceb81848a855d9d3eed Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Tue, 17 Jul 2007 10:54:50 +0200 Subject: [PATCH 046/148] fix small typo --- src/node/ns2-mobility-file-topology.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node/ns2-mobility-file-topology.cc b/src/node/ns2-mobility-file-topology.cc index cc0b135eb..ef7af20ff 100644 --- a/src/node/ns2-mobility-file-topology.cc +++ b/src/node/ns2-mobility-file-topology.cc @@ -20,7 +20,7 @@ */ #include "ns3/debug.h" #include "ns3/simulator.h" -#include "ns2-mobile-file-topology.h" +#include "ns2-mobility-file-topology.h" #include "node-list.h" #include "node.h" #include "static-speed-mobility-model.h" From 8b07f89d1314c779c4b8bb90b378ac94a34eef3c Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Tue, 17 Jul 2007 10:58:13 +0200 Subject: [PATCH 047/148] remove extraneous setters/getters --- src/node/mobility-model.cc | 48 -------------------------- src/node/mobility-model.h | 24 ------------- src/node/ns2-mobility-file-topology.cc | 12 +++++-- 3 files changed, 9 insertions(+), 75 deletions(-) diff --git a/src/node/mobility-model.cc b/src/node/mobility-model.cc index 574a94e0b..c62733f8f 100644 --- a/src/node/mobility-model.cc +++ b/src/node/mobility-model.cc @@ -49,24 +49,6 @@ MobilityModel::Get (void) const { return DoGet (); } -double -MobilityModel::GetX (void) const -{ - Position position = DoGet (); - return position.x; -} -double -MobilityModel::GetY (void) const -{ - Position position = DoGet (); - return position.y; -} -double -MobilityModel::GetZ (void) const -{ - Position position = DoGet (); - return position.z; -} void MobilityModel::Set (double x, double y, double z) @@ -79,36 +61,6 @@ MobilityModel::Set (const Position &position) { DoSet (position); } -void -MobilityModel::SetXY (double x, double y) -{ - Position position = DoGet (); - position.x = x; - position.y = y; - DoSet (position); - } -void -MobilityModel::SetX (double x) -{ - Position position = DoGet (); - position.x = x; - DoSet (position); -} -void -MobilityModel::SetY (double y) -{ - Position position = DoGet (); - position.y = y; - DoSet (position); -} -void -MobilityModel::SetZ (double z) -{ - Position position = DoGet (); - position.z = z; - DoSet (position); -} - double MobilityModel::GetDistanceFrom (const MobilityModel &other) const diff --git a/src/node/mobility-model.h b/src/node/mobility-model.h index 971c14b5b..92cf6a3e7 100644 --- a/src/node/mobility-model.h +++ b/src/node/mobility-model.h @@ -70,33 +70,9 @@ public: * Unit is meters */ Position Get (void) const; - /** - * \returns the current x coordinate - * - * Unit is meters - */ - double GetX (void) const; - /** - * \returns the current y coordinate - * - * Unit is meters - */ - double GetY (void) const; - /** - * \returns the current z coordinate - * - * Unit is meters - */ - double GetZ (void) const; - - void Add (double dx, double dy, double dz); void Set (double x, double y, double z); void Set (const Position &position); - void SetXY (double x, double y); - void SetX (double x); - void SetY (double y); - void SetZ (double z); /** * \param position a reference to another position object instance * \returns the distance between the two objects. diff --git a/src/node/ns2-mobility-file-topology.cc b/src/node/ns2-mobility-file-topology.cc index ef7af20ff..2c58bba30 100644 --- a/src/node/ns2-mobility-file-topology.cc +++ b/src/node/ns2-mobility-file-topology.cc @@ -97,21 +97,27 @@ Ns2MobileFileTopology::LayoutObjectStore (const ObjectStore &store) const { double value = ReadDouble (line.substr (endNodeId + 9, std::string::npos)); std::string coordinate = line.substr (endNodeId + 6, 1); + Position position = model->Get (); if (coordinate == "X") { - model->SetX (value); + position.x = value; NS_DEBUG ("X=" << value); } else if (coordinate == "Y") { - model->SetY (value); + position.y = value; NS_DEBUG ("Y=" << value); } else if (coordinate == "Z") { - model->SetZ (value); + position.z = value; NS_DEBUG ("Z=" << value); } + else + { + continue; + } + model->Set (position); } else { From 204352ca8bca10adcf65e38223ba173f7811df46 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Tue, 17 Jul 2007 11:12:24 +0200 Subject: [PATCH 048/148] fix small typo --- utils/mobility-generator.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/mobility-generator.cc b/utils/mobility-generator.cc index 2eb4a9a2d..dc6815ed9 100644 --- a/utils/mobility-generator.cc +++ b/utils/mobility-generator.cc @@ -19,7 +19,7 @@ * Author: Mathieu Lacage */ -#include "ns3/ns2-mobile-file-topology.h" +#include "ns3/ns2-mobility-file-topology.h" #include "ns3/object.h" #include "ns3/simulator.h" #include "ns3/mobility-model-notifier.h" From ba0f81e65c7cb86b0aec585f3e280983f452fb40 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Tue, 17 Jul 2007 11:13:34 +0200 Subject: [PATCH 049/148] remove unused class declarations --- src/node/mobility-model.cc | 11 ++++------- src/node/mobility-model.h | 21 ++------------------- 2 files changed, 6 insertions(+), 26 deletions(-) diff --git a/src/node/mobility-model.cc b/src/node/mobility-model.cc index c62733f8f..91776aa07 100644 --- a/src/node/mobility-model.cc +++ b/src/node/mobility-model.cc @@ -26,14 +26,11 @@ namespace ns3 { const InterfaceId MobilityModel::iid = MakeInterfaceId ("MobilityModel", Object::iid); -Vector3D::Vector3D (double x, double y, double z) - : x (x), - y (y), - z (z) -{} -Position::Position (double x, double y, double z) - : Vector3D (x, y, z) +Position::Position (double _x, double _y, double _z) + : x (_x), + y (_y), + z (_z) {} MobilityModel::MobilityModel () diff --git a/src/node/mobility-model.h b/src/node/mobility-model.h index 92cf6a3e7..7dd300aff 100644 --- a/src/node/mobility-model.h +++ b/src/node/mobility-model.h @@ -25,31 +25,14 @@ namespace ns3 { -class Vector3D +class Position { public: - Vector3D (double x, double y, double z); + Position (double x, double y, double z); double x; double y; double z; }; -class Position : public Vector3D -{ -public: - Position (double x, double y, double z); -}; -class Speed : public Vector3D -{}; -class Vector2D -{ -public: - double x; - double y; -}; -class Position2D : public Vector2D -{}; -class Speed2D : public Vector2D -{}; /** * \brief keep track of the current position of an object From 683b301fb7658fb83724ab68b5e9a5a0cacf6db9 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Tue, 17 Jul 2007 14:32:19 +0200 Subject: [PATCH 050/148] split useful code out of random direction mobility model --- SConstruct | 2 + src/node/mobility-model-helper.cc | 185 +++++++++++++++++ src/node/mobility-model-helper.h | 80 ++++++++ src/node/random-direction-mobility-model.cc | 216 +++++--------------- src/node/random-direction-mobility-model.h | 14 +- src/node/random-mobility-model.cc | 195 ------------------ src/node/random-mobility-model.h | 131 ------------ 7 files changed, 322 insertions(+), 501 deletions(-) create mode 100644 src/node/mobility-model-helper.cc create mode 100644 src/node/mobility-model-helper.h delete mode 100644 src/node/random-mobility-model.cc delete mode 100644 src/node/random-mobility-model.h diff --git a/SConstruct b/SConstruct index 5cb86153a..c4aa0c7b1 100644 --- a/SConstruct +++ b/SConstruct @@ -258,6 +258,7 @@ node.add_sources ([ 'random-direction-mobility-model.cc', 'hierarchical-mobility-model.cc', 'ns2-mobility-file-topology.cc', + 'mobility-model-helper.cc', ]) node.add_inst_headers ([ 'node.h', @@ -285,6 +286,7 @@ node.add_inst_headers ([ 'random-direction-mobility-model.h', 'hierarchical-mobility-model.h', 'ns2-mobility-file-topology.h', + 'mobility-model-helper.h', ]) applications = build.Ns3Module ('applications', 'src/applications') diff --git a/src/node/mobility-model-helper.cc b/src/node/mobility-model-helper.cc new file mode 100644 index 000000000..aa41f758e --- /dev/null +++ b/src/node/mobility-model-helper.cc @@ -0,0 +1,185 @@ +#include +#include "ns3/simulator.h" +#include "mobility-model-helper.h" + +namespace ns3 { + +Position2D::Position2D (double _x, double _y) + : x (_x), + y (_y) +{} +Position2D::Position2D () + : x (0.0), + y (0.0) +{} + +Speed2D::Speed2D (double _dx, double _dy) + : dx (_dx), + dy (_dy) +{} + +Speed2D::Speed2D () + : dx (0.0), + dy (0.0) +{} + +MobilityModelHelper::MobilityModelHelper () +{} +void +MobilityModelHelper::InitializePosition (const struct Position2D &position) +{ + m_position = position; + m_speed.dx = 0.0; + m_speed.dy = 0.0; + m_lastUpdate = Simulator::Now (); + m_pauseEnd = Simulator::Now (); +} +void +MobilityModelHelper::Update (const struct AreaBounds &bounds) const +{ + Time now = Simulator::Now (); + if (m_pauseEnd > now) + { + return; + } + Time last = std::max (now, m_pauseEnd); + if (m_lastUpdate >= last) + { + return; + } + Time deltaTime = now - last; + m_lastUpdate = now; + double deltaS = deltaTime.GetSeconds (); + m_position.x += m_speed.dx * deltaS; + m_position.y += m_speed.dy * deltaS; + m_position.x = std::min (m_position.x, bounds.xMax); + m_position.y = std::min (m_position.y, bounds.yMax); + m_position.x = std::max (m_position.x, bounds.xMin); + m_position.y = std::max (m_position.y, bounds.yMin); +} +Position2D +MobilityModelHelper::CalculateIntersection (const struct AreaBounds &bounds) const +{ + double xMaxY = m_position.y + (bounds.xMax - m_position.x) / m_speed.dx * m_speed.dy; + double xMinY = m_position.y + (bounds.xMin - m_position.x) / m_speed.dx * m_speed.dy; + double yMaxX = m_position.x + (bounds.yMax - m_position.y) / m_speed.dy * m_speed.dx; + double yMinX = m_position.x + (bounds.yMin - m_position.y) / m_speed.dy * m_speed.dx; + bool xMaxOk = xMaxY <= bounds.yMax && xMaxY >= bounds.yMin; + bool xMinOk = xMinY <= bounds.yMax && xMinY >= bounds.yMin; + bool yMaxOk = yMaxX <= bounds.xMax && yMaxX >= bounds.xMin; + bool yMinOk = yMinX <= bounds.xMax && yMinX >= bounds.xMin; + if (xMaxOk && m_speed.dx >= 0) + { + return Position2D (bounds.xMax, xMaxY); + } + else if (xMinOk && m_speed.dx <= 0) + { + return Position2D (bounds.xMin, xMinY); + } + else if (yMaxOk && m_speed.dy >= 0) + { + return Position2D (yMaxX, bounds.yMax); + } + else if (yMinOk && m_speed.dy <= 0) + { + return Position2D (yMinX, bounds.yMin); + } + else + { + NS_ASSERT (false); + // quiet compiler + return Position2D (0.0, 0.0); + } +} +double +MobilityModelHelper::Distance (const Position2D &a, const Position2D &b) const +{ + double dx = a.x - b.x; + double dy = a.y - b.y; + return sqrt (dx * dx + dy * dy); +} + +Time +MobilityModelHelper::Reset (const struct AreaBounds &bounds, + double direction, + double speed, + const Time &maxMovementDelay, + const Time &pauseDelay) +{ + Update (bounds); + m_pauseEnd = Simulator::Now () + pauseDelay; + m_speed.dx = std::cos (direction) * speed; + m_speed.dy = std::sin (direction) * speed; + Position2D intersection = CalculateIntersection (bounds); + Time seconds = Seconds (Distance (intersection, m_position) / speed); + seconds = std::min (seconds, maxMovementDelay); + return seconds; +} +Time +MobilityModelHelper::Reset (const struct AreaBounds &bounds, + double direction, + double speed, + double maxDistance, + const Time &pauseDelay) +{ + Update (bounds); + m_pauseEnd = Simulator::Now () + pauseDelay; + m_speed.dx = std::cos (direction) * speed; + m_speed.dy = std::sin (direction) * speed; + Position2D intersection = CalculateIntersection (bounds); + double distance = Distance (intersection, m_position); + distance = std::min (distance, maxDistance); + double seconds = distance / speed; + return Seconds (seconds); +} +Time +MobilityModelHelper::Reset (const struct AreaBounds &bounds, + double direction, + double speed, + const Time &pauseDelay) +{ + Update (bounds); + m_pauseEnd = Simulator::Now () + pauseDelay; + m_speed.dx = std::cos (direction) * speed; + m_speed.dy = std::sin (direction) * speed; + Position2D intersection = CalculateIntersection (bounds); + double seconds = Distance (intersection, m_position) / speed; + return Seconds (seconds); +} + +Position2D +MobilityModelHelper::GetCurrentPosition (const struct AreaBounds &bounds) const +{ + Update (bounds); + return m_position; +} + +Speed2D +MobilityModelHelper::GetSpeed (void) const +{ + return m_speed; +} + +enum MobilityModelHelper::Side +MobilityModelHelper::GetSide (const struct AreaBounds &bounds) const +{ + if (m_position.x == bounds.xMin) + { + return MobilityModelHelper::LEFT; + } + if (m_position.x == bounds.xMax) + { + return MobilityModelHelper::RIGHT; + } + if (m_position.y == bounds.yMin) + { + return MobilityModelHelper::BOTTOM; + } + if (m_position.y == bounds.yMax) + { + return MobilityModelHelper::TOP; + } + return MobilityModelHelper::NONE; +} + +} // namespace ns3 diff --git a/src/node/mobility-model-helper.h b/src/node/mobility-model-helper.h new file mode 100644 index 000000000..c92514f5b --- /dev/null +++ b/src/node/mobility-model-helper.h @@ -0,0 +1,80 @@ +#ifndef MOBILITY_MODEL_HELPER_H +#define MOBILITY_MODEL_HELPER_H + +#include "ns3/nstime.h" + +namespace ns3 { + +class Position2D +{ +public: + Position2D (double _x, double _y); + Position2D (); + double x; + double y; +}; + +struct AreaBounds +{ + double xMin; + double xMax; + double yMin; + double yMax; +}; + +class Speed2D +{ +public: + Speed2D (double _dx, double _dy); + Speed2D (); + double dx; + double dy; +}; + +class MobilityModelHelper +{ + public: + enum Side { + LEFT, + RIGHT, + TOP, + BOTTOM, + NONE + }; + MobilityModelHelper (); + void InitializePosition (const struct Position2D &position); + Time Reset (const struct AreaBounds &bounds, + double direction, + double speed, + const Time &maxMovementDelay, + const Time &pauseDelay); + Time Reset (const struct AreaBounds &bounds, + double direction, + double speed, + double maxDistance, + const Time &pauseDelay); + // move in specified direction until + // we hit the area bounds. + // return delay until we reach the bounds. + Time Reset (const struct AreaBounds &bounds, + double direction, + double speed, + const Time &pauseDelay); + + Position2D GetCurrentPosition (const struct AreaBounds &bounds) const; + Speed2D GetSpeed (void) const; + enum MobilityModelHelper::Side GetSide (const struct AreaBounds &bounds) const; + + private: + double Distance (const Position2D &a, const Position2D &b) const; + void Update (const struct AreaBounds &bounds) const; + Position2D CalculateIntersection (const struct AreaBounds &bounds) const; + mutable Position2D m_position; + Speed2D m_speed; + mutable Time m_lastUpdate; + Time m_pauseEnd; +}; + +} // namespace ns3 + +#endif /* MOBILITY_MODEL_HELPER_H */ diff --git a/src/node/random-direction-mobility-model.cc b/src/node/random-direction-mobility-model.cc index f1b6c4c13..a2f59fbca 100644 --- a/src/node/random-direction-mobility-model.cc +++ b/src/node/random-direction-mobility-model.cc @@ -52,31 +52,34 @@ static RectangleDefaultValue RandomDirectionParameters::RandomDirectionParameters () - : m_xMin (g_rectangle.GetMinX ()), - m_xMax (g_rectangle.GetMaxX ()), - m_yMin (g_rectangle.GetMinY ()), - m_yMax (g_rectangle.GetMaxY ()), - m_speedVariable (g_speedVariable.GetCopy ()), + : m_speedVariable (g_speedVariable.GetCopy ()), m_pauseVariable (g_pauseVariable.GetCopy ()) -{} +{ + m_bounds.xMin = g_rectangle.GetMinX (); + m_bounds.xMax = g_rectangle.GetMaxX (); + m_bounds.yMin = g_rectangle.GetMinY (); + m_bounds.yMax = g_rectangle.GetMaxY (); +} RandomDirectionParameters::RandomDirectionParameters (double xMin, double xMax, double yMin, double yMax) - : m_xMin (xMin), - m_xMax (xMax), - m_yMin (yMin), - m_yMax (yMax), - m_speedVariable (g_speedVariable.GetCopy ()), + : m_speedVariable (g_speedVariable.GetCopy ()), m_pauseVariable (g_pauseVariable.GetCopy ()) -{} +{ + m_bounds.xMin = xMin; + m_bounds.xMax = xMax; + m_bounds.yMin = yMin; + m_bounds.yMax = yMax; +} RandomDirectionParameters::RandomDirectionParameters (double xMin, double xMax, double yMin, double yMax, const RandomVariable &speedVariable, const RandomVariable &pauseVariable) - : m_xMin (xMin), - m_xMax (xMax), - m_yMin (yMin), - m_yMax (yMax), - m_speedVariable (speedVariable.Copy ()), + : m_speedVariable (speedVariable.Copy ()), m_pauseVariable (pauseVariable.Copy ()) -{} +{ + m_bounds.xMin = xMin; + m_bounds.xMax = xMax; + m_bounds.yMin = yMin; + m_bounds.yMax = yMax; +} RandomDirectionParameters::~RandomDirectionParameters () { @@ -101,20 +104,20 @@ RandomDirectionParameters::SetPause (const RandomVariable &pauseVariable) void RandomDirectionParameters::SetBounds (double xMin, double xMax, double yMin, double yMax) { - m_xMin = xMin; - m_yMin = yMin; - m_xMax = xMax; - m_yMax = yMax; + m_bounds.xMin = xMin; + m_bounds.yMin = yMin; + m_bounds.xMax = xMax; + m_bounds.yMax = yMax; } Ptr RandomDirectionMobilityModel::GetDefaultParameters (void) { static Ptr parameters = Create (); - if (parameters->m_xMin != g_rectangle.GetMinX () || - parameters->m_yMin != g_rectangle.GetMinY () || - parameters->m_xMax != g_rectangle.GetMaxX () || - parameters->m_yMax != g_rectangle.GetMaxY () || + if (parameters->m_bounds.xMin != g_rectangle.GetMinX () || + parameters->m_bounds.yMin != g_rectangle.GetMinY () || + parameters->m_bounds.xMax != g_rectangle.GetMaxX () || + parameters->m_bounds.yMax != g_rectangle.GetMaxY () || g_speedVariable.IsDirty () || g_pauseVariable.IsDirty ()) { @@ -127,66 +130,29 @@ RandomDirectionMobilityModel::GetDefaultParameters (void) RandomDirectionMobilityModel::RandomDirectionMobilityModel () - : m_parameters (GetDefaultParameters ()), - m_x (0.0), - m_y (0.0), - m_dx (0.0), - m_dy (0.0), - m_prevTime (Simulator::Now ()), - m_pauseStart (Simulator::Now ()) + : m_parameters (GetDefaultParameters ()) { SetInterfaceId (RandomDirectionMobilityModel::iid); InitializeDirectionAndSpeed (); } -bool -RandomDirectionMobilityModel::CheckMobilityModel (void) const -{ - return - m_x <= m_parameters->m_xMax && - m_x >= m_parameters->m_xMin && - m_y <= m_parameters->m_yMax && - m_y >= m_parameters->m_yMin; -} - RandomDirectionMobilityModel::RandomDirectionMobilityModel (double x, double y) - : m_parameters (GetDefaultParameters ()), - m_x (x), - m_y (y), - m_dx (0.0), - m_dy (0.0), - m_prevTime (Simulator::Now ()), - m_pauseStart (Simulator::Now ()) + : m_parameters (GetDefaultParameters ()) { SetInterfaceId (RandomDirectionMobilityModel::iid); - NS_ASSERT (CheckMobilityModel ()); InitializeDirectionAndSpeed (); } RandomDirectionMobilityModel::RandomDirectionMobilityModel (Ptr parameters) - : m_parameters (parameters), - m_x (0.0), - m_y (0.0), - m_dx (0.0), - m_dy (0.0), - m_prevTime (Simulator::Now ()), - m_pauseStart (Simulator::Now ()) + : m_parameters (parameters) { SetInterfaceId (RandomDirectionMobilityModel::iid); InitializeDirectionAndSpeed (); - NS_ASSERT (CheckMobilityModel ()); } RandomDirectionMobilityModel::RandomDirectionMobilityModel (Ptr parameters, - double x, double y) - : m_parameters (parameters), - m_x (x), - m_y (y), - m_dx (0.0), - m_dy (0.0), - m_prevTime (Simulator::Now ()), - m_pauseStart (Simulator::Now ()) + double x, double y) + : m_parameters (parameters) { SetInterfaceId (RandomDirectionMobilityModel::iid); InitializeDirectionAndSpeed (); - NS_ASSERT (CheckMobilityModel ()); } void RandomDirectionMobilityModel::DoDispose (void) @@ -195,52 +161,6 @@ RandomDirectionMobilityModel::DoDispose (void) // chain up. MobilityModel::DoDispose (); } -enum RandomDirectionMobilityModel::Side -RandomDirectionMobilityModel::CalculateIntersection (double &x, double &y) -{ - double xMin = m_parameters->m_xMin; - double xMax = m_parameters->m_xMax; - double yMin = m_parameters->m_yMin; - double yMax = m_parameters->m_yMax; - double xMaxY = m_y + (xMax - m_x) / m_dx * m_dy; - double xMinY = m_y + (xMin - m_x) / m_dx * m_dy; - double yMaxX = m_x + (yMax - m_y) / m_dy * m_dx; - double yMinX = m_x + (yMin - m_y) / m_dy * m_dx; - bool xMaxOk = xMaxY <= yMax && xMaxY >= yMin; - bool xMinOk = xMinY <= yMax && xMinY >= yMin; - bool yMaxOk = yMaxX <= xMax && yMaxX >= xMin; - bool yMinOk = yMinX <= xMax && yMinX >= xMin; - if (xMaxOk && m_dx >= 0) - { - x = xMax; - y = xMaxY; - return RandomDirectionMobilityModel::RIGHT; - } - else if (xMinOk && m_dx <= 0) - { - x = xMin; - y = xMinY; - return RandomDirectionMobilityModel::LEFT; - } - else if (yMaxOk && m_dy >= 0) - { - x = yMaxX; - y = yMax; - return RandomDirectionMobilityModel::TOP; - } - else if (yMinOk && m_dy <= 0) - { - x = yMinX; - y = yMin; - return RandomDirectionMobilityModel::BOTTOM; - } - else - { - NS_ASSERT (false); - // quiet compiler - return RandomDirectionMobilityModel::RIGHT; - } -} void RandomDirectionMobilityModel::InitializeDirectionAndSpeed (void) { @@ -251,82 +171,50 @@ void RandomDirectionMobilityModel::SetDirectionAndSpeed (double direction) { double speed = m_parameters->m_speedVariable->GetValue (); - m_dx = std::cos (direction) * speed; - m_dy = std::sin (direction) * speed; - double x, y; - m_side = CalculateIntersection (x, y); - double deltaX = x - m_x; - double deltaY = y - m_y; - double distance = sqrt (deltaX * deltaX + deltaY * deltaY); - double seconds = distance / speed; - double pause = m_parameters->m_pauseVariable->GetValue (); - m_pauseStart = Simulator::Now () + Seconds (seconds); - m_prevTime = Simulator::Now (); - m_event = Simulator::Schedule (Seconds (seconds + pause), + Time pause = Seconds (m_parameters->m_pauseVariable->GetValue ()); + Time delay = m_helper.Reset (m_parameters->m_bounds, + direction, speed, + pause); + m_event = Simulator::Schedule (delay + pause, &RandomDirectionMobilityModel::ResetDirectionAndSpeed, this); } void RandomDirectionMobilityModel::ResetDirectionAndSpeed (void) { - Update (); double direction = UniformVariable::GetSingleValue (0, PI); - switch (m_side) + + switch (m_helper.GetSide (m_parameters->m_bounds)) { - case RandomDirectionMobilityModel::RIGHT: + case MobilityModelHelper::RIGHT: direction += PI / 2; break; - case RandomDirectionMobilityModel::LEFT: + case MobilityModelHelper::LEFT: direction += - PI / 2; break; - case RandomDirectionMobilityModel::TOP: + case MobilityModelHelper::TOP: direction += PI; break; - case RandomDirectionMobilityModel::BOTTOM: + case MobilityModelHelper::BOTTOM: direction += 0.0; break; + case MobilityModelHelper::NONE: + NS_ASSERT (false); + break; } SetDirectionAndSpeed (direction); NotifyCourseChange (); } -void -RandomDirectionMobilityModel::Update (void) const -{ - Time end = std::min (Simulator::Now (), m_pauseStart); - if (m_prevTime >= end) - { - return; - } - Time deltaTime = end - m_prevTime; - m_prevTime = Simulator::Now (); - double deltaS = deltaTime.GetSeconds (); - NS_ASSERT (CheckMobilityModel ()); - m_x += m_dx * deltaS; - m_y += m_dy * deltaS; - // round to closest boundaries. - m_x = std::min (m_x, m_parameters->m_xMax); - m_x = std::max (m_x, m_parameters->m_xMin); - m_y = std::min (m_y, m_parameters->m_yMax); - m_y = std::max (m_y, m_parameters->m_yMin); - NS_ASSERT (CheckMobilityModel ()); -} Position RandomDirectionMobilityModel::DoGet (void) const { - Update (); - return Position (m_x, m_y, 0.0); + Position2D position = m_helper.GetCurrentPosition (m_parameters->m_bounds); + return Position (position.x, position.y, 0.0); } void RandomDirectionMobilityModel::DoSet (const Position &position) { - bool changed = false; - if (m_x != position.x || m_y != position.y) - { - changed = true; - } - m_x = position.x; - m_y = position.y; - m_prevTime = Simulator::Now (); - m_pauseStart = Simulator::Now (); + Position2D pos (position.x, position.y); + m_helper.InitializePosition (pos); Simulator::Remove (m_event); InitializeDirectionAndSpeed (); NotifyCourseChange (); diff --git a/src/node/random-direction-mobility-model.h b/src/node/random-direction-mobility-model.h index 1ab4d34ed..1227cc1d5 100644 --- a/src/node/random-direction-mobility-model.h +++ b/src/node/random-direction-mobility-model.h @@ -27,6 +27,7 @@ #include "ns3/event-id.h" #include "ns3/component-manager.h" #include "mobility-model.h" +#include "mobility-model-helper.h" namespace ns3 { @@ -47,10 +48,7 @@ class RandomDirectionParameters : public Object void SetBounds (double xMin, double xMax, double yMin, double yMax); private: friend class RandomDirectionMobilityModel; - double m_xMin; - double m_xMax; - double m_yMin; - double m_yMax; + struct AreaBounds m_bounds; RandomVariable *m_speedVariable; RandomVariable *m_pauseVariable; std::string m_speedVariableValue; @@ -88,14 +86,8 @@ class RandomDirectionMobilityModel : public MobilityModel static const double PI; Ptr m_parameters; - mutable double m_x; - mutable double m_y; - double m_dx; - double m_dy; - mutable Time m_prevTime; - Time m_pauseStart; EventId m_event; - enum Side m_side; + MobilityModelHelper m_helper; }; } // namespace ns3 diff --git a/src/node/random-mobility-model.cc b/src/node/random-mobility-model.cc deleted file mode 100644 index 12410f8d5..000000000 --- a/src/node/random-mobility-model.cc +++ /dev/null @@ -1,195 +0,0 @@ -/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ -/* - * Copyright (c) 2006,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 "random-walk-mobility-model.h" -#include "ns3/default-value.h" -#include "ns3/time-default-value.h" -#include "ns3/simulator.h" -#include "ns3/debug.h" -#include - -NS_DEBUG_COMPONENT_DEFINE ("RandomWalk"); - -namespace ns3 { - -const InterfaceId RandomWalkMobilityModel::iid = - MakeInterfaceId ("RandomWalkMobilityModel", MobilityModel::iid); -const ClassId RandomWalkMobilityModel::cid = - MakeClassId ("RandomWalkMobilityModel", RandomWalkMobilityModel::iid); - - -static IntegerDefaultValue g_minSpeed ("RandomWalkMinSpeed", - "Minimum speed used during a random walk", - 0.1); -static IntegerDefaultValue g_maxSpeed ("RandomWalkMaxSpeed", - "Maximum speed used during a random walk", - 0.5); -static EnumDefaultValue g_mode - ("RandomWalkMode", - "The mode indicates the condition used to " - "change the current speed and direction", - RandomWalkMobilityModelParameters::MODE_DISTANCE, "Distance", - RandomWalkMobilityModelParameters::MODE_TIME, "Time", - 0, 0); -static IntegerDefaultValue g_modeDistance ("RandomWalkModeDistance", - "Distance to walk before changing direction and speed.", - 10); -static TimeDefaultValue g_modeTime ("RandomWalkModeTime", - "Time to walk before changing direction and speed.", - Seconds (1)); - -RandomWalkMobilityModelParameters::RandomWalkMobilityModelParameters () - : m_minSpeed (g_minSpeed.GetValue ()), - m_maxSpeed (g_maxSpeed.GetValue ()), - m_mode (g_mode.GetValue ()), - m_modeDistance (g_modeDistance.GetValue ()), - m_modeTime (g_modeTime.GetValue ()) -{} -bool -RandomWalkMobilityModelParameters::IsDefault (void) const -{ - if (m_minSpeed != g_minSpeed.GetValue () || - m_maxSpeed != g_maxSpeed.GetValue () || - m_mode != g_mode.GetValue () || - m_modeDistance != g_modeDistance.GetValue () || - m_modeTime != g_modeTime.GetValue ()) - { - return false; - } - return true; -} - -void -RandomWalkMobilityModelParameters::SetSpeedBounds (double minSpeed, double maxSpeed) -{ - m_minSpeed = minSpeed; - m_maxSpeed = maxSpeed; -} - - -UniformVariable RandomWalkMobilityModel::m_randomDirection (0.0, 2*3.141592); - -Ptr -RandomWalkMobilityModel::GetDefaultParameters (void) -{ - static Ptr parameters = Create (); - if (!parameters->IsDefault ()) - { - parameters = Create (); - } - return parameters; -} - -RandomWalkMobilityModel::RandomWalkMobilityModel () - : m_x (0.0), - m_y (0.0), - m_dx (0.0), - m_dy (0.0), - m_prevTime (Simulator::Now ()), - m_parameters (RandomWalkMobilityModel::GetDefaultParameters ()) -{ - SetInterfaceId (RandomWalkMobilityModel::iid); - Reset (); -} - -RandomWalkMobilityModel::RandomWalkMobilityModel (double x, double y) - : m_x (x), - m_y (y), - m_dx (0.0), - m_dy (0.0), - m_prevTime (Simulator::Now ()), - m_parameters (RandomWalkMobilityModel::GetDefaultParameters ()) -{ - SetInterfaceId (RandomWalkMobilityModel::iid); - Reset (); -} - -void -RandomWalkMobilityModel::Reset (void) -{ - Update (); - double speed = UniformVariable::GetSingleValue (m_parameters->m_minSpeed, - m_parameters->m_maxSpeed); - NS_DEBUG ("min="<< m_parameters->m_minSpeed << ", max=" << m_parameters->m_maxSpeed << - ", speed=" << speed); - double direction = m_randomDirection.GetValue (); - double dx = std::cos (direction) * speed; - double dy = std::sin (direction) * speed; - m_dx = dx; - m_dy = dy; - Time delay; - if (m_parameters->m_mode == RandomWalkMobilityModelParameters::MODE_TIME) - { - delay = m_parameters->m_modeTime; - } - else - { - double distance = m_parameters->m_modeDistance; - delay = Seconds (distance / sqrt (m_dx * m_dx + m_dy * m_dy)); - } - NotifyCourseChange (); - NS_DEBUG ("change speed at " << Simulator::Now () << " in " << delay); - Simulator::Schedule (delay, &RandomWalkMobilityModel::Reset, this); -} - -void -RandomWalkMobilityModel::Update (void) const -{ - Time deltaTime = Simulator::Now () - m_prevTime; - m_prevTime = Simulator::Now (); - double deltaS = deltaTime.GetSeconds (); - m_x += m_dx * deltaS; - m_y += m_dy * deltaS; -} - -void -RandomWalkMobilityModel::DoDispose (void) -{ - m_parameters = 0; - // chain up - MobilityModel::DoDispose (); -} -void -RandomWalkMobilityModel::DoGet (double &x, double &y, double &z) const -{ - Update (); - x = m_x; - y = m_y; - z = 0; -} -void -RandomWalkMobilityModel::DoSet (double x, double y, double z) -{ - bool changed = false; - if (m_x != x || m_y != y) - { - changed = true; - } - m_x = x; - m_y = y; - m_prevTime = Simulator::Now (); - if (changed) - { - NotifyCourseChange (); - } -} - - -} // namespace ns3 diff --git a/src/node/random-mobility-model.h b/src/node/random-mobility-model.h deleted file mode 100644 index 72448e175..000000000 --- a/src/node/random-mobility-model.h +++ /dev/null @@ -1,131 +0,0 @@ -/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ -/* - * Copyright (c) 2006,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 RANDOM_WALK_MOBILITY_MODEL_H -#define RANDOM_WALK_MOBILITY_MODEL_H - -#include "ns3/object.h" -#include "ns3/mobility-model.h" -#include "ns3/nstime.h" -#include "ns3/random-variable.h" -#include "ns3/component-manager.h" - -namespace ns3 { - -/** - * \brief parameters to control a random walk model - */ -class RandomWalkMobilityModelParameters : public Object -{ - public: - enum Mode { - MODE_DISTANCE, - MODE_TIME - }; - /** - * Instantiate a set of RandomWalk parameters initialized - * with the Bind default values. - */ - RandomWalkMobilityModelParameters (); - /** - * \param minSpeed the minimum speed - * \param maxSpeed the maximum speed - * - * The speed of any node is chosen such that minSpeed <= speed <= maxSpeed - */ - void SetSpeedBounds (double minSpeed, double maxSpeed); - /** - * \param distance the distance before a direction change - * - * Unit is meters - */ - void SetModeDistance (double distance); - /** - * \param time the delay before a direction change. - */ - void SetModeTime (Time time); - private: - bool IsDefault (void) const; - friend class RandomWalkMobilityModel; - double m_minSpeed; - double m_maxSpeed; - enum Mode m_mode; - double m_modeDistance; - Time m_modeTime; -}; - -/** - * \brief an unbounded 2D random walk position model - * - * Each instance moves with a speed and direction choosen at random - * in the intervals [minspeed,maxspeed] and [0,2pi] until - * either a fixed distance has been walked or until a fixed amount - * of time. - * - * The parameters of the model can be specified either with the ns3::Bind - * function and the variables "RandomWalkMinSpeed", "RandomWalkMaxSpeed", - * "RandomWalkMode", "RandomWalkModeDistance", and, "RandomWalkModeTime" or - * with an instance of the RandomWalkMobilityModelParameters class which - * must be fed to the RandomWalkMobilityModel constructors. - */ -class RandomWalkMobilityModel : public MobilityModel -{ - public: - static const InterfaceId iid; - static const ClassId cid; - /** - * Create a new position object located at position (0,0,0) - */ - RandomWalkMobilityModel (); - /** - * Create a new position object located at position (x,y,0) - */ - RandomWalkMobilityModel (double x, double y); - /** - * Create a new position object located at position (0,0,0) - */ - RandomWalkMobilityModel (Ptr parameters); - /** - * Create a new position object located at position (x,y,0) - */ - RandomWalkMobilityModel (Ptr parameters, - double x, double y); - private: - virtual void DoDispose (void); - virtual void DoGet (double &x, double &y, double &z) const; - virtual void DoSet (double x, double y, double z); - - void Reset (void); - void Update (void) const; - static Ptr GetDefaultParameters (void); - static UniformVariable m_randomDirection; - - mutable double m_x; - mutable double m_y; - double m_dx; - double m_dy; - mutable Time m_prevTime; - Ptr m_parameters; -}; - - -} // namespace ns3 - -#endif /* RANDOM_WALK_MOBILITY_MODEL_H */ From b10c76a3a694475ea17e40d61c2fd99091ec4bfc Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Tue, 17 Jul 2007 14:37:11 +0200 Subject: [PATCH 051/148] cleanup the header --- src/node/random-direction-mobility-model.h | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/node/random-direction-mobility-model.h b/src/node/random-direction-mobility-model.h index 1227cc1d5..30acc0721 100644 --- a/src/node/random-direction-mobility-model.h +++ b/src/node/random-direction-mobility-model.h @@ -67,19 +67,10 @@ class RandomDirectionMobilityModel : public MobilityModel RandomDirectionMobilityModel (Ptr parameters, double x, double y); private: - enum Side { - TOP, - BOTTOM, - LEFT, - RIGHT - }; static Ptr GetDefaultParameters (void); void ResetDirectionAndSpeed (void); void SetDirectionAndSpeed (double direction); void InitializeDirectionAndSpeed (void); - void Update (void) const; - bool CheckMobilityModel (void) const; - enum RandomDirectionMobilityModel::Side CalculateIntersection (double &x, double &y); virtual void DoDispose (void); virtual Position DoGet (void) const; virtual void DoSet (const Position &position); From c40b0f6f63141ce72da8e0d014537f41caf858cf Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Tue, 17 Jul 2007 16:11:49 +0200 Subject: [PATCH 052/148] start support for random walk and random waypoint models --- SConstruct | 2 + src/node/random-mobility-model.cc | 418 ++++++++++++++++++++++++++++++ src/node/random-mobility-model.h | 103 ++++++++ 3 files changed, 523 insertions(+) create mode 100644 src/node/random-mobility-model.cc create mode 100644 src/node/random-mobility-model.h diff --git a/SConstruct b/SConstruct index c4aa0c7b1..002b84a29 100644 --- a/SConstruct +++ b/SConstruct @@ -255,6 +255,7 @@ node.add_sources ([ 'grid-topology.cc', 'random-rectangle-topology.cc', 'random-walk-mobility-model.cc', + 'random-mobility-model.cc', 'random-direction-mobility-model.cc', 'hierarchical-mobility-model.cc', 'ns2-mobility-file-topology.cc', @@ -283,6 +284,7 @@ node.add_inst_headers ([ 'grid-topology.h', 'random-rectangle-topology.h', 'random-walk-mobility-model.h', + 'random-mobility-model.h', 'random-direction-mobility-model.h', 'hierarchical-mobility-model.h', 'ns2-mobility-file-topology.h', diff --git a/src/node/random-mobility-model.cc b/src/node/random-mobility-model.cc new file mode 100644 index 000000000..fb357747b --- /dev/null +++ b/src/node/random-mobility-model.cc @@ -0,0 +1,418 @@ +/* -*- 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/random-variable-default-value.h" +#include "ns3/rectangle-default-value.h" +#include "ns3/time-default-value.h" +#include "ns3/simulator.h" +#include +#include +#include "random-mobility-model.h" + +namespace ns3 { + +const double RandomMobilityModel::PI = 3.1415; +const InterfaceId RandomMobilityModel::iid = + MakeInterfaceId ("RandomMobilityModel", MobilityModel::iid); +const ClassId RandomMobilityModel::cid = + MakeClassId ("RandomMobilityModel", + RandomMobilityModel::iid); + + +static RandomVariableDefaultValue + g_speedVariable ("RandomMobilityModelSpeed", + "A random variable to control the speed of a random mobility model.", + "Uniform:1:2"); + +static RectangleDefaultValue + g_rectangle ("RandomMobilityModelArea", + "The bounding area for the Random model.", + -100, 100, -100, 100); + +static IntegerDefaultValue + g_walkDistance ("RandomWalkMaxDistance", + "The maximum distance (meters) an object moves before changing speed/direction for a " + "random walk model", + 2.0); + +static IntegerDefaultValue + g_waypointDistance ("RandomWaypointMaxDistance", + "The maximum distance (meters) an object moves before changing speed/direction for a " + "random waypoint model", + 2.0); + +static TimeDefaultValue + g_walkDelay ("RandomWalkMaxDelay", + "The maximum delay an object moves before changing speed/direction for " + "a random walk model.", + Seconds (1.0)); + +static TimeDefaultValue + g_waypointDelay ("RandomWaypointMaxDelay", + "The maximum delay an object moves before changing speed/direction for " + "a random waypoint model.", + Seconds (1.0)); + + +static RandomVariableDefaultValue + g_directionPauseVariable ("RandomDirectionMobilityModelPause", + "A random variable to control the duration of the " + "pause of a random direction mobility model.", + "Uniform:2:4"); + +static RandomVariableDefaultValue + g_waypointPauseVariable ("RandomWaypointMobilityModelPause", + "A random variable to control the duration of the " + "pause of a random waypoint mobility model.", + "Uniform:2:4"); + +static EnumDefaultValue + g_typeVariable ("RandomMobilityModelType", + "The type of mobility model to use.", + RandomMobilityModelParameters::WALK_DISTANCE, "WalkDistance", + RandomMobilityModelParameters::WALK_DELAY, "WalkDelay", + RandomMobilityModelParameters::WAYPOINT_DISTANCE, "WaypointDistance", + RandomMobilityModelParameters::WAYPOINT_DELAY, "WaypointDelay", + RandomMobilityModelParameters::DIRECTION, "Direction", + RandomMobilityModelParameters::BOUNDLESS, "Boundless", + 0, (void*)0); + + +RandomMobilityModelParameters::RandomMobilityModelParameters () + : m_type (g_typeVariable.GetValue ()), + m_speedVariable (g_speedVariable.GetCopy ()) +{ + m_bounds.xMin = g_rectangle.GetMinX (); + m_bounds.xMax = g_rectangle.GetMaxX (); + m_bounds.yMin = g_rectangle.GetMinY (); + m_bounds.yMax = g_rectangle.GetMaxY (); + Initialize (); +} +RandomMobilityModelParameters::RandomMobilityModelParameters (double xMin, double xMax, double yMin, double yMax) + : m_type (g_typeVariable.GetValue ()), + m_speedVariable (g_speedVariable.GetCopy ()) +{ + m_bounds.xMin = xMin; + m_bounds.xMax = xMax; + m_bounds.yMin = yMin; + m_bounds.yMax = yMax; + Initialize (); +} +RandomMobilityModelParameters::RandomMobilityModelParameters (double xMin, double xMax, double yMin, double yMax, + const RandomVariable &speedVariable) + : m_type (g_typeVariable.GetValue ()), + m_speedVariable (speedVariable.Copy ()) +{ + m_bounds.xMin = xMin; + m_bounds.xMax = xMax; + m_bounds.yMin = yMin; + m_bounds.yMax = yMax; + Initialize (); +} + +void +RandomMobilityModelParameters::Initialize (void) +{ + switch (m_type) + { + case RandomMobilityModelParameters::WAYPOINT_DISTANCE: + m_maxDistance = g_waypointDistance.GetValue (); + m_pauseVariable = g_waypointPauseVariable.GetCopy (); + break; + case RandomMobilityModelParameters::WAYPOINT_DELAY: + m_maxDelay = g_waypointDelay.GetValue (); + m_pauseVariable = g_waypointPauseVariable.GetCopy (); + break; + case RandomMobilityModelParameters::WALK_DISTANCE: + m_maxDistance = g_walkDistance.GetValue (); + break; + case RandomMobilityModelParameters::WALK_DELAY: + m_maxDelay = g_walkDelay.GetValue (); + break; + case RandomMobilityModelParameters::DIRECTION: + m_pauseVariable = g_directionPauseVariable.GetCopy (); + break; + case RandomMobilityModelParameters::BOUNDLESS: + break; + } +} + +RandomMobilityModelParameters::~RandomMobilityModelParameters () +{ + delete m_speedVariable; + delete m_pauseVariable; + m_speedVariable = 0; + m_pauseVariable = 0; +} + +void +RandomMobilityModelParameters::SetSpeed (const RandomVariable &speedVariable) +{ + delete m_speedVariable; + m_speedVariable = speedVariable.Copy (); +} +void +RandomMobilityModelParameters::SetWalkModelDistance (double maxDistance) +{ + m_type = RandomMobilityModelParameters::WALK_DISTANCE; + m_maxDistance = maxDistance; +} +void +RandomMobilityModelParameters::SetWalkModelDelay (Time maxDelay) +{ + m_type = RandomMobilityModelParameters::WALK_DELAY; + m_maxDelay = maxDelay; +} +void +RandomMobilityModelParameters::SetWaypointModelDistance (const RandomVariable &pauseVariable, + double maxDistance) +{ + m_type = RandomMobilityModelParameters::WAYPOINT_DISTANCE; + delete m_pauseVariable; + m_pauseVariable = pauseVariable.Copy (); + m_maxDistance = maxDistance; +} +void +RandomMobilityModelParameters::SetWaypointModelDelay (const RandomVariable &pauseVariable, Time maxDelay) +{ + m_type = RandomMobilityModelParameters::WAYPOINT_DELAY; + delete m_pauseVariable; + m_pauseVariable = pauseVariable.Copy (); + m_maxDelay = maxDelay; +} + +void +RandomMobilityModelParameters::SetDirectionModel (const RandomVariable &pauseVariable) +{ + m_type = RandomMobilityModelParameters::DIRECTION; + delete m_pauseVariable; + m_pauseVariable = pauseVariable.Copy (); +} +void +RandomMobilityModelParameters::SetBoundlessModel (void) +{ + m_type = RandomMobilityModelParameters::BOUNDLESS; +} + +void +RandomMobilityModelParameters::SetBounds (double xMin, double xMax, double yMin, double yMax) +{ + m_bounds.xMin = xMin; + m_bounds.yMin = yMin; + m_bounds.xMax = xMax; + m_bounds.yMax = yMax; +} + +Ptr +RandomMobilityModel::GetDefaultParameters (void) +{ + static Ptr parameters = Create (); + if (g_rectangle.IsDirty () || + g_typeVariable.IsDirty () || + g_speedVariable.IsDirty () || + g_waypointPauseVariable.IsDirty () || + g_directionPauseVariable.IsDirty () || + g_walkDistance.IsDirty () || + g_waypointDistance.IsDirty () || + g_walkDelay.IsDirty () || + g_waypointDelay.IsDirty ()) + { + parameters = Create (); + g_rectangle.ClearDirtyFlag (); + g_typeVariable.ClearDirtyFlag (); + g_speedVariable.ClearDirtyFlag (); + g_waypointPauseVariable.ClearDirtyFlag (); + g_directionPauseVariable.ClearDirtyFlag (); + g_walkDelay.ClearDirtyFlag (); + g_waypointDelay.ClearDirtyFlag (); + g_walkDistance.ClearDirtyFlag (); + g_waypointDistance.ClearDirtyFlag (); + } + return parameters; +} + + +RandomMobilityModel::RandomMobilityModel () + : m_parameters (GetDefaultParameters ()) +{ + SetInterfaceId (RandomMobilityModel::iid); + InitializeDirectionAndSpeed (); +} +RandomMobilityModel::RandomMobilityModel (double x, double y) + : m_parameters (GetDefaultParameters ()) +{ + SetInterfaceId (RandomMobilityModel::iid); + InitializeDirectionAndSpeed (); +} +RandomMobilityModel::RandomMobilityModel (Ptr parameters) + : m_parameters (parameters) +{ + SetInterfaceId (RandomMobilityModel::iid); + InitializeDirectionAndSpeed (); +} +RandomMobilityModel::RandomMobilityModel (Ptr parameters, + double x, double y) + : m_parameters (parameters) +{ + SetInterfaceId (RandomMobilityModel::iid); + InitializeDirectionAndSpeed (); +} +void +RandomMobilityModel::DoDispose (void) +{ + m_parameters = 0; + // chain up. + MobilityModel::DoDispose (); +} +void +RandomMobilityModel::InitializeDirectionAndSpeed (void) +{ + double direction = UniformVariable::GetSingleValue (0, 2 * PI); + SetDirectionAndSpeed (direction); +} +void +RandomMobilityModel::SetDirectionAndSpeed (double direction) +{ + double speed = m_parameters->m_speedVariable->GetValue (); + Time pause; + Time delay; + switch (m_parameters->m_type) + { + case RandomMobilityModelParameters::WALK_DISTANCE: + { + pause = MicroSeconds (0); + Time delay = m_helper.Reset (m_parameters->m_bounds, + direction, speed, + m_parameters->m_maxDistance, + pause); + } break; + case RandomMobilityModelParameters::WALK_DELAY: + { + pause = MicroSeconds (0); + Time delay = m_helper.Reset (m_parameters->m_bounds, + direction, speed, + m_parameters->m_maxDelay, + pause); + } break; + case RandomMobilityModelParameters::WAYPOINT_DISTANCE: + { + pause = Seconds (m_parameters->m_pauseVariable->GetValue ()); + Time delay = m_helper.Reset (m_parameters->m_bounds, + direction, speed, + m_parameters->m_maxDistance, + pause); + } break; + break; + case RandomMobilityModelParameters::WAYPOINT_DELAY: + { + pause = Seconds (m_parameters->m_pauseVariable->GetValue ()); + Time delay = m_helper.Reset (m_parameters->m_bounds, + direction, speed, + m_parameters->m_maxDelay, + pause); + } break; + case RandomMobilityModelParameters::DIRECTION: + { + pause = Seconds (m_parameters->m_pauseVariable->GetValue ()); + Time delay = m_helper.Reset (m_parameters->m_bounds, + direction, speed, + pause); + } break; + case RandomMobilityModelParameters::BOUNDLESS: + break; + } + m_event = Simulator::Schedule (delay + pause, + &RandomMobilityModel::ResetDirectionAndSpeed, this); +} +void +RandomMobilityModel::ResetDirectionAndSpeed (void) +{ + double direction; + switch (m_parameters->m_type) + { + default: + //XXX + break; + case RandomMobilityModelParameters::WALK_DISTANCE: + { + switch (m_helper.GetSide (m_parameters->m_bounds)) + { + case MobilityModelHelper::RIGHT: + + break; + case MobilityModelHelper::LEFT: + direction += - PI / 2; + break; + case MobilityModelHelper::TOP: + direction += PI; + break; + case MobilityModelHelper::BOTTOM: + direction += 0.0; + break; + case MobilityModelHelper::NONE: + NS_ASSERT (false); + break; + } + } break; + case RandomMobilityModelParameters::DIRECTION: + { + direction = UniformVariable::GetSingleValue (0, PI); + switch (m_helper.GetSide (m_parameters->m_bounds)) + { + case MobilityModelHelper::RIGHT: + direction += PI / 2; + break; + case MobilityModelHelper::LEFT: + direction += - PI / 2; + break; + case MobilityModelHelper::TOP: + direction += PI; + break; + case MobilityModelHelper::BOTTOM: + direction += 0.0; + break; + case MobilityModelHelper::NONE: + NS_ASSERT (false); + break; + } + } break; + } + SetDirectionAndSpeed (direction); + NotifyCourseChange (); +} +Position +RandomMobilityModel::DoGet (void) const +{ + Position2D position = m_helper.GetCurrentPosition (m_parameters->m_bounds); + return Position (position.x, position.y, 0.0); +} +void +RandomMobilityModel::DoSet (const Position &position) +{ + Position2D pos (position.x, position.y); + m_helper.InitializePosition (pos); + Simulator::Remove (m_event); + InitializeDirectionAndSpeed (); + NotifyCourseChange (); +} + + + +} // namespace ns3 diff --git a/src/node/random-mobility-model.h b/src/node/random-mobility-model.h new file mode 100644 index 000000000..3341bfe42 --- /dev/null +++ b/src/node/random-mobility-model.h @@ -0,0 +1,103 @@ +/* -*- 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 RANDOM_MOBILITY_MODEL_H +#define RANDOM_MOBILITY_MODEL_H + +#include "ns3/object.h" +#include "ns3/ptr.h" +#include "ns3/nstime.h" +#include "ns3/event-id.h" +#include "ns3/component-manager.h" +#include "mobility-model.h" +#include "mobility-model-helper.h" + +namespace ns3 { + +class RandomVariable; + +class RandomMobilityModelParameters : public Object +{ + public: + RandomMobilityModelParameters (); + RandomMobilityModelParameters (double xMin, double xMax, double yMin, double yMax); + RandomMobilityModelParameters (double xMin, double xMax, double yMin, double yMax, + const RandomVariable &speedVariable); + virtual ~RandomMobilityModelParameters (); + + void SetSpeed (const RandomVariable &speedVariable); + void SetBounds (double xMin, double xMax, double yMin, double yMax); + void SetWalkModelDistance (double maxDistance); + void SetWalkModelDelay (Time maxDelay); + void SetWaypointModelDistance (const RandomVariable &pauseVariable, double maxDistance); + void SetWaypointModelDelay (const RandomVariable &pauseVariable, Time maxDelay); + void SetDirectionModel (const RandomVariable &pauseVariable); + void SetBoundlessModel (void); + + + // made public because need to use it for default values. + enum Type { + WALK_DISTANCE, + WALK_DELAY, + WAYPOINT_DISTANCE, + WAYPOINT_DELAY, + DIRECTION, + BOUNDLESS + }; + private: + void Initialize (void); + friend class RandomMobilityModel; + enum Type m_type; + struct AreaBounds m_bounds; + RandomVariable *m_speedVariable; + RandomVariable *m_pauseVariable; + double m_maxDistance; + Time m_maxDelay; +}; + +class RandomMobilityModel : public MobilityModel +{ + public: + static const InterfaceId iid; + static const ClassId cid; + + RandomMobilityModel (); + RandomMobilityModel (double x, double y); + RandomMobilityModel (Ptr parameters); + RandomMobilityModel (Ptr parameters, + double x, double y); + private: + static Ptr GetDefaultParameters (void); + void ResetDirectionAndSpeed (void); + void SetDirectionAndSpeed (double direction); + void InitializeDirectionAndSpeed (void); + virtual void DoDispose (void); + virtual Position DoGet (void) const; + virtual void DoSet (const Position &position); + + static const double PI; + Ptr m_parameters; + EventId m_event; + MobilityModelHelper m_helper; +}; + +} // namespace ns3 + +#endif /* RANDOM_MOBILITY_MODEL_H */ From 0405be5bb040a48840e547242af2aeec6606f743 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 18 Jul 2007 10:13:28 +0200 Subject: [PATCH 053/148] a random position helper class --- SConstruct | 4 ++ src/node/position-2d.cc | 10 +++++ src/node/position-2d.h | 16 +++++++ src/node/random-2d-position.cc | 82 ++++++++++++++++++++++++++++++++++ src/node/random-2d-position.h | 46 +++++++++++++++++++ 5 files changed, 158 insertions(+) create mode 100644 src/node/position-2d.cc create mode 100644 src/node/position-2d.h create mode 100644 src/node/random-2d-position.cc create mode 100644 src/node/random-2d-position.h diff --git a/SConstruct b/SConstruct index 002b84a29..c25806e45 100644 --- a/SConstruct +++ b/SConstruct @@ -260,6 +260,8 @@ node.add_sources ([ 'hierarchical-mobility-model.cc', 'ns2-mobility-file-topology.cc', 'mobility-model-helper.cc', + 'position-2d.cc', + 'random-2d-position.cc', ]) node.add_inst_headers ([ 'node.h', @@ -289,6 +291,8 @@ node.add_inst_headers ([ 'hierarchical-mobility-model.h', 'ns2-mobility-file-topology.h', 'mobility-model-helper.h', + 'position-2d.h', + 'random-2d-position.h', ]) applications = build.Ns3Module ('applications', 'src/applications') diff --git a/src/node/position-2d.cc b/src/node/position-2d.cc new file mode 100644 index 000000000..57c9a3b9e --- /dev/null +++ b/src/node/position-2d.cc @@ -0,0 +1,10 @@ +#include "position-2d.h" + +namespace ns3 { + +Position2d::Position2d (double _x, double _y) + : x (_x), + y (_y) +{} + +} // namespace ns3 diff --git a/src/node/position-2d.h b/src/node/position-2d.h new file mode 100644 index 000000000..d1c10030c --- /dev/null +++ b/src/node/position-2d.h @@ -0,0 +1,16 @@ +#ifndef POSITION_2D_H +#define POSITION_2D_H + +namespace ns3 { + +class Position2d +{ + public: + Position2d (double x, double y); + double x; + double y; +}; + +} // namespace ns3 + +#endif /* POSITION_2D_H */ diff --git a/src/node/random-2d-position.cc b/src/node/random-2d-position.cc new file mode 100644 index 000000000..dd7164ed0 --- /dev/null +++ b/src/node/random-2d-position.cc @@ -0,0 +1,82 @@ +#include "random-2d-position.h" +#include "ns3/random-variable.h" +#include "ns3/random-variable-default-value.h" +#include + +namespace ns3 { + +static RandomVariableDefaultValue +g_rectangleX ("RandomRectanglePositionX", + "A random variable which represents the x position of a position in a random rectangle.", + "Uniform:0:200"); + +static RandomVariableDefaultValue +g_rectangleY ("RandomRectanglePositionY", + "A random variable which represents the y position of a position in a random rectangle.", + "Uniform:0:200"); + +static RandomVariableDefaultValue +g_discTheta ("RandomRectanglePositionTheta", + "A random variable which represents the angle (gradients) of a position in a random disc.", + "Uniform:0:3.1415"); + +static RandomVariableDefaultValue +g_discRho ("RandomRectanglePositionRho", + "A random variable which represents the radius of a position in a random disc.", + "Uniform:0:200"); + +Random2dPosition::~Random2dPosition () +{} + +RandomRectanglePosition::RandomRectanglePosition () + : m_x (g_rectangleX.GetCopy ()), + m_y (g_rectangleY.GetCopy ()) +{} +RandomRectanglePosition::RandomRectanglePosition (const RandomVariable &x, + const RandomVariable &y) + : m_x (x.Copy ()), + m_y (y.Copy ()) +{} +RandomRectanglePosition::~RandomRectanglePosition () +{ + delete m_x; + delete m_y; + m_x = 0; + m_y = 0; +} +Position2d +RandomRectanglePosition::Get (void) const +{ + double x = m_x->GetValue (); + double y = m_y->GetValue (); + return Position2d (x, y); +} + +RandomDiscPosition::RandomDiscPosition () + : m_theta (g_discTheta.GetCopy ()), + m_rho (g_discRho.GetCopy ()) +{} +RandomDiscPosition::RandomDiscPosition (const RandomVariable &theta, + const RandomVariable &rho) + : m_theta (theta.Copy ()), + m_rho (rho.Copy ()) +{} +RandomDiscPosition::~RandomDiscPosition () +{ + delete m_theta; + delete m_rho; + m_theta = 0; + m_rho = 0; +} +Position2d +RandomDiscPosition::Get (void) const +{ + double theta = m_theta->GetValue (); + double rho = m_rho->GetValue (); + double x = std::cos (theta) * rho; + double y = std::sin (theta) * rho; + return Position2d (x, y); +} + + +} // namespace ns3 diff --git a/src/node/random-2d-position.h b/src/node/random-2d-position.h new file mode 100644 index 000000000..a8ae249df --- /dev/null +++ b/src/node/random-2d-position.h @@ -0,0 +1,46 @@ +#ifndef RANDOM_2D_POSITION_H +#define RANDOM_2D_POSITION_H + +#include "ns3/object.h" +#include "position-2d.h" + +namespace ns3 { + +class RandomVariable; + +class Random2dPosition : public Object +{ +public: + virtual ~Random2dPosition (); + virtual Position2d Get (void) const = 0; +}; + +class RandomRectanglePosition : public Random2dPosition +{ +public: + RandomRectanglePosition (); + RandomRectanglePosition (const RandomVariable &x, + const RandomVariable &y); + virtual ~RandomRectanglePosition (); + virtual Position2d Get (void) const; +private: + RandomVariable *m_x; + RandomVariable *m_y; +}; + +class RandomDiscPosition : public Random2dPosition +{ +public: + RandomDiscPosition (); + RandomDiscPosition (const RandomVariable &theta, + const RandomVariable &rho); + virtual ~RandomDiscPosition (); + virtual Position2d Get (void) const; +private: + RandomVariable *m_theta; + RandomVariable *m_rho; +}; + +} // namespace ns3 + +#endif /* RANDOM_2D_POSITION_H */ From bd234eafe35dd942326b04ec350024b50b422483 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 18 Jul 2007 10:46:26 +0200 Subject: [PATCH 054/148] add iid and cid support to Random2dPosition --- src/node/random-2d-position.cc | 14 ++++++++++++++ src/node/random-2d-position.h | 5 +++++ 2 files changed, 19 insertions(+) diff --git a/src/node/random-2d-position.cc b/src/node/random-2d-position.cc index dd7164ed0..6fbb74d04 100644 --- a/src/node/random-2d-position.cc +++ b/src/node/random-2d-position.cc @@ -25,6 +25,20 @@ g_discRho ("RandomRectanglePositionRho", "A random variable which represents the radius of a position in a random disc.", "Uniform:0:200"); +const InterfaceId Random2dPosition::iid = MakeInterfaceId ("Random2dPosition", Object::iid); + +const ClassId RandomRectanglePosition::cid = + MakeClassId ("RandomRectanglePosition", + Random2dPosition::iid); +const ClassId RandomDiscPosition::cid = + MakeClassId ("RandomDiscPosition", + Random2dPosition::iid); + +Random2dPosition::Random2dPosition () +{ + Object::SetInterfaceId (Random2dPosition::iid); +} + Random2dPosition::~Random2dPosition () {} diff --git a/src/node/random-2d-position.h b/src/node/random-2d-position.h index a8ae249df..3c014e21c 100644 --- a/src/node/random-2d-position.h +++ b/src/node/random-2d-position.h @@ -2,6 +2,7 @@ #define RANDOM_2D_POSITION_H #include "ns3/object.h" +#include "ns3/component-manager.h" #include "position-2d.h" namespace ns3 { @@ -11,6 +12,8 @@ class RandomVariable; class Random2dPosition : public Object { public: + static const InterfaceId iid; + Random2dPosition (); virtual ~Random2dPosition (); virtual Position2d Get (void) const = 0; }; @@ -18,6 +21,7 @@ public: class RandomRectanglePosition : public Random2dPosition { public: + static const ClassId cid; RandomRectanglePosition (); RandomRectanglePosition (const RandomVariable &x, const RandomVariable &y); @@ -31,6 +35,7 @@ private: class RandomDiscPosition : public Random2dPosition { public: + static const ClassId cid; RandomDiscPosition (); RandomDiscPosition (const RandomVariable &theta, const RandomVariable &rho); From 2bd88ed30097cb777f3d46d5cb31ea9c722cf06c Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 18 Jul 2007 10:46:44 +0200 Subject: [PATCH 055/148] use Random2dPosition from RandomRectangleTopology --- src/node/random-rectangle-topology.cc | 73 ++++++++++++++------------- src/node/random-rectangle-topology.h | 16 +++--- 2 files changed, 47 insertions(+), 42 deletions(-) diff --git a/src/node/random-rectangle-topology.cc b/src/node/random-rectangle-topology.cc index 05b3af74d..77efba294 100644 --- a/src/node/random-rectangle-topology.cc +++ b/src/node/random-rectangle-topology.cc @@ -18,58 +18,63 @@ * * Author: Mathieu Lacage */ -#include "random-rectangle-topology.h" -#include "static-mobility-model.h" #include "ns3/random-variable-default-value.h" +#include "random-rectangle-topology.h" +#include "random-2d-position.h" +#include "mobility-model.h" namespace ns3 { -static RandomVariableDefaultValue -g_xVariable ("RandomRectangleTopologyX", - "Random variable to choose the x coordinate of the elements of a RandomRectangleTopology.", - "Uniform:-100:100"); -static RandomVariableDefaultValue -g_yVariable ("RandomRectangleTopologyY", - "Random variable to choose the y coordinate of the elements of a RandomRectangleTopology.", - "Uniform:-100:100"); +static ClassIdDefaultValue +g_position ("Random2dTopologyType", + "The type of initial random position in a 2d topology.", + Random2dPosition::iid, + "Rectangle"); + +static ClassIdDefaultValue +g_mobility ("Random2dTopologyMobilityModelType", + "The type of mobility model attached to an object in a 2d topology.", + MobilityModel::iid, + "StaticMobilityModel"); RandomRectangleTopology::RandomRectangleTopology () - : m_xVariable (g_xVariable.GetCopy ()), - m_yVariable (g_yVariable.GetCopy ()), - m_positionModel (StaticMobilityModel::cid) -{} - -RandomRectangleTopology::RandomRectangleTopology (double xMin, double xMax, double yMin, double yMax) - : m_xVariable (new UniformVariable (xMin, xMax)), - m_yVariable (new UniformVariable (yMin, yMax)), - m_positionModel (StaticMobilityModel::cid) -{} -RandomRectangleTopology::RandomRectangleTopology (const RandomVariable &xVariable, - const RandomVariable &yVariable) - : m_xVariable (xVariable.Copy ()), - m_yVariable (yVariable.Copy ()), - m_positionModel (StaticMobilityModel::cid) + : m_mobilityModel (g_mobility.GetValue ()) +{ + m_positionModel = ComponentManager::Create (g_position.GetValue (), + Random2dPosition::iid); +} +RandomRectangleTopology::RandomRectangleTopology (Ptr positionModel, ClassId mobilityModel) + : m_positionModel (positionModel), + m_mobilityModel (mobilityModel) {} RandomRectangleTopology::~RandomRectangleTopology () { - delete m_xVariable; - delete m_yVariable; - m_xVariable = 0; - m_yVariable = 0; + m_positionModel = 0; } void -RandomRectangleTopology::SetMobilityModelModel (ClassId classId) +RandomRectangleTopology::SetMobilityModel (ClassId classId) { - m_positionModel = classId; + m_mobilityModel = classId; +} + +void +RandomRectangleTopology::SetPositionModel (Ptr positionModel) +{ + m_positionModel = positionModel; } void RandomRectangleTopology::LayoutOne (Ptr object) { - double x = m_xVariable->GetValue (); - double y = m_yVariable->GetValue (); - object->AddInterface (ComponentManager::Create (m_positionModel, x, y)); + Position2d position2d = m_positionModel->Get (); + Ptr mobility = ComponentManager::Create (m_mobilityModel, + MobilityModel::iid); + Position position = mobility->Get (); + position.x = position2d.x; + position.y = position2d.y; + mobility->Set (position); + object->AddInterface (mobility); } diff --git a/src/node/random-rectangle-topology.h b/src/node/random-rectangle-topology.h index 7aa557ca2..4a5aa6621 100644 --- a/src/node/random-rectangle-topology.h +++ b/src/node/random-rectangle-topology.h @@ -21,33 +21,33 @@ #ifndef RANDOM_RECTANGLE_TOPOLOGY_H #define RANDOM_RECTANGLE_TOPOLOGY_H -#include "ns3/random-variable.h" #include "ns3/ptr.h" #include "ns3/object.h" #include "ns3/component-manager.h" namespace ns3 { +class Random2dPosition; + class RandomRectangleTopology { public: - RandomRectangleTopology (); - RandomRectangleTopology (double xMin, double xMax, double yMin, double yMax); - RandomRectangleTopology (const RandomVariable &xVariable, const RandomVariable &yVariable); + RandomRectangleTopology (Ptr positionModel, + ClassId mobilityModel); ~RandomRectangleTopology (); - void SetMobilityModelModel (ClassId classId); + void SetMobilityModel (ClassId classId); + void SetPositionModel (Ptr positionModel); void LayoutOne (Ptr object); template void Layout (const T &begin, const T &end); private: - RandomVariable *m_xVariable; - RandomVariable *m_yVariable; - ClassId m_positionModel; + Ptr m_positionModel; + ClassId m_mobilityModel; }; } // namespace ns3 From a011792fb63d34b1e42fb2fda3ac51c6beaa4d5c Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 18 Jul 2007 10:53:53 +0200 Subject: [PATCH 056/148] RandomRectangleTopology -> RandomTopology --- SConstruct | 4 ++-- ...-rectangle-topology.cc => random-topology.cc} | 14 +++++++------- ...om-rectangle-topology.h => random-topology.h} | 16 ++++++++-------- 3 files changed, 17 insertions(+), 17 deletions(-) rename src/node/{random-rectangle-topology.cc => random-topology.cc} (83%) rename src/node/{random-rectangle-topology.h => random-topology.h} (81%) diff --git a/SConstruct b/SConstruct index c25806e45..f19bd992d 100644 --- a/SConstruct +++ b/SConstruct @@ -253,7 +253,7 @@ node.add_sources ([ 'static-mobility-model.cc', 'static-speed-mobility-model.cc', 'grid-topology.cc', - 'random-rectangle-topology.cc', + 'random-topology.cc', 'random-walk-mobility-model.cc', 'random-mobility-model.cc', 'random-direction-mobility-model.cc', @@ -284,7 +284,7 @@ node.add_inst_headers ([ 'static-mobility-model.h', 'static-speed-mobility-model.h', 'grid-topology.h', - 'random-rectangle-topology.h', + 'random-topology.h', 'random-walk-mobility-model.h', 'random-mobility-model.h', 'random-direction-mobility-model.h', diff --git a/src/node/random-rectangle-topology.cc b/src/node/random-topology.cc similarity index 83% rename from src/node/random-rectangle-topology.cc rename to src/node/random-topology.cc index 77efba294..da3567c49 100644 --- a/src/node/random-rectangle-topology.cc +++ b/src/node/random-topology.cc @@ -19,7 +19,7 @@ * Author: Mathieu Lacage */ #include "ns3/random-variable-default-value.h" -#include "random-rectangle-topology.h" +#include "random-topology.h" #include "random-2d-position.h" #include "mobility-model.h" @@ -37,35 +37,35 @@ g_mobility ("Random2dTopologyMobilityModelType", MobilityModel::iid, "StaticMobilityModel"); -RandomRectangleTopology::RandomRectangleTopology () +RandomTopology::RandomTopology () : m_mobilityModel (g_mobility.GetValue ()) { m_positionModel = ComponentManager::Create (g_position.GetValue (), Random2dPosition::iid); } -RandomRectangleTopology::RandomRectangleTopology (Ptr positionModel, ClassId mobilityModel) +RandomTopology::RandomTopology (Ptr positionModel, ClassId mobilityModel) : m_positionModel (positionModel), m_mobilityModel (mobilityModel) {} -RandomRectangleTopology::~RandomRectangleTopology () +RandomTopology::~RandomTopology () { m_positionModel = 0; } void -RandomRectangleTopology::SetMobilityModel (ClassId classId) +RandomTopology::SetMobilityModel (ClassId classId) { m_mobilityModel = classId; } void -RandomRectangleTopology::SetPositionModel (Ptr positionModel) +RandomTopology::SetPositionModel (Ptr positionModel) { m_positionModel = positionModel; } void -RandomRectangleTopology::LayoutOne (Ptr object) +RandomTopology::LayoutOne (Ptr object) { Position2d position2d = m_positionModel->Get (); Ptr mobility = ComponentManager::Create (m_mobilityModel, diff --git a/src/node/random-rectangle-topology.h b/src/node/random-topology.h similarity index 81% rename from src/node/random-rectangle-topology.h rename to src/node/random-topology.h index 4a5aa6621..826186e6c 100644 --- a/src/node/random-rectangle-topology.h +++ b/src/node/random-topology.h @@ -18,8 +18,8 @@ * * Author: Mathieu Lacage */ -#ifndef RANDOM_RECTANGLE_TOPOLOGY_H -#define RANDOM_RECTANGLE_TOPOLOGY_H +#ifndef RANDOM_TOPOLOGY_H +#define RANDOM_TOPOLOGY_H #include "ns3/ptr.h" #include "ns3/object.h" @@ -29,14 +29,14 @@ namespace ns3 { class Random2dPosition; -class RandomRectangleTopology +class RandomTopology { public: - RandomRectangleTopology (); - RandomRectangleTopology (Ptr positionModel, + RandomTopology (); + RandomTopology (Ptr positionModel, ClassId mobilityModel); - ~RandomRectangleTopology (); + ~RandomTopology (); void SetMobilityModel (ClassId classId); void SetPositionModel (Ptr positionModel); @@ -56,7 +56,7 @@ namespace ns3 { template void -RandomRectangleTopology::Layout (const T &begin, const T &end) +RandomTopology::Layout (const T &begin, const T &end) { for (T i = begin; i != end; i++) { @@ -67,4 +67,4 @@ RandomRectangleTopology::Layout (const T &begin, const T &end) } // namespace ns3 -#endif /* RANDOM_RECTANGLE_TOPOLOGY_H */ +#endif /* RANDOM_TOPOLOGY_H */ From 5cc787f64889d43a713f926221a42b0fdd24ef3f Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 18 Jul 2007 11:03:01 +0200 Subject: [PATCH 057/148] move Position class in own header --- src/node/mobility-model.cc | 7 ------- src/node/mobility-model.h | 10 +--------- src/node/position.cc | 12 ++++++++++++ src/node/position.h | 17 +++++++++++++++++ 4 files changed, 30 insertions(+), 16 deletions(-) create mode 100644 src/node/position.cc create mode 100644 src/node/position.h diff --git a/src/node/mobility-model.cc b/src/node/mobility-model.cc index 91776aa07..4752b603e 100644 --- a/src/node/mobility-model.cc +++ b/src/node/mobility-model.cc @@ -26,13 +26,6 @@ namespace ns3 { const InterfaceId MobilityModel::iid = MakeInterfaceId ("MobilityModel", Object::iid); - -Position::Position (double _x, double _y, double _z) - : x (_x), - y (_y), - z (_z) -{} - MobilityModel::MobilityModel () { SetInterfaceId (MobilityModel::iid); diff --git a/src/node/mobility-model.h b/src/node/mobility-model.h index 7dd300aff..7ba6b8d68 100644 --- a/src/node/mobility-model.h +++ b/src/node/mobility-model.h @@ -22,18 +22,10 @@ #define MOBILITY_MODEL_H #include "ns3/object.h" +#include "position.h" namespace ns3 { -class Position -{ -public: - Position (double x, double y, double z); - double x; - double y; - double z; -}; - /** * \brief keep track of the current position of an object * diff --git a/src/node/position.cc b/src/node/position.cc new file mode 100644 index 000000000..66261306d --- /dev/null +++ b/src/node/position.cc @@ -0,0 +1,12 @@ +#include "position.h" + +namespace ns3 { + + +Position::Position (double _x, double _y, double _z) + : x (_x), + y (_y), + z (_z) +{} + +} // namespace ns3 diff --git a/src/node/position.h b/src/node/position.h new file mode 100644 index 000000000..6021139e5 --- /dev/null +++ b/src/node/position.h @@ -0,0 +1,17 @@ +#ifndef POSITION_H +#define POSITION_H + +namespace ns3 { + +class Position +{ +public: + Position (double x, double y, double z); + double x; + double y; + double z; +}; + +} // namespace ns3 + +#endif /* POSITION_H */ From a85bcfc0d7865b54efc75156abc1895e84eb3e0a Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 18 Jul 2007 11:04:54 +0200 Subject: [PATCH 058/148] Random2dPosition -> RandomPosition --- SConstruct | 6 +++-- ...ndom-2d-position.cc => random-position.cc} | 22 ++++++++-------- ...random-2d-position.h => random-position.h} | 24 +++++++++--------- src/node/random-topology.cc | 25 ++++++++----------- src/node/random-topology.h | 10 ++++---- 5 files changed, 43 insertions(+), 44 deletions(-) rename src/node/{random-2d-position.cc => random-position.cc} (85%) rename src/node/{random-2d-position.h => random-position.h} (60%) diff --git a/SConstruct b/SConstruct index f19bd992d..cacfe00d8 100644 --- a/SConstruct +++ b/SConstruct @@ -261,7 +261,8 @@ node.add_sources ([ 'ns2-mobility-file-topology.cc', 'mobility-model-helper.cc', 'position-2d.cc', - 'random-2d-position.cc', + 'position.cc', + 'random-position.cc', ]) node.add_inst_headers ([ 'node.h', @@ -292,7 +293,8 @@ node.add_inst_headers ([ 'ns2-mobility-file-topology.h', 'mobility-model-helper.h', 'position-2d.h', - 'random-2d-position.h', + 'position.h', + 'random-position.h', ]) applications = build.Ns3Module ('applications', 'src/applications') diff --git a/src/node/random-2d-position.cc b/src/node/random-position.cc similarity index 85% rename from src/node/random-2d-position.cc rename to src/node/random-position.cc index 6fbb74d04..f0c126a0b 100644 --- a/src/node/random-2d-position.cc +++ b/src/node/random-position.cc @@ -1,4 +1,4 @@ -#include "random-2d-position.h" +#include "random-position.h" #include "ns3/random-variable.h" #include "ns3/random-variable-default-value.h" #include @@ -25,21 +25,21 @@ g_discRho ("RandomRectanglePositionRho", "A random variable which represents the radius of a position in a random disc.", "Uniform:0:200"); -const InterfaceId Random2dPosition::iid = MakeInterfaceId ("Random2dPosition", Object::iid); +const InterfaceId RandomPosition::iid = MakeInterfaceId ("RandomPosition", Object::iid); const ClassId RandomRectanglePosition::cid = MakeClassId ("RandomRectanglePosition", - Random2dPosition::iid); + RandomPosition::iid); const ClassId RandomDiscPosition::cid = MakeClassId ("RandomDiscPosition", - Random2dPosition::iid); + RandomPosition::iid); -Random2dPosition::Random2dPosition () +RandomPosition::RandomPosition () { - Object::SetInterfaceId (Random2dPosition::iid); + Object::SetInterfaceId (RandomPosition::iid); } -Random2dPosition::~Random2dPosition () +RandomPosition::~RandomPosition () {} RandomRectanglePosition::RandomRectanglePosition () @@ -58,12 +58,12 @@ RandomRectanglePosition::~RandomRectanglePosition () m_x = 0; m_y = 0; } -Position2d +Position RandomRectanglePosition::Get (void) const { double x = m_x->GetValue (); double y = m_y->GetValue (); - return Position2d (x, y); + return Position (x, y, 0.0); } RandomDiscPosition::RandomDiscPosition () @@ -82,14 +82,14 @@ RandomDiscPosition::~RandomDiscPosition () m_theta = 0; m_rho = 0; } -Position2d +Position RandomDiscPosition::Get (void) const { double theta = m_theta->GetValue (); double rho = m_rho->GetValue (); double x = std::cos (theta) * rho; double y = std::sin (theta) * rho; - return Position2d (x, y); + return Position (x, y, 0.0); } diff --git a/src/node/random-2d-position.h b/src/node/random-position.h similarity index 60% rename from src/node/random-2d-position.h rename to src/node/random-position.h index 3c014e21c..6c1609642 100644 --- a/src/node/random-2d-position.h +++ b/src/node/random-position.h @@ -1,24 +1,24 @@ -#ifndef RANDOM_2D_POSITION_H -#define RANDOM_2D_POSITION_H +#ifndef RANDOM_POSITION_H +#define RANDOM_POSITION_H #include "ns3/object.h" #include "ns3/component-manager.h" -#include "position-2d.h" +#include "position.h" namespace ns3 { class RandomVariable; -class Random2dPosition : public Object +class RandomPosition : public Object { public: static const InterfaceId iid; - Random2dPosition (); - virtual ~Random2dPosition (); - virtual Position2d Get (void) const = 0; + RandomPosition (); + virtual ~RandomPosition (); + virtual Position Get (void) const = 0; }; -class RandomRectanglePosition : public Random2dPosition +class RandomRectanglePosition : public RandomPosition { public: static const ClassId cid; @@ -26,13 +26,13 @@ public: RandomRectanglePosition (const RandomVariable &x, const RandomVariable &y); virtual ~RandomRectanglePosition (); - virtual Position2d Get (void) const; + virtual Position Get (void) const; private: RandomVariable *m_x; RandomVariable *m_y; }; -class RandomDiscPosition : public Random2dPosition +class RandomDiscPosition : public RandomPosition { public: static const ClassId cid; @@ -40,7 +40,7 @@ public: RandomDiscPosition (const RandomVariable &theta, const RandomVariable &rho); virtual ~RandomDiscPosition (); - virtual Position2d Get (void) const; + virtual Position Get (void) const; private: RandomVariable *m_theta; RandomVariable *m_rho; @@ -48,4 +48,4 @@ private: } // namespace ns3 -#endif /* RANDOM_2D_POSITION_H */ +#endif /* RANDOM_POSITION_H */ diff --git a/src/node/random-topology.cc b/src/node/random-topology.cc index da3567c49..fa925c32c 100644 --- a/src/node/random-topology.cc +++ b/src/node/random-topology.cc @@ -20,30 +20,30 @@ */ #include "ns3/random-variable-default-value.h" #include "random-topology.h" -#include "random-2d-position.h" +#include "random-position.h" #include "mobility-model.h" namespace ns3 { static ClassIdDefaultValue -g_position ("Random2dTopologyType", - "The type of initial random position in a 2d topology.", - Random2dPosition::iid, +g_position ("RandomTopologyType", + "The type of initial random position in a 3d topology.", + RandomPosition::iid, "Rectangle"); static ClassIdDefaultValue -g_mobility ("Random2dTopologyMobilityModelType", - "The type of mobility model attached to an object in a 2d topology.", +g_mobility ("RandomTopologyMobilityModelType", + "The type of mobility model attached to an object in a 3d topology.", MobilityModel::iid, "StaticMobilityModel"); RandomTopology::RandomTopology () : m_mobilityModel (g_mobility.GetValue ()) { - m_positionModel = ComponentManager::Create (g_position.GetValue (), - Random2dPosition::iid); + m_positionModel = ComponentManager::Create (g_position.GetValue (), + RandomPosition::iid); } -RandomTopology::RandomTopology (Ptr positionModel, ClassId mobilityModel) +RandomTopology::RandomTopology (Ptr positionModel, ClassId mobilityModel) : m_positionModel (positionModel), m_mobilityModel (mobilityModel) {} @@ -59,7 +59,7 @@ RandomTopology::SetMobilityModel (ClassId classId) } void -RandomTopology::SetPositionModel (Ptr positionModel) +RandomTopology::SetPositionModel (Ptr positionModel) { m_positionModel = positionModel; } @@ -67,12 +67,9 @@ RandomTopology::SetPositionModel (Ptr positionModel) void RandomTopology::LayoutOne (Ptr object) { - Position2d position2d = m_positionModel->Get (); Ptr mobility = ComponentManager::Create (m_mobilityModel, MobilityModel::iid); - Position position = mobility->Get (); - position.x = position2d.x; - position.y = position2d.y; + Position position = m_positionModel->Get (); mobility->Set (position); object->AddInterface (mobility); } diff --git a/src/node/random-topology.h b/src/node/random-topology.h index 826186e6c..f3c63b853 100644 --- a/src/node/random-topology.h +++ b/src/node/random-topology.h @@ -27,26 +27,26 @@ namespace ns3 { -class Random2dPosition; +class RandomPosition; class RandomTopology { public: RandomTopology (); - RandomTopology (Ptr positionModel, - ClassId mobilityModel); + RandomTopology (Ptr positionModel, + ClassId mobilityModel); ~RandomTopology (); void SetMobilityModel (ClassId classId); - void SetPositionModel (Ptr positionModel); + void SetPositionModel (Ptr positionModel); void LayoutOne (Ptr object); template void Layout (const T &begin, const T &end); private: - Ptr m_positionModel; + Ptr m_positionModel; ClassId m_mobilityModel; }; From aef4192d6acfa6958ab1a1392a812e011e54808c Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 18 Jul 2007 13:56:37 +0200 Subject: [PATCH 059/148] add a 3d random waypoint model --- SConstruct | 6 ++ src/node/mobility-model.cc | 5 +- src/node/position.cc | 17 +++++ src/node/position.h | 3 + src/node/random-waypoint-mobility-model.cc | 82 ++++++++++++++++++++++ src/node/random-waypoint-mobility-model.h | 66 +++++++++++++++++ src/node/speed.cc | 17 +++++ src/node/speed.h | 18 +++++ src/node/static-speed-helper.cc | 60 ++++++++++++++++ src/node/static-speed-helper.h | 31 ++++++++ 10 files changed, 301 insertions(+), 4 deletions(-) create mode 100644 src/node/random-waypoint-mobility-model.cc create mode 100644 src/node/random-waypoint-mobility-model.h create mode 100644 src/node/speed.cc create mode 100644 src/node/speed.h create mode 100644 src/node/static-speed-helper.cc create mode 100644 src/node/static-speed-helper.h diff --git a/SConstruct b/SConstruct index cacfe00d8..82e530348 100644 --- a/SConstruct +++ b/SConstruct @@ -263,6 +263,9 @@ node.add_sources ([ 'position-2d.cc', 'position.cc', 'random-position.cc', + 'speed.cc', + 'static-speed-helper.cc', + 'random-waypoint-mobility-model.cc', ]) node.add_inst_headers ([ 'node.h', @@ -295,6 +298,9 @@ node.add_inst_headers ([ 'position-2d.h', 'position.h', 'random-position.h', + 'speed.h', + 'static-speed-helper.h', + 'random-waypoint-mobility-model.h', ]) applications = build.Ns3Module ('applications', 'src/applications') diff --git a/src/node/mobility-model.cc b/src/node/mobility-model.cc index 4752b603e..4e1de6e41 100644 --- a/src/node/mobility-model.cc +++ b/src/node/mobility-model.cc @@ -57,10 +57,7 @@ MobilityModel::GetDistanceFrom (const MobilityModel &other) const { Position oPosition = other.DoGet (); Position position = DoGet (); - double dx = oPosition.x - position.x; - double dy = oPosition.y - position.y; - double dz = oPosition.z - position.z; - return sqrt (dx*dx+dy*dy+dz*dz); + return CalculateDistance (position, oPosition); } void diff --git a/src/node/position.cc b/src/node/position.cc index 66261306d..802fe3bb7 100644 --- a/src/node/position.cc +++ b/src/node/position.cc @@ -1,4 +1,5 @@ #include "position.h" +#include namespace ns3 { @@ -9,4 +10,20 @@ Position::Position (double _x, double _y, double _z) z (_z) {} +Position::Position () + : x (0.0), + y (0.0), + z (0.0) +{} + +double +CalculateDistance (const Position &a, const Position &b) +{ + double dx = b.x - a.x; + double dy = b.y - a.y; + double dz = b.z - a.z; + double distance = std::sqrt (dx * dx + dy * dy + dz * dz); + return distance; +} + } // namespace ns3 diff --git a/src/node/position.h b/src/node/position.h index 6021139e5..261ba2e99 100644 --- a/src/node/position.h +++ b/src/node/position.h @@ -7,11 +7,14 @@ class Position { public: Position (double x, double y, double z); + Position (); double x; double y; double z; }; +double CalculateDistance (const Position &a, const Position &b); + } // namespace ns3 #endif /* POSITION_H */ diff --git a/src/node/random-waypoint-mobility-model.cc b/src/node/random-waypoint-mobility-model.cc new file mode 100644 index 000000000..c85616a6a --- /dev/null +++ b/src/node/random-waypoint-mobility-model.cc @@ -0,0 +1,82 @@ +#include "ns3/simulator.h" +#include "ns3/random-variable.h" +#include "random-waypoint-mobility-model.h" +#include "random-position.h" + +namespace ns3 { + +RandomWaypointMobilityModelParameters::RandomWaypointMobilityModelParameters () +{} +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; +} + + +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); + Start (); +} + + +} // namespace ns3 diff --git a/src/node/random-waypoint-mobility-model.h b/src/node/random-waypoint-mobility-model.h new file mode 100644 index 000000000..b56ae997e --- /dev/null +++ b/src/node/random-waypoint-mobility-model.h @@ -0,0 +1,66 @@ +/* -*- 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 RANDOM_WAYPOINT_MOBILITY_MODEL_H +#define RANDOM_WAYPOINT_MOBILITY_MODEL_H + +#include "static-speed-helper.h" +#include "mobility-model.h" +#include "random-position.h" +#include "ns3/ptr.h" + +namespace ns3 { + +class RandomVariable; + +class RandomWaypointMobilityModelParameters : public Object +{ +public: + RandomWaypointMobilityModelParameters (); + RandomWaypointMobilityModelParameters (Ptr randomPosition, + const RandomVariable &speed, + const RandomVariable &pause); + void SetWaypointPositionModel (Ptr randomPosition); + void SetSpeed (const RandomVariable &speed); + void SetPause (const RandomVariable &pause); +private: + friend class RandomWaypointMobilityModel; + virtual void DoDispose (void); + RandomVariable *m_speed; + RandomVariable *m_pause; + Ptr m_position; +}; + +class RandomWaypointMobilityModel : public MobilityModel +{ +public: + RandomWaypointMobilityModel (Ptr parameters); +private: + void Start (void); + virtual Position DoGet (void) const; + virtual void DoSet (const Position &position); + StaticSpeedHelper m_helper; + Ptr m_parameters; + EventId m_event; +}; + +} // namespace ns3 + +#endif /* RANDOM_WAYPOINT_MOBILITY_MODEL_H */ diff --git a/src/node/speed.cc b/src/node/speed.cc new file mode 100644 index 000000000..3f97ded7c --- /dev/null +++ b/src/node/speed.cc @@ -0,0 +1,17 @@ +#include "speed.h" + +namespace ns3 { + +Speed::Speed (double _dx, double _dy, double _dz) + : dx (_dx), + dy (_dy), + dz (_dz) +{} + +Speed::Speed () + : dx (0.0), + dy (0.0), + dz (0.0) +{} + +} // namespace ns3 diff --git a/src/node/speed.h b/src/node/speed.h new file mode 100644 index 000000000..c6bd45d8a --- /dev/null +++ b/src/node/speed.h @@ -0,0 +1,18 @@ +#ifndef SPEED_H +#define SPEED_H + +namespace ns3 { + +class Speed +{ +public: + Speed (double _dx, double _dy, double _dz); + Speed (); + double dx; + double dy; + double dz; +}; + +} // namespace ns3 + +#endif /* SPEED_H */ diff --git a/src/node/static-speed-helper.cc b/src/node/static-speed-helper.cc new file mode 100644 index 000000000..ed66d63b1 --- /dev/null +++ b/src/node/static-speed-helper.cc @@ -0,0 +1,60 @@ +#include "ns3/simulator.h" +#include "static-speed-helper.h" + +namespace ns3 { + +StaticSpeedHelper::StaticSpeedHelper () +{} +void +StaticSpeedHelper::InitializePosition (const Position &position) +{ + m_position = position; + m_speed.dx = 0.0; + m_speed.dy = 0.0; + m_lastUpdate = Simulator::Now (); + m_pauseEnd = Simulator::Now (); +} + +void +StaticSpeedHelper::Reset (const Speed &speed, const Time &pauseDelay) +{ + Update (); + m_speed = speed; + m_pauseEnd = Simulator::Now () + pauseDelay; +} + +Position +StaticSpeedHelper::GetCurrentPosition (void) const +{ + Update (); + return m_position; +} + +Speed +StaticSpeedHelper::GetCurrentSpeed (void) const +{ + return m_speed; +} + +void +StaticSpeedHelper::Update (void) const +{ + Time now = Simulator::Now (); + if (m_pauseEnd > now) + { + return; + } + Time last = std::max (now, m_pauseEnd); + if (m_lastUpdate >= last) + { + return; + } + Time deltaTime = now - last; + m_lastUpdate = now; + double deltaS = deltaTime.GetSeconds (); + m_position.x += m_speed.dx * deltaS; + m_position.y += m_speed.dy * deltaS; + m_position.z += m_speed.dz * deltaS; +} + +} // namespace ns3 diff --git a/src/node/static-speed-helper.h b/src/node/static-speed-helper.h new file mode 100644 index 000000000..cd651c951 --- /dev/null +++ b/src/node/static-speed-helper.h @@ -0,0 +1,31 @@ +#ifndef STATIC_SPEED_HELPER_H +#define STATIC_SPEED_HELPER_H + +#include "ns3/nstime.h" +#include "position.h" +#include "speed.h" + +namespace ns3 { + +class StaticSpeedHelper +{ + public: + StaticSpeedHelper (); + void InitializePosition (const Position &position); + + void Reset (const Speed &speed, const Time &pauseDelay); + + Position GetCurrentPosition (void) const; + Speed GetCurrentSpeed (void) const; + + private: + void Update (void) const; + mutable Time m_lastUpdate; + mutable Position m_position; + Speed m_speed; + Time m_pauseEnd; +}; + +} // namespace ns3 + +#endif /* STATIC_SPEED_HELPER_H */ From 5e3d0126e35059640890bc7bd3d25ab6688f7bbd Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 18 Jul 2007 14:08:34 +0200 Subject: [PATCH 060/148] add default value support to random waypoint --- src/node/random-waypoint-mobility-model.cc | 42 +++++++++++++++++++++- src/node/random-waypoint-mobility-model.h | 2 ++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/node/random-waypoint-mobility-model.cc b/src/node/random-waypoint-mobility-model.cc index c85616a6a..a80a9c944 100644 --- a/src/node/random-waypoint-mobility-model.cc +++ b/src/node/random-waypoint-mobility-model.cc @@ -1,12 +1,35 @@ #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"); + 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) @@ -41,6 +64,23 @@ RandomWaypointMobilityModelParameters::DoDispose (void) 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 ()) +{} RandomWaypointMobilityModel::RandomWaypointMobilityModel (Ptr parameters) : m_parameters (parameters) diff --git a/src/node/random-waypoint-mobility-model.h b/src/node/random-waypoint-mobility-model.h index b56ae997e..8b07040d5 100644 --- a/src/node/random-waypoint-mobility-model.h +++ b/src/node/random-waypoint-mobility-model.h @@ -42,6 +42,7 @@ public: void SetPause (const RandomVariable &pause); private: friend class RandomWaypointMobilityModel; + static Ptr GetCurrent (void); virtual void DoDispose (void); RandomVariable *m_speed; RandomVariable *m_pause; @@ -51,6 +52,7 @@ private: class RandomWaypointMobilityModel : public MobilityModel { public: + RandomWaypointMobilityModel (); RandomWaypointMobilityModel (Ptr parameters); private: void Start (void); From 88b0af83b5ad946b533f2436fb697ea844917a59 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 18 Jul 2007 14:11:33 +0200 Subject: [PATCH 061/148] add cid support to waypoint model --- src/node/random-waypoint-mobility-model.cc | 3 +++ src/node/random-waypoint-mobility-model.h | 2 ++ 2 files changed, 5 insertions(+) diff --git a/src/node/random-waypoint-mobility-model.cc b/src/node/random-waypoint-mobility-model.cc index a80a9c944..b83f515c0 100644 --- a/src/node/random-waypoint-mobility-model.cc +++ b/src/node/random-waypoint-mobility-model.cc @@ -23,6 +23,9 @@ g_position ("RandomWaypointPosition", RandomPosition::iid, "RandomPositionRectangle"); +const ClassId RandomWaypointMobilityModel::cid = + MakeClassId ("RandomWaypointMobilityModel", MobilityModel::iid); + RandomWaypointMobilityModelParameters::RandomWaypointMobilityModelParameters () : m_speed (g_speed.GetCopy ()), m_pause (g_pause.GetCopy ()) diff --git a/src/node/random-waypoint-mobility-model.h b/src/node/random-waypoint-mobility-model.h index 8b07040d5..7409b97e6 100644 --- a/src/node/random-waypoint-mobility-model.h +++ b/src/node/random-waypoint-mobility-model.h @@ -52,6 +52,8 @@ private: class RandomWaypointMobilityModel : public MobilityModel { public: + static const ClassId cid; + RandomWaypointMobilityModel (); RandomWaypointMobilityModel (Ptr parameters); private: From e896c2576be8657a208fc788a1b1579bea4619f7 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 18 Jul 2007 17:06:36 +0200 Subject: [PATCH 062/148] make sure we schedule the start event correctly --- src/node/random-waypoint-mobility-model.cc | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/node/random-waypoint-mobility-model.cc b/src/node/random-waypoint-mobility-model.cc index b83f515c0..8e0460414 100644 --- a/src/node/random-waypoint-mobility-model.cc +++ b/src/node/random-waypoint-mobility-model.cc @@ -83,7 +83,9 @@ RandomWaypointMobilityModelParameters::GetCurrent (void) RandomWaypointMobilityModel::RandomWaypointMobilityModel () : m_parameters (RandomWaypointMobilityModelParameters::GetCurrent ()) -{} +{ + Simulator::ScheduleNow (&RandomWaypointMobilityModel::Start, this); +} RandomWaypointMobilityModel::RandomWaypointMobilityModel (Ptr parameters) : m_parameters (parameters) @@ -118,7 +120,7 @@ RandomWaypointMobilityModel::DoSet (const Position &position) { m_helper.InitializePosition (position); Simulator::Remove (m_event); - Start (); + Simulator::ScheduleNow (&RandomWaypointMobilityModel::Start, this); } From d0d364d72952e7fedb044e264cd157884a02d9cf Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 18 Jul 2007 17:07:15 +0200 Subject: [PATCH 063/148] add Rectangle class and use it. --- src/core/rectangle-default-value.cc | 59 +++++++++++------------------ src/core/rectangle-default-value.h | 17 +++------ src/core/rectangle.cc | 40 +++++++++++++++++++ src/core/rectangle.h | 40 +++++++++++++++++++ src/node/position.cc | 9 +++++ src/node/position.h | 3 ++ 6 files changed, 120 insertions(+), 48 deletions(-) create mode 100644 src/core/rectangle.cc create mode 100644 src/core/rectangle.h diff --git a/src/core/rectangle-default-value.cc b/src/core/rectangle-default-value.cc index 96fa63be7..1526f5a8e 100644 --- a/src/core/rectangle-default-value.cc +++ b/src/core/rectangle-default-value.cc @@ -28,37 +28,29 @@ RectangleDefaultValue::RectangleDefaultValue (std::string name, double xMin, double xMax, double yMin, double yMax) : DefaultValueBase (name, help), - m_xMinDefault (xMin), - m_xMaxDefault (xMax), - m_yMinDefault (yMin), - m_yMaxDefault (yMax), - m_xMin (xMin), - m_xMax (xMax), - m_yMin (yMin), - m_yMax (yMax) + m_default (xMin, xMax, yMin, yMax), + m_rectangle (xMin, xMax, yMin, yMax) { DefaultValueList::Add (this); } -double -RectangleDefaultValue::GetMinX (void) const +Rectangle +RectangleDefaultValue::GetValue (void) const { - return m_xMin; + return m_rectangle; } -double -RectangleDefaultValue::GetMinY (void) const +double +RectangleDefaultValue::ReadDouble (std::string str, bool &ok) { - return m_yMin; -} -double -RectangleDefaultValue::GetMaxX (void) const -{ - return m_xMax; -} -double -RectangleDefaultValue::GetMaxY (void) const -{ - return m_yMax; + double value; + std::istringstream iss; + iss.str (str); + iss >> value; + if (iss.bad () || iss.fail ()) + { + ok = false; + } + return value; } bool RectangleDefaultValue::DoParseValue (const std::string &value) @@ -77,17 +69,12 @@ RectangleDefaultValue::DoParseValue (const std::string &value) std::string yMinString = value.substr (yMinStart, yMinEnd); std::string yMaxString = value.substr (yMaxStart, yMaxEnd); - std::istringstream iss; - iss.str (xMinString); - iss >> m_xMin; - iss.str (xMaxString); - iss >> m_xMax; - iss.str (yMinString); - iss >> m_yMin; - iss.str (yMaxString); - iss >> m_yMax; - - return !iss.bad () && !iss.fail (); + bool ok = true; + m_rectangle.xMin = ReadDouble (xMinString, ok); + m_rectangle.yMin = ReadDouble (yMinString, ok); + m_rectangle.xMax = ReadDouble (xMaxString, ok); + m_rectangle.yMax = ReadDouble (yMaxString, ok); + return ok; } std::string RectangleDefaultValue::DoGetType (void) const @@ -98,7 +85,7 @@ std::string RectangleDefaultValue::DoGetDefaultValue (void) const { std::ostringstream oss; - oss << m_xMinDefault << ":" << m_xMaxDefault << ":" << m_yMinDefault << ":" << m_yMaxDefault; + oss << m_default.xMin << ":" << m_default.xMax << ":" << m_default.yMin << ":" << m_default.yMax; return oss.str (); } diff --git a/src/core/rectangle-default-value.h b/src/core/rectangle-default-value.h index f81388780..9e0448290 100644 --- a/src/core/rectangle-default-value.h +++ b/src/core/rectangle-default-value.h @@ -23,6 +23,7 @@ #include #include "default-value.h" +#include "rectangle.h" namespace ns3 { @@ -34,23 +35,15 @@ class RectangleDefaultValue : public DefaultValueBase double xMin, double xMax, double yMin, double yMax); - double GetMinX (void) const; - double GetMinY (void) const; - double GetMaxX (void) const; - double GetMaxY (void) const; + Rectangle GetValue (void) const; private: + double ReadDouble (std::string str, bool &ok); virtual bool DoParseValue (const std::string &value); virtual std::string DoGetType (void) const; virtual std::string DoGetDefaultValue (void) const; - double m_xMinDefault; - double m_xMaxDefault; - double m_yMinDefault; - double m_yMaxDefault; - double m_xMin; - double m_xMax; - double m_yMin; - double m_yMax; + Rectangle m_default; + Rectangle m_rectangle; }; } // namespace ns3 diff --git a/src/core/rectangle.cc b/src/core/rectangle.cc new file mode 100644 index 000000000..9fdd739d5 --- /dev/null +++ b/src/core/rectangle.cc @@ -0,0 +1,40 @@ +/* -*- 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 "rectangle.h" + +namespace ns3 { + +Rectangle::Rectangle (double _xMin, double _xMax, + double _yMin, double _yMax) + : xMin (_xMin), + xMax (_xMax), + yMin (_yMin), + yMax (_yMax) +{} + +Rectangle::Rectangle () + : xMin (0.0), + xMax (0.0), + yMin (0.0), + yMax (0.0) +{} + +} // namespace ns3 diff --git a/src/core/rectangle.h b/src/core/rectangle.h new file mode 100644 index 000000000..b108f9c21 --- /dev/null +++ b/src/core/rectangle.h @@ -0,0 +1,40 @@ +/* -*- 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 RECTANGLE_H +#define RECTANGLE_H + +namespace ns3 { + +class Rectangle +{ +public: + Rectangle (double _xMin, double _xMax, + double _yMin, double _yMax); + Rectangle (); + double xMin; + double xMax; + double yMin; + double yMax; +}; + +} // namespace ns3 + +#endif /* RECTANGLE_H */ diff --git a/src/node/position.cc b/src/node/position.cc index 802fe3bb7..b4efadb7f 100644 --- a/src/node/position.cc +++ b/src/node/position.cc @@ -1,4 +1,5 @@ #include "position.h" +#include "ns3/rectangle.h" #include namespace ns3 { @@ -16,6 +17,14 @@ Position::Position () z (0.0) {} +bool +Position::IsInside (const Rectangle &rectangle) const +{ + return + x <= rectangle.xMax && x >= rectangle.xMin && + y <= rectangle.yMax && y >= rectangle.yMin; +} + double CalculateDistance (const Position &a, const Position &b) { diff --git a/src/node/position.h b/src/node/position.h index 261ba2e99..7a97dede7 100644 --- a/src/node/position.h +++ b/src/node/position.h @@ -3,11 +3,14 @@ namespace ns3 { +class Rectangle; + class Position { public: Position (double x, double y, double z); Position (); + bool IsInside (const Rectangle &rectangle) const; double x; double y; double z; From bda63626d227d37c6eddc435fa63b2c5d2c6ff8b Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 18 Jul 2007 17:07:25 +0200 Subject: [PATCH 064/148] a real random walk model --- SConstruct | 8 +- src/node/random-walk-2d-mobility-model.cc | 179 ++++++++++++++++++++++ src/node/random-walk-2d-mobility-model.h | 123 +++++++++++++++ src/node/static-speed-helper.cc | 98 +++++++++++- src/node/static-speed-helper.h | 9 ++ 5 files changed, 411 insertions(+), 6 deletions(-) create mode 100644 src/node/random-walk-2d-mobility-model.cc create mode 100644 src/node/random-walk-2d-mobility-model.h diff --git a/SConstruct b/SConstruct index 82e530348..c854eb3d8 100644 --- a/SConstruct +++ b/SConstruct @@ -64,6 +64,7 @@ core.add_sources([ 'default-value.cc', 'random-variable-default-value.cc', 'rectangle-default-value.cc', + 'rectangle.cc', 'command-line.cc', 'type-name.cc', 'component-manager.cc', @@ -97,6 +98,7 @@ core.add_inst_headers([ 'default-value.h', 'random-variable-default-value.h', 'rectangle-default-value.h', + 'rectangle.h', 'command-line.h', 'type-name.h', 'component-manager.h', @@ -255,8 +257,7 @@ node.add_sources ([ 'grid-topology.cc', 'random-topology.cc', 'random-walk-mobility-model.cc', - 'random-mobility-model.cc', - 'random-direction-mobility-model.cc', + 'random-walk-2d-mobility-model.cc', 'hierarchical-mobility-model.cc', 'ns2-mobility-file-topology.cc', 'mobility-model-helper.cc', @@ -290,8 +291,7 @@ node.add_inst_headers ([ 'grid-topology.h', 'random-topology.h', 'random-walk-mobility-model.h', - 'random-mobility-model.h', - 'random-direction-mobility-model.h', + 'random-walk-2d-mobility-model.h', 'hierarchical-mobility-model.h', 'ns2-mobility-file-topology.h', 'mobility-model-helper.h', diff --git a/src/node/random-walk-2d-mobility-model.cc b/src/node/random-walk-2d-mobility-model.cc new file mode 100644 index 000000000..d2267286e --- /dev/null +++ b/src/node/random-walk-2d-mobility-model.cc @@ -0,0 +1,179 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2006,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 "random-walk-2d-mobility-model.h" +#include "ns3/default-value.h" +#include "ns3/time-default-value.h" +#include "ns3/rectangle-default-value.h" +#include "ns3/random-variable-default-value.h" +#include "ns3/simulator.h" +#include "ns3/debug.h" +#include + +NS_DEBUG_COMPONENT_DEFINE ("RandomWalk2d"); + +namespace ns3 { + +const ClassId RandomWalk2dMobilityModel::cid = + MakeClassId ("RandomWalk2dMobilityModel", RandomWalk2dMobilityModel::iid); + + +static EnumDefaultValue +g_mode ("RandomWalk2dMode", + "The mode indicates the condition used to " + "change the current speed and direction", + RandomWalk2dMobilityModelParameters::MODE_DISTANCE, "Distance", + RandomWalk2dMobilityModelParameters::MODE_TIME, "Time", + 0, 0); + +static IntegerDefaultValue +g_modeDistance ("RandomWalk2dDistance", + "Change current direction and speed after moving this distance.", + 2.0); + +static TimeDefaultValue +g_modeTime ("RandomWalk2dDelay", + "Change current direction and speed after moving for this delay.", + Seconds (1.0)); + +static RandomVariableDefaultValue +g_speed ("RandomWalk2dSpeed", + "A random variable used to pick the speed.", + "Uniform:2:4"); +static RandomVariableDefaultValue +g_direction ("RandomWalk2dDirection", + "A random variable used to pick the direction (gradients).", + "Uniform:0.0:6.283184"); + +static RectangleDefaultValue +g_rectangle ("RandomWalk2dBounds", + "Bounds of the area to cruise.", + 0.0, 0.0, 100.0, 100.0); + +RandomWalk2dMobilityModelParameters::RandomWalk2dMobilityModelParameters () + : m_mode (g_mode.GetValue ()), + m_modeDistance (g_modeDistance.GetValue ()), + m_modeTime (g_modeTime.GetValue ()), + m_speed (g_speed.GetCopy ()), + m_direction (g_direction.GetCopy ()), + m_bounds (g_rectangle.GetValue ()) +{} + +RandomWalk2dMobilityModelParameters::~RandomWalk2dMobilityModelParameters () +{ + delete m_speed; + delete m_direction; + m_speed = 0; + m_direction = 0; +} + +Ptr +RandomWalk2dMobilityModelParameters::GetCurrent (void) +{ + static Ptr parameters = 0; + if (parameters == 0 || + g_speed.IsDirty () || + g_direction.IsDirty () || + g_mode.IsDirty () || + g_modeDistance.IsDirty () || + g_modeTime.IsDirty () || + g_rectangle.IsDirty ()) + { + parameters = Create (); + } + return parameters; +} + +RandomWalk2dMobilityModel::RandomWalk2dMobilityModel () + : m_parameters (RandomWalk2dMobilityModelParameters::GetCurrent ()) +{ + SetInterfaceId (RandomWalk2dMobilityModel::iid); + Simulator::ScheduleNow (&RandomWalk2dMobilityModel::Start, this); +} + +void +RandomWalk2dMobilityModel::Start (void) +{ + double speed = m_parameters->m_speed->GetValue (); + double direction = m_parameters->m_direction->GetValue (); + Speed vector (std::cos (direction) * speed, + std::sin (direction) * speed, + 0.0); + m_helper.Reset (vector); + + Time delayLeft; + if (m_parameters->m_mode == RandomWalk2dMobilityModelParameters::MODE_TIME) + { + delayLeft = m_parameters->m_modeTime; + } + else + { + delayLeft = Seconds (m_parameters->m_modeDistance / speed); + } + DoWalk (delayLeft); +} + +void +RandomWalk2dMobilityModel::DoWalk (Time delayLeft) +{ + Time delay = m_helper.GetDelayToNextPosition (m_parameters->m_bounds, + delayLeft); + if (delay < delayLeft) + { + m_event = Simulator::Schedule (delay, &RandomWalk2dMobilityModel::Rebound, this, + delayLeft - delay); + } + else + { + NS_ASSERT (delay == delayLeft); + m_event = Simulator::Schedule (delay, &RandomWalk2dMobilityModel::Start, this); + } +} + +void +RandomWalk2dMobilityModel::Rebound (Time delayLeft) +{ + m_helper.Rebound (m_parameters->m_bounds); + DoWalk (delayLeft); +} + +void +RandomWalk2dMobilityModel::DoDispose (void) +{ + m_parameters = 0; + // chain up + MobilityModel::DoDispose (); +} +Position +RandomWalk2dMobilityModel::DoGet (void) const +{ + return m_helper.GetCurrentPosition (m_parameters->m_bounds); +} +void +RandomWalk2dMobilityModel::DoSet (const Position &position) +{ + NS_ASSERT (position.IsInside (m_parameters->m_bounds)); + m_helper.InitializePosition (position); + Simulator::Remove (m_event); + Simulator::ScheduleNow (&RandomWalk2dMobilityModel::Start, this); +} + + +} // namespace ns3 diff --git a/src/node/random-walk-2d-mobility-model.h b/src/node/random-walk-2d-mobility-model.h new file mode 100644 index 000000000..d30ebfb87 --- /dev/null +++ b/src/node/random-walk-2d-mobility-model.h @@ -0,0 +1,123 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2006,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 RANDOM_WALK_2D_MOBILITY_MODEL_H +#define RANDOM_WALK_2D_MOBILITY_MODEL_H + +#include "ns3/object.h" +#include "ns3/nstime.h" +#include "ns3/component-manager.h" +#include "ns3/event-id.h" +#include "ns3/rectangle.h" +#include "mobility-model.h" +#include "static-speed-helper.h" + +namespace ns3 { + +class RandomVariable; + +/** + * \brief parameters to control a random walk model + */ +class RandomWalk2dMobilityModelParameters : public Object +{ + public: + /** + * Instantiate a set of RandomWalk parameters initialized + * with the Bind default values. + */ + RandomWalk2dMobilityModelParameters (); + virtual ~RandomWalk2dMobilityModelParameters (); + /** + */ + void SetSpeed (const RandomVariable &speed); + void SetDirection (const RandomVariable &direction); + /** + * \param distance the distance before a direction change + * + * Unit is meters + */ + void SetModeDistance (double distance); + /** + * \param time the delay before a direction change. + */ + void SetModeTime (Time time); + + + enum Mode { + MODE_DISTANCE, + MODE_TIME + }; + private: + friend class RandomWalk2dMobilityModel; + static Ptr GetCurrent (void); + + enum Mode m_mode; + double m_modeDistance; + Time m_modeTime; + RandomVariable *m_speed; + RandomVariable *m_direction; + Rectangle m_bounds; +}; + +/** + * \brief a 2D random walk position model + * + * Each instance moves with a speed and direction choosen at random + * in the intervals [minspeed,maxspeed] and [0,2pi] until + * either a fixed distance has been walked or until a fixed amount + * of time. + * + * The parameters of the model can be specified either with the ns3::Bind + * function and the variables "RandomWalk2dMinSpeed", "RandomWalk2dMaxSpeed", + * "RandomWalk2dMode", "RandomWalk2dModeDistance", and, "RandomWalk2dModeTime" or + * with an instance of the RandomWalk2dMobilityModelParameters class which + * must be fed to the RandomWalk2dMobilityModel constructors. + */ +class RandomWalk2dMobilityModel : public MobilityModel +{ + public: + static const ClassId cid; + /** + * Create a new position object located at position (0,0,0) + */ + RandomWalk2dMobilityModel (); + /** + * Create a new position object located at position (0,0,0) + */ + RandomWalk2dMobilityModel (Ptr parameters); + + private: + void Start (void); + void Rebound (Time timeLeft); + void DoWalk (Time timeLeft); + virtual void DoDispose (void); + virtual Position DoGet (void) const; + virtual void DoSet (const Position &position); + + StaticSpeedHelper m_helper; + EventId m_event; + Ptr m_parameters; +}; + + +} // namespace ns3 + +#endif /* RANDOM_WALK_2D_MOBILITY_MODEL_H */ diff --git a/src/node/static-speed-helper.cc b/src/node/static-speed-helper.cc index ed66d63b1..c56eb5dd9 100644 --- a/src/node/static-speed-helper.cc +++ b/src/node/static-speed-helper.cc @@ -1,4 +1,5 @@ #include "ns3/simulator.h" +#include "ns3/rectangle.h" #include "static-speed-helper.h" namespace ns3 { @@ -18,8 +19,7 @@ StaticSpeedHelper::InitializePosition (const Position &position) void StaticSpeedHelper::Reset (const Speed &speed, const Time &pauseDelay) { - Update (); - m_speed = speed; + Reset (speed); m_pauseEnd = Simulator::Now () + pauseDelay; } @@ -57,4 +57,98 @@ StaticSpeedHelper::Update (void) const m_position.z += m_speed.dz * deltaS; } +void +StaticSpeedHelper::Reset (const Speed &speed) +{ + Update (); + m_speed = speed; + m_pauseEnd = Simulator::Now (); +} +Position +StaticSpeedHelper::IntersectCurrentDirection (const Rectangle &bounds) +{ + double xMaxY = m_position.y + (bounds.xMax - m_position.x) / m_speed.dx * m_speed.dy; + double xMinY = m_position.y + (bounds.xMin - m_position.x) / m_speed.dx * m_speed.dy; + double yMaxX = m_position.x + (bounds.yMax - m_position.y) / m_speed.dy * m_speed.dx; + double yMinX = m_position.x + (bounds.yMin - m_position.y) / m_speed.dy * m_speed.dx; + bool xMaxOk = xMaxY <= bounds.yMax && xMaxY >= bounds.yMin; + bool xMinOk = xMinY <= bounds.yMax && xMinY >= bounds.yMin; + bool yMaxOk = yMaxX <= bounds.xMax && yMaxX >= bounds.xMin; + bool yMinOk = yMinX <= bounds.xMax && yMinX >= bounds.xMin; + if (xMaxOk && m_speed.dx >= 0) + { + return Position (bounds.xMax, xMaxY, 0.0); + } + else if (xMinOk && m_speed.dx <= 0) + { + return Position (bounds.xMin, xMinY, 0.0); + } + else if (yMaxOk && m_speed.dy >= 0) + { + return Position (yMaxX, bounds.yMax, 0.0); + } + else if (yMinOk && m_speed.dy <= 0) + { + return Position (yMinX, bounds.yMin, 0.0); + } + else + { + NS_ASSERT (false); + // quiet compiler + return Position (0.0, 0.0, 0.0); + } +} +Time +StaticSpeedHelper::GetDelayToNextPosition (const Rectangle &bounds, Time delayLeft) +{ + UpdateFull (bounds); + Position nextPosition = m_position; + nextPosition.x += m_speed.dx * delayLeft.GetSeconds (); + nextPosition.y += m_speed.dy * delayLeft.GetSeconds (); + if (nextPosition.IsInside (bounds)) + { + return delayLeft; + } + nextPosition = IntersectCurrentDirection (bounds); + Time delay = Seconds ((nextPosition.x - m_position.x) / m_speed.dx); + return delay; +} +void +StaticSpeedHelper::Rebound (const Rectangle &bounds) +{ + UpdateFull (bounds); + double xMinDist = m_position.x - bounds.xMin; + double xMaxDist = bounds.xMax - m_position.x; + double yMinDist = m_position.y - bounds.yMin; + double yMaxDist = bounds.yMax - m_position.y; + double minX = std::min (xMinDist, xMaxDist); + double minY = std::min (yMinDist, yMaxDist); + if (minY < minX) + { + m_speed.dy = - m_speed.dy; + } + else + { + m_speed.dx = - m_speed.dx; + } +} + +void +StaticSpeedHelper::UpdateFull (const Rectangle &bounds) const +{ + Update (); + m_position.x = std::min (bounds.xMax, m_position.x); + m_position.x = std::max (bounds.xMin, m_position.x); + m_position.y = std::min (bounds.yMax, m_position.y); + m_position.y = std::max (bounds.yMin, m_position.y); +} + +Position +StaticSpeedHelper::GetCurrentPosition (const Rectangle &bounds) const +{ + UpdateFull (bounds); + return m_position; +} + + } // namespace ns3 diff --git a/src/node/static-speed-helper.h b/src/node/static-speed-helper.h index cd651c951..aae5abbc8 100644 --- a/src/node/static-speed-helper.h +++ b/src/node/static-speed-helper.h @@ -7,6 +7,8 @@ namespace ns3 { +class Rectangle; + class StaticSpeedHelper { public: @@ -15,11 +17,18 @@ class StaticSpeedHelper void Reset (const Speed &speed, const Time &pauseDelay); + void Reset (const Speed &speed); + Time GetDelayToNextPosition (const Rectangle &bounds, Time delayLeft); + void Rebound (const Rectangle &bounds); + Position GetCurrentPosition (const Rectangle &bounds) const; + Position GetCurrentPosition (void) const; Speed GetCurrentSpeed (void) const; private: void Update (void) const; + void UpdateFull (const Rectangle &rectangle) const; + Position IntersectCurrentDirection (const Rectangle &bounds); mutable Time m_lastUpdate; mutable Position m_position; Speed m_speed; From 41920cc74fe00b407e45a43f6231ae48defb64b2 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 18 Jul 2007 17:44:32 +0200 Subject: [PATCH 065/148] add debugging output --- src/core/random-variable-default-value.cc | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/core/random-variable-default-value.cc b/src/core/random-variable-default-value.cc index df36d02ba..689d05d2e 100644 --- a/src/core/random-variable-default-value.cc +++ b/src/core/random-variable-default-value.cc @@ -19,6 +19,9 @@ * Author: Mathieu Lacage */ #include "random-variable-default-value.h" +#include "ns3/debug.h" + +NS_DEBUG_COMPONENT_DEFINE ("RandomVariableDefaultValue"); namespace ns3 { @@ -69,6 +72,7 @@ RandomVariableDefaultValue::Parse (const std::string &value, if (type == "Constant") { double constant = ReadAsDouble (v, ok); + NS_DEBUG ("Constant constant=" << constant); if (mustCreate) { *pVariable = new ConstantVariable (constant); @@ -88,6 +92,7 @@ RandomVariableDefaultValue::Parse (const std::string &value, double maxVal; minVal = ReadAsDouble (min, ok); maxVal = ReadAsDouble (max, ok); + NS_DEBUG ("Uniform min=" << min << ", max=" << max); if (mustCreate) { *pVariable = new UniformVariable (minVal, maxVal); From 5ef0970789dce42b5a6ec11499c66e7fd2b44a3b Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 18 Jul 2007 17:45:07 +0200 Subject: [PATCH 066/148] add disc center position and debugging --- src/node/random-position.cc | 34 +++++++++++++++++++++++++++------- src/node/random-position.h | 5 ++++- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/node/random-position.cc b/src/node/random-position.cc index f0c126a0b..9ae038a79 100644 --- a/src/node/random-position.cc +++ b/src/node/random-position.cc @@ -1,8 +1,12 @@ #include "random-position.h" #include "ns3/random-variable.h" +#include "ns3/default-value.h" #include "ns3/random-variable-default-value.h" +#include "ns3/debug.h" #include +NS_DEBUG_COMPONENT_DEFINE ("RandomPosition"); + namespace ns3 { static RandomVariableDefaultValue @@ -16,15 +20,25 @@ g_rectangleY ("RandomRectanglePositionY", "Uniform:0:200"); static RandomVariableDefaultValue -g_discTheta ("RandomRectanglePositionTheta", +g_discTheta ("RandomDiscPositionTheta", "A random variable which represents the angle (gradients) of a position in a random disc.", "Uniform:0:3.1415"); static RandomVariableDefaultValue -g_discRho ("RandomRectanglePositionRho", +g_discRho ("RandomDiscPositionRho", "A random variable which represents the radius of a position in a random disc.", "Uniform:0:200"); +static IntegerDefaultValue +g_discX ("RandomDiscPositionX", + "The x coordinate of the center of the random position disc.", + 0.0); + +static IntegerDefaultValue +g_discY ("RandomDiscPositionY", + "The y coordinate of the center of the random position disc.", + 0.0); + const InterfaceId RandomPosition::iid = MakeInterfaceId ("RandomPosition", Object::iid); const ClassId RandomRectanglePosition::cid = @@ -68,12 +82,17 @@ RandomRectanglePosition::Get (void) const RandomDiscPosition::RandomDiscPosition () : m_theta (g_discTheta.GetCopy ()), - m_rho (g_discRho.GetCopy ()) + m_rho (g_discRho.GetCopy ()), + m_x (g_discX.GetValue ()), + m_y (g_discY.GetValue ()) {} RandomDiscPosition::RandomDiscPosition (const RandomVariable &theta, - const RandomVariable &rho) + const RandomVariable &rho, + double x, double y) : m_theta (theta.Copy ()), - m_rho (rho.Copy ()) + m_rho (rho.Copy ()), + m_x (0.0), + m_y (0.0) {} RandomDiscPosition::~RandomDiscPosition () { @@ -87,8 +106,9 @@ RandomDiscPosition::Get (void) const { double theta = m_theta->GetValue (); double rho = m_rho->GetValue (); - double x = std::cos (theta) * rho; - double y = std::sin (theta) * rho; + double x = m_x + std::cos (theta) * rho; + double y = m_y + std::sin (theta) * rho; + NS_DEBUG ("Disc position x=" << x << ", y=" << y); return Position (x, y, 0.0); } diff --git a/src/node/random-position.h b/src/node/random-position.h index 6c1609642..98a771c44 100644 --- a/src/node/random-position.h +++ b/src/node/random-position.h @@ -38,12 +38,15 @@ public: static const ClassId cid; RandomDiscPosition (); RandomDiscPosition (const RandomVariable &theta, - const RandomVariable &rho); + const RandomVariable &rho, + double x, double y); virtual ~RandomDiscPosition (); virtual Position Get (void) const; private: RandomVariable *m_theta; RandomVariable *m_rho; + double m_x; + double m_y; }; } // namespace ns3 From 83cbc1f4504369335331ae4b7d1321b2c8eae582 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 18 Jul 2007 17:45:35 +0200 Subject: [PATCH 067/148] be consistant with the rest of the code --- src/node/random-walk-2d-mobility-model.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node/random-walk-2d-mobility-model.cc b/src/node/random-walk-2d-mobility-model.cc index d2267286e..b5181c3cb 100644 --- a/src/node/random-walk-2d-mobility-model.cc +++ b/src/node/random-walk-2d-mobility-model.cc @@ -49,7 +49,7 @@ g_modeDistance ("RandomWalk2dDistance", 2.0); static TimeDefaultValue -g_modeTime ("RandomWalk2dDelay", +g_modeTime ("RandomWalk2dTime", "Change current direction and speed after moving for this delay.", Seconds (1.0)); From 9bc8eb952bf0561ed92f7fe04ebda9c3b5fbe106 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 18 Jul 2007 17:45:53 +0200 Subject: [PATCH 068/148] fix typos --- src/node/random-topology.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/node/random-topology.cc b/src/node/random-topology.cc index fa925c32c..a39a382f1 100644 --- a/src/node/random-topology.cc +++ b/src/node/random-topology.cc @@ -26,13 +26,13 @@ namespace ns3 { static ClassIdDefaultValue -g_position ("RandomTopologyType", +g_position ("RandomTopologyPositionType", "The type of initial random position in a 3d topology.", RandomPosition::iid, - "Rectangle"); + "RandomRectanglePosition"); static ClassIdDefaultValue -g_mobility ("RandomTopologyMobilityModelType", +g_mobility ("RandomTopologyMobilityType", "The type of mobility model attached to an object in a 3d topology.", MobilityModel::iid, "StaticMobilityModel"); From 2ad239b6c917d0aeeff4420f44afb8ff846bebc6 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 18 Jul 2007 17:52:18 +0200 Subject: [PATCH 069/148] more debugging, make sure to save value string if it is value after parsing. --- src/core/random-variable-default-value.cc | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/core/random-variable-default-value.cc b/src/core/random-variable-default-value.cc index 689d05d2e..6417e7c01 100644 --- a/src/core/random-variable-default-value.cc +++ b/src/core/random-variable-default-value.cc @@ -72,11 +72,15 @@ RandomVariableDefaultValue::Parse (const std::string &value, if (type == "Constant") { double constant = ReadAsDouble (v, ok); - NS_DEBUG ("Constant constant=" << constant); if (mustCreate) { + NS_DEBUG ("create Constant constant=" << constant); *pVariable = new ConstantVariable (constant); } + else + { + NS_DEBUG ("parse Constant constant=" << constant); + } return ok; } else if (type == "Uniform") @@ -92,11 +96,15 @@ RandomVariableDefaultValue::Parse (const std::string &value, double maxVal; minVal = ReadAsDouble (min, ok); maxVal = ReadAsDouble (max, ok); - NS_DEBUG ("Uniform min=" << min << ", max=" << max); if (mustCreate) { + NS_DEBUG ("create Uniform min=" << min << ", max=" << max); *pVariable = new UniformVariable (minVal, maxVal); } + else + { + NS_DEBUG ("parse Uniform min=" << min << ", max=" << max); + } return ok; } else @@ -108,7 +116,12 @@ RandomVariableDefaultValue::Parse (const std::string &value, bool RandomVariableDefaultValue::DoParseValue (const std::string &value) { - return Parse (value, false, 0); + bool ok = Parse (value, false, 0); + if (ok) + { + m_value = value; + } + return ok; } std::string RandomVariableDefaultValue::DoGetType (void) const From 9c46074145f413aebb1d7ae3766a5bb7aec8576e Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 18 Jul 2007 17:55:58 +0200 Subject: [PATCH 070/148] do not try to remove invalid events --- src/simulator/simulator.cc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/simulator/simulator.cc b/src/simulator/simulator.cc index 226f7357a..6467969b9 100644 --- a/src/simulator/simulator.cc +++ b/src/simulator/simulator.cc @@ -248,6 +248,10 @@ SimulatorPrivate::Now (void) const void SimulatorPrivate::Remove (EventId ev) { + if (IsExpired (ev)) + { + return; + } Scheduler::EventKey key; EventImpl *impl = m_events->Remove (ev, &key); delete impl; From 1ac757bd601449bbeb61d8fdc4140a8f94ed0a86 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 18 Jul 2007 17:56:43 +0200 Subject: [PATCH 071/148] get rid of old buggy random walk model --- SConstruct | 2 - samples/main-random-walk.cc | 38 +++-- src/node/random-walk-mobility-model.cc | 193 ------------------------- src/node/random-walk-mobility-model.h | 131 ----------------- 4 files changed, 27 insertions(+), 337 deletions(-) delete mode 100644 src/node/random-walk-mobility-model.cc delete mode 100644 src/node/random-walk-mobility-model.h diff --git a/SConstruct b/SConstruct index c854eb3d8..2fea9dad1 100644 --- a/SConstruct +++ b/SConstruct @@ -256,7 +256,6 @@ node.add_sources ([ 'static-speed-mobility-model.cc', 'grid-topology.cc', 'random-topology.cc', - 'random-walk-mobility-model.cc', 'random-walk-2d-mobility-model.cc', 'hierarchical-mobility-model.cc', 'ns2-mobility-file-topology.cc', @@ -290,7 +289,6 @@ node.add_inst_headers ([ 'static-speed-mobility-model.h', 'grid-topology.h', 'random-topology.h', - 'random-walk-mobility-model.h', 'random-walk-2d-mobility-model.h', 'hierarchical-mobility-model.h', 'ns2-mobility-file-topology.h', diff --git a/samples/main-random-walk.cc b/samples/main-random-walk.cc index 295a89c74..1aed4ced9 100644 --- a/samples/main-random-walk.cc +++ b/samples/main-random-walk.cc @@ -1,9 +1,12 @@ /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +#include + #include "ns3/ptr.h" #include "ns3/mobility-model.h" #include "ns3/mobility-model-notifier.h" -#include "ns3/random-walk-mobility-model.h" +#include "ns3/random-walk-2d-mobility-model.h" +#include "ns3/random-topology.h" #include "ns3/default-value.h" #include "ns3/command-line.h" #include "ns3/simulator.h" @@ -21,20 +24,33 @@ CourseChange (Ptr position) int main (int argc, char *argv[]) { - Bind ("RandomWalkMinSpeed", "2"); - Bind ("RandomWalkMaxSpeed", "3"); - Bind ("RandomWalkMode", "Time"); - Bind ("RandomWalkModeDistance", "20"); - Bind ("RandomWalkModeTime", "2s"); + Bind ("RandomWalk2dMode", "Time"); + Bind ("RandomWalk2dTime", "2s"); + Bind ("RandomWalk2dSpeed", "Constant:1.0"); + Bind ("RandomWalk2dBounds", "0:200:0:100"); + + Bind ("RandomDiscPositionX", "100"); + Bind ("RandomDiscPositionY", "50"); + Bind ("RandomDiscPositionRho", "Uniform:0:30"); + + Bind ("RandomTopologyPositionType", "RandomDiscPosition"); + Bind ("RandomTopologyMobilityType", "RandomWalk2dMobilityModel"); CommandLine::Parse (argc, argv); - Ptr notifier = Create (); - Ptr position = Create (); - position->AddInterface (notifier); - notifier->RegisterListener (MakeCallback (&CourseChange)); + RandomTopology topology; - Simulator::StopAt (Seconds (20.0)); + std::vector > objects; + for (uint32_t i = 0; i < 100; i++) + { + Ptr notifier = Create (); + notifier->RegisterListener (MakeCallback (&CourseChange)); + objects.push_back (notifier); + } + + topology.Layout (objects.begin (), objects.end ()); + + Simulator::StopAt (Seconds (100.0)); Simulator::Run (); diff --git a/src/node/random-walk-mobility-model.cc b/src/node/random-walk-mobility-model.cc deleted file mode 100644 index 8e288a31c..000000000 --- a/src/node/random-walk-mobility-model.cc +++ /dev/null @@ -1,193 +0,0 @@ -/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ -/* - * Copyright (c) 2006,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 "random-walk-mobility-model.h" -#include "ns3/default-value.h" -#include "ns3/time-default-value.h" -#include "ns3/simulator.h" -#include "ns3/debug.h" -#include - -NS_DEBUG_COMPONENT_DEFINE ("RandomWalk"); - -namespace ns3 { - -const InterfaceId RandomWalkMobilityModel::iid = - MakeInterfaceId ("RandomWalkMobilityModel", MobilityModel::iid); -const ClassId RandomWalkMobilityModel::cid = - MakeClassId ("RandomWalkMobilityModel", RandomWalkMobilityModel::iid); - - -static IntegerDefaultValue g_minSpeed ("RandomWalkMinSpeed", - "Minimum speed used during a random walk", - 0.1); -static IntegerDefaultValue g_maxSpeed ("RandomWalkMaxSpeed", - "Maximum speed used during a random walk", - 0.5); -static EnumDefaultValue g_mode - ("RandomWalkMode", - "The mode indicates the condition used to " - "change the current speed and direction", - RandomWalkMobilityModelParameters::MODE_DISTANCE, "Distance", - RandomWalkMobilityModelParameters::MODE_TIME, "Time", - 0, 0); -static IntegerDefaultValue g_modeDistance ("RandomWalkModeDistance", - "Distance to walk before changing direction and speed.", - 10); -static TimeDefaultValue g_modeTime ("RandomWalkModeTime", - "Time to walk before changing direction and speed.", - Seconds (1)); - -RandomWalkMobilityModelParameters::RandomWalkMobilityModelParameters () - : m_minSpeed (g_minSpeed.GetValue ()), - m_maxSpeed (g_maxSpeed.GetValue ()), - m_mode (g_mode.GetValue ()), - m_modeDistance (g_modeDistance.GetValue ()), - m_modeTime (g_modeTime.GetValue ()) -{} -bool -RandomWalkMobilityModelParameters::IsDefault (void) const -{ - if (m_minSpeed != g_minSpeed.GetValue () || - m_maxSpeed != g_maxSpeed.GetValue () || - m_mode != g_mode.GetValue () || - m_modeDistance != g_modeDistance.GetValue () || - m_modeTime != g_modeTime.GetValue ()) - { - return false; - } - return true; -} - -void -RandomWalkMobilityModelParameters::SetSpeedBounds (double minSpeed, double maxSpeed) -{ - m_minSpeed = minSpeed; - m_maxSpeed = maxSpeed; -} - - -UniformVariable RandomWalkMobilityModel::m_randomDirection (0.0, 2*3.141592); - -Ptr -RandomWalkMobilityModel::GetDefaultParameters (void) -{ - static Ptr parameters = Create (); - if (!parameters->IsDefault ()) - { - parameters = Create (); - } - return parameters; -} - -RandomWalkMobilityModel::RandomWalkMobilityModel () - : m_x (0.0), - m_y (0.0), - m_dx (0.0), - m_dy (0.0), - m_prevTime (Simulator::Now ()), - m_parameters (RandomWalkMobilityModel::GetDefaultParameters ()) -{ - SetInterfaceId (RandomWalkMobilityModel::iid); - Reset (); -} - -RandomWalkMobilityModel::RandomWalkMobilityModel (double x, double y) - : m_x (x), - m_y (y), - m_dx (0.0), - m_dy (0.0), - m_prevTime (Simulator::Now ()), - m_parameters (RandomWalkMobilityModel::GetDefaultParameters ()) -{ - SetInterfaceId (RandomWalkMobilityModel::iid); - Reset (); -} - -void -RandomWalkMobilityModel::Reset (void) -{ - Update (); - double speed = UniformVariable::GetSingleValue (m_parameters->m_minSpeed, - m_parameters->m_maxSpeed); - NS_DEBUG ("min="<< m_parameters->m_minSpeed << ", max=" << m_parameters->m_maxSpeed << - ", speed=" << speed); - double direction = m_randomDirection.GetValue (); - double dx = std::cos (direction) * speed; - double dy = std::sin (direction) * speed; - m_dx = dx; - m_dy = dy; - Time delay; - if (m_parameters->m_mode == RandomWalkMobilityModelParameters::MODE_TIME) - { - delay = m_parameters->m_modeTime; - } - else - { - double distance = m_parameters->m_modeDistance; - delay = Seconds (distance / sqrt (m_dx * m_dx + m_dy * m_dy)); - } - NotifyCourseChange (); - NS_DEBUG ("change speed at " << Simulator::Now () << " in " << delay); - Simulator::Schedule (delay, &RandomWalkMobilityModel::Reset, this); -} - -void -RandomWalkMobilityModel::Update (void) const -{ - Time deltaTime = Simulator::Now () - m_prevTime; - m_prevTime = Simulator::Now (); - double deltaS = deltaTime.GetSeconds (); - m_x += m_dx * deltaS; - m_y += m_dy * deltaS; -} - -void -RandomWalkMobilityModel::DoDispose (void) -{ - m_parameters = 0; - // chain up - MobilityModel::DoDispose (); -} -Position -RandomWalkMobilityModel::DoGet (void) const -{ - Update (); - return Position (m_x, m_y, 0.0); -} -void -RandomWalkMobilityModel::DoSet (const Position &position) -{ - bool changed = false; - if (m_x != position.x || m_y != position.y) - { - changed = true; - } - m_x = position.x; - m_y = position.y; - m_prevTime = Simulator::Now (); - if (changed) - { - NotifyCourseChange (); - } -} - - -} // namespace ns3 diff --git a/src/node/random-walk-mobility-model.h b/src/node/random-walk-mobility-model.h deleted file mode 100644 index b2fef2953..000000000 --- a/src/node/random-walk-mobility-model.h +++ /dev/null @@ -1,131 +0,0 @@ -/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ -/* - * Copyright (c) 2006,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 RANDOM_WALK_MOBILITY_MODEL_H -#define RANDOM_WALK_MOBILITY_MODEL_H - -#include "ns3/object.h" -#include "ns3/mobility-model.h" -#include "ns3/nstime.h" -#include "ns3/random-variable.h" -#include "ns3/component-manager.h" - -namespace ns3 { - -/** - * \brief parameters to control a random walk model - */ -class RandomWalkMobilityModelParameters : public Object -{ - public: - enum Mode { - MODE_DISTANCE, - MODE_TIME - }; - /** - * Instantiate a set of RandomWalk parameters initialized - * with the Bind default values. - */ - RandomWalkMobilityModelParameters (); - /** - * \param minSpeed the minimum speed - * \param maxSpeed the maximum speed - * - * The speed of any node is chosen such that minSpeed <= speed <= maxSpeed - */ - void SetSpeedBounds (double minSpeed, double maxSpeed); - /** - * \param distance the distance before a direction change - * - * Unit is meters - */ - void SetModeDistance (double distance); - /** - * \param time the delay before a direction change. - */ - void SetModeTime (Time time); - private: - bool IsDefault (void) const; - friend class RandomWalkMobilityModel; - double m_minSpeed; - double m_maxSpeed; - enum Mode m_mode; - double m_modeDistance; - Time m_modeTime; -}; - -/** - * \brief an unbounded 2D random walk position model - * - * Each instance moves with a speed and direction choosen at random - * in the intervals [minspeed,maxspeed] and [0,2pi] until - * either a fixed distance has been walked or until a fixed amount - * of time. - * - * The parameters of the model can be specified either with the ns3::Bind - * function and the variables "RandomWalkMinSpeed", "RandomWalkMaxSpeed", - * "RandomWalkMode", "RandomWalkModeDistance", and, "RandomWalkModeTime" or - * with an instance of the RandomWalkMobilityModelParameters class which - * must be fed to the RandomWalkMobilityModel constructors. - */ -class RandomWalkMobilityModel : public MobilityModel -{ - public: - static const InterfaceId iid; - static const ClassId cid; - /** - * Create a new position object located at position (0,0,0) - */ - RandomWalkMobilityModel (); - /** - * Create a new position object located at position (x,y,0) - */ - RandomWalkMobilityModel (double x, double y); - /** - * Create a new position object located at position (0,0,0) - */ - RandomWalkMobilityModel (Ptr parameters); - /** - * Create a new position object located at position (x,y,0) - */ - RandomWalkMobilityModel (Ptr parameters, - double x, double y); - private: - virtual void DoDispose (void); - virtual Position DoGet (void) const; - virtual void DoSet (const Position &position); - - void Reset (void); - void Update (void) const; - static Ptr GetDefaultParameters (void); - static UniformVariable m_randomDirection; - - mutable double m_x; - mutable double m_y; - double m_dx; - double m_dy; - mutable Time m_prevTime; - Ptr m_parameters; -}; - - -} // namespace ns3 - -#endif /* RANDOM_WALK_MOBILITY_MODEL_H */ From e62ea4cdb4dd635722145d4c8c401b7bb099ce64 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 19 Jul 2007 08:29:48 +0200 Subject: [PATCH 072/148] add operator == and != to EventId --- src/simulator/event-id.cc | 13 +++++++++++++ src/simulator/event-id.h | 4 ++++ 2 files changed, 17 insertions(+) diff --git a/src/simulator/event-id.cc b/src/simulator/event-id.cc index 7357070b0..18f875935 100644 --- a/src/simulator/event-id.cc +++ b/src/simulator/event-id.cc @@ -70,5 +70,18 @@ EventId::GetUid (void) const return m_uid; } +bool operator == (const EventId &a, const EventId &b) +{ + return + a.m_uid == b.m_uid && + a.m_ts == b.m_ts && + a.m_eventImpl == b.m_eventImpl; +} +bool operator != (const EventId &a, const EventId &b) +{ + return !(a == b); +} + + }; // namespace ns3 diff --git a/src/simulator/event-id.h b/src/simulator/event-id.h index a7514289d..4350c9bbe 100644 --- a/src/simulator/event-id.h +++ b/src/simulator/event-id.h @@ -55,11 +55,15 @@ public: uint64_t GetTs (void) const; uint32_t GetUid (void) const; private: + friend bool operator == (const EventId &a, const EventId &b); EventImpl *m_eventImpl; uint64_t m_ts; uint32_t m_uid; }; +bool operator == (const EventId &a, const EventId &b); +bool operator != (const EventId &a, const EventId &b); + }; // namespace ns3 #endif /* EVENT_ID_H */ From a59e3bb3cdd7b8a0234824d83165647c309d5b9c Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 19 Jul 2007 08:43:28 +0200 Subject: [PATCH 073/148] remove schedule API to allow canceling and removing now and destroy events --- src/simulator/scheduler-heap.cc | 39 ++++----- src/simulator/scheduler-heap.h | 8 +- src/simulator/scheduler-list.cc | 40 ++++----- src/simulator/scheduler-list.h | 8 +- src/simulator/scheduler-map.cc | 41 ++++------ src/simulator/scheduler-map.h | 9 +- src/simulator/scheduler.cc | 20 ++--- src/simulator/scheduler.h | 20 ++--- src/simulator/simulator.cc | 104 +++++++++++++++--------- src/simulator/simulator.h | 140 ++++++++++++++++---------------- 10 files changed, 203 insertions(+), 226 deletions(-) diff --git a/src/simulator/scheduler-heap.cc b/src/simulator/scheduler-heap.cc index a73e38719..0339dfed1 100644 --- a/src/simulator/scheduler-heap.cc +++ b/src/simulator/scheduler-heap.cc @@ -222,23 +222,22 @@ SchedulerHeap::TopDown (uint32_t start) } -EventId -SchedulerHeap::RealInsert (EventImpl *event, Scheduler::EventKey key) +void +SchedulerHeap::RealInsert (EventId id) { + EventImpl *event = id.GetEventImpl (); + Scheduler::EventKey key; + key.m_ts = id.GetTs (); + key.m_uid = id.GetUid (); m_heap.push_back (std::make_pair (event, key)); BottomUp (); - return EventId (event, key.m_ts, key.m_uid); } -EventImpl * +EventId SchedulerHeap::RealPeekNext (void) const { - return m_heap[Root ()].first; -} -Scheduler::EventKey -SchedulerHeap::RealPeekNextKey (void) const -{ - return m_heap[Root ()].second; + std::pair next = m_heap[Root ()]; + return EventId (next.first, next.second.m_ts, next.second.m_uid); } void SchedulerHeap::RealRemoveNext (void) @@ -249,31 +248,25 @@ SchedulerHeap::RealRemoveNext (void) } -EventImpl * -SchedulerHeap::RealRemove (EventId id, Scheduler::EventKey *key) +bool +SchedulerHeap::RealRemove (EventId id) { - key->m_ts = id.GetTs (); - key->m_uid = id.GetUid (); + uint32_t uid = id.GetUid (); for (uint32_t i = 1; i < m_heap.size (); i++) { - if (key->m_uid == m_heap[i].second.m_uid) + if (uid == m_heap[i].second.m_uid) { - EventImpl *retval = m_heap[i].first; + NS_ASSERT (m_heap[i].first == id.GetEventImpl ()); Exch (i, Last ()); m_heap.pop_back (); TopDown (i); - return retval; + return true; } } NS_ASSERT (false); // quiet compiler - return 0; + return false; } -bool -SchedulerHeap::RealIsValid (EventId id) -{ - return true; -} }; // namespace ns3 diff --git a/src/simulator/scheduler-heap.h b/src/simulator/scheduler-heap.h index 5f3761431..c57fe1368 100644 --- a/src/simulator/scheduler-heap.h +++ b/src/simulator/scheduler-heap.h @@ -36,13 +36,11 @@ public: virtual ~SchedulerHeap (); private: - virtual EventId RealInsert (EventImpl *event, Scheduler::EventKey key); + virtual void RealInsert (EventId id); virtual bool RealIsEmpty (void) const; - virtual EventImpl *RealPeekNext (void) const; - virtual Scheduler::EventKey RealPeekNextKey (void) const; + virtual EventId RealPeekNext (void) const; virtual void RealRemoveNext (void); - virtual EventImpl *RealRemove (EventId ev, Scheduler::EventKey *key); - virtual bool RealIsValid (EventId id); + virtual bool RealRemove (EventId ev); typedef std::vector > BinaryHeap; diff --git a/src/simulator/scheduler-list.cc b/src/simulator/scheduler-list.cc index fe5b3317b..ba3960458 100644 --- a/src/simulator/scheduler-list.cc +++ b/src/simulator/scheduler-list.cc @@ -66,34 +66,33 @@ SchedulerList::IsLower (Scheduler::EventKey const*a, Scheduler::EventKey const*b } } -EventId -SchedulerList::RealInsert (EventImpl *event, Scheduler::EventKey key) +void +SchedulerList::RealInsert (EventId id) { + Scheduler::EventKey key; + EventImpl *event = id.GetEventImpl (); + key.m_ts = id.GetTs (); + key.m_uid = id.GetUid (); for (EventsI i = m_events.begin (); i != m_events.end (); i++) { if (IsLower (&key, &i->second)) { m_events.insert (i, std::make_pair (event, key)); - return EventId (event, key.m_ts, key.m_uid); + return; } } m_events.push_back (std::make_pair (event, key)); - return EventId (event, key.m_ts, key.m_uid); } bool SchedulerList::RealIsEmpty (void) const { return m_events.empty (); } -EventImpl * +EventId SchedulerList::RealPeekNext (void) const { - return m_events.front ().first; -} -Scheduler::EventKey -SchedulerList::RealPeekNextKey (void) const -{ - return m_events.front ().second; + std::pair next = m_events.front (); + return EventId (next.first, next.second.m_ts, next.second.m_uid); } void @@ -102,29 +101,20 @@ SchedulerList::RealRemoveNext (void) m_events.pop_front (); } -EventImpl * -SchedulerList::RealRemove (EventId id, Scheduler::EventKey *key) +bool +SchedulerList::RealRemove (EventId id) { for (EventsI i = m_events.begin (); i != m_events.end (); i++) { if (i->second.m_uid == id.GetUid ()) { - EventImpl *retval = i->first; - NS_ASSERT (id.GetEventImpl () == retval); - key->m_ts = id.GetTs (); - key->m_uid = id.GetUid (); + NS_ASSERT (id.GetEventImpl () == i->first); m_events.erase (i); - return retval; + return true; } } NS_ASSERT (false); - return 0; -} - -bool -SchedulerList::RealIsValid (EventId id) -{ - return true; + return false; } }; // namespace ns3 diff --git a/src/simulator/scheduler-list.h b/src/simulator/scheduler-list.h index 8d9ebce1c..cbce5e68f 100644 --- a/src/simulator/scheduler-list.h +++ b/src/simulator/scheduler-list.h @@ -38,13 +38,11 @@ class SchedulerList : public Scheduler { virtual ~SchedulerList (); private: - virtual EventId RealInsert (EventImpl *event, EventKey key); + virtual void RealInsert (EventId id); virtual bool RealIsEmpty (void) const; - virtual EventImpl *RealPeekNext (void) const; - virtual Scheduler::EventKey RealPeekNextKey (void) const; + virtual EventId RealPeekNext (void) const; virtual void RealRemoveNext (void); - virtual EventImpl *RealRemove (EventId ev, Scheduler::EventKey *key); - virtual bool RealIsValid (EventId id); + virtual bool RealRemove (EventId ev); inline bool IsLower (Scheduler::EventKey const*a, Scheduler::EventKey const*b) const; diff --git a/src/simulator/scheduler-map.cc b/src/simulator/scheduler-map.cc index 46ad3bae5..59be61ca7 100644 --- a/src/simulator/scheduler-map.cc +++ b/src/simulator/scheduler-map.cc @@ -87,13 +87,16 @@ SchedulerMap::EventKeyCompare::operator () (struct EventKey const&a, struct Even -EventId -SchedulerMap::RealInsert (EventImpl *event, Scheduler::EventKey key) +void +SchedulerMap::RealInsert (EventId id) { + EventImpl *event = id.GetEventImpl (); + Scheduler::EventKey key; + key.m_ts = id.GetTs (); + key.m_uid = id.GetUid (); std::pair result; result = m_list.insert (std::make_pair (key, event)); NS_ASSERT (result.second); - return EventId (event, key.m_ts, key.m_uid); } bool @@ -102,19 +105,13 @@ SchedulerMap::RealIsEmpty (void) const return m_list.empty (); } -EventImpl * +EventId SchedulerMap::RealPeekNext (void) const { EventMapCI i = m_list.begin (); NS_ASSERT (i != m_list.end ()); - return (*i).second; -} -Scheduler::EventKey -SchedulerMap::RealPeekNextKey (void) const -{ - EventMapCI i = m_list.begin (); - NS_ASSERT (i != m_list.end ()); - return (*i).first; + + return EventId (i->second, i->first.m_ts, i->first.m_uid); } void SchedulerMap::RealRemoveNext (void) @@ -122,22 +119,16 @@ SchedulerMap::RealRemoveNext (void) m_list.erase (m_list.begin ()); } -EventImpl * -SchedulerMap::RealRemove (EventId id, Scheduler::EventKey *key) -{ - key->m_ts = id.GetTs (); - key->m_uid = id.GetUid (); - EventMapI i = m_list.find (*key); - EventImpl *retval = i->second; - m_list.erase (i); - return retval; -} - bool -SchedulerMap::RealIsValid (EventId id) +SchedulerMap::RealRemove (EventId id) { + Scheduler::EventKey key; + key.m_ts = id.GetTs (); + key.m_uid = id.GetUid (); + EventMapI i = m_list.find (key); + NS_ASSERT (i->second == id.GetEventImpl ()); + m_list.erase (i); return true; } - }; // namespace ns3 diff --git a/src/simulator/scheduler-map.h b/src/simulator/scheduler-map.h index 4b32c34de..b8fb2cfa5 100644 --- a/src/simulator/scheduler-map.h +++ b/src/simulator/scheduler-map.h @@ -37,13 +37,11 @@ public: virtual ~SchedulerMap (); private: - virtual EventId RealInsert (EventImpl *event, Scheduler::EventKey key); + virtual void RealInsert (EventId id); virtual bool RealIsEmpty (void) const; - virtual EventImpl *RealPeekNext (void) const; - virtual Scheduler::EventKey RealPeekNextKey (void) const; + virtual EventId RealPeekNext (void) const; virtual void RealRemoveNext (void); - virtual EventImpl *RealRemove (EventId ev, Scheduler::EventKey *key); - virtual bool RealIsValid (EventId id); + virtual bool RealRemove (EventId ev); class EventKeyCompare { public: @@ -56,7 +54,6 @@ private: EventMap m_list; - uint32_t m_uid; }; }; // namespace ns3 diff --git a/src/simulator/scheduler.cc b/src/simulator/scheduler.cc index df450d466..f8190bb39 100644 --- a/src/simulator/scheduler.cc +++ b/src/simulator/scheduler.cc @@ -27,39 +27,33 @@ namespace ns3 { Scheduler::~Scheduler () {} -EventId -Scheduler::Insert (EventImpl *event, struct EventKey key) +void +Scheduler::Insert (EventId id) { - return RealInsert (event, key); + return RealInsert (id); } bool Scheduler::IsEmpty (void) const { return RealIsEmpty (); } -EventImpl * +EventId Scheduler::PeekNext (void) const { NS_ASSERT (!RealIsEmpty ()); return RealPeekNext (); } -Scheduler::EventKey -Scheduler::PeekNextKey (void) const -{ - NS_ASSERT (!RealIsEmpty ()); - return RealPeekNextKey (); -} void Scheduler::RemoveNext (void) { NS_ASSERT (!RealIsEmpty ()); return RealRemoveNext (); } -EventImpl * -Scheduler::Remove (EventId id, EventKey *key) +bool +Scheduler::Remove (EventId id) { NS_ASSERT (!RealIsEmpty ()); - return RealRemove (id, key); + return RealRemove (id); } }; // namespace ns3 diff --git a/src/simulator/scheduler.h b/src/simulator/scheduler.h index 150aa16fc..0df7a77df 100644 --- a/src/simulator/scheduler.h +++ b/src/simulator/scheduler.h @@ -42,7 +42,6 @@ class EventImpl; * - ns3::Scheduler::realPeekNextKey * - ns3::Scheduler::realRemoveNext * - ns3::Scheduler::realRemove - * - ns3::Scheduler::realIsValid * * If you need to provide a new event list scheduler without * editing the main simulator class, you need to also implement @@ -61,12 +60,11 @@ class Scheduler { virtual ~Scheduler () = 0; - EventId Insert (EventImpl *event, EventKey key); + void Insert (EventId id); bool IsEmpty (void) const; - EventImpl *PeekNext (void) const; - Scheduler::EventKey PeekNextKey (void) const ; + EventId PeekNext (void) const; void RemoveNext (void); - EventImpl *Remove (EventId id, EventKey *key); + bool Remove (EventId); private: /** @@ -76,7 +74,7 @@ private: * * This method takes ownership of the event pointer. */ - virtual EventId RealInsert (EventImpl *event, EventKey key) = 0; + virtual void RealInsert (EventId id) = 0; /** * \returns true if the event list is empty and false otherwise. */ @@ -87,13 +85,7 @@ private: * * This method cannot be invoked if the list is empty. */ - virtual EventImpl *RealPeekNext (void) const = 0; - /** - * \returns the timecode associated with the next earliest event. - * - * This method cannot be invoked if the list is empty. - */ - virtual Scheduler::EventKey RealPeekNextKey (void) const = 0; + virtual EventId RealPeekNext (void) const = 0; /** * This method cannot be invoked if the list is empty. * Remove the next earliest event from the event list. @@ -107,7 +99,7 @@ private: * * This methods cannot be invoked if the list is empty. */ - virtual EventImpl *RealRemove (EventId id, EventKey *key) = 0; + virtual bool RealRemove (EventId id) = 0; }; }; // namespace ns3 diff --git a/src/simulator/simulator.cc b/src/simulator/simulator.cc index 6467969b9..7b6c6791c 100644 --- a/src/simulator/simulator.cc +++ b/src/simulator/simulator.cc @@ -62,8 +62,8 @@ public: void Stop (void); void StopAt (Time const &time); EventId Schedule (Time const &time, EventImpl *event); - void ScheduleNow (EventImpl *event); - void ScheduleDestroy (EventImpl *event); + EventId ScheduleNow (EventImpl *event); + EventId ScheduleDestroy (EventImpl *event); void Remove (EventId ev); void Cancel (EventId &ev); bool IsExpired (EventId ev); @@ -74,8 +74,8 @@ private: void ProcessOneEvent (void); uint64_t NextTs (void) const; - typedef std::list > Events; - Events m_destroy; + typedef std::list DestroyEvents; + DestroyEvents m_destroyEvents; uint64_t m_stopAt; bool m_stop; Scheduler *m_events; @@ -98,8 +98,11 @@ SimulatorPrivate::SimulatorPrivate (Scheduler *events) m_stop = false; m_stopAt = 0; m_events = events; - // uids are allocated from 1. - m_uid = 1; + // uids are allocated from 4. + // uid 0 is "invalid" events + // uid 1 is "now" events + // uid 2 is "destroy" events + m_uid = 4; // before ::Run is entered, the m_currentUid will be zero m_currentUid = 0; m_logEnable = false; @@ -109,13 +112,16 @@ SimulatorPrivate::SimulatorPrivate (Scheduler *events) SimulatorPrivate::~SimulatorPrivate () { - while (!m_destroy.empty ()) + while (!m_destroyEvents.empty ()) { - EventImpl *ev = m_destroy.front ().first; - m_destroy.pop_front (); + EventImpl *ev = m_destroyEvents.front ().GetEventImpl (); + m_destroyEvents.pop_front (); TRACE ("handle destroy " << ev); - ev->Invoke (); - delete ev; + if (!ev->IsCancelled ()) + { + ev->Invoke (); + delete ev; + } } delete m_events; m_events = (Scheduler *)0xdeadbeaf; @@ -132,22 +138,22 @@ SimulatorPrivate::EnableLogTo (char const *filename) void SimulatorPrivate::ProcessOneEvent (void) { - EventImpl *nextEv = m_events->PeekNext (); - Scheduler::EventKey nextKey = m_events->PeekNextKey (); + EventId next = m_events->PeekNext (); m_events->RemoveNext (); - NS_ASSERT (nextKey.m_ts >= m_currentTs); + NS_ASSERT (next.GetTs () >= m_currentTs); --m_unscheduledEvents; TRACE ("handle " << nextEv); - m_currentTs = nextKey.m_ts; - m_currentUid = nextKey.m_uid; + m_currentTs = next.GetTs (); + m_currentUid = next.GetUid (); if (m_logEnable) { - m_log << "e "<Invoke (); - delete nextEv; + EventImpl *event = next.GetEventImpl (); + event->Invoke (); + delete event; } bool @@ -159,8 +165,8 @@ uint64_t SimulatorPrivate::NextTs (void) const { NS_ASSERT (!m_events->IsEmpty ()); - Scheduler::EventKey nextKey = m_events->PeekNextKey (); - return nextKey.m_ts; + EventId id = m_events->PeekNext (); + return id.GetTs (); } Time SimulatorPrivate::Next (void) const @@ -203,7 +209,7 @@ SimulatorPrivate::Schedule (Time const &time, EventImpl *event) NS_ASSERT (time.IsPositive ()); NS_ASSERT (time >= TimeStep (m_currentTs)); uint64_t ts = (uint64_t) time.GetTimeStep (); - Scheduler::EventKey key = {ts, m_uid}; + EventId id (event, ts, m_uid); if (m_logEnable) { m_log << "i "<Insert (event, key); + m_events->Insert (id); + return id; } -void +EventId SimulatorPrivate::ScheduleNow (EventImpl *event) { - uint64_t ts = m_currentTs; - Scheduler::EventKey key = {ts, m_uid}; + EventId id (event, m_currentTs, m_uid); if (m_logEnable) { m_log << "i "<Insert (event, key); + m_events->Insert (id); + return id; } -void +EventId SimulatorPrivate::ScheduleDestroy (EventImpl *event) { - m_destroy.push_back (std::make_pair (event, m_uid)); + EventId id (event, m_currentTs, 2); + m_destroyEvents.push_back (id); if (m_logEnable) { m_log << "id " << m_currentUid << " " << Now ().GetTimeStep () << " " << m_uid << std::endl; } m_uid++; + return id; } Time @@ -248,17 +257,29 @@ SimulatorPrivate::Now (void) const void SimulatorPrivate::Remove (EventId ev) { + if (ev.GetUid () == 2) + { + // destroy events. + for (DestroyEvents::iterator i = m_destroyEvents.begin (); i != m_destroyEvents.end (); i++) + { + if (*i == ev) + { + m_destroyEvents.erase (i); + break; + } + } + return; + } if (IsExpired (ev)) { return; } - Scheduler::EventKey key; - EventImpl *impl = m_events->Remove (ev, &key); - delete impl; + m_events->Remove (ev); + delete ev.GetEventImpl (); if (m_logEnable) { m_log << "r " << m_currentUid << " " << m_currentTs << " " - << key.m_uid << " " << key.m_ts << std::endl; + << ev.GetUid () << " " << ev.GetTs () << std::endl; } --m_unscheduledEvents; } @@ -403,27 +424,27 @@ Simulator::Schedule (Time const &time, EventImpl *ev) { return GetPriv ()->Schedule (Now () + time, ev); } -void +EventId Simulator::ScheduleNow (EventImpl *ev) { - GetPriv ()->ScheduleNow (ev); + return GetPriv ()->ScheduleNow (ev); } -void +EventId Simulator::ScheduleDestroy (EventImpl *ev) { - GetPriv ()->ScheduleDestroy (ev); + return GetPriv ()->ScheduleDestroy (ev); } EventId Simulator::Schedule (Time const &time, void (*f) (void)) { return Schedule (time, MakeEvent (f)); } -void +EventId Simulator::ScheduleNow (void (*f) (void)) { return ScheduleNow (MakeEvent (f)); } -void +EventId Simulator::ScheduleDestroy (void (*f) (void)) { return ScheduleDestroy (MakeEvent (f)); @@ -691,6 +712,9 @@ SimulatorTests::RunTests (void) Simulator::ScheduleDestroy (&SimulatorTests::bar4, Ptr (this), 0, 0, 0, 0); Simulator::ScheduleDestroy (&SimulatorTests::bar5, Ptr (this), 0, 0, 0, 0, 0); + EventId nowId = Simulator::ScheduleNow (&foo0); + EventId destroyId = Simulator::ScheduleDestroy (&foo0); + Simulator::Run (); Simulator::Destroy (); diff --git a/src/simulator/simulator.h b/src/simulator/simulator.h index 02cf6741c..d117a605a 100644 --- a/src/simulator/simulator.h +++ b/src/simulator/simulator.h @@ -287,14 +287,14 @@ public: * @param obj the object on which to invoke the member method */ template - static void ScheduleNow (void (T::*mem_ptr) (void), OBJ obj); + static EventId ScheduleNow (void (T::*mem_ptr) (void), OBJ obj); /** * @param mem_ptr member method pointer to invoke * @param obj the object on which to invoke the member method * @param a1 the first argument to pass to the invoked method */ template - static void ScheduleNow (void (T::*mem_ptr) (T1), OBJ obj, T1 a1); + static EventId ScheduleNow (void (T::*mem_ptr) (T1), OBJ obj, T1 a1); /** * @param mem_ptr member method pointer to invoke * @param obj the object on which to invoke the member method @@ -302,7 +302,7 @@ public: * @param a2 the second argument to pass to the invoked method */ template - static void ScheduleNow (void (T::*mem_ptr) (T1,T2), OBJ obj, T1 a1, T2 a2); + static EventId ScheduleNow (void (T::*mem_ptr) (T1,T2), OBJ obj, T1 a1, T2 a2); /** * @param mem_ptr member method pointer to invoke * @param obj the object on which to invoke the member method @@ -311,7 +311,7 @@ public: * @param a3 the third argument to pass to the invoked method */ template - static void ScheduleNow (void (T::*mem_ptr) (T1,T2,T3), OBJ obj, T1 a1, T2 a2, T3 a3); + static EventId ScheduleNow (void (T::*mem_ptr) (T1,T2,T3), OBJ obj, T1 a1, T2 a2, T3 a3); /** * @param mem_ptr member method pointer to invoke * @param obj the object on which to invoke the member method @@ -321,7 +321,7 @@ public: * @param a4 the fourth argument to pass to the invoked method */ template - static void ScheduleNow (void (T::*mem_ptr) (T1,T2,T3,T4), OBJ obj, + static EventId ScheduleNow (void (T::*mem_ptr) (T1,T2,T3,T4), OBJ obj, T1 a1, T2 a2, T3 a3, T4 a4); /** * @param mem_ptr member method pointer to invoke @@ -333,25 +333,25 @@ public: * @param a5 the fifth argument to pass to the invoked method */ template - static void ScheduleNow (void (T::*mem_ptr) (T1,T2,T3,T4,T5), OBJ obj, + static EventId ScheduleNow (void (T::*mem_ptr) (T1,T2,T3,T4,T5), OBJ obj, T1 a1, T2 a2, T3 a3, T4 a4, T5 a5); /** * @param f the function to invoke */ - static void ScheduleNow (void (*f) (void)); + static EventId ScheduleNow (void (*f) (void)); /** * @param f the function to invoke * @param a1 the first argument to pass to the function to invoke */ template - static void ScheduleNow (void (*f) (T1), T1 a1); + static EventId ScheduleNow (void (*f) (T1), T1 a1); /** * @param f the function to invoke * @param a1 the first argument to pass to the function to invoke * @param a2 the second argument to pass to the function to invoke */ template - static void ScheduleNow (void (*f) (T1,T2), T1 a1, T2 a2); + static EventId ScheduleNow (void (*f) (T1,T2), T1 a1, T2 a2); /** * @param f the function to invoke * @param a1 the first argument to pass to the function to invoke @@ -359,7 +359,7 @@ public: * @param a3 the third argument to pass to the function to invoke */ template - static void ScheduleNow (void (*f) (T1,T2,T3), T1 a1, T2 a2, T3 a3); + static EventId ScheduleNow (void (*f) (T1,T2,T3), T1 a1, T2 a2, T3 a3); /** * @param f the function to invoke * @param a1 the first argument to pass to the function to invoke @@ -368,7 +368,7 @@ public: * @param a4 the fourth argument to pass to the function to invoke */ template - static void ScheduleNow (void (*f) (T1,T2,T3,T4), T1 a1, T2 a2, T3 a3, T4 a4); + static EventId ScheduleNow (void (*f) (T1,T2,T3,T4), T1 a1, T2 a2, T3 a3, T4 a4); /** * @param f the function to invoke * @param a1 the first argument to pass to the function to invoke @@ -378,7 +378,7 @@ public: * @param a5 the fifth argument to pass to the function to invoke */ template - static void ScheduleNow (void (*f) (T1,T2,T3,T4,T5), T1 a1, T2 a2, T3 a3, T4 a4, T5 a5); + static EventId ScheduleNow (void (*f) (T1,T2,T3,T4,T5), T1 a1, T2 a2, T3 a3, T4 a4, T5 a5); /** @@ -391,14 +391,14 @@ public: * @param obj the object on which to invoke the member method */ template - static void ScheduleDestroy (void (T::*mem_ptr) (void), OBJ obj); + static EventId ScheduleDestroy (void (T::*mem_ptr) (void), OBJ obj); /** * @param mem_ptr member method pointer to invoke * @param obj the object on which to invoke the member method * @param a1 the first argument to pass to the invoked method */ template - static void ScheduleDestroy (void (T::*mem_ptr) (T1), OBJ obj, T1 a1); + static EventId ScheduleDestroy (void (T::*mem_ptr) (T1), OBJ obj, T1 a1); /** * @param mem_ptr member method pointer to invoke * @param obj the object on which to invoke the member method @@ -406,7 +406,7 @@ public: * @param a2 the second argument to pass to the invoked method */ template - static void ScheduleDestroy (void (T::*mem_ptr) (T1,T2), OBJ obj, T1 a1, T2 a2); + static EventId ScheduleDestroy (void (T::*mem_ptr) (T1,T2), OBJ obj, T1 a1, T2 a2); /** * @param mem_ptr member method pointer to invoke * @param obj the object on which to invoke the member method @@ -415,7 +415,7 @@ public: * @param a3 the third argument to pass to the invoked method */ template - static void ScheduleDestroy (void (T::*mem_ptr) (T1,T2,T3), OBJ obj, T1 a1, T2 a2, T3 a3); + static EventId ScheduleDestroy (void (T::*mem_ptr) (T1,T2,T3), OBJ obj, T1 a1, T2 a2, T3 a3); /** * @param mem_ptr member method pointer to invoke * @param obj the object on which to invoke the member method @@ -425,7 +425,7 @@ public: * @param a4 the fourth argument to pass to the invoked method */ template - static void ScheduleDestroy (void (T::*mem_ptr) (T1,T2,T3,T4), OBJ obj, + static EventId ScheduleDestroy (void (T::*mem_ptr) (T1,T2,T3,T4), OBJ obj, T1 a1, T2 a2, T3 a3, T4 a4); /** * @param mem_ptr member method pointer to invoke @@ -437,25 +437,25 @@ public: * @param a5 the fifth argument to pass to the invoked method */ template - static void ScheduleDestroy (void (T::*mem_ptr) (T1,T2,T3,T4,T5), OBJ obj, + static EventId ScheduleDestroy (void (T::*mem_ptr) (T1,T2,T3,T4,T5), OBJ obj, T1 a1, T2 a2, T3 a3, T4 a4, T5 a5); /** * @param f the function to invoke */ - static void ScheduleDestroy (void (*f) (void)); + static EventId ScheduleDestroy (void (*f) (void)); /** * @param f the function to invoke * @param a1 the first argument to pass to the function to invoke */ template - static void ScheduleDestroy (void (*f) (T1), T1 a1); + static EventId ScheduleDestroy (void (*f) (T1), T1 a1); /** * @param f the function to invoke * @param a1 the first argument to pass to the function to invoke * @param a2 the second argument to pass to the function to invoke */ template - static void ScheduleDestroy (void (*f) (T1,T2), T1 a1, T2 a2); + static EventId ScheduleDestroy (void (*f) (T1,T2), T1 a1, T2 a2); /** * @param f the function to invoke * @param a1 the first argument to pass to the function to invoke @@ -463,7 +463,7 @@ public: * @param a3 the third argument to pass to the function to invoke */ template - static void ScheduleDestroy (void (*f) (T1,T2,T3), T1 a1, T2 a2, T3 a3); + static EventId ScheduleDestroy (void (*f) (T1,T2,T3), T1 a1, T2 a2, T3 a3); /** * @param f the function to invoke * @param a1 the first argument to pass to the function to invoke @@ -472,7 +472,7 @@ public: * @param a4 the fourth argument to pass to the function to invoke */ template - static void ScheduleDestroy (void (*f) (T1,T2,T3,T4), T1 a1, T2 a2, T3 a3, T4 a4); + static EventId ScheduleDestroy (void (*f) (T1,T2,T3,T4), T1 a1, T2 a2, T3 a3, T4 a4); /** * @param f the function to invoke * @param a1 the first argument to pass to the function to invoke @@ -482,7 +482,7 @@ public: * @param a5 the fifth argument to pass to the function to invoke */ template - static void ScheduleDestroy (void (*f) (T1,T2,T3,T4,T5), T1 a1, T2 a2, T3 a3, T4 a4, T5 a5); + static EventId ScheduleDestroy (void (*f) (T1,T2,T3,T4,T5), T1 a1, T2 a2, T3 a3, T4 a4, T5 a5); /** * Remove an event from the event list. @@ -556,8 +556,8 @@ private: static SimulatorPrivate *GetPriv (void); static EventId Schedule (Time const &time, EventImpl *event); - static void ScheduleDestroy (EventImpl *event); - static void ScheduleNow (EventImpl *event); + static EventId ScheduleDestroy (EventImpl *event); + static EventId ScheduleNow (EventImpl *event); static SimulatorPrivate *m_priv; }; @@ -979,163 +979,163 @@ EventId Simulator::Schedule (Time const &time, void (*f) (T1,T2,T3,T4,T5), T1 a1 template -void +EventId Simulator::ScheduleNow (void (T::*mem_ptr) (void), OBJ obj) { - ScheduleNow (MakeEvent (mem_ptr, obj)); + return ScheduleNow (MakeEvent (mem_ptr, obj)); } template -void +EventId Simulator::ScheduleNow (void (T::*mem_ptr) (T1), OBJ obj, T1 a1) { - ScheduleNow (MakeEvent (mem_ptr, obj, a1)); + return ScheduleNow (MakeEvent (mem_ptr, obj, a1)); } template -void +EventId Simulator::ScheduleNow (void (T::*mem_ptr) (T1,T2), OBJ obj, T1 a1, T2 a2) { - ScheduleNow (MakeEvent (mem_ptr, obj, a1, a2)); + return ScheduleNow (MakeEvent (mem_ptr, obj, a1, a2)); } template -void +EventId Simulator::ScheduleNow (void (T::*mem_ptr) (T1,T2,T3), OBJ obj, T1 a1, T2 a2, T3 a3) { - ScheduleNow (MakeEvent (mem_ptr, obj, a1, a2, a3)); + return ScheduleNow (MakeEvent (mem_ptr, obj, a1, a2, a3)); } template -void +EventId Simulator::ScheduleNow (void (T::*mem_ptr) (T1,T2,T3,T4), OBJ obj, T1 a1, T2 a2, T3 a3, T4 a4) { - ScheduleNow (MakeEvent (mem_ptr, obj, a1, a2, a3, a4)); + return ScheduleNow (MakeEvent (mem_ptr, obj, a1, a2, a3, a4)); } template -void +EventId Simulator::ScheduleNow (void (T::*mem_ptr) (T1,T2,T3,T4,T5), OBJ obj, T1 a1, T2 a2, T3 a3, T4 a4, T5 a5) { - ScheduleNow (MakeEvent (mem_ptr, obj, a1, a2, a3, a4, a5)); + return ScheduleNow (MakeEvent (mem_ptr, obj, a1, a2, a3, a4, a5)); } template -void +EventId Simulator::ScheduleNow (void (*f) (T1), T1 a1) { - ScheduleNow (MakeEvent (f, a1)); + return ScheduleNow (MakeEvent (f, a1)); } template -void +EventId Simulator::ScheduleNow (void (*f) (T1,T2), T1 a1, T2 a2) { - ScheduleNow (MakeEvent (f, a1, a2)); + return ScheduleNow (MakeEvent (f, a1, a2)); } template -void +EventId Simulator::ScheduleNow (void (*f) (T1,T2,T3), T1 a1, T2 a2, T3 a3) { - ScheduleNow (MakeEvent (f, a1, a2, a3)); + return ScheduleNow (MakeEvent (f, a1, a2, a3)); } template -void +EventId Simulator::ScheduleNow (void (*f) (T1,T2,T3,T4), T1 a1, T2 a2, T3 a3, T4 a4) { - ScheduleNow (MakeEvent (f, a1, a2, a3, a4)); + return ScheduleNow (MakeEvent (f, a1, a2, a3, a4)); } template -void +EventId Simulator::ScheduleNow (void (*f) (T1,T2,T3,T4,T5), T1 a1, T2 a2, T3 a3, T4 a4, T5 a5) { - ScheduleNow (MakeEvent (f, a1, a2, a3, a4, a5)); + return ScheduleNow (MakeEvent (f, a1, a2, a3, a4, a5)); } template -void +EventId Simulator::ScheduleDestroy (void (T::*mem_ptr) (void), OBJ obj) { - ScheduleDestroy (MakeEvent (mem_ptr, obj)); + return ScheduleDestroy (MakeEvent (mem_ptr, obj)); } template -void +EventId Simulator::ScheduleDestroy (void (T::*mem_ptr) (T1), OBJ obj, T1 a1) { - ScheduleDestroy (MakeEvent (mem_ptr, obj, a1)); + return ScheduleDestroy (MakeEvent (mem_ptr, obj, a1)); } template -void +EventId Simulator::ScheduleDestroy (void (T::*mem_ptr) (T1,T2), OBJ obj, T1 a1, T2 a2) { - ScheduleDestroy (MakeEvent (mem_ptr, obj, a1, a2)); + return ScheduleDestroy (MakeEvent (mem_ptr, obj, a1, a2)); } template -void +EventId Simulator::ScheduleDestroy (void (T::*mem_ptr) (T1,T2,T3), OBJ obj, T1 a1, T2 a2, T3 a3) { - ScheduleDestroy (MakeEvent (mem_ptr, obj, a1, a2, a3)); + return ScheduleDestroy (MakeEvent (mem_ptr, obj, a1, a2, a3)); } template -void +EventId Simulator::ScheduleDestroy (void (T::*mem_ptr) (T1,T2,T3,T4), OBJ obj, T1 a1, T2 a2, T3 a3, T4 a4) { - ScheduleDestroy (MakeEvent (mem_ptr, obj, a1, a2, a3, a4)); + return ScheduleDestroy (MakeEvent (mem_ptr, obj, a1, a2, a3, a4)); } template -void +EventId Simulator::ScheduleDestroy (void (T::*mem_ptr) (T1,T2,T3,T4,T5), OBJ obj, T1 a1, T2 a2, T3 a3, T4 a4, T5 a5) { - ScheduleDestroy (MakeEvent (mem_ptr, obj, a1, a2, a3, a4, a5)); + return ScheduleDestroy (MakeEvent (mem_ptr, obj, a1, a2, a3, a4, a5)); } template -void +EventId Simulator::ScheduleDestroy (void (*f) (T1), T1 a1) { - ScheduleDestroy (MakeEvent (f, a1)); + return ScheduleDestroy (MakeEvent (f, a1)); } template -void +EventId Simulator::ScheduleDestroy (void (*f) (T1,T2), T1 a1, T2 a2) { - ScheduleDestroy (MakeEvent (f, a1, a2)); + return ScheduleDestroy (MakeEvent (f, a1, a2)); } template -void +EventId Simulator::ScheduleDestroy (void (*f) (T1,T2,T3), T1 a1, T2 a2, T3 a3) { - ScheduleDestroy (MakeEvent (f, a1, a2, a3)); + return ScheduleDestroy (MakeEvent (f, a1, a2, a3)); } template -void +EventId Simulator::ScheduleDestroy (void (*f) (T1,T2,T3,T4), T1 a1, T2 a2, T3 a3, T4 a4) { - ScheduleDestroy (MakeEvent (f, a1, a2, a3, a4)); + return ScheduleDestroy (MakeEvent (f, a1, a2, a3, a4)); } template -void +EventId Simulator::ScheduleDestroy (void (*f) (T1,T2,T3,T4,T5), T1 a1, T2 a2, T3 a3, T4 a4, T5 a5) { - ScheduleDestroy (MakeEvent (f, a1, a2, a3, a4, a5)); + return ScheduleDestroy (MakeEvent (f, a1, a2, a3, a4, a5)); } }; // namespace ns3 From 3f4edebda6f5dffc382d779d556611ffd967d2f2 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 19 Jul 2007 08:43:43 +0200 Subject: [PATCH 074/148] remove 'now' events --- src/node/random-walk-2d-mobility-model.cc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/node/random-walk-2d-mobility-model.cc b/src/node/random-walk-2d-mobility-model.cc index b5181c3cb..9b85fed57 100644 --- a/src/node/random-walk-2d-mobility-model.cc +++ b/src/node/random-walk-2d-mobility-model.cc @@ -105,7 +105,7 @@ RandomWalk2dMobilityModel::RandomWalk2dMobilityModel () : m_parameters (RandomWalk2dMobilityModelParameters::GetCurrent ()) { SetInterfaceId (RandomWalk2dMobilityModel::iid); - Simulator::ScheduleNow (&RandomWalk2dMobilityModel::Start, this); + m_event = Simulator::ScheduleNow (&RandomWalk2dMobilityModel::Start, this); } void @@ -145,6 +145,7 @@ RandomWalk2dMobilityModel::DoWalk (Time delayLeft) NS_ASSERT (delay == delayLeft); m_event = Simulator::Schedule (delay, &RandomWalk2dMobilityModel::Start, this); } + NotifyCourseChange (); } void @@ -172,7 +173,7 @@ RandomWalk2dMobilityModel::DoSet (const Position &position) NS_ASSERT (position.IsInside (m_parameters->m_bounds)); m_helper.InitializePosition (position); Simulator::Remove (m_event); - Simulator::ScheduleNow (&RandomWalk2dMobilityModel::Start, this); + m_event = Simulator::ScheduleNow (&RandomWalk2dMobilityModel::Start, this); } From ab084a9399a95c503abbf01028f40acb5d3b0d19 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 19 Jul 2007 09:34:36 +0200 Subject: [PATCH 075/148] move around rectangle class and move IsInside method from Position to Rectangle --- SConstruct | 8 ++-- src/node/position.cc | 9 ---- src/node/position.h | 3 -- src/node/random-walk-2d-mobility-model.cc | 2 +- src/{core => node}/rectangle-default-value.cc | 0 src/{core => node}/rectangle-default-value.h | 2 +- src/{core => node}/rectangle.cc | 45 +++++++++++++++++++ src/{core => node}/rectangle.h | 11 +++++ src/node/static-speed-helper.cc | 2 +- 9 files changed, 63 insertions(+), 19 deletions(-) rename src/{core => node}/rectangle-default-value.cc (100%) rename src/{core => node}/rectangle-default-value.h (98%) rename src/{core => node}/rectangle.cc (55%) rename src/{core => node}/rectangle.h (86%) diff --git a/SConstruct b/SConstruct index 2fea9dad1..cac6a76a6 100644 --- a/SConstruct +++ b/SConstruct @@ -63,8 +63,6 @@ core.add_sources([ 'uid-manager.cc', 'default-value.cc', 'random-variable-default-value.cc', - 'rectangle-default-value.cc', - 'rectangle.cc', 'command-line.cc', 'type-name.cc', 'component-manager.cc', @@ -97,8 +95,6 @@ core.add_inst_headers([ 'rng-stream.h', 'default-value.h', 'random-variable-default-value.h', - 'rectangle-default-value.h', - 'rectangle.h', 'command-line.h', 'type-name.h', 'component-manager.h', @@ -266,6 +262,8 @@ node.add_sources ([ 'speed.cc', 'static-speed-helper.cc', 'random-waypoint-mobility-model.cc', + 'rectangle-default-value.cc', + 'rectangle.cc', ]) node.add_inst_headers ([ 'node.h', @@ -299,6 +297,8 @@ node.add_inst_headers ([ 'speed.h', 'static-speed-helper.h', 'random-waypoint-mobility-model.h', + 'rectangle-default-value.h', + 'rectangle.h', ]) applications = build.Ns3Module ('applications', 'src/applications') diff --git a/src/node/position.cc b/src/node/position.cc index b4efadb7f..802fe3bb7 100644 --- a/src/node/position.cc +++ b/src/node/position.cc @@ -1,5 +1,4 @@ #include "position.h" -#include "ns3/rectangle.h" #include namespace ns3 { @@ -17,14 +16,6 @@ Position::Position () z (0.0) {} -bool -Position::IsInside (const Rectangle &rectangle) const -{ - return - x <= rectangle.xMax && x >= rectangle.xMin && - y <= rectangle.yMax && y >= rectangle.yMin; -} - double CalculateDistance (const Position &a, const Position &b) { diff --git a/src/node/position.h b/src/node/position.h index 7a97dede7..261ba2e99 100644 --- a/src/node/position.h +++ b/src/node/position.h @@ -3,14 +3,11 @@ namespace ns3 { -class Rectangle; - class Position { public: Position (double x, double y, double z); Position (); - bool IsInside (const Rectangle &rectangle) const; double x; double y; double z; diff --git a/src/node/random-walk-2d-mobility-model.cc b/src/node/random-walk-2d-mobility-model.cc index 9b85fed57..c96ab24c5 100644 --- a/src/node/random-walk-2d-mobility-model.cc +++ b/src/node/random-walk-2d-mobility-model.cc @@ -170,7 +170,7 @@ RandomWalk2dMobilityModel::DoGet (void) const void RandomWalk2dMobilityModel::DoSet (const Position &position) { - NS_ASSERT (position.IsInside (m_parameters->m_bounds)); + NS_ASSERT (m_parameters->m_bounds.IsInside (position)); m_helper.InitializePosition (position); Simulator::Remove (m_event); m_event = Simulator::ScheduleNow (&RandomWalk2dMobilityModel::Start, this); diff --git a/src/core/rectangle-default-value.cc b/src/node/rectangle-default-value.cc similarity index 100% rename from src/core/rectangle-default-value.cc rename to src/node/rectangle-default-value.cc diff --git a/src/core/rectangle-default-value.h b/src/node/rectangle-default-value.h similarity index 98% rename from src/core/rectangle-default-value.h rename to src/node/rectangle-default-value.h index 9e0448290..047e7bbb6 100644 --- a/src/core/rectangle-default-value.h +++ b/src/node/rectangle-default-value.h @@ -22,7 +22,7 @@ #define RECTANGLE_DEFAULT_VALUE_H #include -#include "default-value.h" +#include "ns3/default-value.h" #include "rectangle.h" namespace ns3 { diff --git a/src/core/rectangle.cc b/src/node/rectangle.cc similarity index 55% rename from src/core/rectangle.cc rename to src/node/rectangle.cc index 9fdd739d5..87ca20690 100644 --- a/src/core/rectangle.cc +++ b/src/node/rectangle.cc @@ -19,6 +19,9 @@ * Author: Mathieu Lacage */ #include "rectangle.h" +#include "position.h" +#include +#include namespace ns3 { @@ -37,4 +40,46 @@ Rectangle::Rectangle () yMax (0.0) {} +bool +Rectangle::IsInside (const Position &position) const +{ + return + position.x <= xMax && position.x >= xMin && + position.y <= yMax && position.y >= yMin; +} + +Rectangle::Side +Rectangle::GetClosestSide (const Position &position) const +{ + double xMinDist = std::abs (position.x - xMin); + double xMaxDist = std::abs (xMax - position.x); + double yMinDist = std::abs (position.y - yMin); + double yMaxDist = std::abs (yMax - position.y); + double minX = std::min (xMinDist, xMaxDist); + double minY = std::min (yMinDist, yMaxDist); + if (minX < minY) + { + if (xMinDist < xMaxDist) + { + return LEFT; + } + else + { + return RIGHT; + } + } + else + { + if (yMinDist < yMaxDist) + { + return BOTTOM; + } + else + { + return TOP; + } + } +} + + } // namespace ns3 diff --git a/src/core/rectangle.h b/src/node/rectangle.h similarity index 86% rename from src/core/rectangle.h rename to src/node/rectangle.h index b108f9c21..a6de28ba1 100644 --- a/src/core/rectangle.h +++ b/src/node/rectangle.h @@ -23,12 +23,23 @@ namespace ns3 { +class Position; + class Rectangle { public: + enum Side { + RIGHT, + LEFT, + TOP, + BOTTOM + }; Rectangle (double _xMin, double _xMax, double _yMin, double _yMax); Rectangle (); + bool IsInside (const Position &position) const; + Side GetClosestSide (const Position &position) const; + double xMin; double xMax; double yMin; diff --git a/src/node/static-speed-helper.cc b/src/node/static-speed-helper.cc index c56eb5dd9..92d811287 100644 --- a/src/node/static-speed-helper.cc +++ b/src/node/static-speed-helper.cc @@ -105,7 +105,7 @@ StaticSpeedHelper::GetDelayToNextPosition (const Rectangle &bounds, Time delayLe Position nextPosition = m_position; nextPosition.x += m_speed.dx * delayLeft.GetSeconds (); nextPosition.y += m_speed.dy * delayLeft.GetSeconds (); - if (nextPosition.IsInside (bounds)) + if (bounds.IsInside (nextPosition)) { return delayLeft; } From e682be574b26d7734e3d5c4573d4b4d778888702 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 19 Jul 2007 09:48:10 +0200 Subject: [PATCH 076/148] move intersection code to rectangle class --- src/node/rectangle.cc | 38 +++++++++++++++++++++++++++++++++ src/node/rectangle.h | 2 ++ src/node/static-speed-helper.cc | 37 ++------------------------------ src/node/static-speed-helper.h | 1 - 4 files changed, 42 insertions(+), 36 deletions(-) diff --git a/src/node/rectangle.cc b/src/node/rectangle.cc index 87ca20690..34e23e58b 100644 --- a/src/node/rectangle.cc +++ b/src/node/rectangle.cc @@ -20,6 +20,8 @@ */ #include "rectangle.h" #include "position.h" +#include "speed.h" +#include "ns3/assert.h" #include #include @@ -81,5 +83,41 @@ Rectangle::GetClosestSide (const Position &position) const } } +Position +Rectangle::CalculateIntersection (const Position ¤t, const Speed &speed) const +{ + double xMaxY = current.y + (xMax - current.x) / speed.dx * speed.dy; + double xMinY = current.y + (xMin - current.x) / speed.dx * speed.dy; + double yMaxX = current.x + (yMax - current.y) / speed.dy * speed.dx; + double yMinX = current.x + (yMin - current.y) / speed.dy * speed.dx; + bool xMaxOk = xMaxY <= yMax && xMaxY >= yMin; + bool xMinOk = xMinY <= yMax && xMinY >= yMin; + bool yMaxOk = yMaxX <= xMax && yMaxX >= xMin; + bool yMinOk = yMinX <= xMax && yMinX >= xMin; + if (xMaxOk && speed.dx >= 0) + { + return Position (xMax, xMaxY, 0.0); + } + else if (xMinOk && speed.dx <= 0) + { + return Position (xMin, xMinY, 0.0); + } + else if (yMaxOk && speed.dy >= 0) + { + return Position (yMaxX, yMax, 0.0); + } + else if (yMinOk && speed.dy <= 0) + { + return Position (yMinX, yMin, 0.0); + } + else + { + NS_ASSERT (false); + // quiet compiler + return Position (0.0, 0.0, 0.0); + } + +} + } // namespace ns3 diff --git a/src/node/rectangle.h b/src/node/rectangle.h index a6de28ba1..ccceeb450 100644 --- a/src/node/rectangle.h +++ b/src/node/rectangle.h @@ -24,6 +24,7 @@ namespace ns3 { class Position; +class Speed; class Rectangle { @@ -39,6 +40,7 @@ public: Rectangle (); bool IsInside (const Position &position) const; Side GetClosestSide (const Position &position) const; + Position CalculateIntersection (const Position ¤t, const Speed &speed) const; double xMin; double xMax; diff --git a/src/node/static-speed-helper.cc b/src/node/static-speed-helper.cc index 92d811287..772514dc1 100644 --- a/src/node/static-speed-helper.cc +++ b/src/node/static-speed-helper.cc @@ -64,40 +64,6 @@ StaticSpeedHelper::Reset (const Speed &speed) m_speed = speed; m_pauseEnd = Simulator::Now (); } -Position -StaticSpeedHelper::IntersectCurrentDirection (const Rectangle &bounds) -{ - double xMaxY = m_position.y + (bounds.xMax - m_position.x) / m_speed.dx * m_speed.dy; - double xMinY = m_position.y + (bounds.xMin - m_position.x) / m_speed.dx * m_speed.dy; - double yMaxX = m_position.x + (bounds.yMax - m_position.y) / m_speed.dy * m_speed.dx; - double yMinX = m_position.x + (bounds.yMin - m_position.y) / m_speed.dy * m_speed.dx; - bool xMaxOk = xMaxY <= bounds.yMax && xMaxY >= bounds.yMin; - bool xMinOk = xMinY <= bounds.yMax && xMinY >= bounds.yMin; - bool yMaxOk = yMaxX <= bounds.xMax && yMaxX >= bounds.xMin; - bool yMinOk = yMinX <= bounds.xMax && yMinX >= bounds.xMin; - if (xMaxOk && m_speed.dx >= 0) - { - return Position (bounds.xMax, xMaxY, 0.0); - } - else if (xMinOk && m_speed.dx <= 0) - { - return Position (bounds.xMin, xMinY, 0.0); - } - else if (yMaxOk && m_speed.dy >= 0) - { - return Position (yMaxX, bounds.yMax, 0.0); - } - else if (yMinOk && m_speed.dy <= 0) - { - return Position (yMinX, bounds.yMin, 0.0); - } - else - { - NS_ASSERT (false); - // quiet compiler - return Position (0.0, 0.0, 0.0); - } -} Time StaticSpeedHelper::GetDelayToNextPosition (const Rectangle &bounds, Time delayLeft) { @@ -109,7 +75,8 @@ StaticSpeedHelper::GetDelayToNextPosition (const Rectangle &bounds, Time delayLe { return delayLeft; } - nextPosition = IntersectCurrentDirection (bounds); + + nextPosition = bounds.CalculateIntersection (m_position, m_speed); Time delay = Seconds ((nextPosition.x - m_position.x) / m_speed.dx); return delay; } diff --git a/src/node/static-speed-helper.h b/src/node/static-speed-helper.h index aae5abbc8..2c4a2fe72 100644 --- a/src/node/static-speed-helper.h +++ b/src/node/static-speed-helper.h @@ -28,7 +28,6 @@ class StaticSpeedHelper private: void Update (void) const; void UpdateFull (const Rectangle &rectangle) const; - Position IntersectCurrentDirection (const Rectangle &bounds); mutable Time m_lastUpdate; mutable Position m_position; Speed m_speed; From 3ae6f849acbd32c13aadd1626fdafc1cc1bfc8e2 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 19 Jul 2007 09:54:57 +0200 Subject: [PATCH 077/148] move rebound code from helper to random walk model --- src/node/random-walk-2d-mobility-model.cc | 15 ++++++++++++++- src/node/static-speed-helper.cc | 21 +-------------------- src/node/static-speed-helper.h | 3 +-- 3 files changed, 16 insertions(+), 23 deletions(-) diff --git a/src/node/random-walk-2d-mobility-model.cc b/src/node/random-walk-2d-mobility-model.cc index c96ab24c5..83fd8761d 100644 --- a/src/node/random-walk-2d-mobility-model.cc +++ b/src/node/random-walk-2d-mobility-model.cc @@ -151,7 +151,20 @@ RandomWalk2dMobilityModel::DoWalk (Time delayLeft) void RandomWalk2dMobilityModel::Rebound (Time delayLeft) { - m_helper.Rebound (m_parameters->m_bounds); + Position position = m_helper.GetCurrentPosition (m_parameters->m_bounds); + Speed speed = m_helper.GetSpeed (); + switch (m_parameters->m_bounds.GetClosestSide (position)) + { + case Rectangle::RIGHT: + case Rectangle::LEFT: + speed.dx = - speed.dx; + break; + case Rectangle::TOP: + case Rectangle::BOTTOM: + speed.dy = - speed.dy; + break; + } + m_helper.Reset (speed); DoWalk (delayLeft); } diff --git a/src/node/static-speed-helper.cc b/src/node/static-speed-helper.cc index 772514dc1..e09a7112f 100644 --- a/src/node/static-speed-helper.cc +++ b/src/node/static-speed-helper.cc @@ -31,7 +31,7 @@ StaticSpeedHelper::GetCurrentPosition (void) const } Speed -StaticSpeedHelper::GetCurrentSpeed (void) const +StaticSpeedHelper::GetSpeed (void) const { return m_speed; } @@ -80,25 +80,6 @@ StaticSpeedHelper::GetDelayToNextPosition (const Rectangle &bounds, Time delayLe Time delay = Seconds ((nextPosition.x - m_position.x) / m_speed.dx); return delay; } -void -StaticSpeedHelper::Rebound (const Rectangle &bounds) -{ - UpdateFull (bounds); - double xMinDist = m_position.x - bounds.xMin; - double xMaxDist = bounds.xMax - m_position.x; - double yMinDist = m_position.y - bounds.yMin; - double yMaxDist = bounds.yMax - m_position.y; - double minX = std::min (xMinDist, xMaxDist); - double minY = std::min (yMinDist, yMaxDist); - if (minY < minX) - { - m_speed.dy = - m_speed.dy; - } - else - { - m_speed.dx = - m_speed.dx; - } -} void StaticSpeedHelper::UpdateFull (const Rectangle &bounds) const diff --git a/src/node/static-speed-helper.h b/src/node/static-speed-helper.h index 2c4a2fe72..4b97b39f9 100644 --- a/src/node/static-speed-helper.h +++ b/src/node/static-speed-helper.h @@ -19,11 +19,10 @@ class StaticSpeedHelper void Reset (const Speed &speed); Time GetDelayToNextPosition (const Rectangle &bounds, Time delayLeft); - void Rebound (const Rectangle &bounds); Position GetCurrentPosition (const Rectangle &bounds) const; Position GetCurrentPosition (void) const; - Speed GetCurrentSpeed (void) const; + Speed GetSpeed (void) const; private: void Update (void) const; From bb7208b75471001487ba106d85df7ce1a4fc033e Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 19 Jul 2007 10:01:42 +0200 Subject: [PATCH 078/148] move delay calculation code from helper to random walk class --- src/node/random-walk-2d-mobility-model.cc | 20 ++++++++++++-------- src/node/static-speed-helper.cc | 17 ----------------- src/node/static-speed-helper.h | 3 --- 3 files changed, 12 insertions(+), 28 deletions(-) diff --git a/src/node/random-walk-2d-mobility-model.cc b/src/node/random-walk-2d-mobility-model.cc index 83fd8761d..ee0b82fc9 100644 --- a/src/node/random-walk-2d-mobility-model.cc +++ b/src/node/random-walk-2d-mobility-model.cc @@ -133,18 +133,22 @@ RandomWalk2dMobilityModel::Start (void) void RandomWalk2dMobilityModel::DoWalk (Time delayLeft) { - Time delay = m_helper.GetDelayToNextPosition (m_parameters->m_bounds, - delayLeft); - if (delay < delayLeft) + Position position = m_helper.GetCurrentPosition (); + Speed speed = m_helper.GetSpeed (); + Position nextPosition = position; + nextPosition.x += speed.dx * delayLeft.GetSeconds (); + nextPosition.y += speed.dy * delayLeft.GetSeconds (); + if (m_parameters->m_bounds.IsInside (nextPosition)) { - m_event = Simulator::Schedule (delay, &RandomWalk2dMobilityModel::Rebound, this, - delayLeft - delay); + m_event = Simulator::Schedule (delayLeft, &RandomWalk2dMobilityModel::Start, this); } else { - NS_ASSERT (delay == delayLeft); - m_event = Simulator::Schedule (delay, &RandomWalk2dMobilityModel::Start, this); - } + nextPosition = m_parameters->m_bounds.CalculateIntersection (position, speed); + Time delay = Seconds ((nextPosition.x - position.x) / speed.dx); + m_event = Simulator::Schedule (delay, &RandomWalk2dMobilityModel::Rebound, this, + delayLeft - delay); + } NotifyCourseChange (); } diff --git a/src/node/static-speed-helper.cc b/src/node/static-speed-helper.cc index e09a7112f..03e1f066d 100644 --- a/src/node/static-speed-helper.cc +++ b/src/node/static-speed-helper.cc @@ -64,23 +64,6 @@ StaticSpeedHelper::Reset (const Speed &speed) m_speed = speed; m_pauseEnd = Simulator::Now (); } -Time -StaticSpeedHelper::GetDelayToNextPosition (const Rectangle &bounds, Time delayLeft) -{ - UpdateFull (bounds); - Position nextPosition = m_position; - nextPosition.x += m_speed.dx * delayLeft.GetSeconds (); - nextPosition.y += m_speed.dy * delayLeft.GetSeconds (); - if (bounds.IsInside (nextPosition)) - { - return delayLeft; - } - - nextPosition = bounds.CalculateIntersection (m_position, m_speed); - Time delay = Seconds ((nextPosition.x - m_position.x) / m_speed.dx); - return delay; -} - void StaticSpeedHelper::UpdateFull (const Rectangle &bounds) const { diff --git a/src/node/static-speed-helper.h b/src/node/static-speed-helper.h index 4b97b39f9..90fe0601a 100644 --- a/src/node/static-speed-helper.h +++ b/src/node/static-speed-helper.h @@ -16,11 +16,8 @@ class StaticSpeedHelper void InitializePosition (const Position &position); void Reset (const Speed &speed, const Time &pauseDelay); - void Reset (const Speed &speed); - Time GetDelayToNextPosition (const Rectangle &bounds, Time delayLeft); Position GetCurrentPosition (const Rectangle &bounds) const; - Position GetCurrentPosition (void) const; Speed GetSpeed (void) const; From f671c117735a7bd8bb1354de690932b134190872 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 19 Jul 2007 10:05:07 +0200 Subject: [PATCH 079/148] remove mobility model helper, re-enable random direction model with new helper --- SConstruct | 4 +- src/node/mobility-model-helper.cc | 185 -------------------- src/node/mobility-model-helper.h | 80 --------- src/node/random-direction-mobility-model.cc | 115 +++++------- src/node/random-direction-mobility-model.h | 22 ++- 5 files changed, 53 insertions(+), 353 deletions(-) delete mode 100644 src/node/mobility-model-helper.cc delete mode 100644 src/node/mobility-model-helper.h diff --git a/SConstruct b/SConstruct index cac6a76a6..ac117488f 100644 --- a/SConstruct +++ b/SConstruct @@ -255,7 +255,6 @@ node.add_sources ([ 'random-walk-2d-mobility-model.cc', 'hierarchical-mobility-model.cc', 'ns2-mobility-file-topology.cc', - 'mobility-model-helper.cc', 'position-2d.cc', 'position.cc', 'random-position.cc', @@ -264,6 +263,7 @@ node.add_sources ([ 'random-waypoint-mobility-model.cc', 'rectangle-default-value.cc', 'rectangle.cc', + 'random-direction-mobility-model.cc', ]) node.add_inst_headers ([ 'node.h', @@ -290,7 +290,6 @@ node.add_inst_headers ([ 'random-walk-2d-mobility-model.h', 'hierarchical-mobility-model.h', 'ns2-mobility-file-topology.h', - 'mobility-model-helper.h', 'position-2d.h', 'position.h', 'random-position.h', @@ -299,6 +298,7 @@ node.add_inst_headers ([ 'random-waypoint-mobility-model.h', 'rectangle-default-value.h', 'rectangle.h', + 'random-direction-mobility-model.h', ]) applications = build.Ns3Module ('applications', 'src/applications') diff --git a/src/node/mobility-model-helper.cc b/src/node/mobility-model-helper.cc deleted file mode 100644 index aa41f758e..000000000 --- a/src/node/mobility-model-helper.cc +++ /dev/null @@ -1,185 +0,0 @@ -#include -#include "ns3/simulator.h" -#include "mobility-model-helper.h" - -namespace ns3 { - -Position2D::Position2D (double _x, double _y) - : x (_x), - y (_y) -{} -Position2D::Position2D () - : x (0.0), - y (0.0) -{} - -Speed2D::Speed2D (double _dx, double _dy) - : dx (_dx), - dy (_dy) -{} - -Speed2D::Speed2D () - : dx (0.0), - dy (0.0) -{} - -MobilityModelHelper::MobilityModelHelper () -{} -void -MobilityModelHelper::InitializePosition (const struct Position2D &position) -{ - m_position = position; - m_speed.dx = 0.0; - m_speed.dy = 0.0; - m_lastUpdate = Simulator::Now (); - m_pauseEnd = Simulator::Now (); -} -void -MobilityModelHelper::Update (const struct AreaBounds &bounds) const -{ - Time now = Simulator::Now (); - if (m_pauseEnd > now) - { - return; - } - Time last = std::max (now, m_pauseEnd); - if (m_lastUpdate >= last) - { - return; - } - Time deltaTime = now - last; - m_lastUpdate = now; - double deltaS = deltaTime.GetSeconds (); - m_position.x += m_speed.dx * deltaS; - m_position.y += m_speed.dy * deltaS; - m_position.x = std::min (m_position.x, bounds.xMax); - m_position.y = std::min (m_position.y, bounds.yMax); - m_position.x = std::max (m_position.x, bounds.xMin); - m_position.y = std::max (m_position.y, bounds.yMin); -} -Position2D -MobilityModelHelper::CalculateIntersection (const struct AreaBounds &bounds) const -{ - double xMaxY = m_position.y + (bounds.xMax - m_position.x) / m_speed.dx * m_speed.dy; - double xMinY = m_position.y + (bounds.xMin - m_position.x) / m_speed.dx * m_speed.dy; - double yMaxX = m_position.x + (bounds.yMax - m_position.y) / m_speed.dy * m_speed.dx; - double yMinX = m_position.x + (bounds.yMin - m_position.y) / m_speed.dy * m_speed.dx; - bool xMaxOk = xMaxY <= bounds.yMax && xMaxY >= bounds.yMin; - bool xMinOk = xMinY <= bounds.yMax && xMinY >= bounds.yMin; - bool yMaxOk = yMaxX <= bounds.xMax && yMaxX >= bounds.xMin; - bool yMinOk = yMinX <= bounds.xMax && yMinX >= bounds.xMin; - if (xMaxOk && m_speed.dx >= 0) - { - return Position2D (bounds.xMax, xMaxY); - } - else if (xMinOk && m_speed.dx <= 0) - { - return Position2D (bounds.xMin, xMinY); - } - else if (yMaxOk && m_speed.dy >= 0) - { - return Position2D (yMaxX, bounds.yMax); - } - else if (yMinOk && m_speed.dy <= 0) - { - return Position2D (yMinX, bounds.yMin); - } - else - { - NS_ASSERT (false); - // quiet compiler - return Position2D (0.0, 0.0); - } -} -double -MobilityModelHelper::Distance (const Position2D &a, const Position2D &b) const -{ - double dx = a.x - b.x; - double dy = a.y - b.y; - return sqrt (dx * dx + dy * dy); -} - -Time -MobilityModelHelper::Reset (const struct AreaBounds &bounds, - double direction, - double speed, - const Time &maxMovementDelay, - const Time &pauseDelay) -{ - Update (bounds); - m_pauseEnd = Simulator::Now () + pauseDelay; - m_speed.dx = std::cos (direction) * speed; - m_speed.dy = std::sin (direction) * speed; - Position2D intersection = CalculateIntersection (bounds); - Time seconds = Seconds (Distance (intersection, m_position) / speed); - seconds = std::min (seconds, maxMovementDelay); - return seconds; -} -Time -MobilityModelHelper::Reset (const struct AreaBounds &bounds, - double direction, - double speed, - double maxDistance, - const Time &pauseDelay) -{ - Update (bounds); - m_pauseEnd = Simulator::Now () + pauseDelay; - m_speed.dx = std::cos (direction) * speed; - m_speed.dy = std::sin (direction) * speed; - Position2D intersection = CalculateIntersection (bounds); - double distance = Distance (intersection, m_position); - distance = std::min (distance, maxDistance); - double seconds = distance / speed; - return Seconds (seconds); -} -Time -MobilityModelHelper::Reset (const struct AreaBounds &bounds, - double direction, - double speed, - const Time &pauseDelay) -{ - Update (bounds); - m_pauseEnd = Simulator::Now () + pauseDelay; - m_speed.dx = std::cos (direction) * speed; - m_speed.dy = std::sin (direction) * speed; - Position2D intersection = CalculateIntersection (bounds); - double seconds = Distance (intersection, m_position) / speed; - return Seconds (seconds); -} - -Position2D -MobilityModelHelper::GetCurrentPosition (const struct AreaBounds &bounds) const -{ - Update (bounds); - return m_position; -} - -Speed2D -MobilityModelHelper::GetSpeed (void) const -{ - return m_speed; -} - -enum MobilityModelHelper::Side -MobilityModelHelper::GetSide (const struct AreaBounds &bounds) const -{ - if (m_position.x == bounds.xMin) - { - return MobilityModelHelper::LEFT; - } - if (m_position.x == bounds.xMax) - { - return MobilityModelHelper::RIGHT; - } - if (m_position.y == bounds.yMin) - { - return MobilityModelHelper::BOTTOM; - } - if (m_position.y == bounds.yMax) - { - return MobilityModelHelper::TOP; - } - return MobilityModelHelper::NONE; -} - -} // namespace ns3 diff --git a/src/node/mobility-model-helper.h b/src/node/mobility-model-helper.h deleted file mode 100644 index c92514f5b..000000000 --- a/src/node/mobility-model-helper.h +++ /dev/null @@ -1,80 +0,0 @@ -#ifndef MOBILITY_MODEL_HELPER_H -#define MOBILITY_MODEL_HELPER_H - -#include "ns3/nstime.h" - -namespace ns3 { - -class Position2D -{ -public: - Position2D (double _x, double _y); - Position2D (); - double x; - double y; -}; - -struct AreaBounds -{ - double xMin; - double xMax; - double yMin; - double yMax; -}; - -class Speed2D -{ -public: - Speed2D (double _dx, double _dy); - Speed2D (); - double dx; - double dy; -}; - -class MobilityModelHelper -{ - public: - enum Side { - LEFT, - RIGHT, - TOP, - BOTTOM, - NONE - }; - MobilityModelHelper (); - void InitializePosition (const struct Position2D &position); - Time Reset (const struct AreaBounds &bounds, - double direction, - double speed, - const Time &maxMovementDelay, - const Time &pauseDelay); - Time Reset (const struct AreaBounds &bounds, - double direction, - double speed, - double maxDistance, - const Time &pauseDelay); - // move in specified direction until - // we hit the area bounds. - // return delay until we reach the bounds. - Time Reset (const struct AreaBounds &bounds, - double direction, - double speed, - const Time &pauseDelay); - - Position2D GetCurrentPosition (const struct AreaBounds &bounds) const; - Speed2D GetSpeed (void) const; - enum MobilityModelHelper::Side GetSide (const struct AreaBounds &bounds) const; - - private: - double Distance (const Position2D &a, const Position2D &b) const; - void Update (const struct AreaBounds &bounds) const; - Position2D CalculateIntersection (const struct AreaBounds &bounds) const; - mutable Position2D m_position; - Speed2D m_speed; - mutable Time m_lastUpdate; - Time m_pauseEnd; -}; - -} // namespace ns3 - -#endif /* MOBILITY_MODEL_HELPER_H */ diff --git a/src/node/random-direction-mobility-model.cc b/src/node/random-direction-mobility-model.cc index a2f59fbca..a847628fe 100644 --- a/src/node/random-direction-mobility-model.cc +++ b/src/node/random-direction-mobility-model.cc @@ -31,8 +31,8 @@ const double RandomDirectionMobilityModel::PI = 3.1415; const InterfaceId RandomDirectionMobilityModel::iid = MakeInterfaceId ("RandomDirectionMobilityModel", MobilityModel::iid); const ClassId RandomDirectionMobilityModel::cid = - MakeClassId ("RandomDirectionMobilityModel", - RandomDirectionMobilityModel::iid); + MakeClassId ("RandomDirectionMobilityModel", + RandomDirectionMobilityModel::iid); static RandomVariableDefaultValue @@ -42,44 +42,29 @@ static RandomVariableDefaultValue static RandomVariableDefaultValue g_pauseVariable ("RandomDirectionPause", - "A random variable to control the duration of of the pause of a RandomDiretion mobility model.", + "A random variable to control the duration " + "of the pause of a RandomDiretion mobility model.", "Constant:2"); static RectangleDefaultValue - g_rectangle ("RandomDirectionArea", + g_bounds ("RandomDirectionArea", "The bounding area for the RandomDirection model.", -100, 100, -100, 100); RandomDirectionParameters::RandomDirectionParameters () - : m_speedVariable (g_speedVariable.GetCopy ()), + : m_bounds (g_bounds.GetValue ()), + m_speedVariable (g_speedVariable.GetCopy ()), m_pauseVariable (g_pauseVariable.GetCopy ()) -{ - m_bounds.xMin = g_rectangle.GetMinX (); - m_bounds.xMax = g_rectangle.GetMaxX (); - m_bounds.yMin = g_rectangle.GetMinY (); - m_bounds.yMax = g_rectangle.GetMaxY (); -} -RandomDirectionParameters::RandomDirectionParameters (double xMin, double xMax, double yMin, double yMax) - : m_speedVariable (g_speedVariable.GetCopy ()), - m_pauseVariable (g_pauseVariable.GetCopy ()) -{ - m_bounds.xMin = xMin; - m_bounds.xMax = xMax; - m_bounds.yMin = yMin; - m_bounds.yMax = yMax; -} -RandomDirectionParameters::RandomDirectionParameters (double xMin, double xMax, double yMin, double yMax, + +{} +RandomDirectionParameters::RandomDirectionParameters (const Rectangle &bounds, const RandomVariable &speedVariable, const RandomVariable &pauseVariable) - : m_speedVariable (speedVariable.Copy ()), + : m_bounds (bounds), + m_speedVariable (speedVariable.Copy ()), m_pauseVariable (pauseVariable.Copy ()) -{ - m_bounds.xMin = xMin; - m_bounds.xMax = xMax; - m_bounds.yMin = yMin; - m_bounds.yMax = yMax; -} +{} RandomDirectionParameters::~RandomDirectionParameters () { @@ -102,26 +87,22 @@ RandomDirectionParameters::SetPause (const RandomVariable &pauseVariable) m_pauseVariable = pauseVariable.Copy (); } void -RandomDirectionParameters::SetBounds (double xMin, double xMax, double yMin, double yMax) +RandomDirectionParameters::SetBounds (const Rectangle &bounds) { - m_bounds.xMin = xMin; - m_bounds.yMin = yMin; - m_bounds.xMax = xMax; - m_bounds.yMax = yMax; + m_bounds = bounds; } Ptr -RandomDirectionMobilityModel::GetDefaultParameters (void) +RandomDirectionParameters::GetCurrent (void) { - static Ptr parameters = Create (); - if (parameters->m_bounds.xMin != g_rectangle.GetMinX () || - parameters->m_bounds.yMin != g_rectangle.GetMinY () || - parameters->m_bounds.xMax != g_rectangle.GetMaxX () || - parameters->m_bounds.yMax != g_rectangle.GetMaxY () || + static Ptr parameters = 0; + if (parameters == 0 || + g_bounds.IsDirty () || g_speedVariable.IsDirty () || g_pauseVariable.IsDirty ()) { parameters = Create (); + g_bounds.ClearDirtyFlag (); g_speedVariable.ClearDirtyFlag (); g_pauseVariable.ClearDirtyFlag (); } @@ -130,29 +111,16 @@ RandomDirectionMobilityModel::GetDefaultParameters (void) RandomDirectionMobilityModel::RandomDirectionMobilityModel () - : m_parameters (GetDefaultParameters ()) + : m_parameters (RandomDirectionParameters::GetCurrent ()) { SetInterfaceId (RandomDirectionMobilityModel::iid); - InitializeDirectionAndSpeed (); -} -RandomDirectionMobilityModel::RandomDirectionMobilityModel (double x, double y) - : m_parameters (GetDefaultParameters ()) -{ - SetInterfaceId (RandomDirectionMobilityModel::iid); - InitializeDirectionAndSpeed (); + m_event = Simulator::ScheduleNow (&RandomDirectionMobilityModel::Start, this); } RandomDirectionMobilityModel::RandomDirectionMobilityModel (Ptr parameters) : m_parameters (parameters) { SetInterfaceId (RandomDirectionMobilityModel::iid); - InitializeDirectionAndSpeed (); -} -RandomDirectionMobilityModel::RandomDirectionMobilityModel (Ptr parameters, - double x, double y) - : m_parameters (parameters) -{ - SetInterfaceId (RandomDirectionMobilityModel::iid); - InitializeDirectionAndSpeed (); + m_event = Simulator::ScheduleNow (&RandomDirectionMobilityModel::Start, this); } void RandomDirectionMobilityModel::DoDispose (void) @@ -162,7 +130,7 @@ RandomDirectionMobilityModel::DoDispose (void) MobilityModel::DoDispose (); } void -RandomDirectionMobilityModel::InitializeDirectionAndSpeed (void) +RandomDirectionMobilityModel::Start (void) { double direction = UniformVariable::GetSingleValue (0, 2 * PI); SetDirectionAndSpeed (direction); @@ -171,53 +139,52 @@ void RandomDirectionMobilityModel::SetDirectionAndSpeed (double direction) { double speed = m_parameters->m_speedVariable->GetValue (); + const Speed vector (std::cos (direction) * speed, + std::sin (direction) * speed, + 0.0); Time pause = Seconds (m_parameters->m_pauseVariable->GetValue ()); - Time delay = m_helper.Reset (m_parameters->m_bounds, - direction, speed, - pause); + Position position = m_helper.GetCurrentPosition (m_parameters->m_bounds); + m_helper.Reset (vector, pause); + Position next = m_parameters->m_bounds.CalculateIntersection (position, vector); + Time delay = Seconds (CalculateDistance (position, next) / speed); m_event = Simulator::Schedule (delay + pause, &RandomDirectionMobilityModel::ResetDirectionAndSpeed, this); + NotifyCourseChange (); } void RandomDirectionMobilityModel::ResetDirectionAndSpeed (void) { double direction = UniformVariable::GetSingleValue (0, PI); - switch (m_helper.GetSide (m_parameters->m_bounds)) + Position position = m_helper.GetCurrentPosition (m_parameters->m_bounds); + switch (m_parameters->m_bounds.GetClosestSide (position)) { - case MobilityModelHelper::RIGHT: + case Rectangle::RIGHT: direction += PI / 2; break; - case MobilityModelHelper::LEFT: + case Rectangle::LEFT: direction += - PI / 2; break; - case MobilityModelHelper::TOP: + case Rectangle::TOP: direction += PI; break; - case MobilityModelHelper::BOTTOM: + case Rectangle::BOTTOM: direction += 0.0; break; - case MobilityModelHelper::NONE: - NS_ASSERT (false); - break; } SetDirectionAndSpeed (direction); - NotifyCourseChange (); } Position RandomDirectionMobilityModel::DoGet (void) const { - Position2D position = m_helper.GetCurrentPosition (m_parameters->m_bounds); - return Position (position.x, position.y, 0.0); + return m_helper.GetCurrentPosition (m_parameters->m_bounds); } void RandomDirectionMobilityModel::DoSet (const Position &position) { - Position2D pos (position.x, position.y); - m_helper.InitializePosition (pos); + m_helper.InitializePosition (position); Simulator::Remove (m_event); - InitializeDirectionAndSpeed (); - NotifyCourseChange (); + m_event = Simulator::ScheduleNow (&RandomDirectionMobilityModel::Start, this); } diff --git a/src/node/random-direction-mobility-model.h b/src/node/random-direction-mobility-model.h index 30acc0721..73c54eb0c 100644 --- a/src/node/random-direction-mobility-model.h +++ b/src/node/random-direction-mobility-model.h @@ -26,8 +26,9 @@ #include "ns3/nstime.h" #include "ns3/event-id.h" #include "ns3/component-manager.h" +#include "ns3/rectangle.h" #include "mobility-model.h" -#include "mobility-model-helper.h" +#include "static-speed-helper.h" namespace ns3 { @@ -37,22 +38,22 @@ class RandomDirectionParameters : public Object { public: RandomDirectionParameters (); - RandomDirectionParameters (double xMin, double xMax, double yMin, double yMax); - RandomDirectionParameters (double xMin, double xMax, double yMin, double yMax, + RandomDirectionParameters (const Rectangle &bounds, const RandomVariable &speedVariable, const RandomVariable &pauseVariable); virtual ~RandomDirectionParameters (); void SetSpeed (const RandomVariable &speedVariable); void SetPause (const RandomVariable &pauseVariable); - void SetBounds (double xMin, double xMax, double yMin, double yMax); + void SetBounds (const Rectangle &bounds); private: friend class RandomDirectionMobilityModel; - struct AreaBounds m_bounds; + + static Ptr GetCurrent (void); + + Rectangle m_bounds; RandomVariable *m_speedVariable; RandomVariable *m_pauseVariable; - std::string m_speedVariableValue; - std::string m_pauseVariableValue; }; class RandomDirectionMobilityModel : public MobilityModel @@ -62,12 +63,9 @@ class RandomDirectionMobilityModel : public MobilityModel static const ClassId cid; RandomDirectionMobilityModel (); - RandomDirectionMobilityModel (double x, double y); RandomDirectionMobilityModel (Ptr parameters); - RandomDirectionMobilityModel (Ptr parameters, - double x, double y); private: - static Ptr GetDefaultParameters (void); + void Start (void); void ResetDirectionAndSpeed (void); void SetDirectionAndSpeed (double direction); void InitializeDirectionAndSpeed (void); @@ -78,7 +76,7 @@ class RandomDirectionMobilityModel : public MobilityModel static const double PI; Ptr m_parameters; EventId m_event; - MobilityModelHelper m_helper; + StaticSpeedHelper m_helper; }; } // namespace ns3 From 9a367b2be73379cc176f9dd68c4ee2c9b3965f28 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 19 Jul 2007 10:17:02 +0200 Subject: [PATCH 080/148] use the helper from the static speed mobility model --- src/node/ns2-mobility-file-topology.cc | 2 +- src/node/static-speed-helper.cc | 15 ++++ src/node/static-speed-helper.h | 4 ++ src/node/static-speed-mobility-model.cc | 92 ++++--------------------- src/node/static-speed-mobility-model.h | 41 ++--------- 5 files changed, 42 insertions(+), 112 deletions(-) diff --git a/src/node/ns2-mobility-file-topology.cc b/src/node/ns2-mobility-file-topology.cc index 2c58bba30..ee44355b9 100644 --- a/src/node/ns2-mobility-file-topology.cc +++ b/src/node/ns2-mobility-file-topology.cc @@ -129,7 +129,7 @@ Ns2MobileFileTopology::LayoutObjectStore (const ObjectStore &store) const 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); + Speed (xSpeed, ySpeed, zSpeed)); } } file.close(); diff --git a/src/node/static-speed-helper.cc b/src/node/static-speed-helper.cc index 03e1f066d..b46f88721 100644 --- a/src/node/static-speed-helper.cc +++ b/src/node/static-speed-helper.cc @@ -6,12 +6,21 @@ namespace ns3 { StaticSpeedHelper::StaticSpeedHelper () {} +StaticSpeedHelper::StaticSpeedHelper (const Position &position) + : m_position (position) +{} +StaticSpeedHelper::StaticSpeedHelper (const Position &position, + const Speed &speed) + : m_position (position), + m_speed (speed) +{} void StaticSpeedHelper::InitializePosition (const Position &position) { m_position = position; m_speed.dx = 0.0; m_speed.dy = 0.0; + m_speed.dz = 0.0; m_lastUpdate = Simulator::Now (); m_pauseEnd = Simulator::Now (); } @@ -35,6 +44,12 @@ StaticSpeedHelper::GetSpeed (void) const { return m_speed; } +void +StaticSpeedHelper::SetSpeed (const Speed &speed) +{ + Update (); + m_speed = speed; +} void StaticSpeedHelper::Update (void) const diff --git a/src/node/static-speed-helper.h b/src/node/static-speed-helper.h index 90fe0601a..0ba541b75 100644 --- a/src/node/static-speed-helper.h +++ b/src/node/static-speed-helper.h @@ -13,6 +13,9 @@ class StaticSpeedHelper { public: StaticSpeedHelper (); + StaticSpeedHelper (const Position &position); + StaticSpeedHelper (const Position &position, + const Speed &speed); void InitializePosition (const Position &position); void Reset (const Speed &speed, const Time &pauseDelay); @@ -20,6 +23,7 @@ class StaticSpeedHelper Position GetCurrentPosition (const Rectangle &bounds) const; Position GetCurrentPosition (void) const; Speed GetSpeed (void) const; + void SetSpeed (const Speed &speed); private: void Update (void) const; diff --git a/src/node/static-speed-mobility-model.cc b/src/node/static-speed-mobility-model.cc index 217e0ac8d..eb622e241 100644 --- a/src/node/static-speed-mobility-model.cc +++ b/src/node/static-speed-mobility-model.cc @@ -23,54 +23,25 @@ namespace ns3 { -const InterfaceId StaticSpeedMobilityModel::iid = MakeInterfaceId ("StaticSpeedMobilityModel", MobilityModel::iid); +const InterfaceId StaticSpeedMobilityModel::iid = + MakeInterfaceId ("StaticSpeedMobilityModel", MobilityModel::iid); const ClassId StaticSpeedMobilityModel::cid = - MakeClassId ("StaticSpeedMobilityModel", - StaticSpeedMobilityModel::iid); + MakeClassId ("StaticSpeedMobilityModel", + StaticSpeedMobilityModel::iid); StaticSpeedMobilityModel::StaticSpeedMobilityModel () - : m_x (0.0), - m_y (0.0), - m_z (0.0), - m_dx (0.0), - m_dy (0.0), - m_dz (0.0), - m_prevTime (Simulator::Now ()) { SetInterfaceId (StaticSpeedMobilityModel::iid); } -StaticSpeedMobilityModel::StaticSpeedMobilityModel (double x, double y, double z) - : m_x (x), - m_y (y), - m_z (z), - m_dx (0.0), - m_dy (0.0), - m_dz (0.0), - m_prevTime (Simulator::Now ()) +StaticSpeedMobilityModel::StaticSpeedMobilityModel (const Position &position) + : m_helper (position) { SetInterfaceId (StaticSpeedMobilityModel::iid); } -StaticSpeedMobilityModel::StaticSpeedMobilityModel (double x, double y) - : m_x (x), - m_y (y), - m_z (0.0), - m_dx (0.0), - m_dy (0.0), - m_dz (0.0), - m_prevTime (Simulator::Now ()) -{ - SetInterfaceId (StaticSpeedMobilityModel::iid); -} -StaticSpeedMobilityModel::StaticSpeedMobilityModel (double x, double y, double z, - double dx, double dy, double dz) - : m_x (x), - m_y (y), - m_z (z), - m_dx (dx), - m_dy (dy), - m_dz (dz), - m_prevTime (Simulator::Now ()) +StaticSpeedMobilityModel::StaticSpeedMobilityModel (const Position &position, + const Speed &speed) + : m_helper (position, speed) { SetInterfaceId (StaticSpeedMobilityModel::iid); } @@ -79,56 +50,23 @@ StaticSpeedMobilityModel::~StaticSpeedMobilityModel () {} void -StaticSpeedMobilityModel::SetSpeed (double dx, double dy, double dz) +StaticSpeedMobilityModel::SetSpeed (const Speed speed) { - bool changed = false; - Update (); - if (m_dx != dx || m_dy != dy || m_dz != dz) - { - changed = true; - } - m_dx = dx; - m_dy = dy; - m_dz = dz; - if (changed) - { - NotifyCourseChange (); - } + m_helper.SetSpeed (speed); + NotifyCourseChange (); } -void -StaticSpeedMobilityModel::Update (void) const -{ - Time deltaTime = Simulator::Now () - m_prevTime; - m_prevTime = Simulator::Now (); - double deltaS = deltaTime.GetSeconds (); - m_x += m_dx * deltaS; - m_y += m_dy * deltaS; - m_z += m_dz * deltaS; -} Position StaticSpeedMobilityModel::DoGet (void) const { - Update (); - return Position (m_x, m_y, m_z); + return m_helper.GetCurrentPosition (); } void StaticSpeedMobilityModel::DoSet (const Position &position) { - bool changed = false; - Update (); - if (m_x != position.x || m_y != position.y || m_z != position.z) - { - changed = true; - } - m_x = position.x; - m_y = position.y; - m_z = position.z; - if (changed) - { - NotifyCourseChange (); - } + m_helper.InitializePosition (position); + NotifyCourseChange (); } }; // namespace ns3 diff --git a/src/node/static-speed-mobility-model.h b/src/node/static-speed-mobility-model.h index 41a1e16bd..3eab6eba8 100644 --- a/src/node/static-speed-mobility-model.h +++ b/src/node/static-speed-mobility-model.h @@ -25,6 +25,8 @@ #include "mobility-model.h" #include "ns3/nstime.h" #include "ns3/component-manager.h" +#include "static-speed-helper.h" +#include "speed.h" namespace ns3 { @@ -39,60 +41,31 @@ public: */ StaticSpeedMobilityModel (); /** - * \param x x coordinate - * \param y y coordinate - * - * Create a position located at coordinates (x,y,0) with - * speed (0,0,0). - * Unit is meters - */ - StaticSpeedMobilityModel (double x, double y); - /** - * \param x x coordinate - * \param y y coordinate - * \param z z coordinate - * * Create a position located at coordinates (x,y,z) with * speed (0,0,0). - * Unit is meters */ - StaticSpeedMobilityModel (double x, double y, double z); + StaticSpeedMobilityModel (const Position &position); /** - * \param x x coordinate - * \param y y coordinate - * \param z z coordinate - * \param dx x coordinate speed - * \param dy y coordinate speed - * \param dz z coordinate speed * * Create a position located at coordinates (x,y,z) with * speed (dx,dy,dz). * Unit is meters and meters/s */ - StaticSpeedMobilityModel (double x, double y, double z, - double dx, double dy, double dz); + StaticSpeedMobilityModel (const Position &position, + const Speed &speed); virtual ~StaticSpeedMobilityModel (); /* - * \param dx x coordinate speed - * \param dy y coordinate speed - * \param dz z coordinate speed * * Set the current speed now to (dx,dy,dz) * Unit is meters/s */ - void SetSpeed (double dx, double dy, double dz); + void SetSpeed (const Speed speed); private: virtual Position DoGet (void) const; virtual void DoSet (const Position &position); void Update (void) const; - mutable double m_x; - mutable double m_y; - mutable double m_z; - double m_dx; - double m_dy; - double m_dz; - mutable Time m_prevTime; + StaticSpeedHelper m_helper; }; }; // namespace ns3 From 7a3849ffdb079f9a6a20fd3c51331979d0e74ff6 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 19 Jul 2007 10:21:23 +0200 Subject: [PATCH 081/148] remove dead code --- src/node/random-mobility-model.h | 103 ------------------------------- 1 file changed, 103 deletions(-) delete mode 100644 src/node/random-mobility-model.h diff --git a/src/node/random-mobility-model.h b/src/node/random-mobility-model.h deleted file mode 100644 index 3341bfe42..000000000 --- a/src/node/random-mobility-model.h +++ /dev/null @@ -1,103 +0,0 @@ -/* -*- 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 RANDOM_MOBILITY_MODEL_H -#define RANDOM_MOBILITY_MODEL_H - -#include "ns3/object.h" -#include "ns3/ptr.h" -#include "ns3/nstime.h" -#include "ns3/event-id.h" -#include "ns3/component-manager.h" -#include "mobility-model.h" -#include "mobility-model-helper.h" - -namespace ns3 { - -class RandomVariable; - -class RandomMobilityModelParameters : public Object -{ - public: - RandomMobilityModelParameters (); - RandomMobilityModelParameters (double xMin, double xMax, double yMin, double yMax); - RandomMobilityModelParameters (double xMin, double xMax, double yMin, double yMax, - const RandomVariable &speedVariable); - virtual ~RandomMobilityModelParameters (); - - void SetSpeed (const RandomVariable &speedVariable); - void SetBounds (double xMin, double xMax, double yMin, double yMax); - void SetWalkModelDistance (double maxDistance); - void SetWalkModelDelay (Time maxDelay); - void SetWaypointModelDistance (const RandomVariable &pauseVariable, double maxDistance); - void SetWaypointModelDelay (const RandomVariable &pauseVariable, Time maxDelay); - void SetDirectionModel (const RandomVariable &pauseVariable); - void SetBoundlessModel (void); - - - // made public because need to use it for default values. - enum Type { - WALK_DISTANCE, - WALK_DELAY, - WAYPOINT_DISTANCE, - WAYPOINT_DELAY, - DIRECTION, - BOUNDLESS - }; - private: - void Initialize (void); - friend class RandomMobilityModel; - enum Type m_type; - struct AreaBounds m_bounds; - RandomVariable *m_speedVariable; - RandomVariable *m_pauseVariable; - double m_maxDistance; - Time m_maxDelay; -}; - -class RandomMobilityModel : public MobilityModel -{ - public: - static const InterfaceId iid; - static const ClassId cid; - - RandomMobilityModel (); - RandomMobilityModel (double x, double y); - RandomMobilityModel (Ptr parameters); - RandomMobilityModel (Ptr parameters, - double x, double y); - private: - static Ptr GetDefaultParameters (void); - void ResetDirectionAndSpeed (void); - void SetDirectionAndSpeed (double direction); - void InitializeDirectionAndSpeed (void); - virtual void DoDispose (void); - virtual Position DoGet (void) const; - virtual void DoSet (const Position &position); - - static const double PI; - Ptr m_parameters; - EventId m_event; - MobilityModelHelper m_helper; -}; - -} // namespace ns3 - -#endif /* RANDOM_MOBILITY_MODEL_H */ From 39ba117abfe1d2d74b3f1899a8af174bd253fc20 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 19 Jul 2007 10:21:27 +0200 Subject: [PATCH 082/148] remove dead code --- src/node/random-mobility-model.cc | 418 ------------------------------ 1 file changed, 418 deletions(-) delete mode 100644 src/node/random-mobility-model.cc diff --git a/src/node/random-mobility-model.cc b/src/node/random-mobility-model.cc deleted file mode 100644 index fb357747b..000000000 --- a/src/node/random-mobility-model.cc +++ /dev/null @@ -1,418 +0,0 @@ -/* -*- 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/random-variable-default-value.h" -#include "ns3/rectangle-default-value.h" -#include "ns3/time-default-value.h" -#include "ns3/simulator.h" -#include -#include -#include "random-mobility-model.h" - -namespace ns3 { - -const double RandomMobilityModel::PI = 3.1415; -const InterfaceId RandomMobilityModel::iid = - MakeInterfaceId ("RandomMobilityModel", MobilityModel::iid); -const ClassId RandomMobilityModel::cid = - MakeClassId ("RandomMobilityModel", - RandomMobilityModel::iid); - - -static RandomVariableDefaultValue - g_speedVariable ("RandomMobilityModelSpeed", - "A random variable to control the speed of a random mobility model.", - "Uniform:1:2"); - -static RectangleDefaultValue - g_rectangle ("RandomMobilityModelArea", - "The bounding area for the Random model.", - -100, 100, -100, 100); - -static IntegerDefaultValue - g_walkDistance ("RandomWalkMaxDistance", - "The maximum distance (meters) an object moves before changing speed/direction for a " - "random walk model", - 2.0); - -static IntegerDefaultValue - g_waypointDistance ("RandomWaypointMaxDistance", - "The maximum distance (meters) an object moves before changing speed/direction for a " - "random waypoint model", - 2.0); - -static TimeDefaultValue - g_walkDelay ("RandomWalkMaxDelay", - "The maximum delay an object moves before changing speed/direction for " - "a random walk model.", - Seconds (1.0)); - -static TimeDefaultValue - g_waypointDelay ("RandomWaypointMaxDelay", - "The maximum delay an object moves before changing speed/direction for " - "a random waypoint model.", - Seconds (1.0)); - - -static RandomVariableDefaultValue - g_directionPauseVariable ("RandomDirectionMobilityModelPause", - "A random variable to control the duration of the " - "pause of a random direction mobility model.", - "Uniform:2:4"); - -static RandomVariableDefaultValue - g_waypointPauseVariable ("RandomWaypointMobilityModelPause", - "A random variable to control the duration of the " - "pause of a random waypoint mobility model.", - "Uniform:2:4"); - -static EnumDefaultValue - g_typeVariable ("RandomMobilityModelType", - "The type of mobility model to use.", - RandomMobilityModelParameters::WALK_DISTANCE, "WalkDistance", - RandomMobilityModelParameters::WALK_DELAY, "WalkDelay", - RandomMobilityModelParameters::WAYPOINT_DISTANCE, "WaypointDistance", - RandomMobilityModelParameters::WAYPOINT_DELAY, "WaypointDelay", - RandomMobilityModelParameters::DIRECTION, "Direction", - RandomMobilityModelParameters::BOUNDLESS, "Boundless", - 0, (void*)0); - - -RandomMobilityModelParameters::RandomMobilityModelParameters () - : m_type (g_typeVariable.GetValue ()), - m_speedVariable (g_speedVariable.GetCopy ()) -{ - m_bounds.xMin = g_rectangle.GetMinX (); - m_bounds.xMax = g_rectangle.GetMaxX (); - m_bounds.yMin = g_rectangle.GetMinY (); - m_bounds.yMax = g_rectangle.GetMaxY (); - Initialize (); -} -RandomMobilityModelParameters::RandomMobilityModelParameters (double xMin, double xMax, double yMin, double yMax) - : m_type (g_typeVariable.GetValue ()), - m_speedVariable (g_speedVariable.GetCopy ()) -{ - m_bounds.xMin = xMin; - m_bounds.xMax = xMax; - m_bounds.yMin = yMin; - m_bounds.yMax = yMax; - Initialize (); -} -RandomMobilityModelParameters::RandomMobilityModelParameters (double xMin, double xMax, double yMin, double yMax, - const RandomVariable &speedVariable) - : m_type (g_typeVariable.GetValue ()), - m_speedVariable (speedVariable.Copy ()) -{ - m_bounds.xMin = xMin; - m_bounds.xMax = xMax; - m_bounds.yMin = yMin; - m_bounds.yMax = yMax; - Initialize (); -} - -void -RandomMobilityModelParameters::Initialize (void) -{ - switch (m_type) - { - case RandomMobilityModelParameters::WAYPOINT_DISTANCE: - m_maxDistance = g_waypointDistance.GetValue (); - m_pauseVariable = g_waypointPauseVariable.GetCopy (); - break; - case RandomMobilityModelParameters::WAYPOINT_DELAY: - m_maxDelay = g_waypointDelay.GetValue (); - m_pauseVariable = g_waypointPauseVariable.GetCopy (); - break; - case RandomMobilityModelParameters::WALK_DISTANCE: - m_maxDistance = g_walkDistance.GetValue (); - break; - case RandomMobilityModelParameters::WALK_DELAY: - m_maxDelay = g_walkDelay.GetValue (); - break; - case RandomMobilityModelParameters::DIRECTION: - m_pauseVariable = g_directionPauseVariable.GetCopy (); - break; - case RandomMobilityModelParameters::BOUNDLESS: - break; - } -} - -RandomMobilityModelParameters::~RandomMobilityModelParameters () -{ - delete m_speedVariable; - delete m_pauseVariable; - m_speedVariable = 0; - m_pauseVariable = 0; -} - -void -RandomMobilityModelParameters::SetSpeed (const RandomVariable &speedVariable) -{ - delete m_speedVariable; - m_speedVariable = speedVariable.Copy (); -} -void -RandomMobilityModelParameters::SetWalkModelDistance (double maxDistance) -{ - m_type = RandomMobilityModelParameters::WALK_DISTANCE; - m_maxDistance = maxDistance; -} -void -RandomMobilityModelParameters::SetWalkModelDelay (Time maxDelay) -{ - m_type = RandomMobilityModelParameters::WALK_DELAY; - m_maxDelay = maxDelay; -} -void -RandomMobilityModelParameters::SetWaypointModelDistance (const RandomVariable &pauseVariable, - double maxDistance) -{ - m_type = RandomMobilityModelParameters::WAYPOINT_DISTANCE; - delete m_pauseVariable; - m_pauseVariable = pauseVariable.Copy (); - m_maxDistance = maxDistance; -} -void -RandomMobilityModelParameters::SetWaypointModelDelay (const RandomVariable &pauseVariable, Time maxDelay) -{ - m_type = RandomMobilityModelParameters::WAYPOINT_DELAY; - delete m_pauseVariable; - m_pauseVariable = pauseVariable.Copy (); - m_maxDelay = maxDelay; -} - -void -RandomMobilityModelParameters::SetDirectionModel (const RandomVariable &pauseVariable) -{ - m_type = RandomMobilityModelParameters::DIRECTION; - delete m_pauseVariable; - m_pauseVariable = pauseVariable.Copy (); -} -void -RandomMobilityModelParameters::SetBoundlessModel (void) -{ - m_type = RandomMobilityModelParameters::BOUNDLESS; -} - -void -RandomMobilityModelParameters::SetBounds (double xMin, double xMax, double yMin, double yMax) -{ - m_bounds.xMin = xMin; - m_bounds.yMin = yMin; - m_bounds.xMax = xMax; - m_bounds.yMax = yMax; -} - -Ptr -RandomMobilityModel::GetDefaultParameters (void) -{ - static Ptr parameters = Create (); - if (g_rectangle.IsDirty () || - g_typeVariable.IsDirty () || - g_speedVariable.IsDirty () || - g_waypointPauseVariable.IsDirty () || - g_directionPauseVariable.IsDirty () || - g_walkDistance.IsDirty () || - g_waypointDistance.IsDirty () || - g_walkDelay.IsDirty () || - g_waypointDelay.IsDirty ()) - { - parameters = Create (); - g_rectangle.ClearDirtyFlag (); - g_typeVariable.ClearDirtyFlag (); - g_speedVariable.ClearDirtyFlag (); - g_waypointPauseVariable.ClearDirtyFlag (); - g_directionPauseVariable.ClearDirtyFlag (); - g_walkDelay.ClearDirtyFlag (); - g_waypointDelay.ClearDirtyFlag (); - g_walkDistance.ClearDirtyFlag (); - g_waypointDistance.ClearDirtyFlag (); - } - return parameters; -} - - -RandomMobilityModel::RandomMobilityModel () - : m_parameters (GetDefaultParameters ()) -{ - SetInterfaceId (RandomMobilityModel::iid); - InitializeDirectionAndSpeed (); -} -RandomMobilityModel::RandomMobilityModel (double x, double y) - : m_parameters (GetDefaultParameters ()) -{ - SetInterfaceId (RandomMobilityModel::iid); - InitializeDirectionAndSpeed (); -} -RandomMobilityModel::RandomMobilityModel (Ptr parameters) - : m_parameters (parameters) -{ - SetInterfaceId (RandomMobilityModel::iid); - InitializeDirectionAndSpeed (); -} -RandomMobilityModel::RandomMobilityModel (Ptr parameters, - double x, double y) - : m_parameters (parameters) -{ - SetInterfaceId (RandomMobilityModel::iid); - InitializeDirectionAndSpeed (); -} -void -RandomMobilityModel::DoDispose (void) -{ - m_parameters = 0; - // chain up. - MobilityModel::DoDispose (); -} -void -RandomMobilityModel::InitializeDirectionAndSpeed (void) -{ - double direction = UniformVariable::GetSingleValue (0, 2 * PI); - SetDirectionAndSpeed (direction); -} -void -RandomMobilityModel::SetDirectionAndSpeed (double direction) -{ - double speed = m_parameters->m_speedVariable->GetValue (); - Time pause; - Time delay; - switch (m_parameters->m_type) - { - case RandomMobilityModelParameters::WALK_DISTANCE: - { - pause = MicroSeconds (0); - Time delay = m_helper.Reset (m_parameters->m_bounds, - direction, speed, - m_parameters->m_maxDistance, - pause); - } break; - case RandomMobilityModelParameters::WALK_DELAY: - { - pause = MicroSeconds (0); - Time delay = m_helper.Reset (m_parameters->m_bounds, - direction, speed, - m_parameters->m_maxDelay, - pause); - } break; - case RandomMobilityModelParameters::WAYPOINT_DISTANCE: - { - pause = Seconds (m_parameters->m_pauseVariable->GetValue ()); - Time delay = m_helper.Reset (m_parameters->m_bounds, - direction, speed, - m_parameters->m_maxDistance, - pause); - } break; - break; - case RandomMobilityModelParameters::WAYPOINT_DELAY: - { - pause = Seconds (m_parameters->m_pauseVariable->GetValue ()); - Time delay = m_helper.Reset (m_parameters->m_bounds, - direction, speed, - m_parameters->m_maxDelay, - pause); - } break; - case RandomMobilityModelParameters::DIRECTION: - { - pause = Seconds (m_parameters->m_pauseVariable->GetValue ()); - Time delay = m_helper.Reset (m_parameters->m_bounds, - direction, speed, - pause); - } break; - case RandomMobilityModelParameters::BOUNDLESS: - break; - } - m_event = Simulator::Schedule (delay + pause, - &RandomMobilityModel::ResetDirectionAndSpeed, this); -} -void -RandomMobilityModel::ResetDirectionAndSpeed (void) -{ - double direction; - switch (m_parameters->m_type) - { - default: - //XXX - break; - case RandomMobilityModelParameters::WALK_DISTANCE: - { - switch (m_helper.GetSide (m_parameters->m_bounds)) - { - case MobilityModelHelper::RIGHT: - - break; - case MobilityModelHelper::LEFT: - direction += - PI / 2; - break; - case MobilityModelHelper::TOP: - direction += PI; - break; - case MobilityModelHelper::BOTTOM: - direction += 0.0; - break; - case MobilityModelHelper::NONE: - NS_ASSERT (false); - break; - } - } break; - case RandomMobilityModelParameters::DIRECTION: - { - direction = UniformVariable::GetSingleValue (0, PI); - switch (m_helper.GetSide (m_parameters->m_bounds)) - { - case MobilityModelHelper::RIGHT: - direction += PI / 2; - break; - case MobilityModelHelper::LEFT: - direction += - PI / 2; - break; - case MobilityModelHelper::TOP: - direction += PI; - break; - case MobilityModelHelper::BOTTOM: - direction += 0.0; - break; - case MobilityModelHelper::NONE: - NS_ASSERT (false); - break; - } - } break; - } - SetDirectionAndSpeed (direction); - NotifyCourseChange (); -} -Position -RandomMobilityModel::DoGet (void) const -{ - Position2D position = m_helper.GetCurrentPosition (m_parameters->m_bounds); - return Position (position.x, position.y, 0.0); -} -void -RandomMobilityModel::DoSet (const Position &position) -{ - Position2D pos (position.x, position.y); - m_helper.InitializePosition (pos); - Simulator::Remove (m_event); - InitializeDirectionAndSpeed (); - NotifyCourseChange (); -} - - - -} // namespace ns3 From b2d7640a1df2881cfbbf03d7f10db451de750377 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 19 Jul 2007 10:28:48 +0200 Subject: [PATCH 083/148] add MobilityModel::GetSpeed method and implement it in all mobility models --- src/node/hierarchical-mobility-model.cc | 10 ++++++ src/node/hierarchical-mobility-model.h | 1 + src/node/mobility-model.cc | 5 +++ src/node/mobility-model.h | 4 ++- src/node/random-direction-mobility-model.cc | 5 +++ src/node/random-direction-mobility-model.h | 1 + src/node/random-walk-2d-mobility-model.cc | 6 ++++ src/node/random-walk-2d-mobility-model.h | 1 + src/node/random-waypoint-mobility-model.cc | 5 +++ src/node/random-waypoint-mobility-model.h | 2 ++ src/node/static-mobility-model.cc | 39 +++++++-------------- src/node/static-mobility-model.h | 20 +++-------- src/node/static-speed-mobility-model.cc | 5 +++ src/node/static-speed-mobility-model.h | 1 + 14 files changed, 62 insertions(+), 43 deletions(-) diff --git a/src/node/hierarchical-mobility-model.cc b/src/node/hierarchical-mobility-model.cc index 2c2e84f21..7e4aa3fe3 100644 --- a/src/node/hierarchical-mobility-model.cc +++ b/src/node/hierarchical-mobility-model.cc @@ -77,6 +77,16 @@ HierarchicalMobilityModel::DoSet (const Position &position) position.z - parentPosition.z); m_child->Set (childPosition); } +Speed +HierarchicalMobilityModel::DoGetSpeed (void) const +{ + Speed parentSpeed = m_parent->GetSpeed (); + Speed childSpeed = m_child->GetSpeed (); + Speed speed (parentSpeed.dx + childSpeed.dx, + parentSpeed.dy + childSpeed.dy, + parentSpeed.dz + childSpeed.dz); + return speed; +} void HierarchicalMobilityModel::ParentChanged (Ptr model) diff --git a/src/node/hierarchical-mobility-model.h b/src/node/hierarchical-mobility-model.h index a1bb61713..e17555387 100644 --- a/src/node/hierarchical-mobility-model.h +++ b/src/node/hierarchical-mobility-model.h @@ -38,6 +38,7 @@ public: private: virtual Position DoGet (void) const; virtual void DoSet (const Position &position); + virtual Speed DoGetSpeed (void) const; void ParentChanged (Ptr model); void ChildChanged (Ptr model); diff --git a/src/node/mobility-model.cc b/src/node/mobility-model.cc index 4e1de6e41..033f9d50b 100644 --- a/src/node/mobility-model.cc +++ b/src/node/mobility-model.cc @@ -39,6 +39,11 @@ MobilityModel::Get (void) const { return DoGet (); } +Speed +MobilityModel::GetSpeed (void) const +{ + return DoGetSpeed (); +} void MobilityModel::Set (double x, double y, double z) diff --git a/src/node/mobility-model.h b/src/node/mobility-model.h index 7ba6b8d68..ef4a8b0bb 100644 --- a/src/node/mobility-model.h +++ b/src/node/mobility-model.h @@ -23,6 +23,7 @@ #include "ns3/object.h" #include "position.h" +#include "speed.h" namespace ns3 { @@ -45,9 +46,9 @@ public: * Unit is meters */ Position Get (void) const; - void Set (double x, double y, double z); void Set (const Position &position); + Speed GetSpeed (void) const; /** * \param position a reference to another position object instance * \returns the distance between the two objects. @@ -74,6 +75,7 @@ private: */ virtual Position DoGet (void) const = 0; virtual void DoSet (const Position &position) = 0; + virtual Speed DoGetSpeed (void) const = 0; }; }; // namespace ns3 diff --git a/src/node/random-direction-mobility-model.cc b/src/node/random-direction-mobility-model.cc index a847628fe..e2b5a614a 100644 --- a/src/node/random-direction-mobility-model.cc +++ b/src/node/random-direction-mobility-model.cc @@ -186,6 +186,11 @@ RandomDirectionMobilityModel::DoSet (const Position &position) Simulator::Remove (m_event); m_event = Simulator::ScheduleNow (&RandomDirectionMobilityModel::Start, this); } +Speed +RandomDirectionMobilityModel::DoGetSpeed (void) const +{ + return m_helper.GetSpeed (); +} diff --git a/src/node/random-direction-mobility-model.h b/src/node/random-direction-mobility-model.h index 73c54eb0c..9e14da559 100644 --- a/src/node/random-direction-mobility-model.h +++ b/src/node/random-direction-mobility-model.h @@ -72,6 +72,7 @@ class RandomDirectionMobilityModel : public MobilityModel virtual void DoDispose (void); virtual Position DoGet (void) const; virtual void DoSet (const Position &position); + virtual Speed DoGetSpeed (void) const; static const double PI; Ptr m_parameters; diff --git a/src/node/random-walk-2d-mobility-model.cc b/src/node/random-walk-2d-mobility-model.cc index ee0b82fc9..b8530ce4f 100644 --- a/src/node/random-walk-2d-mobility-model.cc +++ b/src/node/random-walk-2d-mobility-model.cc @@ -192,6 +192,12 @@ RandomWalk2dMobilityModel::DoSet (const Position &position) Simulator::Remove (m_event); m_event = Simulator::ScheduleNow (&RandomWalk2dMobilityModel::Start, this); } +Speed +RandomWalk2dMobilityModel::DoGetSpeed (void) const +{ + return m_helper.GetSpeed (); +} + } // namespace ns3 diff --git a/src/node/random-walk-2d-mobility-model.h b/src/node/random-walk-2d-mobility-model.h index d30ebfb87..d1cbd9aef 100644 --- a/src/node/random-walk-2d-mobility-model.h +++ b/src/node/random-walk-2d-mobility-model.h @@ -111,6 +111,7 @@ class RandomWalk2dMobilityModel : public MobilityModel virtual void DoDispose (void); virtual Position DoGet (void) const; virtual void DoSet (const Position &position); + virtual Speed DoGetSpeed (void) const; StaticSpeedHelper m_helper; EventId m_event; diff --git a/src/node/random-waypoint-mobility-model.cc b/src/node/random-waypoint-mobility-model.cc index 8e0460414..255af7aa6 100644 --- a/src/node/random-waypoint-mobility-model.cc +++ b/src/node/random-waypoint-mobility-model.cc @@ -122,6 +122,11 @@ RandomWaypointMobilityModel::DoSet (const Position &position) Simulator::Remove (m_event); Simulator::ScheduleNow (&RandomWaypointMobilityModel::Start, this); } +Speed +RandomWaypointMobilityModel::DoGetSpeed (void) const +{ + return m_helper.GetSpeed (); +} } // namespace ns3 diff --git a/src/node/random-waypoint-mobility-model.h b/src/node/random-waypoint-mobility-model.h index 7409b97e6..ae27037bc 100644 --- a/src/node/random-waypoint-mobility-model.h +++ b/src/node/random-waypoint-mobility-model.h @@ -60,6 +60,8 @@ private: void Start (void); virtual Position DoGet (void) const; virtual void DoSet (const Position &position); + virtual Speed DoGetSpeed (void) const; + StaticSpeedHelper m_helper; Ptr m_parameters; EventId m_event; diff --git a/src/node/static-mobility-model.cc b/src/node/static-mobility-model.cc index 980692660..e6d68f5c3 100644 --- a/src/node/static-mobility-model.cc +++ b/src/node/static-mobility-model.cc @@ -22,22 +22,15 @@ namespace ns3 { -const InterfaceId StaticMobilityModel::iid = MakeInterfaceId ("StaticMobilityModel", MobilityModel::iid); -const ClassId StaticMobilityModel::cid = MakeClassId ("StaticMobilityModel", - StaticMobilityModel::iid); - +const ClassId StaticMobilityModel::cid = MakeClassId ("StaticMobilityModel", + MobilityModel::iid); + StaticMobilityModel::StaticMobilityModel () - : m_x (0.0), m_y (0.0), m_z (0.0) { SetInterfaceId (StaticMobilityModel::iid); } -StaticMobilityModel::StaticMobilityModel (double x, double y) - : m_x (x), m_y (y), m_z (0.0) -{ - SetInterfaceId (StaticMobilityModel::iid); -} -StaticMobilityModel::StaticMobilityModel (double x, double y, double z) - : m_x (x), m_y (y), m_z (z) +StaticMobilityModel::StaticMobilityModel (const Position &position) + : m_position (position) { SetInterfaceId (StaticMobilityModel::iid); } @@ -47,24 +40,18 @@ StaticMobilityModel::~StaticMobilityModel () Position StaticMobilityModel::DoGet (void) const { - return Position (m_x, m_y, m_z); + return m_position; } void StaticMobilityModel::DoSet (const Position &position) { - bool changed = false; - if (m_x != position.x || m_y != position.y || m_z != position.z) - { - changed = true; - } - m_x = position.x; - m_y = position.y; - m_z = position.z; - if (changed) - { - NotifyCourseChange (); - } + m_position = position; + NotifyCourseChange (); +} +Speed +StaticMobilityModel::DoGetSpeed (void) const +{ + return Speed (); } - }; // namespace ns3 diff --git a/src/node/static-mobility-model.h b/src/node/static-mobility-model.h index d7600c1bd..9884c699a 100644 --- a/src/node/static-mobility-model.h +++ b/src/node/static-mobility-model.h @@ -34,37 +34,25 @@ namespace ns3 { class StaticMobilityModel : public MobilityModel { public: - static const InterfaceId iid; static const ClassId cid; /** * Create a position located at coordinates (0,0,0) */ StaticMobilityModel (); /** - * \param x x coordinate - * \param y y coordinate - * - * Create a position located at coordinates (x,y,0). - * Unit is meters - */ - StaticMobilityModel (double x, double y); - /** - * \param x x coordinate - * \param y y coordinate - * \param z z coordinate * * Create a position located at coordinates (x,y,z). * Unit is meters */ - StaticMobilityModel (double x, double y, double z); + StaticMobilityModel (const Position &position); virtual ~StaticMobilityModel (); private: virtual Position DoGet (void) const; virtual void DoSet (const Position &position); - double m_x; - double m_y; - double m_z; + virtual Speed DoGetSpeed (void) const; + + Position m_position; }; }; // namespace ns3 diff --git a/src/node/static-speed-mobility-model.cc b/src/node/static-speed-mobility-model.cc index eb622e241..d4cfc9873 100644 --- a/src/node/static-speed-mobility-model.cc +++ b/src/node/static-speed-mobility-model.cc @@ -68,5 +68,10 @@ StaticSpeedMobilityModel::DoSet (const Position &position) m_helper.InitializePosition (position); NotifyCourseChange (); } +Speed +StaticSpeedMobilityModel::DoGetSpeed (void) const +{ + return m_helper.GetSpeed (); +} }; // namespace ns3 diff --git a/src/node/static-speed-mobility-model.h b/src/node/static-speed-mobility-model.h index 3eab6eba8..1ceecbff5 100644 --- a/src/node/static-speed-mobility-model.h +++ b/src/node/static-speed-mobility-model.h @@ -64,6 +64,7 @@ public: private: virtual Position DoGet (void) const; virtual void DoSet (const Position &position); + virtual Speed DoGetSpeed (void) const; void Update (void) const; StaticSpeedHelper m_helper; }; From eadaabbe62fcf9b3ba8dbe0d4853b5eb2bf04a0e Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 19 Jul 2007 10:30:18 +0200 Subject: [PATCH 084/148] random-direction -> random-direction-2d --- SConstruct | 4 ++-- ...obility-model.cc => random-direction-2d-mobility-model.cc} | 2 +- ...-mobility-model.h => random-direction-2d-mobility-model.h} | 0 3 files changed, 3 insertions(+), 3 deletions(-) rename src/node/{random-direction-mobility-model.cc => random-direction-2d-mobility-model.cc} (99%) rename src/node/{random-direction-mobility-model.h => random-direction-2d-mobility-model.h} (100%) diff --git a/SConstruct b/SConstruct index ac117488f..d813deda3 100644 --- a/SConstruct +++ b/SConstruct @@ -263,7 +263,7 @@ node.add_sources ([ 'random-waypoint-mobility-model.cc', 'rectangle-default-value.cc', 'rectangle.cc', - 'random-direction-mobility-model.cc', + 'random-direction-2d-mobility-model.cc', ]) node.add_inst_headers ([ 'node.h', @@ -298,7 +298,7 @@ node.add_inst_headers ([ 'random-waypoint-mobility-model.h', 'rectangle-default-value.h', 'rectangle.h', - 'random-direction-mobility-model.h', + 'random-direction-2d-mobility-model.h', ]) applications = build.Ns3Module ('applications', 'src/applications') diff --git a/src/node/random-direction-mobility-model.cc b/src/node/random-direction-2d-mobility-model.cc similarity index 99% rename from src/node/random-direction-mobility-model.cc rename to src/node/random-direction-2d-mobility-model.cc index e2b5a614a..3f4ed93f4 100644 --- a/src/node/random-direction-mobility-model.cc +++ b/src/node/random-direction-2d-mobility-model.cc @@ -23,7 +23,7 @@ #include "ns3/simulator.h" #include #include -#include "random-direction-mobility-model.h" +#include "random-direction-2d-mobility-model.h" namespace ns3 { diff --git a/src/node/random-direction-mobility-model.h b/src/node/random-direction-2d-mobility-model.h similarity index 100% rename from src/node/random-direction-mobility-model.h rename to src/node/random-direction-2d-mobility-model.h From 5ec60a6a2ceff6c4451d7bd7030ee06b9e36cfde Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 19 Jul 2007 10:31:04 +0200 Subject: [PATCH 085/148] RandomDirection -> RandomDirection2d --- .../random-direction-2d-mobility-model.cc | 74 +++++++++---------- src/node/random-direction-2d-mobility-model.h | 20 ++--- 2 files changed, 47 insertions(+), 47 deletions(-) diff --git a/src/node/random-direction-2d-mobility-model.cc b/src/node/random-direction-2d-mobility-model.cc index 3f4ed93f4..2af5e792c 100644 --- a/src/node/random-direction-2d-mobility-model.cc +++ b/src/node/random-direction-2d-mobility-model.cc @@ -27,38 +27,38 @@ namespace ns3 { -const double RandomDirectionMobilityModel::PI = 3.1415; -const InterfaceId RandomDirectionMobilityModel::iid = - MakeInterfaceId ("RandomDirectionMobilityModel", MobilityModel::iid); -const ClassId RandomDirectionMobilityModel::cid = - MakeClassId ("RandomDirectionMobilityModel", - RandomDirectionMobilityModel::iid); +const double RandomDirection2dMobilityModel::PI = 3.1415; +const InterfaceId RandomDirection2dMobilityModel::iid = + MakeInterfaceId ("RandomDirection2dMobilityModel", MobilityModel::iid); +const ClassId RandomDirection2dMobilityModel::cid = + MakeClassId ("RandomDirection2dMobilityModel", + RandomDirection2dMobilityModel::iid); static RandomVariableDefaultValue - g_speedVariable ("RandomDirectionSpeed", - "A random variable to control the speed of a RandomDirection mobility model.", + g_speedVariable ("RandomDirection2dSpeed", + "A random variable to control the speed of a RandomDirection2d mobility model.", "Uniform:1:2"); static RandomVariableDefaultValue - g_pauseVariable ("RandomDirectionPause", + g_pauseVariable ("RandomDirection2dPause", "A random variable to control the duration " "of the pause of a RandomDiretion mobility model.", "Constant:2"); static RectangleDefaultValue - g_bounds ("RandomDirectionArea", - "The bounding area for the RandomDirection model.", + g_bounds ("RandomDirection2dArea", + "The bounding area for the RandomDirection2d model.", -100, 100, -100, 100); -RandomDirectionParameters::RandomDirectionParameters () +RandomDirection2dParameters::RandomDirection2dParameters () : m_bounds (g_bounds.GetValue ()), m_speedVariable (g_speedVariable.GetCopy ()), m_pauseVariable (g_pauseVariable.GetCopy ()) {} -RandomDirectionParameters::RandomDirectionParameters (const Rectangle &bounds, +RandomDirection2dParameters::RandomDirection2dParameters (const Rectangle &bounds, const RandomVariable &speedVariable, const RandomVariable &pauseVariable) : m_bounds (bounds), @@ -66,7 +66,7 @@ RandomDirectionParameters::RandomDirectionParameters (const Rectangle &bounds, m_pauseVariable (pauseVariable.Copy ()) {} -RandomDirectionParameters::~RandomDirectionParameters () +RandomDirection2dParameters::~RandomDirection2dParameters () { delete m_speedVariable; delete m_pauseVariable; @@ -75,33 +75,33 @@ RandomDirectionParameters::~RandomDirectionParameters () } void -RandomDirectionParameters::SetSpeed (const RandomVariable &speedVariable) +RandomDirection2dParameters::SetSpeed (const RandomVariable &speedVariable) { delete m_speedVariable; m_speedVariable = speedVariable.Copy (); } void -RandomDirectionParameters::SetPause (const RandomVariable &pauseVariable) +RandomDirection2dParameters::SetPause (const RandomVariable &pauseVariable) { delete m_pauseVariable; m_pauseVariable = pauseVariable.Copy (); } void -RandomDirectionParameters::SetBounds (const Rectangle &bounds) +RandomDirection2dParameters::SetBounds (const Rectangle &bounds) { m_bounds = bounds; } -Ptr -RandomDirectionParameters::GetCurrent (void) +Ptr +RandomDirection2dParameters::GetCurrent (void) { - static Ptr parameters = 0; + static Ptr parameters = 0; if (parameters == 0 || g_bounds.IsDirty () || g_speedVariable.IsDirty () || g_pauseVariable.IsDirty ()) { - parameters = Create (); + parameters = Create (); g_bounds.ClearDirtyFlag (); g_speedVariable.ClearDirtyFlag (); g_pauseVariable.ClearDirtyFlag (); @@ -110,33 +110,33 @@ RandomDirectionParameters::GetCurrent (void) } -RandomDirectionMobilityModel::RandomDirectionMobilityModel () - : m_parameters (RandomDirectionParameters::GetCurrent ()) +RandomDirection2dMobilityModel::RandomDirection2dMobilityModel () + : m_parameters (RandomDirection2dParameters::GetCurrent ()) { - SetInterfaceId (RandomDirectionMobilityModel::iid); - m_event = Simulator::ScheduleNow (&RandomDirectionMobilityModel::Start, this); + SetInterfaceId (RandomDirection2dMobilityModel::iid); + m_event = Simulator::ScheduleNow (&RandomDirection2dMobilityModel::Start, this); } -RandomDirectionMobilityModel::RandomDirectionMobilityModel (Ptr parameters) +RandomDirection2dMobilityModel::RandomDirection2dMobilityModel (Ptr parameters) : m_parameters (parameters) { - SetInterfaceId (RandomDirectionMobilityModel::iid); - m_event = Simulator::ScheduleNow (&RandomDirectionMobilityModel::Start, this); + SetInterfaceId (RandomDirection2dMobilityModel::iid); + m_event = Simulator::ScheduleNow (&RandomDirection2dMobilityModel::Start, this); } void -RandomDirectionMobilityModel::DoDispose (void) +RandomDirection2dMobilityModel::DoDispose (void) { m_parameters = 0; // chain up. MobilityModel::DoDispose (); } void -RandomDirectionMobilityModel::Start (void) +RandomDirection2dMobilityModel::Start (void) { double direction = UniformVariable::GetSingleValue (0, 2 * PI); SetDirectionAndSpeed (direction); } void -RandomDirectionMobilityModel::SetDirectionAndSpeed (double direction) +RandomDirection2dMobilityModel::SetDirectionAndSpeed (double direction) { double speed = m_parameters->m_speedVariable->GetValue (); const Speed vector (std::cos (direction) * speed, @@ -148,11 +148,11 @@ RandomDirectionMobilityModel::SetDirectionAndSpeed (double direction) Position next = m_parameters->m_bounds.CalculateIntersection (position, vector); Time delay = Seconds (CalculateDistance (position, next) / speed); m_event = Simulator::Schedule (delay + pause, - &RandomDirectionMobilityModel::ResetDirectionAndSpeed, this); + &RandomDirection2dMobilityModel::ResetDirectionAndSpeed, this); NotifyCourseChange (); } void -RandomDirectionMobilityModel::ResetDirectionAndSpeed (void) +RandomDirection2dMobilityModel::ResetDirectionAndSpeed (void) { double direction = UniformVariable::GetSingleValue (0, PI); @@ -175,19 +175,19 @@ RandomDirectionMobilityModel::ResetDirectionAndSpeed (void) SetDirectionAndSpeed (direction); } Position -RandomDirectionMobilityModel::DoGet (void) const +RandomDirection2dMobilityModel::DoGet (void) const { return m_helper.GetCurrentPosition (m_parameters->m_bounds); } void -RandomDirectionMobilityModel::DoSet (const Position &position) +RandomDirection2dMobilityModel::DoSet (const Position &position) { m_helper.InitializePosition (position); Simulator::Remove (m_event); - m_event = Simulator::ScheduleNow (&RandomDirectionMobilityModel::Start, this); + m_event = Simulator::ScheduleNow (&RandomDirection2dMobilityModel::Start, this); } Speed -RandomDirectionMobilityModel::DoGetSpeed (void) const +RandomDirection2dMobilityModel::DoGetSpeed (void) const { return m_helper.GetSpeed (); } diff --git a/src/node/random-direction-2d-mobility-model.h b/src/node/random-direction-2d-mobility-model.h index 9e14da559..db1f840aa 100644 --- a/src/node/random-direction-2d-mobility-model.h +++ b/src/node/random-direction-2d-mobility-model.h @@ -34,36 +34,36 @@ namespace ns3 { class RandomVariable; -class RandomDirectionParameters : public Object +class RandomDirection2dParameters : public Object { public: - RandomDirectionParameters (); - RandomDirectionParameters (const Rectangle &bounds, + RandomDirection2dParameters (); + RandomDirection2dParameters (const Rectangle &bounds, const RandomVariable &speedVariable, const RandomVariable &pauseVariable); - virtual ~RandomDirectionParameters (); + virtual ~RandomDirection2dParameters (); void SetSpeed (const RandomVariable &speedVariable); void SetPause (const RandomVariable &pauseVariable); void SetBounds (const Rectangle &bounds); private: - friend class RandomDirectionMobilityModel; + friend class RandomDirection2dMobilityModel; - static Ptr GetCurrent (void); + static Ptr GetCurrent (void); Rectangle m_bounds; RandomVariable *m_speedVariable; RandomVariable *m_pauseVariable; }; -class RandomDirectionMobilityModel : public MobilityModel +class RandomDirection2dMobilityModel : public MobilityModel { public: static const InterfaceId iid; static const ClassId cid; - RandomDirectionMobilityModel (); - RandomDirectionMobilityModel (Ptr parameters); + RandomDirection2dMobilityModel (); + RandomDirection2dMobilityModel (Ptr parameters); private: void Start (void); void ResetDirectionAndSpeed (void); @@ -75,7 +75,7 @@ class RandomDirectionMobilityModel : public MobilityModel virtual Speed DoGetSpeed (void) const; static const double PI; - Ptr m_parameters; + Ptr m_parameters; EventId m_event; StaticSpeedHelper m_helper; }; From 198b031d033371f8a0d3730d2dbaee0a2d5aa770 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 19 Jul 2007 10:43:41 +0200 Subject: [PATCH 086/148] remove dead code --- src/node/mobility-model.cc | 6 ------ src/node/mobility-model.h | 1 - 2 files changed, 7 deletions(-) diff --git a/src/node/mobility-model.cc b/src/node/mobility-model.cc index 033f9d50b..959e29a1e 100644 --- a/src/node/mobility-model.cc +++ b/src/node/mobility-model.cc @@ -45,12 +45,6 @@ MobilityModel::GetSpeed (void) const return DoGetSpeed (); } -void -MobilityModel::Set (double x, double y, double z) -{ - Position position (x, y, z); - DoSet (position); -} void MobilityModel::Set (const Position &position) { diff --git a/src/node/mobility-model.h b/src/node/mobility-model.h index ef4a8b0bb..f483378ad 100644 --- a/src/node/mobility-model.h +++ b/src/node/mobility-model.h @@ -46,7 +46,6 @@ public: * Unit is meters */ Position Get (void) const; - void Set (double x, double y, double z); void Set (const Position &position); Speed GetSpeed (void) const; /** From 4e2af32bcfae33fccd3cf7058c95ca9b4706ddfb Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 19 Jul 2007 10:46:50 +0200 Subject: [PATCH 087/148] doxygen for mobility models --- src/node/mobility-model.h | 35 +++++++++++++++-------- src/node/random-walk-2d-mobility-model.cc | 30 +++++++++++++++++++ src/node/random-walk-2d-mobility-model.h | 35 +++++++++++++++++++---- 3 files changed, 82 insertions(+), 18 deletions(-) diff --git a/src/node/mobility-model.h b/src/node/mobility-model.h index f483378ad..4c4cf9000 100644 --- a/src/node/mobility-model.h +++ b/src/node/mobility-model.h @@ -43,16 +43,19 @@ public: /** * \returns the current position - * Unit is meters */ Position Get (void) const; + /** + * \param position the position to set. + */ void Set (const Position &position); + /** + * \returns the current position. + */ Speed GetSpeed (void) const; /** - * \param position a reference to another position object instance - * \returns the distance between the two objects. - * - * Unit is meters + * \param position a reference to another mobility model + * \returns the distance between the two objects. Unit is meters. */ double GetDistanceFrom (const MobilityModel &position) const; protected: @@ -63,17 +66,25 @@ protected: void NotifyCourseChange (void) const; private: /** - * \param x reference to floating-point variable for x coordinate. - * \param y reference to floating-point variable for y coordinate. - * \param z reference to floating-point variable for z coordinate. + * \returns the current position. * - * Store in the x, y, and z variables the current coordinates - * managed by this position object. Concrete subclasses of this - * base class must implement this method. - * Unit is meters + * Concrete subclasses of this base class must + * implement this method. */ virtual Position DoGet (void) const = 0; + /** + * \param position the position to set. + * + * Concrete subclasses of this base class must + * implement this method. + */ virtual void DoSet (const Position &position) = 0; + /** + * \returns the current speed. + * + * Concrete subclasses of this base class must + * implement this method. + */ virtual Speed DoGetSpeed (void) const = 0; }; diff --git a/src/node/random-walk-2d-mobility-model.cc b/src/node/random-walk-2d-mobility-model.cc index b8530ce4f..158a32244 100644 --- a/src/node/random-walk-2d-mobility-model.cc +++ b/src/node/random-walk-2d-mobility-model.cc @@ -84,6 +84,36 @@ RandomWalk2dMobilityModelParameters::~RandomWalk2dMobilityModelParameters () m_direction = 0; } +void +RandomWalk2dMobilityModelParameters::SetSpeed (const RandomVariable &speed) +{ + delete m_speed; + m_speed = speed.Copy (); +} +void +RandomWalk2dMobilityModelParameters::SetDirection (const RandomVariable &direction) +{ + delete m_direction; + m_direction = direction.Copy (); +} +void +RandomWalk2dMobilityModelParameters::SetModeDistance (double distance) +{ + m_mode = RandomWalk2dMobilityModelParameters::MODE_DISTANCE; + m_modeDistance = distance; +} +void +RandomWalk2dMobilityModelParameters::SetModeTime (Time time) +{ + m_mode = RandomWalk2dMobilityModelParameters::MODE_TIME; + m_modeTime = time; +} +void +RandomWalk2dMobilityModelParameters::SetBounds (const Rectangle &bounds) +{ + m_bounds = bounds; +} + Ptr RandomWalk2dMobilityModelParameters::GetCurrent (void) { diff --git a/src/node/random-walk-2d-mobility-model.h b/src/node/random-walk-2d-mobility-model.h index d1cbd9aef..2e921216b 100644 --- a/src/node/random-walk-2d-mobility-model.h +++ b/src/node/random-walk-2d-mobility-model.h @@ -34,7 +34,10 @@ namespace ns3 { class RandomVariable; /** - * \brief parameters to control a random walk model + * \brief parameters to control a random walk 2d model + * + * A single parameter object can be shared by multiple random + * walk models. */ class RandomWalk2dMobilityModelParameters : public Object { @@ -46,21 +49,37 @@ class RandomWalk2dMobilityModelParameters : public Object RandomWalk2dMobilityModelParameters (); virtual ~RandomWalk2dMobilityModelParameters (); /** + * \param speed the random variable used to pick a new + * speed when the direction is changed. + * */ void SetSpeed (const RandomVariable &speed); + /** + * \param direction the random variable used to pick a new + * direction. + */ void SetDirection (const RandomVariable &direction); /** * \param distance the distance before a direction change * - * Unit is meters + * Unit is meters. + * "time" mode is incompatible with "distance" mode. */ void SetModeDistance (double distance); /** * \param time the delay before a direction change. + * + * "time" mode is incompatible with "distance" mode. */ void SetModeTime (Time time); + /** + * \param bounds the bounds of the random walk + */ + void SetBounds (const Rectangle &bounds); + + // needed public for internal default value code. enum Mode { MODE_DISTANCE, MODE_TIME @@ -81,13 +100,13 @@ class RandomWalk2dMobilityModelParameters : public Object * \brief a 2D random walk position model * * Each instance moves with a speed and direction choosen at random - * in the intervals [minspeed,maxspeed] and [0,2pi] until + * with the user-provided random variables until * either a fixed distance has been walked or until a fixed amount * of time. * * The parameters of the model can be specified either with the ns3::Bind - * function and the variables "RandomWalk2dMinSpeed", "RandomWalk2dMaxSpeed", - * "RandomWalk2dMode", "RandomWalk2dModeDistance", and, "RandomWalk2dModeTime" or + * function and the variables "RandomWalk2dSpeed", "RandomWalk2dMode", + * "RandomWalk2dDistance", "RandomWalk2dTime", and, "RandomWalk2dBounds" or * with an instance of the RandomWalk2dMobilityModelParameters class which * must be fed to the RandomWalk2dMobilityModel constructors. */ @@ -100,7 +119,11 @@ class RandomWalk2dMobilityModel : public MobilityModel */ RandomWalk2dMobilityModel (); /** - * Create a new position object located at position (0,0,0) + * \param parameters the parameters to use to control + * the movement of this mobile object. + * + * Create a new position object located at position (0,0,0) with + * the specified parameters. */ RandomWalk2dMobilityModel (Ptr parameters); From e831a050cd20bf3213b79bbb612f8c8d0ec4f060 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 19 Jul 2007 11:05:47 +0200 Subject: [PATCH 088/148] more dox --- src/node/hierarchical-mobility-model.h | 23 +++++++++++++++++++++++ src/node/position.h | 26 +++++++++++++++++++++++++- src/node/rectangle.h | 14 ++++++++++++++ src/node/speed.h | 24 ++++++++++++++++++++++++ src/node/static-mobility-model.h | 1 + src/node/static-speed-mobility-model.h | 8 +++++++- 6 files changed, 94 insertions(+), 2 deletions(-) diff --git a/src/node/hierarchical-mobility-model.h b/src/node/hierarchical-mobility-model.h index e17555387..1502c516a 100644 --- a/src/node/hierarchical-mobility-model.h +++ b/src/node/hierarchical-mobility-model.h @@ -25,14 +25,37 @@ namespace ns3 { +/** + * \brief a hierachical mobility model. + * + * This model allows you to specify the position of a + * child object relative to a parent object. + */ class HierarchicalMobilityModel : public MobilityModel { public: static const InterfaceId iid; + /** + * \param child the "relative" mobility model + * \param parent the "reference" mobility model + */ HierarchicalMobilityModel (Ptr child, Ptr parent); + /** + * \returns the child mobility model. + * + * This allows you to get access to the position of the child + * relative to its parent. + */ Ptr GetChild (void) const; + /** + * \returns the parent mobility model. + * + * This allows you to get access to the position of the + * parent mobility model which is used as the reference + * position by the child mobility model. + */ Ptr GetParent (void) const; private: diff --git a/src/node/position.h b/src/node/position.h index 261ba2e99..884ba50b0 100644 --- a/src/node/position.h +++ b/src/node/position.h @@ -3,13 +3,37 @@ namespace ns3 { +/** + * \brief a 3d cartesian position vector + * + * Unit is meters. + */ class Position { public: - Position (double x, double y, double z); + /** + * \param _x x coordinate of position vector + * \param _y y coordinate of position vector + * \param _z z coordinate of position vector + * + * Create position vector (_x, _y, _z) + */ + Position (double _x, double _y, double _z); + /** + * Create position vector (0.0, 0.0, 0.0) + */ Position (); + /** + * x coordinate of position vector + */ double x; + /** + * y coordinate of position vector + */ double y; + /** + * z coordinate of position vector + */ double z; }; diff --git a/src/node/rectangle.h b/src/node/rectangle.h index ccceeb450..d8de57675 100644 --- a/src/node/rectangle.h +++ b/src/node/rectangle.h @@ -26,6 +26,9 @@ namespace ns3 { class Position; class Speed; +/** + * \brief a 2d rectangle + */ class Rectangle { public: @@ -35,8 +38,19 @@ public: TOP, BOTTOM }; + /** + * \param xMin x coordinates of left boundary. + * \param xMax x coordinates of right boundary. + * \param yMin y coordinates of bottom boundary. + * \param yMin y coordinates of top boundary. + * + * Create a rectangle. + */ Rectangle (double _xMin, double _xMax, double _yMin, double _yMax); + /** + * Create a zero-sized rectangle located at coordinates (0.0,0.0) + */ Rectangle (); bool IsInside (const Position &position) const; Side GetClosestSide (const Position &position) const; diff --git a/src/node/speed.h b/src/node/speed.h index c6bd45d8a..5e2ad3c45 100644 --- a/src/node/speed.h +++ b/src/node/speed.h @@ -3,13 +3,37 @@ namespace ns3 { +/** + * \brief keep track of 3d cartesian speed vectors + * + * Unit is meters/s. + */ class Speed { public: + /** + * \param _dx x coordinate of speed vector + * \param _dy y coordinate of speed vector + * \param _dz z coordinate of speed vector + * + * Create speed vector (_dx, _dy, _dz) + */ Speed (double _dx, double _dy, double _dz); + /** + * Create speed vector (0.0, 0.0, 0.0) + */ Speed (); + /** + * x coordinate of speed vector + */ double dx; + /** + * y coordinate of speed vector + */ double dy; + /** + * z coordinate of speed vector + */ double dz; }; diff --git a/src/node/static-mobility-model.h b/src/node/static-mobility-model.h index 9884c699a..d70890cc0 100644 --- a/src/node/static-mobility-model.h +++ b/src/node/static-mobility-model.h @@ -40,6 +40,7 @@ public: */ StaticMobilityModel (); /** + * \param position the initial position. * * Create a position located at coordinates (x,y,z). * Unit is meters diff --git a/src/node/static-speed-mobility-model.h b/src/node/static-speed-mobility-model.h index 1ceecbff5..74f5b51f9 100644 --- a/src/node/static-speed-mobility-model.h +++ b/src/node/static-speed-mobility-model.h @@ -30,6 +30,11 @@ namespace ns3 { +/** + * \brief a position model for which the current speed does not + * change once it has been set and until it is set again + * explicitely to a new value. + */ class StaticSpeedMobilityModel : public MobilityModel { public: @@ -55,7 +60,8 @@ public: const Speed &speed); virtual ~StaticSpeedMobilityModel (); - /* + /** + * \param speed the new speed to set. * * Set the current speed now to (dx,dy,dz) * Unit is meters/s From b7a44adca00cabb0984ecba7a1fa09b13ff76e1a Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 19 Jul 2007 11:09:13 +0200 Subject: [PATCH 089/148] remove dead code --- SConstruct | 2 -- src/node/position-2d.cc | 10 ---------- src/node/position-2d.h | 16 ---------------- 3 files changed, 28 deletions(-) delete mode 100644 src/node/position-2d.cc delete mode 100644 src/node/position-2d.h diff --git a/SConstruct b/SConstruct index d813deda3..831532e0c 100644 --- a/SConstruct +++ b/SConstruct @@ -255,7 +255,6 @@ node.add_sources ([ 'random-walk-2d-mobility-model.cc', 'hierarchical-mobility-model.cc', 'ns2-mobility-file-topology.cc', - 'position-2d.cc', 'position.cc', 'random-position.cc', 'speed.cc', @@ -290,7 +289,6 @@ node.add_inst_headers ([ 'random-walk-2d-mobility-model.h', 'hierarchical-mobility-model.h', 'ns2-mobility-file-topology.h', - 'position-2d.h', 'position.h', 'random-position.h', 'speed.h', diff --git a/src/node/position-2d.cc b/src/node/position-2d.cc deleted file mode 100644 index 57c9a3b9e..000000000 --- a/src/node/position-2d.cc +++ /dev/null @@ -1,10 +0,0 @@ -#include "position-2d.h" - -namespace ns3 { - -Position2d::Position2d (double _x, double _y) - : x (_x), - y (_y) -{} - -} // namespace ns3 diff --git a/src/node/position-2d.h b/src/node/position-2d.h deleted file mode 100644 index d1c10030c..000000000 --- a/src/node/position-2d.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef POSITION_2D_H -#define POSITION_2D_H - -namespace ns3 { - -class Position2d -{ - public: - Position2d (double x, double y); - double x; - double y; -}; - -} // namespace ns3 - -#endif /* POSITION_2D_H */ From 2e6e200962781d5941781e750926a15356933eaf Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 19 Jul 2007 11:30:47 +0200 Subject: [PATCH 090/148] fix typo --- samples/main-grid-topology.cc | 2 +- src/node/grid-topology.cc | 2 +- src/node/grid-topology.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/samples/main-grid-topology.cc b/samples/main-grid-topology.cc index d9f6f3d2a..e2fb1c3e6 100644 --- a/samples/main-grid-topology.cc +++ b/samples/main-grid-topology.cc @@ -27,7 +27,7 @@ int main (int argc, char *argv[]) GridTopology grid (-100, -100, 20, 5, 20); // each object will be attached a static position. - grid.SetMobilityModelModel (StaticMobilityModel::cid); + grid.SetMobilityModel (StaticMobilityModel::cid); // finalize the setup by attaching to each object // in the input array a position and initializing diff --git a/src/node/grid-topology.cc b/src/node/grid-topology.cc index 11911e8c7..2f5d19f72 100644 --- a/src/node/grid-topology.cc +++ b/src/node/grid-topology.cc @@ -33,7 +33,7 @@ GridTopology::GridTopology (double xMin, double yMin, uint32_t n, double deltaX, {} void -GridTopology::SetMobilityModelModel (ClassId classId) +GridTopology::SetMobilityModel (ClassId classId) { m_positionClassId = classId; } diff --git a/src/node/grid-topology.h b/src/node/grid-topology.h index 825900870..cd23ed0b2 100644 --- a/src/node/grid-topology.h +++ b/src/node/grid-topology.h @@ -48,7 +48,7 @@ class GridTopology * \param classId the classId of the position object to attach to each * input object. */ - void SetMobilityModelModel (ClassId classId); + void SetMobilityModel (ClassId classId); /** * \param objects a vector of objects From 7033df8c0cbc9753ccb7f6e1351f7209ef86e68d Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 19 Jul 2007 11:31:07 +0200 Subject: [PATCH 091/148] Ns2Mobile -> Ns2Mobility --- src/node/ns2-mobility-file-topology.cc | 12 ++++---- src/node/ns2-mobility-file-topology.h | 38 ++++++++++++++++++++++---- utils/mobility-generator.cc | 2 +- 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/src/node/ns2-mobility-file-topology.cc b/src/node/ns2-mobility-file-topology.cc index ee44355b9..9845d62f0 100644 --- a/src/node/ns2-mobility-file-topology.cc +++ b/src/node/ns2-mobility-file-topology.cc @@ -27,18 +27,18 @@ #include #include -NS_DEBUG_COMPONENT_DEFINE ("Ns2MobileFileTopology"); +NS_DEBUG_COMPONENT_DEFINE ("Ns2MobilityFileTopology"); namespace ns3 { -Ns2MobileFileTopology::Ns2MobileFileTopology (std::string filename) +Ns2MobilityFileTopology::Ns2MobilityFileTopology (std::string filename) : m_filename (filename) {} Ptr -Ns2MobileFileTopology::GetMobilityModel (std::string idString, const ObjectStore &store) const +Ns2MobilityFileTopology::GetMobilityModel (std::string idString, const ObjectStore &store) const { std::istringstream iss; iss.str (idString); @@ -60,7 +60,7 @@ Ns2MobileFileTopology::GetMobilityModel (std::string idString, const ObjectStore } double -Ns2MobileFileTopology::ReadDouble (std::string valueString) const +Ns2MobilityFileTopology::ReadDouble (std::string valueString) const { std::istringstream iss; iss.str (valueString); @@ -70,7 +70,7 @@ Ns2MobileFileTopology::ReadDouble (std::string valueString) const } void -Ns2MobileFileTopology::LayoutObjectStore (const ObjectStore &store) const +Ns2MobilityFileTopology::LayoutObjectStore (const ObjectStore &store) const { std::ifstream file (m_filename.c_str (), std::ios::in); if (file.is_open()) @@ -137,7 +137,7 @@ Ns2MobileFileTopology::LayoutObjectStore (const ObjectStore &store) const } void -Ns2MobileFileTopology::Layout (void) const +Ns2MobilityFileTopology::Layout (void) const { Layout (NodeList::Begin (), NodeList::End ()); } diff --git a/src/node/ns2-mobility-file-topology.h b/src/node/ns2-mobility-file-topology.h index 7d08bad26..1bcc1a5fe 100644 --- a/src/node/ns2-mobility-file-topology.h +++ b/src/node/ns2-mobility-file-topology.h @@ -18,8 +18,8 @@ * * Author: Mathieu Lacage */ -#ifndef NS2_MOBILE_FILE_TOPOLOGY_H -#define NS2_MOBILE_FILE_TOPOLOGY_H +#ifndef NS2_MOBILITY_FILE_TOPOLOGY_H +#define NS2_MOBILITY_FILE_TOPOLOGY_H #include #include @@ -29,12 +29,38 @@ namespace ns3 { -class Ns2MobileFileTopology +/** + * \brief a topology object which can read ns2's movement files + * generated by the CMU setdest tool. + */ +class Ns2MobilityFileTopology { public: - Ns2MobileFileTopology (std::string filename); + /** + * \param filename filename of file which contains the + * ns2 movement trace. + */ + Ns2MobilityFileTopology (std::string filename); + /** + * Read the ns2 trace file and configure the movement + * patterns of all nodes contained in the global ns3::NodeList + * whose nodeId is matches the nodeId of the nodes in the trace + * file. + */ void Layout (void) const; + + /** + * \param begin an iterator which points to the start of the input + * object array. + * \param end an iterator which points to the end of the input + * object array. + * + * Read the ns2 trace file and configure the movement + * patterns of all input objects. Each input object + * is identified by a unique node id which reflects + * the index of the object in the input array. + */ template void Layout (T begin, T end) const; private: @@ -56,7 +82,7 @@ namespace ns3 { template void -Ns2MobileFileTopology::Layout (T begin, T end) const +Ns2MobilityFileTopology::Layout (T begin, T end) const { class MyObjectStore : public ObjectStore { @@ -84,4 +110,4 @@ Ns2MobileFileTopology::Layout (T begin, T end) const } // namespace ns3 -#endif /* NS2_MOBILE_FILE_TOPOLOGY_H */ +#endif /* NS2_MOBILITY_FILE_TOPOLOGY_H */ diff --git a/utils/mobility-generator.cc b/utils/mobility-generator.cc index dc6815ed9..7c7a7159c 100644 --- a/utils/mobility-generator.cc +++ b/utils/mobility-generator.cc @@ -54,7 +54,7 @@ int main (int argc, char *argv[]) strlen ("--ns2-topology=")) == 0) { const char *filename = *argv + strlen ("--ns2-topology="); - Ns2MobileFileTopology topology (filename); + Ns2MobilityFileTopology topology (filename); topology.Layout (objects.begin (), objects.end ()); } argc--; From de8d7725bebc816a71ae24751c936edf201a58db Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 19 Jul 2007 11:31:17 +0200 Subject: [PATCH 092/148] more dox --- src/node/random-position.h | 42 +++++++++++++++++++++++++++++++++++++ src/node/random-topology.h | 43 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) diff --git a/src/node/random-position.h b/src/node/random-position.h index 98a771c44..ee97a2286 100644 --- a/src/node/random-position.h +++ b/src/node/random-position.h @@ -9,20 +9,42 @@ namespace ns3 { class RandomVariable; +/** + * \brief choose a position at random. + * + * This is a pure abstract base class. + */ class RandomPosition : public Object { public: static const InterfaceId iid; RandomPosition (); virtual ~RandomPosition (); + /** + * \returns the next randomly-choosen position. + */ virtual Position Get (void) const = 0; }; +/** + * \brief allocate random positions within a rectangle + * according to a pair of random variables. + */ class RandomRectanglePosition : public RandomPosition { public: static const ClassId cid; + /** + * Create a random position model based on the + * Bind default values. + */ RandomRectanglePosition (); + /** + * \param x the random variable which is used to choose + * the x coordinates. + * \param y the random variable which is used to choose + * the y coordinates. + */ RandomRectanglePosition (const RandomVariable &x, const RandomVariable &y); virtual ~RandomRectanglePosition (); @@ -32,11 +54,31 @@ private: RandomVariable *m_y; }; +/** + * \brief allocate random positions within a disc + * according to a pair of random variables. + */ class RandomDiscPosition : public RandomPosition { public: static const ClassId cid; + /** + * Create a random position model based on the + * Bind default values. + */ RandomDiscPosition (); + /** + * \param theta the random variable used to pick + * the angle of the random position in polar + * coordinates. + * \param rho the random variable used to pick the + * radius of the random position in polar + * coordinates. + * \param x the x coordinate of the center of the + * polar coodinate system. + * \param y the y coordinate of the center of the + * polar coodinate system. + */ RandomDiscPosition (const RandomVariable &theta, const RandomVariable &rho, double x, double y); diff --git a/src/node/random-topology.h b/src/node/random-topology.h index f3c63b853..e3dbef0be 100644 --- a/src/node/random-topology.h +++ b/src/node/random-topology.h @@ -29,20 +29,63 @@ namespace ns3 { class RandomPosition; +/** + * \brief layout objects randomly in 3d space. + * + * This assigns an initial position to each object + * according to its position model and assigns + * an instance of a mobility model to each object + * according to its mobility model class id. + */ class RandomTopology { public: + /** + * Create a default random topology based + * on Bind configuration. + */ RandomTopology (); + /** + * \param positionModel model to set the initial position + * of each object. + * \param type of mobility model to attach to each object. + * + * Create a random topology based on the + * specified position and mobility models. + */ RandomTopology (Ptr positionModel, ClassId mobilityModel); ~RandomTopology (); + /** + * \param classId the type of mobility model attached to each + * input object if it does not have one already. + */ void SetMobilityModel (ClassId classId); + /** + * \param positionModel the position model used to initialize + * the position of each object. + */ void SetPositionModel (Ptr positionModel); + /** + * \param object the object to layout + * + * Assign an initial position and a mobility model + * to the object. + */ void LayoutOne (Ptr object); + /** + * \param begin iterator which identifies the first + * object to configure. + * \param end iterator which identifies the last + * object to configure. + * + * Assign an initial position and a mobility model + * to the objects. + */ template void Layout (const T &begin, const T &end); private: From be743da973001d4a13b0ecdf3c11bc702f8d5510 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 19 Jul 2007 12:21:48 +0200 Subject: [PATCH 093/148] more dox --- .../random-direction-2d-mobility-model.cc | 4 +- src/node/random-direction-2d-mobility-model.h | 41 +++++++++++++++++-- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/node/random-direction-2d-mobility-model.cc b/src/node/random-direction-2d-mobility-model.cc index 2af5e792c..33a10e820 100644 --- a/src/node/random-direction-2d-mobility-model.cc +++ b/src/node/random-direction-2d-mobility-model.cc @@ -28,11 +28,9 @@ namespace ns3 { const double RandomDirection2dMobilityModel::PI = 3.1415; -const InterfaceId RandomDirection2dMobilityModel::iid = - MakeInterfaceId ("RandomDirection2dMobilityModel", MobilityModel::iid); const ClassId RandomDirection2dMobilityModel::cid = MakeClassId ("RandomDirection2dMobilityModel", - RandomDirection2dMobilityModel::iid); + MobilityModel::iid); static RandomVariableDefaultValue diff --git a/src/node/random-direction-2d-mobility-model.h b/src/node/random-direction-2d-mobility-model.h index db1f840aa..fa5b64739 100644 --- a/src/node/random-direction-2d-mobility-model.h +++ b/src/node/random-direction-2d-mobility-model.h @@ -34,17 +34,37 @@ namespace ns3 { class RandomVariable; +/** + * \brief the parameters to control a RandomDirection mobility model. + */ class RandomDirection2dParameters : public Object { public: + /** + * Create a default parameter object from Bind default values. + */ RandomDirection2dParameters (); + /** + * \param bounds the 2d bounds of the mobility model + * \param speedVariable the random variable used to pick a random speed + * \param pauseVariable the random variable used to pick a random pause delay + */ RandomDirection2dParameters (const Rectangle &bounds, - const RandomVariable &speedVariable, - const RandomVariable &pauseVariable); + const RandomVariable &speedVariable, + const RandomVariable &pauseVariable); virtual ~RandomDirection2dParameters (); + /** + * \param speedVariable the random variable used to pick a random speed. + */ void SetSpeed (const RandomVariable &speedVariable); + /** + * \param pauseVariable the random variable used to pick a random pause delay. + */ void SetPause (const RandomVariable &pauseVariable); + /** + * \param bounds the 2d bounds of the mobility model. + */ void SetBounds (const Rectangle &bounds); private: friend class RandomDirection2dMobilityModel; @@ -56,13 +76,28 @@ class RandomDirection2dParameters : public Object RandomVariable *m_pauseVariable; }; +/** + * \brief a RandomDirection mobility model + * + * The movement of objects is based on random directions: each object + * pauses for a specific delay, chooses a random direction and speed and + * then travels in the specific direction until it reaches one of + * the boundaries of the model. When it reaches the boundary, it pauses, + * selects a new direction and speed, aso. + */ class RandomDirection2dMobilityModel : public MobilityModel { public: - static const InterfaceId iid; static const ClassId cid; + /** + * Create a RandomDirection model from the default Bind values. + */ RandomDirection2dMobilityModel (); + /** + * \param parameters the parameters which control the behavior of the model. + * Create a RandomDirection model using the parameters specified. + */ RandomDirection2dMobilityModel (Ptr parameters); private: void Start (void); From f3060d8a33a8a3c66514b947ec7e4ab6ebe2180c Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 19 Jul 2007 12:23:12 +0200 Subject: [PATCH 094/148] RandomDirection2dParameters -> RandomDirection2dMobilityModelParameters --- .../random-direction-2d-mobility-model.cc | 30 ++++++++++--------- src/node/random-direction-2d-mobility-model.h | 14 ++++----- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/node/random-direction-2d-mobility-model.cc b/src/node/random-direction-2d-mobility-model.cc index 33a10e820..63c4a7d51 100644 --- a/src/node/random-direction-2d-mobility-model.cc +++ b/src/node/random-direction-2d-mobility-model.cc @@ -50,21 +50,22 @@ static RectangleDefaultValue -100, 100, -100, 100); -RandomDirection2dParameters::RandomDirection2dParameters () +RandomDirection2dMobilityModelParameters::RandomDirection2dMobilityModelParameters () : m_bounds (g_bounds.GetValue ()), m_speedVariable (g_speedVariable.GetCopy ()), m_pauseVariable (g_pauseVariable.GetCopy ()) {} -RandomDirection2dParameters::RandomDirection2dParameters (const Rectangle &bounds, - const RandomVariable &speedVariable, - const RandomVariable &pauseVariable) +RandomDirection2dMobilityModelParameters::RandomDirection2dMobilityModelParameters +(const Rectangle &bounds, + const RandomVariable &speedVariable, + const RandomVariable &pauseVariable) : m_bounds (bounds), m_speedVariable (speedVariable.Copy ()), m_pauseVariable (pauseVariable.Copy ()) {} -RandomDirection2dParameters::~RandomDirection2dParameters () +RandomDirection2dMobilityModelParameters::~RandomDirection2dMobilityModelParameters () { delete m_speedVariable; delete m_pauseVariable; @@ -73,33 +74,33 @@ RandomDirection2dParameters::~RandomDirection2dParameters () } void -RandomDirection2dParameters::SetSpeed (const RandomVariable &speedVariable) +RandomDirection2dMobilityModelParameters::SetSpeed (const RandomVariable &speedVariable) { delete m_speedVariable; m_speedVariable = speedVariable.Copy (); } void -RandomDirection2dParameters::SetPause (const RandomVariable &pauseVariable) +RandomDirection2dMobilityModelParameters::SetPause (const RandomVariable &pauseVariable) { delete m_pauseVariable; m_pauseVariable = pauseVariable.Copy (); } void -RandomDirection2dParameters::SetBounds (const Rectangle &bounds) +RandomDirection2dMobilityModelParameters::SetBounds (const Rectangle &bounds) { m_bounds = bounds; } -Ptr -RandomDirection2dParameters::GetCurrent (void) +Ptr +RandomDirection2dMobilityModelParameters::GetCurrent (void) { - static Ptr parameters = 0; + static Ptr parameters = 0; if (parameters == 0 || g_bounds.IsDirty () || g_speedVariable.IsDirty () || g_pauseVariable.IsDirty ()) { - parameters = Create (); + parameters = Create (); g_bounds.ClearDirtyFlag (); g_speedVariable.ClearDirtyFlag (); g_pauseVariable.ClearDirtyFlag (); @@ -109,12 +110,13 @@ RandomDirection2dParameters::GetCurrent (void) RandomDirection2dMobilityModel::RandomDirection2dMobilityModel () - : m_parameters (RandomDirection2dParameters::GetCurrent ()) + : m_parameters (RandomDirection2dMobilityModelParameters::GetCurrent ()) { SetInterfaceId (RandomDirection2dMobilityModel::iid); m_event = Simulator::ScheduleNow (&RandomDirection2dMobilityModel::Start, this); } -RandomDirection2dMobilityModel::RandomDirection2dMobilityModel (Ptr parameters) +RandomDirection2dMobilityModel::RandomDirection2dMobilityModel +(Ptr parameters) : m_parameters (parameters) { SetInterfaceId (RandomDirection2dMobilityModel::iid); diff --git a/src/node/random-direction-2d-mobility-model.h b/src/node/random-direction-2d-mobility-model.h index fa5b64739..dda1b1173 100644 --- a/src/node/random-direction-2d-mobility-model.h +++ b/src/node/random-direction-2d-mobility-model.h @@ -37,22 +37,22 @@ class RandomVariable; /** * \brief the parameters to control a RandomDirection mobility model. */ -class RandomDirection2dParameters : public Object +class RandomDirection2dMobilityModelParameters : public Object { public: /** * Create a default parameter object from Bind default values. */ - RandomDirection2dParameters (); + RandomDirection2dMobilityModelParameters (); /** * \param bounds the 2d bounds of the mobility model * \param speedVariable the random variable used to pick a random speed * \param pauseVariable the random variable used to pick a random pause delay */ - RandomDirection2dParameters (const Rectangle &bounds, + RandomDirection2dMobilityModelParameters (const Rectangle &bounds, const RandomVariable &speedVariable, const RandomVariable &pauseVariable); - virtual ~RandomDirection2dParameters (); + virtual ~RandomDirection2dMobilityModelParameters (); /** * \param speedVariable the random variable used to pick a random speed. @@ -69,7 +69,7 @@ class RandomDirection2dParameters : public Object private: friend class RandomDirection2dMobilityModel; - static Ptr GetCurrent (void); + static Ptr GetCurrent (void); Rectangle m_bounds; RandomVariable *m_speedVariable; @@ -98,7 +98,7 @@ class RandomDirection2dMobilityModel : public MobilityModel * \param parameters the parameters which control the behavior of the model. * Create a RandomDirection model using the parameters specified. */ - RandomDirection2dMobilityModel (Ptr parameters); + RandomDirection2dMobilityModel (Ptr parameters); private: void Start (void); void ResetDirectionAndSpeed (void); @@ -110,7 +110,7 @@ class RandomDirection2dMobilityModel : public MobilityModel virtual Speed DoGetSpeed (void) const; static const double PI; - Ptr m_parameters; + Ptr m_parameters; EventId m_event; StaticSpeedHelper m_helper; }; From fdb89d13ef872df56dc638dfa67f5fe3ee5e314d Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 19 Jul 2007 12:29:21 +0200 Subject: [PATCH 095/148] more dox --- src/node/random-waypoint-mobility-model.h | 39 +++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/node/random-waypoint-mobility-model.h b/src/node/random-waypoint-mobility-model.h index ae27037bc..aef1c3d86 100644 --- a/src/node/random-waypoint-mobility-model.h +++ b/src/node/random-waypoint-mobility-model.h @@ -30,15 +30,36 @@ namespace ns3 { class RandomVariable; +/** + * \brief the parameters which control the behavior of a random waypoint + * mobility model. + */ class RandomWaypointMobilityModelParameters : public Object { public: + /** + * Defaults parameters based on the Bind values. + */ RandomWaypointMobilityModelParameters (); + /** + * \param randomPosition a random position model to choose the position of waypoints. + * \param speed a random variable to choose the speed + * \param pause a random variable to choose the pause delay + */ RandomWaypointMobilityModelParameters (Ptr randomPosition, const RandomVariable &speed, const RandomVariable &pause); + /** + * \param randomPosition a random position model to choose the position of waypoints. + */ void SetWaypointPositionModel (Ptr randomPosition); + /** + * \param speed a random variable to choose the speed + */ void SetSpeed (const RandomVariable &speed); + /** + * \param pause a random variable to choose the pause delay + */ void SetPause (const RandomVariable &pause); private: friend class RandomWaypointMobilityModel; @@ -49,12 +70,30 @@ private: Ptr m_position; }; +/** + * \brief a random waypoint mobility model + * + * Each object chooses a random destination "waypoint", a random speed, + * and a random pause time: it then pauses for the specified pause time, + * and starts moving towards the specified destination with the specified + * speed. Once the destination is reached the process starts again. + * + * The implementation of this model is not 2d-specific. i.e. if you provide + * a 3d random waypoint position model to this mobility model, the model + * will still work. + */ class RandomWaypointMobilityModel : public MobilityModel { public: static const ClassId cid; + /** + * Create a waypoint mobility model from the Bind default values. + */ RandomWaypointMobilityModel (); + /** + * \param parameters the parameters which control the behavior of this model. + */ RandomWaypointMobilityModel (Ptr parameters); private: void Start (void); From 347b7238dca96351450a5fd1fd65e297a352e10d Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 19 Jul 2007 12:38:30 +0200 Subject: [PATCH 096/148] create a mobility module --- SConstruct | 80 ++++++++++--------- src/{node => mobility}/grid-topology.cc | 0 src/{node => mobility}/grid-topology.h | 0 .../hierarchical-mobility-model.cc | 0 .../hierarchical-mobility-model.h | 0 .../mobility-model-notifier.cc | 0 .../mobility-model-notifier.h | 0 src/{node => mobility}/mobility-model.cc | 0 src/{node => mobility}/mobility-model.h | 0 .../ns2-mobility-file-topology.cc | 12 +-- .../ns2-mobility-file-topology.h | 0 src/{node => mobility}/position.cc | 0 src/{node => mobility}/position.h | 0 .../random-direction-2d-mobility-model.cc | 0 .../random-direction-2d-mobility-model.h | 0 src/{node => mobility}/random-position.cc | 0 src/{node => mobility}/random-position.h | 0 src/{node => mobility}/random-topology.cc | 0 src/{node => mobility}/random-topology.h | 0 .../random-walk-2d-mobility-model.cc | 0 .../random-walk-2d-mobility-model.h | 0 .../random-waypoint-mobility-model.cc | 0 .../random-waypoint-mobility-model.h | 0 .../rectangle-default-value.cc | 0 .../rectangle-default-value.h | 0 src/{node => mobility}/rectangle.cc | 0 src/{node => mobility}/rectangle.h | 0 src/{node => mobility}/speed.cc | 0 src/{node => mobility}/speed.h | 0 .../static-mobility-model.cc | 0 .../static-mobility-model.h | 0 src/{node => mobility}/static-speed-helper.cc | 0 src/{node => mobility}/static-speed-helper.h | 0 .../static-speed-mobility-model.cc | 0 .../static-speed-mobility-model.h | 0 35 files changed, 50 insertions(+), 42 deletions(-) rename src/{node => mobility}/grid-topology.cc (100%) rename src/{node => mobility}/grid-topology.h (100%) rename src/{node => mobility}/hierarchical-mobility-model.cc (100%) rename src/{node => mobility}/hierarchical-mobility-model.h (100%) rename src/{node => mobility}/mobility-model-notifier.cc (100%) rename src/{node => mobility}/mobility-model-notifier.h (100%) rename src/{node => mobility}/mobility-model.cc (100%) rename src/{node => mobility}/mobility-model.h (100%) rename src/{node => mobility}/ns2-mobility-file-topology.cc (98%) rename src/{node => mobility}/ns2-mobility-file-topology.h (100%) rename src/{node => mobility}/position.cc (100%) rename src/{node => mobility}/position.h (100%) rename src/{node => mobility}/random-direction-2d-mobility-model.cc (100%) rename src/{node => mobility}/random-direction-2d-mobility-model.h (100%) rename src/{node => mobility}/random-position.cc (100%) rename src/{node => mobility}/random-position.h (100%) rename src/{node => mobility}/random-topology.cc (100%) rename src/{node => mobility}/random-topology.h (100%) rename src/{node => mobility}/random-walk-2d-mobility-model.cc (100%) rename src/{node => mobility}/random-walk-2d-mobility-model.h (100%) rename src/{node => mobility}/random-waypoint-mobility-model.cc (100%) rename src/{node => mobility}/random-waypoint-mobility-model.h (100%) rename src/{node => mobility}/rectangle-default-value.cc (100%) rename src/{node => mobility}/rectangle-default-value.h (100%) rename src/{node => mobility}/rectangle.cc (100%) rename src/{node => mobility}/rectangle.h (100%) rename src/{node => mobility}/speed.cc (100%) rename src/{node => mobility}/speed.h (100%) rename src/{node => mobility}/static-mobility-model.cc (100%) rename src/{node => mobility}/static-mobility-model.h (100%) rename src/{node => mobility}/static-speed-helper.cc (100%) rename src/{node => mobility}/static-speed-helper.h (100%) rename src/{node => mobility}/static-speed-mobility-model.cc (100%) rename src/{node => mobility}/static-speed-mobility-model.h (100%) diff --git a/SConstruct b/SConstruct index 831532e0c..38c89ceaf 100644 --- a/SConstruct +++ b/SConstruct @@ -227,6 +227,48 @@ common.add_inst_headers([ 'data-rate.h', ]) +mobility = build.Ns3Module ('mobility', 'src/mobility'); +ns3.add (mobility); +mobility.add_deps (['core', 'simulator', 'node']) +mobility.add_sources ([ + 'position.cc', + 'speed.cc', + 'random-position.cc', + 'rectangle-default-value.cc', + 'rectangle.cc', + 'mobility-model.cc', + 'mobility-model-notifier.cc', + 'static-speed-helper.cc', + 'static-mobility-model.cc', + 'static-speed-mobility-model.cc', + 'hierarchical-mobility-model.cc', + 'random-direction-2d-mobility-model.cc', + 'random-waypoint-mobility-model.cc', + 'random-walk-2d-mobility-model.cc', + 'grid-topology.cc', + 'random-topology.cc', + 'ns2-mobility-file-topology.cc', + ]) +mobility.add_inst_headers ([ + 'position.h', + 'speed.h', + 'random-position.h', + 'rectangle-default-value.h', + 'rectangle.h', + 'mobility-model.h', + 'mobility-model-notifier.h', + 'static-speed-helper.h', + 'static-mobility-model.h', + 'static-speed-mobility-model.h', + 'hierarchical-mobility-model.h', + 'random-direction-2d-mobility-model.h', + 'random-waypoint-mobility-model.h', + 'random-walk-2d-mobility-model.h', + 'grid-topology.h', + 'random-topology.h', + 'ns2-mobility-file-topology.h', + ]) + node = build.Ns3Module ('node', 'src/node') ns3.add (node) node.add_deps (['core', 'common', 'simulator']) @@ -246,23 +288,6 @@ node.add_sources ([ 'udp.cc', 'ipv4.cc', 'application.cc', - 'mobility-model.cc', - 'mobility-model-notifier.cc', - 'static-mobility-model.cc', - 'static-speed-mobility-model.cc', - 'grid-topology.cc', - 'random-topology.cc', - 'random-walk-2d-mobility-model.cc', - 'hierarchical-mobility-model.cc', - 'ns2-mobility-file-topology.cc', - 'position.cc', - 'random-position.cc', - 'speed.cc', - 'static-speed-helper.cc', - 'random-waypoint-mobility-model.cc', - 'rectangle-default-value.cc', - 'rectangle.cc', - 'random-direction-2d-mobility-model.cc', ]) node.add_inst_headers ([ 'node.h', @@ -280,23 +305,6 @@ node.add_inst_headers ([ 'udp.h', 'ipv4.h', 'application.h', - 'mobility-model.h', - 'mobility-model-notifier.h', - 'static-mobility-model.h', - 'static-speed-mobility-model.h', - 'grid-topology.h', - 'random-topology.h', - 'random-walk-2d-mobility-model.h', - 'hierarchical-mobility-model.h', - 'ns2-mobility-file-topology.h', - 'position.h', - 'random-position.h', - 'speed.h', - 'static-speed-helper.h', - 'random-waypoint-mobility-model.h', - 'rectangle-default-value.h', - 'rectangle.h', - 'random-direction-2d-mobility-model.h', ]) applications = build.Ns3Module ('applications', 'src/applications') @@ -446,13 +454,13 @@ sample_callback.add_source('main-callback.cc') sample_random_walk = build.Ns3Module('sample-random-walk', 'samples') sample_random_walk.set_executable() ns3.add(sample_random_walk) -sample_random_walk.add_deps(['core', 'node']) +sample_random_walk.add_deps(['core', 'mobility']) sample_random_walk.add_source('main-random-walk.cc') sample_grid_topology = build.Ns3Module('sample-grid-topology', 'samples') sample_grid_topology.set_executable() ns3.add(sample_grid_topology) -sample_grid_topology.add_deps(['core', 'internet-node']) +sample_grid_topology.add_deps(['core', 'internet-node', 'mobility']) sample_grid_topology.add_source('main-grid-topology.cc') sample_ptr = build.Ns3Module('sample-ptr', 'samples') diff --git a/src/node/grid-topology.cc b/src/mobility/grid-topology.cc similarity index 100% rename from src/node/grid-topology.cc rename to src/mobility/grid-topology.cc diff --git a/src/node/grid-topology.h b/src/mobility/grid-topology.h similarity index 100% rename from src/node/grid-topology.h rename to src/mobility/grid-topology.h diff --git a/src/node/hierarchical-mobility-model.cc b/src/mobility/hierarchical-mobility-model.cc similarity index 100% rename from src/node/hierarchical-mobility-model.cc rename to src/mobility/hierarchical-mobility-model.cc diff --git a/src/node/hierarchical-mobility-model.h b/src/mobility/hierarchical-mobility-model.h similarity index 100% rename from src/node/hierarchical-mobility-model.h rename to src/mobility/hierarchical-mobility-model.h diff --git a/src/node/mobility-model-notifier.cc b/src/mobility/mobility-model-notifier.cc similarity index 100% rename from src/node/mobility-model-notifier.cc rename to src/mobility/mobility-model-notifier.cc diff --git a/src/node/mobility-model-notifier.h b/src/mobility/mobility-model-notifier.h similarity index 100% rename from src/node/mobility-model-notifier.h rename to src/mobility/mobility-model-notifier.h diff --git a/src/node/mobility-model.cc b/src/mobility/mobility-model.cc similarity index 100% rename from src/node/mobility-model.cc rename to src/mobility/mobility-model.cc diff --git a/src/node/mobility-model.h b/src/mobility/mobility-model.h similarity index 100% rename from src/node/mobility-model.h rename to src/mobility/mobility-model.h diff --git a/src/node/ns2-mobility-file-topology.cc b/src/mobility/ns2-mobility-file-topology.cc similarity index 98% rename from src/node/ns2-mobility-file-topology.cc rename to src/mobility/ns2-mobility-file-topology.cc index 9845d62f0..7d0b8c915 100644 --- a/src/node/ns2-mobility-file-topology.cc +++ b/src/mobility/ns2-mobility-file-topology.cc @@ -18,14 +18,14 @@ * * Author: Mathieu Lacage */ -#include "ns3/debug.h" -#include "ns3/simulator.h" -#include "ns2-mobility-file-topology.h" -#include "node-list.h" -#include "node.h" -#include "static-speed-mobility-model.h" #include #include +#include "ns3/debug.h" +#include "ns3/simulator.h" +#include "ns3/node-list.h" +#include "ns3/node.h" +#include "ns2-mobility-file-topology.h" +#include "static-speed-mobility-model.h" NS_DEBUG_COMPONENT_DEFINE ("Ns2MobilityFileTopology"); diff --git a/src/node/ns2-mobility-file-topology.h b/src/mobility/ns2-mobility-file-topology.h similarity index 100% rename from src/node/ns2-mobility-file-topology.h rename to src/mobility/ns2-mobility-file-topology.h diff --git a/src/node/position.cc b/src/mobility/position.cc similarity index 100% rename from src/node/position.cc rename to src/mobility/position.cc diff --git a/src/node/position.h b/src/mobility/position.h similarity index 100% rename from src/node/position.h rename to src/mobility/position.h diff --git a/src/node/random-direction-2d-mobility-model.cc b/src/mobility/random-direction-2d-mobility-model.cc similarity index 100% rename from src/node/random-direction-2d-mobility-model.cc rename to src/mobility/random-direction-2d-mobility-model.cc diff --git a/src/node/random-direction-2d-mobility-model.h b/src/mobility/random-direction-2d-mobility-model.h similarity index 100% rename from src/node/random-direction-2d-mobility-model.h rename to src/mobility/random-direction-2d-mobility-model.h diff --git a/src/node/random-position.cc b/src/mobility/random-position.cc similarity index 100% rename from src/node/random-position.cc rename to src/mobility/random-position.cc diff --git a/src/node/random-position.h b/src/mobility/random-position.h similarity index 100% rename from src/node/random-position.h rename to src/mobility/random-position.h diff --git a/src/node/random-topology.cc b/src/mobility/random-topology.cc similarity index 100% rename from src/node/random-topology.cc rename to src/mobility/random-topology.cc diff --git a/src/node/random-topology.h b/src/mobility/random-topology.h similarity index 100% rename from src/node/random-topology.h rename to src/mobility/random-topology.h diff --git a/src/node/random-walk-2d-mobility-model.cc b/src/mobility/random-walk-2d-mobility-model.cc similarity index 100% rename from src/node/random-walk-2d-mobility-model.cc rename to src/mobility/random-walk-2d-mobility-model.cc diff --git a/src/node/random-walk-2d-mobility-model.h b/src/mobility/random-walk-2d-mobility-model.h similarity index 100% rename from src/node/random-walk-2d-mobility-model.h rename to src/mobility/random-walk-2d-mobility-model.h diff --git a/src/node/random-waypoint-mobility-model.cc b/src/mobility/random-waypoint-mobility-model.cc similarity index 100% rename from src/node/random-waypoint-mobility-model.cc rename to src/mobility/random-waypoint-mobility-model.cc diff --git a/src/node/random-waypoint-mobility-model.h b/src/mobility/random-waypoint-mobility-model.h similarity index 100% rename from src/node/random-waypoint-mobility-model.h rename to src/mobility/random-waypoint-mobility-model.h diff --git a/src/node/rectangle-default-value.cc b/src/mobility/rectangle-default-value.cc similarity index 100% rename from src/node/rectangle-default-value.cc rename to src/mobility/rectangle-default-value.cc diff --git a/src/node/rectangle-default-value.h b/src/mobility/rectangle-default-value.h similarity index 100% rename from src/node/rectangle-default-value.h rename to src/mobility/rectangle-default-value.h diff --git a/src/node/rectangle.cc b/src/mobility/rectangle.cc similarity index 100% rename from src/node/rectangle.cc rename to src/mobility/rectangle.cc diff --git a/src/node/rectangle.h b/src/mobility/rectangle.h similarity index 100% rename from src/node/rectangle.h rename to src/mobility/rectangle.h diff --git a/src/node/speed.cc b/src/mobility/speed.cc similarity index 100% rename from src/node/speed.cc rename to src/mobility/speed.cc diff --git a/src/node/speed.h b/src/mobility/speed.h similarity index 100% rename from src/node/speed.h rename to src/mobility/speed.h diff --git a/src/node/static-mobility-model.cc b/src/mobility/static-mobility-model.cc similarity index 100% rename from src/node/static-mobility-model.cc rename to src/mobility/static-mobility-model.cc diff --git a/src/node/static-mobility-model.h b/src/mobility/static-mobility-model.h similarity index 100% rename from src/node/static-mobility-model.h rename to src/mobility/static-mobility-model.h diff --git a/src/node/static-speed-helper.cc b/src/mobility/static-speed-helper.cc similarity index 100% rename from src/node/static-speed-helper.cc rename to src/mobility/static-speed-helper.cc diff --git a/src/node/static-speed-helper.h b/src/mobility/static-speed-helper.h similarity index 100% rename from src/node/static-speed-helper.h rename to src/mobility/static-speed-helper.h diff --git a/src/node/static-speed-mobility-model.cc b/src/mobility/static-speed-mobility-model.cc similarity index 100% rename from src/node/static-speed-mobility-model.cc rename to src/mobility/static-speed-mobility-model.cc diff --git a/src/node/static-speed-mobility-model.h b/src/mobility/static-speed-mobility-model.h similarity index 100% rename from src/node/static-speed-mobility-model.h rename to src/mobility/static-speed-mobility-model.h From 8e0837b6a111462d9b22a1a012d36b5a58836326 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 19 Jul 2007 12:43:32 +0200 Subject: [PATCH 097/148] fix link --- SConstruct | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SConstruct b/SConstruct index 38c89ceaf..794cd503c 100644 --- a/SConstruct +++ b/SConstruct @@ -403,7 +403,7 @@ p2p.add_inst_headers ([ mobgen = build.Ns3Module ('mobility-generator', 'utils') ns3.add (mobgen) mobgen.set_executable () -mobgen.add_deps (['simulator', 'node']) +mobgen.add_deps (['simulator', 'node', 'mobility']) mobgen.add_source ('mobility-generator.cc') run_tests = build.Ns3Module('run-tests', 'utils') From 3fe8f75d1bc814cd1bbee6514b8b486dbe6b5a43 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 19 Jul 2007 13:02:02 +0200 Subject: [PATCH 098/148] a dox summary header for the mobility support --- SConstruct | 3 +++ src/mobility/mobility.h | 42 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 src/mobility/mobility.h diff --git a/SConstruct b/SConstruct index 794cd503c..a3a002d90 100644 --- a/SConstruct +++ b/SConstruct @@ -268,6 +268,9 @@ mobility.add_inst_headers ([ 'random-topology.h', 'ns2-mobility-file-topology.h', ]) +mobility.add_headers ([ + 'mobility.h' + ]) node = build.Ns3Module ('node', 'src/node') ns3.add (node) diff --git a/src/mobility/mobility.h b/src/mobility/mobility.h new file mode 100644 index 000000000..46864e5a5 --- /dev/null +++ b/src/mobility/mobility.h @@ -0,0 +1,42 @@ +/** + * \defgroup mobility Mobility + * + * The mobility support includes: + * - a set of mobility models which are used to track and maintain + * the "current" cartesian position and speed of an object. + * + * - a "course change notifier" which can be used to register listeners + * to the course changes of a mobility model: ns3::MobilityModelNotifier. + * + * - a set of topology constructors which are used to set the initial + * position and associate a specific mobility model to a set of objects. + * + * The mobility models themselves are: + * - StaticMobilityModel: a model which maintains a constant position + * until it is changed by the user. + * + * - StaticSpeedMobilityModel: a model which maintains a constant speed + * until it is changed by the user. + * + * - HierarchicalMobilityModel: a model which calculates the current + * absolute position from a "reference" (parent) mobility model + * and a "relative" (child) mobility model. This allows users to + * compose mobility models. + * + * - RandomWalk2dMobilityModel: a 2d "brownian" motion mobility model + * where the bounds of the mobility area are a rectangle. + * + * - RandomWaypointMobilityModel: a 3d random waypoint mobility model. + * + * - RandomDirection2dMobilityModel: a 2d random direction mobility + * model where the bounds of the mobility are are a rectangle. + * + * The topology constructors: + * - GridTopology: layout objects in a 2d grid. + * + * - RandomTopology: layout objects in a 3d space, according to a + * RandomPosition model. + * + * - Ns2MobilityFileTopology: layout objects in a 3d space according + * to an ns2 CMU mobility file (as generated by the setdest tool). + */ From 3e00c2d842584bb5124a30a1c5846fecb5ad7442 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 19 Jul 2007 13:11:08 +0200 Subject: [PATCH 099/148] fix dox warnings --- src/mobility/grid-topology.h | 16 +++++++++------- src/mobility/random-topology.h | 2 +- src/mobility/rectangle.h | 8 ++++---- src/simulator/scheduler.h | 13 ++++--------- 4 files changed, 18 insertions(+), 21 deletions(-) diff --git a/src/mobility/grid-topology.h b/src/mobility/grid-topology.h index cd23ed0b2..93457503d 100644 --- a/src/mobility/grid-topology.h +++ b/src/mobility/grid-topology.h @@ -42,7 +42,7 @@ class GridTopology * * The first object is positioned at (xMin,yMin). */ - GridTopology (double xMin, double yMin, uint32_t nCols, double deltaX, double deltaY); + GridTopology (double xMin, double yMin, uint32_t n, double deltaX, double deltaY); /** * \param classId the classId of the position object to attach to each @@ -51,11 +51,12 @@ class GridTopology void SetMobilityModel (ClassId classId); /** - * \param objects a vector of objects + * \param begin an iterator to the first object to layout. + * \param end an iterator to the last object to layout. * * Attach a position (the type of position is specified through - * the ClassId given to SetMobilityModelModel) to each object present - * in the input vector and configure its initial location with a set + * the ClassId given to SetMobilityModelModel) to each input object + * and configure its initial location with a set * of coordinates arranged according to a regular rectangular grid, * one row after the other. */ @@ -63,11 +64,12 @@ class GridTopology void LayoutRowFirst (const T &begin, const T &end); /** - * \param objects a vector of objects + * \param begin an iterator to the first object to layout. + * \param end an iterator to the last object to layout. * * Attach a position (the type of position is specified through - * the ClassId given to SetMobilityModelModel) to each object present - * in the input vector and configure its initial location with a set + * the ClassId given to SetMobilityModelModel) to each input object + * and configure its initial location with a set * of coordinates arranged according to a regular rectangular grid, * one column after the other. */ diff --git a/src/mobility/random-topology.h b/src/mobility/random-topology.h index e3dbef0be..a955839e0 100644 --- a/src/mobility/random-topology.h +++ b/src/mobility/random-topology.h @@ -48,7 +48,7 @@ class RandomTopology /** * \param positionModel model to set the initial position * of each object. - * \param type of mobility model to attach to each object. + * \param mobilityModel type of mobility model to attach to each object. * * Create a random topology based on the * specified position and mobility models. diff --git a/src/mobility/rectangle.h b/src/mobility/rectangle.h index d8de57675..a53f641d6 100644 --- a/src/mobility/rectangle.h +++ b/src/mobility/rectangle.h @@ -39,10 +39,10 @@ public: BOTTOM }; /** - * \param xMin x coordinates of left boundary. - * \param xMax x coordinates of right boundary. - * \param yMin y coordinates of bottom boundary. - * \param yMin y coordinates of top boundary. + * \param _xMin x coordinates of left boundary. + * \param _xMax x coordinates of right boundary. + * \param _yMin y coordinates of bottom boundary. + * \param _yMax y coordinates of top boundary. * * Create a rectangle. */ diff --git a/src/simulator/scheduler.h b/src/simulator/scheduler.h index 0df7a77df..d7f7f2cfb 100644 --- a/src/simulator/scheduler.h +++ b/src/simulator/scheduler.h @@ -68,11 +68,8 @@ class Scheduler { private: /** - * \param event event to store in the event list - * \param key timecode associated to this new event - * \returns an event id which identifies the event inserted + * \param id the event id to store. * - * This method takes ownership of the event pointer. */ virtual void RealInsert (EventId id) = 0; /** @@ -80,8 +77,7 @@ private: */ virtual bool RealIsEmpty (void) const = 0; /** - * \returns a pointer to the next earliest event. The caller - * takes ownership of the returned pointer. + * \returns a the next earliest event. * * This method cannot be invoked if the list is empty. */ @@ -93,9 +89,8 @@ private: virtual void RealRemoveNext (void) = 0; /** * \param id the id of the event to remove - * \param key the timecode of the event removed - * \returns a pointer to the event removed. The caller - * takes ownership of the returned pointer. + * \returns true if the event was found and removed + * successfully, false otherwise. * * This methods cannot be invoked if the list is empty. */ From 421e19ca4b33a2e2a1a35d9588d75c025384e164 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 19 Jul 2007 13:17:35 +0200 Subject: [PATCH 100/148] make sure that dox generates html links --- src/mobility/mobility.h | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/mobility/mobility.h b/src/mobility/mobility.h index 46864e5a5..6d7a39f46 100644 --- a/src/mobility/mobility.h +++ b/src/mobility/mobility.h @@ -12,31 +12,31 @@ * position and associate a specific mobility model to a set of objects. * * The mobility models themselves are: - * - StaticMobilityModel: a model which maintains a constant position + * - ns3::StaticMobilityModel: a model which maintains a constant position * until it is changed by the user. * - * - StaticSpeedMobilityModel: a model which maintains a constant speed + * - ns3::StaticSpeedMobilityModel: a model which maintains a constant speed * until it is changed by the user. * - * - HierarchicalMobilityModel: a model which calculates the current + * - ns3::HierarchicalMobilityModel: a model which calculates the current * absolute position from a "reference" (parent) mobility model * and a "relative" (child) mobility model. This allows users to * compose mobility models. * - * - RandomWalk2dMobilityModel: a 2d "brownian" motion mobility model + * - ns3::RandomWalk2dMobilityModel: a 2d "brownian" motion mobility model * where the bounds of the mobility area are a rectangle. * - * - RandomWaypointMobilityModel: a 3d random waypoint mobility model. + * - ns3::RandomWaypointMobilityModel: a 3d random waypoint mobility model. * - * - RandomDirection2dMobilityModel: a 2d random direction mobility + * - ns3::RandomDirection2dMobilityModel: a 2d random direction mobility * model where the bounds of the mobility are are a rectangle. * * The topology constructors: - * - GridTopology: layout objects in a 2d grid. + * - ns3::GridTopology: layout objects in a 2d grid. * - * - RandomTopology: layout objects in a 3d space, according to a + * - ns3::RandomTopology: layout objects in a 3d space, according to a * RandomPosition model. * - * - Ns2MobilityFileTopology: layout objects in a 3d space according + * - ns3::Ns2MobilityFileTopology: layout objects in a 3d space according * to an ns2 CMU mobility file (as generated by the setdest tool). */ From 71125e48330511d7eabb3a681aaccf380592877d Mon Sep 17 00:00:00 2001 From: "Gustavo J. A. M. Carneiro" Date: Thu, 27 Sep 2007 12:50:26 +0200 Subject: [PATCH 101/148] Event Garbage Collector --- src/simulator/event-garbage-collector.cc | 152 +++++++++++++++++++++++ src/simulator/event-garbage-collector.h | 62 +++++++++ 2 files changed, 214 insertions(+) create mode 100644 src/simulator/event-garbage-collector.cc create mode 100644 src/simulator/event-garbage-collector.h diff --git a/src/simulator/event-garbage-collector.cc b/src/simulator/event-garbage-collector.cc new file mode 100644 index 000000000..776f2a999 --- /dev/null +++ b/src/simulator/event-garbage-collector.cc @@ -0,0 +1,152 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 INESC Porto + * 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: Gustavo J. A. M. Carneiro + */ +#include "event-garbage-collector.h" + +#define CLEANUP_CHUNK_MIN_SIZE 8 +#define CLEANUP_CHUNK_MAX_SIZE 128 + + +namespace ns3 { + + +EventGarbageCollector::EventGarbageCollector () : + m_nextCleanupSize (CLEANUP_CHUNK_MIN_SIZE) +{} + +void +EventGarbageCollector::Track (EventId event) +{ + m_events.push_back (event); + if (m_events.size () >= m_nextCleanupSize) + Cleanup (); +} + +inline bool +EventExpiredPredicate (const EventId &event) +{ + return event.IsExpired (); +} + +void +EventGarbageCollector::Grow () +{ + m_nextCleanupSize += (m_nextCleanupSize < CLEANUP_CHUNK_MAX_SIZE? + m_nextCleanupSize : CLEANUP_CHUNK_MAX_SIZE); +} + +void +EventGarbageCollector::Shrink () +{ + while (m_nextCleanupSize > m_events.size ()) + m_nextCleanupSize >>= 1; + Grow (); +} + +// Called when a new event was added and the cleanup limit was exceeded in consequence. +void +EventGarbageCollector::Cleanup () +{ + m_events.remove_if (EventExpiredPredicate); + + // If after cleanup we are still over the limit, increase the limit. + if (m_events.size () >= m_nextCleanupSize) + Grow (); + else + Shrink (); +} + + +EventGarbageCollector::~EventGarbageCollector () +{ + for (std::list::iterator event = m_events.begin (); + event != m_events.end (); event++) + { + Simulator::Cancel (*event); + } +} + +}; // namespace ns3 + + + +#ifdef RUN_SELF_TESTS + +#include "ns3/test.h" + +namespace ns3 { + +class EventGarbageCollectorTests : public Test +{ + int m_counter; + EventGarbageCollector *m_events; + + void EventGarbageCollectorCallback (); + +public: + + EventGarbageCollectorTests (); + virtual ~EventGarbageCollectorTests (); + virtual bool RunTests (void); +}; + +EventGarbageCollectorTests::EventGarbageCollectorTests () + : Test ("EventGarbageCollector"), m_counter (0), m_events (0) +{} + +EventGarbageCollectorTests::~EventGarbageCollectorTests () +{} + +void +EventGarbageCollectorTests::EventGarbageCollectorCallback () +{ + m_counter++; + if (m_counter == 50) + { + // this should cause the remaining (50) events to be cancelled + delete m_events; + m_events = 0; + } +} + +bool EventGarbageCollectorTests::RunTests (void) +{ + bool result = true; + + m_events = new EventGarbageCollector (); + + for (int n = 0; n < 100; n++) + { + m_events->Track (Simulator::Schedule + (Simulator::Now (), + &EventGarbageCollectorTests::EventGarbageCollectorCallback, + this)); + } + Simulator::Run (); + NS_TEST_ASSERT_EQUAL (m_events, 0); + NS_TEST_ASSERT_EQUAL (m_counter, 50); + return result; +} + +static EventGarbageCollectorTests g_eventCollectorTests; + +}; + +#endif /* RUN_SELF_TESTS */ diff --git a/src/simulator/event-garbage-collector.h b/src/simulator/event-garbage-collector.h new file mode 100644 index 000000000..b03b86322 --- /dev/null +++ b/src/simulator/event-garbage-collector.h @@ -0,0 +1,62 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 INESC Porto + * 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: Gustavo J. A. M. Carneiro + */ +#ifndef EVENT_GARBAGE_COLLECTOR_H +#define EVENT_GARBAGE_COLLECTOR_H + +#include +#include "event-id.h" +#include "simulator.h" + +namespace ns3 { + +/** + * \brief An object that tracks scheduled events and automatically + * cancels them when it is destroyed. It is useful in situations + * where multiple instances of the same type of event can + * simultaneously be scheduled, and when the events should be limited + * to the lifetime of a container object. + */ +class EventGarbageCollector +{ +public: + + EventGarbageCollector (); + + /** + * \brief Tracks a new event + */ + void Track (EventId event); + + ~EventGarbageCollector (); + +private: + + std::list::size_type m_nextCleanupSize; + std::list m_events; + + void Cleanup (); + void Grow (); + void Shrink (); +}; + +}; // namespace ns3 + +#endif /* EVENT_GARBAGE_COLLECTOR_H */ From 8180ca7a9d76b596857cb7620b807602561e5498 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 27 Sep 2007 12:50:55 +0200 Subject: [PATCH 102/148] add TypeTraits::NonConstType --- src/core/type-traits.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/core/type-traits.h b/src/core/type-traits.h index 4a65404ad..df52a2670 100644 --- a/src/core/type-traits.h +++ b/src/core/type-traits.h @@ -8,18 +8,29 @@ template struct TypeTraits { typedef T ReferencedType; + typedef T NonConstType; }; +template +struct TypeTraits +{ + typedef T ReferencedType; + typedef T NonConstType; +}; + + template struct TypeTraits { typedef T ReferencedType; + typedef T & NonConstType; }; template struct TypeTraits { typedef T ReferencedType; + typedef T & NonConstType; }; From da44de59b959becdb459d40f81e745965dd1d635 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 27 Sep 2007 12:51:17 +0200 Subject: [PATCH 103/148] Implement a Timer API --- src/simulator/timer.cc | 175 +++++++++++++++++++++++++++++++++++++++++ src/simulator/timer.h | 142 +++++++++++++++++++++++++++++++++ src/simulator/wscript | 3 + 3 files changed, 320 insertions(+) create mode 100644 src/simulator/timer.cc create mode 100644 src/simulator/timer.h diff --git a/src/simulator/timer.cc b/src/simulator/timer.cc new file mode 100644 index 000000000..b8891049d --- /dev/null +++ b/src/simulator/timer.cc @@ -0,0 +1,175 @@ +#include "timer.h" +#include "simulator.h" +#include "simulation-singleton.h" +#include "event-garbage-collector.h" + +namespace ns3 { + +Timer::Timer () + : m_flags (0), + m_delay (FemtoSeconds (0)), + m_event (), + m_impl (0) +{} + +Timer::Timer (int flags) + : m_flags (flags), + m_delay (FemtoSeconds (0)), + m_event (), + m_impl (0) +{} + +Timer::~Timer () +{ + if (m_flags & CHECK_ON_DESTROY) + { + if (m_event.IsRunning ()) + { + NS_FATAL_ERROR ("Event is still running while destroying."); + } + } + else if (m_flags & CANCEL_ON_DESTROY) + { + m_event.Cancel (); + } + else if (m_flags & REMOVE_ON_DESTROY) + { + Simulator::Remove (m_event); + } + delete m_impl; +} + +void +Timer::SetDelay (const Time &time) +{ + m_delay = time; +} +Time +Timer::GetDelay (void) const +{ + return m_delay; +} + + +void +Timer::Cancel (void) +{ + Simulator::Cancel (m_event); +} +void +Timer::Remove (void) +{ + Simulator::Remove (m_event); +} +bool +Timer::IsExpired (void) const +{ + return m_event.IsExpired (); +} +bool +Timer::IsRunning (void) const +{ + return m_event.IsRunning (); +} + + +void +Timer::Schedule (void) +{ + NS_ASSERT (m_impl != 0); + if (m_flags & CHECK_ON_SCHEDULE) + { + if (m_event.IsRunning ()) + { + NS_FATAL_ERROR ("Event is still running while re-scheduling."); + } + } + else if (m_flags & CANCEL_ON_SCHEDULE) + { + m_event.Cancel (); + } + else if (m_flags & REMOVE_ON_SCHEDULE) + { + Simulator::Remove (m_event); + } + m_event = m_impl->Schedule (m_delay); + if (m_flags & GARBAGE_COLLECT) + { + SimulationSingleton::Get ()->Track (m_event); + } +} + + +} // namespace ns3 + + +#ifdef RUN_SELF_TESTS +#include "ns3/test.h" + +namespace { +void bari (int) +{} +void barcir (const int &) +{} +void barir (int &) +{} +void barip (int *) +{} +void barcip (const int *) +{} +} + +namespace ns3 { + +class TimerTests : public Test +{ +public: + TimerTests (); + virtual bool RunTests (void); +}; + +TimerTests::TimerTests () + : Test ("Timer") +{} + +bool +TimerTests::RunTests (void) +{ + bool ok = true; + + int a = 0; + int &b = a; + const int &c = a; + Timer timer; + + timer.SetFunction (&bari, a); + timer.SetArguments (2); + timer.SetArguments (a); + timer.SetArguments (b); + timer.SetArguments (c); + timer.SetFunction (&barir, a); + timer.SetArguments (2); + timer.SetArguments (a); + timer.SetArguments (b); + timer.SetArguments (c); + timer.SetFunction (&barcir, a); + timer.SetArguments (2); + timer.SetArguments (a); + timer.SetArguments (b); + timer.SetArguments (c); + // the following call cannot possibly work and is flagged by + // a runtime error. + //timer.SetArguments (0.0); + timer.SetDelay (Seconds (1.0)); + timer.Schedule (); + + Simulator::Run (); + Simulator::Destroy (); + return ok; +} + +TimerTests g_tests; + +} // namespace ns3 + +#endif /* RUN_SELF_TESTS */ diff --git a/src/simulator/timer.h b/src/simulator/timer.h new file mode 100644 index 000000000..a2623ec5f --- /dev/null +++ b/src/simulator/timer.h @@ -0,0 +1,142 @@ +#ifndef TIMER_H +#define TIMER_H + +#include "ns3/fatal-error.h" +#include "nstime.h" +#include "event-id.h" + +namespace ns3 { + +class TimerImpl; + +class Timer +{ +public: + enum { + CHECK_ON_SCHEDULE = (1<<0), + CHECK_ON_DESTROY = (1<<1), + CANCEL_ON_SCHEDULE = (1<<2), + CANCEL_ON_DESTROY = (1<<3), + REMOVE_ON_SCHEDULE = (1<<4), + REMOVE_ON_DESTROY = (1<<5), + GARBAGE_COLLECT = (1<<6), + }; + Timer (); + Timer (int flags); + ~Timer (); + + void SetFunction (void (*fn) (void)); + template + void SetFunction (void (*fn) (U1), T1 a1); + template + void SetFunction (void (*fn) (U1, U2), T1 a1, T2 a2); + template + void SetFunction (void (*fn) (U1, U2, U3), T1 a1, T2 a2, T3 a3); + + template + void SetArguments (T1 a1); + template + void SetArguments (T1 a1, T2 a2); + template + void SetArguments (T1 a1, T2 a2, T3 a3); + + void SetDelay (const Time &time); + Time GetDelay (void) const; + void Cancel (void); + void Remove (void); + bool IsExpired (void) const; + bool IsRunning (void) const; + + void Schedule (void); + +private: + int m_flags; + Time m_delay; + EventId m_event; + TimerImpl *m_impl; +}; + +} // namespace ns3 + + +// The actual implementation. +#include "simulator.h" +#include "ns3/type-traits.h" + +namespace ns3 { + +template +struct TimerTraits; +template +struct TimerTraits +{ + typedef typename TypeTraits::ReferencedType>::NonConstType StoredType; + typedef const StoredType &ParameterType; +}; + +class TimerImpl +{ +public: + virtual ~TimerImpl () {} + virtual EventId Schedule (const Time &delay) = 0; +}; + + +template +struct TimerImplOne : public TimerImpl +{ + virtual void SetArguments (T1 a1) = 0; +}; + + +template +void +Timer::SetFunction (void (*fn) (U1), T1 a1) +{ + struct FnTimerImplOne : public TimerImplOne::ParameterType> + { + typedef void (*FN) (U1); + FnTimerImplOne (FN fn) + : m_fn (fn) {} + virtual void SetArguments (typename TimerTraits::ParameterType a1) { + m_a1 = a1; + } + virtual EventId Schedule (const Time &delay) { + return Simulator::Schedule (delay, m_fn, m_a1); + } + FN m_fn; + typename TimerTraits::StoredType m_a1; + } *function = new FnTimerImplOne (fn); + function->SetArguments (a1); + delete m_impl; + m_impl = function; + +} + +template +void +Timer::SetArguments (T1 a1) +{ + if (m_impl == 0) + { + NS_FATAL_ERROR ("You cannot set the arguments of a Timer before setting its function."); + return; + } + struct TimerImplOne::ParameterType> *impl = + dynamic_cast::ParameterType> *> (m_impl); + if (impl == 0) + { + NS_FATAL_ERROR ("You tried to set Timer arguments incompatible with its function."); + return; + } + impl->SetArguments (a1); +} + + + + +} // namespace ns3 + +#endif /* TIMER_H */ diff --git a/src/simulator/wscript b/src/simulator/wscript index 970a43aea..68279c9e3 100644 --- a/src/simulator/wscript +++ b/src/simulator/wscript @@ -60,6 +60,8 @@ def build(bld): 'event-impl.cc', 'simulator.cc', 'time-default-value.cc', + 'timer.cc', + 'event-garbage-collector.cc', ] headers = bld.create_obj('ns3header') @@ -73,6 +75,7 @@ def build(bld): 'scheduler-factory.h', 'simulation-singleton.h', 'time-default-value.h', + 'timer.h' ] env = bld.env_of_name('default') From e4352e3c5a15a04c537506435c93dc37ea96fc00 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 27 Sep 2007 13:27:18 +0200 Subject: [PATCH 104/148] add doxygen for Timer class and implement the policy flags --- src/simulator/timer.cc | 47 ++++++++++++++++- src/simulator/timer.h | 116 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 161 insertions(+), 2 deletions(-) diff --git a/src/simulator/timer.cc b/src/simulator/timer.cc index b8891049d..3a48bebbf 100644 --- a/src/simulator/timer.cc +++ b/src/simulator/timer.cc @@ -17,7 +17,52 @@ Timer::Timer (int flags) m_delay (FemtoSeconds (0)), m_event (), m_impl (0) -{} +{ + if (flags & GARBAGE_COLLECT) + { + if (flags != GARBAGE_COLLECT) + { + NS_FATAL_ERROR ("You cannot specify another Timer flag with the GARBAGE_COLLECT flag."); + } + } + else + { + int onSchedule = 0; + if (m_flags & CHECK_ON_SCHEDULE) + { + onSchedule++; + } + if (m_flags & CANCEL_ON_SCHEDULE) + { + onSchedule++; + } + if (m_flags & REMOVE_ON_SCHEDULE) + { + onSchedule++; + } + int onDestroy = 0; + if (m_flags & CHECK_ON_DESTROY) + { + onDestroy++; + } + if (m_flags & CANCEL_ON_DESTROY) + { + onDestroy++; + } + if (m_flags & REMOVE_ON_DESTROY) + { + onDestroy++; + } + if (onSchedule > 1) + { + NS_FATAL_ERROR ("You cannot specify more than one ON_SCHEDULE flag on a Timer."); + } + if (onDestroy > 1) + { + NS_FATAL_ERROR ("You cannot specify more than one ON_DESTROY flag on a Timer."); + } + } +} Timer::~Timer () { diff --git a/src/simulator/timer.h b/src/simulator/timer.h index a2623ec5f..71a76a290 100644 --- a/src/simulator/timer.h +++ b/src/simulator/timer.h @@ -9,19 +9,89 @@ namespace ns3 { class TimerImpl; +/** + * \brief a simple Timer class + * + * A timer is used to hold together a delay, a function to invoke + * when the delay expires, and a set of arguments to pass to the function + * when the delay expires. + * + * A timer can also be used to enforce a set of predefined event lifetime + * management policies. These policies are specified at construction time + * and cannot be changed after. + */ class Timer { public: enum { + /** + * This policy enforces a check before each call to Timer::Schedule + * to verify that the timer has already expired. This policy + * is incompatible with CANCEL_ON_SCHEDULE REMOVE_ON_SCHEDULE, and, + * GARBAGE_COLLECT. + */ CHECK_ON_SCHEDULE = (1<<0), + /** + * This policy enforces a check from the destructor of the Timer + * to verify that the timer has already expired. This policy is + * incompatible with CANCEL_ON_DESTROY, REMOVE_ON_DESTROY, and + * GARBAGE_COLLECT. + */ CHECK_ON_DESTROY = (1<<1), + /** + * This policy cancels the event before scheduling a new event + * for each call to Timer::Schedule. This policy + * is incompatible with CHECK_ON_SCHEDULE, REMOVE_ON_SCHEDULE, and, + * GARBAGE_COLLECT. + */ CANCEL_ON_SCHEDULE = (1<<2), + /** + * This policy cancels the event from the destructor of the Timer + * to verify that the event has already expired. This policy is + * incompatible with CHECK_ON_DESTROY, REMOVE_ON_DESTROY, and + * GARBAGE_COLLECT. + */ CANCEL_ON_DESTROY = (1<<3), + /** + * This policy removes the event from the simulation event list + * before scheduling a new event for each call to Timer::Schedule. + * This policy is incompatible with CHECK_ON_SCHEDULE, + * CANCEL_ON_SCHEDULE, and, GARBAGE_COLLECT. + */ REMOVE_ON_SCHEDULE = (1<<4), + /** + * This policy removes the event from the simulation event list + * when the destructor of the Timer is invoked. This policy is + * incompatible with CHECK_ON_DESTROY, CANCEL_ON_DESTROY, and + * GARBAGE_COLLECT. + */ REMOVE_ON_DESTROY = (1<<5), + /** + * This policy is incompatible with all other policies. Event + * event scheduled with this policy is kept track of by an + * event garbage collector which makes sure that all events + * of timers with a GARBAGE_COLLECT policy are cancelled at the + * end of the simulation. + */ GARBAGE_COLLECT = (1<<6), }; + /** + * create a timer with a default event lifetime management policy: + * - CHECK_ON_SCHEDULE + * - CHECK_ON_DESTROY + */ Timer (); + /** + * \param flags the event lifetime management policies to use + * + * The set of flag combinations allowed is: + * - none + * - GARBAGE_COLLECT + * - one of CANCEL_ON_DESTROY, REMOVE_ON_DESTROY, or, CHECK_ON_DESTROY + * - one of CANCEL_ON_SCHEDULE, REMOVE_ON_SCHEDULE, or, CHECK_ON_SCHEDULE + * - one of CANCEL_ON_DESTROY, REMOVE_ON_DESTROY, or, CHECK_ON_DESTROY ored + * with one of CANCEL_ON_SCHEDULE, REMOVE_ON_SCHEDULE, or, CHECK_ON_SCHEDULE. + */ Timer (int flags); ~Timer (); @@ -35,20 +105,64 @@ public: typename T1, typename T2, typename T3> void SetFunction (void (*fn) (U1, U2, U3), T1 a1, T2 a2, T3 a3); + /** + * \param a1 the first argument + * + * Store this argument in this Timer for later use by Timer::Schedule. + */ template void SetArguments (T1 a1); + /** + * \param a1 the first argument + * \param a2 the second argument + * + * Store these arguments in this Timer for later use by Timer::Schedule. + */ template void SetArguments (T1 a1, T2 a2); + /** + * \param a1 the first argument + * \param a2 the second argument + * \param a3 the third argument + * + * Store these arguments in this Timer for later use by Timer::Schedule. + */ template void SetArguments (T1 a1, T2 a2, T3 a3); - void SetDelay (const Time &time); + /** + * \param delay the delay + * + * The next call to Schedule will schedule the timer with this delay. + */ + void SetDelay (const Time &delay); + /** + * \returns the currently-configured delay for the next Schedule. + */ Time GetDelay (void) const; + /** + * Cancel the currently-running event if there is one. Do nothing + * otherwise. + */ void Cancel (void); + /** + * Remove from the simulation event-list the currently-running event + * if there is one. Do nothing otherwise. + */ void Remove (void); + /** + * \return true if there is no currently-running event, false otherwise. + */ bool IsExpired (void) const; + /** + * \return true if there is a currently-running event, false otherwise. + */ bool IsRunning (void) const; + /** + * Schedule a new event using the currently-configured delay, function, + * and arguments. + */ void Schedule (void); private: From 265df9d0f2a0e24e103a11b83655d6c3367908bb Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 27 Sep 2007 13:31:20 +0200 Subject: [PATCH 105/148] add some doxygen --- src/simulator/timer.h | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/src/simulator/timer.h b/src/simulator/timer.h index 71a76a290..8bd7e996c 100644 --- a/src/simulator/timer.h +++ b/src/simulator/timer.h @@ -95,12 +95,41 @@ public: Timer (int flags); ~Timer (); + /** + * \param fn the function + * + * Store this function in this Timer for later use by Timer::Schedule. + */ void SetFunction (void (*fn) (void)); + /** + * \param fn the function + * \param a1 the first argument + * + * Store this function and this argument in this Timer for later use by + * Timer::Schedule. + */ template void SetFunction (void (*fn) (U1), T1 a1); + /** + * \param fn the function + * \param a1 the first argument + * \param a2 the second argument + * + * Store this function and these arguments in this Timer for later use by + * Timer::Schedule. + */ template void SetFunction (void (*fn) (U1, U2), T1 a1, T2 a2); + /** + * \param fn the function + * \param a1 the first argument + * \param a2 the second argument + * \param a3 the third argument + * + * Store this function and these arguments in this Timer for later use by + * Timer::Schedule. + */ template void SetFunction (void (*fn) (U1, U2, U3), T1 a1, T2 a2, T3 a3); @@ -225,8 +254,7 @@ Timer::SetFunction (void (*fn) (U1), T1 a1) } *function = new FnTimerImplOne (fn); function->SetArguments (a1); delete m_impl; - m_impl = function; - + m_impl = function; } template From 74a57d9e12df401789e90b8fe5b0dd8fec7263bc Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 27 Sep 2007 13:45:55 +0200 Subject: [PATCH 106/148] implement the member function version of Timer::SetFunction (a1) --- src/simulator/timer.cc | 28 ++++++++++++- src/simulator/timer.h | 90 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+), 1 deletion(-) diff --git a/src/simulator/timer.cc b/src/simulator/timer.cc index 3a48bebbf..24f6a9207 100644 --- a/src/simulator/timer.cc +++ b/src/simulator/timer.cc @@ -84,6 +84,23 @@ Timer::~Timer () delete m_impl; } +void +Timer::SetFunction (void (*fn) (void)) +{ + struct FnTimerImplZero : public TimerImpl + { + typedef void (*FN) (void); + FnTimerImplZero (FN fn) + : m_fn (fn) {} + virtual EventId Schedule (const Time &delay) { + return Simulator::Schedule (delay, m_fn); + } + FN m_fn; + } *function = new FnTimerImplZero (fn); + delete m_impl; + m_impl = function; +} + void Timer::SetDelay (const Time &time) { @@ -171,6 +188,11 @@ class TimerTests : public Test public: TimerTests (); virtual bool RunTests (void); + void bazi (int) {} + void bazcir (const int&) {} + void bazir (int&) {} + void bazip (int *) {} + void bazcip (const int *) {} }; TimerTests::TimerTests () @@ -185,7 +207,7 @@ TimerTests::RunTests (void) int a = 0; int &b = a; const int &c = a; - Timer timer; + Timer timer = Timer (0); timer.SetFunction (&bari, a); timer.SetArguments (2); @@ -208,6 +230,10 @@ TimerTests::RunTests (void) timer.SetDelay (Seconds (1.0)); timer.Schedule (); + timer.SetFunction (&TimerTests::bazi, this, 1); + timer.SetFunction (&TimerTests::bazir, this, 1); + timer.SetFunction (&TimerTests::bazcir, this, 1); + Simulator::Run (); Simulator::Destroy (); return ok; diff --git a/src/simulator/timer.h b/src/simulator/timer.h index 8bd7e996c..dbd790051 100644 --- a/src/simulator/timer.h +++ b/src/simulator/timer.h @@ -134,6 +134,52 @@ public: typename T1, typename T2, typename T3> void SetFunction (void (*fn) (U1, U2, U3), T1 a1, T2 a2, T3 a3); + /** + * \param memPtr the member function pointer + * \param objPtr the pointer to object + * + * Store this function and object in this Timer for later use by Timer::Schedule. + */ + template + void SetFunction (MEM_PTR memPtr, OBJ_PTR objPtr); + /** + * \param memPtr the member function pointer + * \param objPtr the pointer to object + * \param a1 the first argument + * + * Store this function and this argument in this Timer for later use by + * Timer::Schedule. + */ + template + void SetFunction (MEM_PTR memPtr, OBJ_PTR objPtr, T1 a1); + /** + * \param memPtr the member function pointer + * \param objPtr the pointer to object + * \param a1 the first argument + * \param a2 the second argument + * + * Store this function and these arguments in this Timer for later use by + * Timer::Schedule. + */ + template + void SetFunction (MEM_PTR memPtr, OBJ_PTR objPtr, T1 a1, T2 a2); + /** + * \param memPtr the member function pointer + * \param objPtr the pointer to object + * \param a1 the first argument + * \param a2 the second argument + * \param a3 the third argument + * + * Store this function and these arguments in this Timer for later use by + * Timer::Schedule. + */ + template + void SetFunction (MEM_PTR memPtr, OBJ_PTR objPtr, T1 a1, T2 a2, T3 a3); + + /** * \param a1 the first argument * @@ -276,6 +322,50 @@ Timer::SetArguments (T1 a1) impl->SetArguments (a1); } +template +void +Timer::SetFunction (MEM_PTR memPtr, OBJ_PTR objPtr) +{ + struct MemFnTimerImplZero : public TimerImpl + { + MemFnTimerImplZero (MEM_PTR memPtr, OBJ_PTR objPtr) + : m_memPtr (memPtr), m_objPtr (objPtr) {} + virtual EventId Schedule (const Time &delay) { + return Simulator::Schedule (delay, m_memPtr, m_objPtr); + } + MEM_PTR m_memPtr; + OBJ_PTR m_objPtr; + } *function = new MemFnTimerImplZero (memPtr, objPtr); + delete m_impl; + m_impl = function; +} + + + +template +void +Timer::SetFunction (MEM_PTR memPtr, OBJ_PTR objPtr, T1 a1) +{ + struct MemFnTimerImplZero : public TimerImpl + { + MemFnTimerImplZero (MEM_PTR memPtr, OBJ_PTR objPtr) + : m_memPtr (memPtr), m_objPtr (objPtr) {} + virtual void SetArguments (typename TimerTraits::ParameterType a1) { + m_a1 = a1; + } + virtual EventId Schedule (const Time &delay) { + return Simulator::Schedule (delay, m_memPtr, m_objPtr, m_a1); + } + MEM_PTR m_memPtr; + OBJ_PTR m_objPtr; + typename TimerTraits::StoredType m_a1; + } *function = new MemFnTimerImplZero (memPtr, objPtr); + function->SetArguments (a1); + delete m_impl; + m_impl = function; +} + From c1717498f4cac857d23b1b8cdca38e2bca4c0cc3 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 27 Sep 2007 13:48:32 +0200 Subject: [PATCH 107/148] add testcase, make it work --- src/simulator/timer.cc | 3 +++ src/simulator/timer.h | 6 +++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/simulator/timer.cc b/src/simulator/timer.cc index 24f6a9207..489847c3b 100644 --- a/src/simulator/timer.cc +++ b/src/simulator/timer.cc @@ -231,8 +231,11 @@ TimerTests::RunTests (void) timer.Schedule (); timer.SetFunction (&TimerTests::bazi, this, 1); + timer.SetArguments (3); timer.SetFunction (&TimerTests::bazir, this, 1); + timer.SetArguments (3); timer.SetFunction (&TimerTests::bazcir, this, 1); + timer.SetArguments (3); Simulator::Run (); Simulator::Destroy (); diff --git a/src/simulator/timer.h b/src/simulator/timer.h index dbd790051..ebd38a306 100644 --- a/src/simulator/timer.h +++ b/src/simulator/timer.h @@ -347,9 +347,9 @@ template ::ParameterType> { - MemFnTimerImplZero (MEM_PTR memPtr, OBJ_PTR objPtr) + MemFnTimerImplOne (MEM_PTR memPtr, OBJ_PTR objPtr) : m_memPtr (memPtr), m_objPtr (objPtr) {} virtual void SetArguments (typename TimerTraits::ParameterType a1) { m_a1 = a1; @@ -360,7 +360,7 @@ Timer::SetFunction (MEM_PTR memPtr, OBJ_PTR objPtr, T1 a1) MEM_PTR m_memPtr; OBJ_PTR m_objPtr; typename TimerTraits::StoredType m_a1; - } *function = new MemFnTimerImplZero (memPtr, objPtr); + } *function = new MemFnTimerImplOne (memPtr, objPtr); function->SetArguments (a1); delete m_impl; m_impl = function; From 4d1ab8f4badb403ac1e279d006ec45de2cb25092 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 28 Sep 2007 09:27:57 +0200 Subject: [PATCH 108/148] rewrite the TypeTraits and add some tests --- src/core/type-traits-test.cc | 40 ++++++++++ src/core/type-traits.h | 145 ++++++++++++++++++++++++++++------- src/core/wscript | 1 + 3 files changed, 159 insertions(+), 27 deletions(-) create mode 100644 src/core/type-traits-test.cc diff --git a/src/core/type-traits-test.cc b/src/core/type-traits-test.cc new file mode 100644 index 000000000..93e310bf4 --- /dev/null +++ b/src/core/type-traits-test.cc @@ -0,0 +1,40 @@ +#include "type-traits.h" +#include "test.h" + +#ifdef RUN_SELF_TESTS + +namespace ns3 { + +class TypeTraitsTest : public Test +{ +public: + TypeTraitsTest (); + virtual bool RunTests (void); +}; + +TypeTraitsTest::TypeTraitsTest () + : Test ("TypeTraits") +{} +bool +TypeTraitsTest::RunTests (void) +{ + bool result = true; + + //TypeTraits::ReferencedType ir; + //TypeTraits::NonConstType uci; + NS_TEST_ASSERT_EQUAL (TypeTraits::IsPointerToMember, 1); + NS_TEST_ASSERT_EQUAL (TypeTraits::IsPointerToMember, 1); + NS_TEST_ASSERT_EQUAL (TypeTraits::IsPointerToMember, 1); + NS_TEST_ASSERT_EQUAL (TypeTraits::IsPointerToMember, 1); + NS_TEST_ASSERT_EQUAL (TypeTraits::PointerToMemberTraits::nArgs, 0); + NS_TEST_ASSERT_EQUAL (TypeTraits::PointerToMemberTraits::nArgs, 1); + + return result; +} + +static TypeTraitsTest g_typeTraitsTest; + +} // namespace ns3 + +#endif /* RUN_SELF_TESTS */ + diff --git a/src/core/type-traits.h b/src/core/type-traits.h index df52a2670..d26df8c13 100644 --- a/src/core/type-traits.h +++ b/src/core/type-traits.h @@ -1,36 +1,127 @@ #ifndef TYPE_TRAITS_H #define TYPE_TRAITS_H -template -struct TypeTraits; - template struct TypeTraits { - typedef T ReferencedType; - typedef T NonConstType; -}; - -template -struct TypeTraits -{ - typedef T ReferencedType; - typedef T NonConstType; -}; - - -template -struct TypeTraits -{ - typedef T ReferencedType; - typedef T & NonConstType; -}; - -template -struct TypeTraits -{ - typedef T ReferencedType; - typedef T & NonConstType; +private: + struct NullType {}; + template struct UnConst + { + typedef U Result; + }; + template struct UnConst + { + typedef U Result; + }; + template struct ReferenceTraits + { + enum {IsReference = 0}; + typedef U ReferencedType; + }; + template struct ReferenceTraits + { + enum {IsReference = 1}; + typedef U ReferencedType; + }; + template struct PointerTraits + { + enum {IsPointer = 0}; + typedef U PointeeType; + }; + template struct PointerTraits + { + enum {IsPointer = 1}; + typedef U PointeeType; + }; + template struct FunctionPtrTraits + { + enum {IsFunctionPointer = 0}; + }; + template + struct FunctionPtrTraits + { + enum {IsFunctionPointer = 1}; + typedef U ReturnType; + }; + template + struct FunctionPtrTraits + { + enum {IsFunctionPointer = 1}; + typedef U ReturnType; + typedef V1 Arg1Type; + }; + template + struct FunctionPtrTraits + { + enum {IsFunctionPointer = 1}; + typedef U ReturnType; + typedef V1 Arg1Type; + typedef V2 Arg2Type; + }; + template struct PtrToMemberTraits + { + enum {IsPointerToMember = 0}; + }; + template + struct PtrToMemberTraits + { + enum {IsPointerToMember = 1}; + enum {nArgs = 0}; + typedef U ReturnType; + }; + template + struct PtrToMemberTraits + { + enum {IsPointerToMember = 1}; + enum {nArgs = 0}; + typedef U ReturnType; + }; + template + struct PtrToMemberTraits + { + enum {IsPointerToMember = 1}; + enum {nArgs = 1}; + typedef U ReturnType; + typedef W1 Arg1Type; + }; + template + struct PtrToMemberTraits + { + enum {IsPointerToMember = 1}; + enum {nArgs = 1}; + typedef U ReturnType; + typedef W1 Arg1Type; + }; + template + struct PtrToMemberTraits + { + enum {IsPointerToMember = 1}; + enum {nArgs = 2}; + typedef U ReturnType; + typedef W1 Arg1Type; + typedef W2 Arg2Type; + }; + template + struct PtrToMemberTraits + { + enum {IsPointerToMember = 1}; + enum {nArgs = 2}; + typedef U ReturnType; + typedef W1 Arg1Type; + typedef W2 Arg2Type; + }; + +public: + typedef typename UnConst::Result NonConstType; + typedef typename ReferenceTraits::ReferencedType ReferencedType; + typedef typename PointerTraits::PointeeType PointeeType; + enum {IsPointerToMember = PtrToMemberTraits::IsPointerToMember}; + enum {IsPointer = PointerTraits::IsPointer}; + enum {IsReference = ReferenceTraits::IsReference}; + enum {IsFunctionPointer = FunctionPtrTraits::IsFunctionPointer}; + typedef PtrToMemberTraits PointerToMemberTraits; + typedef FunctionPtrTraits FunctionPointerTraits; }; diff --git a/src/core/wscript b/src/core/wscript index 5806f254b..c69bb018c 100644 --- a/src/core/wscript +++ b/src/core/wscript @@ -51,6 +51,7 @@ def build(bld): 'composite-trace-resolver.cc', 'trace-doc.cc', 'trace-source.cc', + 'type-traits-test.cc', ] if sys.platform == 'win32': From 343d9fd63fe472f27557a4189b5246c76088d226 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 28 Sep 2007 10:32:15 +0200 Subject: [PATCH 109/148] forgot to add the nArgs enum in FunctionPtrTraits --- src/core/type-traits.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/core/type-traits.h b/src/core/type-traits.h index d26df8c13..2fcb83550 100644 --- a/src/core/type-traits.h +++ b/src/core/type-traits.h @@ -42,12 +42,14 @@ private: struct FunctionPtrTraits { enum {IsFunctionPointer = 1}; + enum {nArgs = 0}; typedef U ReturnType; }; template struct FunctionPtrTraits { enum {IsFunctionPointer = 1}; + enum {nArgs = 1}; typedef U ReturnType; typedef V1 Arg1Type; }; @@ -55,6 +57,7 @@ private: struct FunctionPtrTraits { enum {IsFunctionPointer = 1}; + enum {nArgs = 2}; typedef U ReturnType; typedef V1 Arg1Type; typedef V2 Arg2Type; From 13347878df50f1421fdb73544ded91c201b0df05 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 28 Sep 2007 10:32:59 +0200 Subject: [PATCH 110/148] implement the helper IntToType template --- src/core/int-to-type.h | 19 +++++++++++++++++++ src/core/wscript | 1 + 2 files changed, 20 insertions(+) create mode 100644 src/core/int-to-type.h diff --git a/src/core/int-to-type.h b/src/core/int-to-type.h new file mode 100644 index 000000000..240fe8a71 --- /dev/null +++ b/src/core/int-to-type.h @@ -0,0 +1,19 @@ +#ifndef INT_TO_TYPE_H +#define INT_TO_TYPE_H + +namespace ns3 { + +/** + * This trivial template is extremely useful, as explained in + * "Modern C++ Design", p29, section 2.4, + * "Mapping Integral Constants to Types" + */ +template +struct IntToType +{ + enum {value = v}; +}; + +} // namespace ns3 + +#endif /* INT_TO_TYPE_H */ diff --git a/src/core/wscript b/src/core/wscript index c69bb018c..bfb784825 100644 --- a/src/core/wscript +++ b/src/core/wscript @@ -95,5 +95,6 @@ def build(bld): 'composite-trace-resolver.h', 'array-trace-resolver.h', 'trace-doc.h', + 'int-to-type.h', ] From cbf46c9ec4221e193c1883d3c3a631a60f030f1a Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 28 Sep 2007 10:33:19 +0200 Subject: [PATCH 111/148] re-implement the Timer class with the new type traits --- src/simulator/timer.cc | 33 +++------ src/simulator/timer.h | 154 ++++++++++++++++++----------------------- 2 files changed, 74 insertions(+), 113 deletions(-) diff --git a/src/simulator/timer.cc b/src/simulator/timer.cc index 489847c3b..bd7523807 100644 --- a/src/simulator/timer.cc +++ b/src/simulator/timer.cc @@ -84,23 +84,6 @@ Timer::~Timer () delete m_impl; } -void -Timer::SetFunction (void (*fn) (void)) -{ - struct FnTimerImplZero : public TimerImpl - { - typedef void (*FN) (void); - FnTimerImplZero (FN fn) - : m_fn (fn) {} - virtual EventId Schedule (const Time &delay) { - return Simulator::Schedule (delay, m_fn); - } - FN m_fn; - } *function = new FnTimerImplZero (fn); - delete m_impl; - m_impl = function; -} - void Timer::SetDelay (const Time &time) { @@ -202,24 +185,24 @@ TimerTests::TimerTests () bool TimerTests::RunTests (void) { - bool ok = true; + bool result = true; int a = 0; int &b = a; const int &c = a; Timer timer = Timer (0); - timer.SetFunction (&bari, a); + timer.SetFunction (&bari); timer.SetArguments (2); timer.SetArguments (a); timer.SetArguments (b); timer.SetArguments (c); - timer.SetFunction (&barir, a); + timer.SetFunction (&barir); timer.SetArguments (2); timer.SetArguments (a); timer.SetArguments (b); timer.SetArguments (c); - timer.SetFunction (&barcir, a); + timer.SetFunction (&barcir); timer.SetArguments (2); timer.SetArguments (a); timer.SetArguments (b); @@ -230,16 +213,16 @@ TimerTests::RunTests (void) timer.SetDelay (Seconds (1.0)); timer.Schedule (); - timer.SetFunction (&TimerTests::bazi, this, 1); + timer.SetFunction (&TimerTests::bazi, this); timer.SetArguments (3); - timer.SetFunction (&TimerTests::bazir, this, 1); + timer.SetFunction (&TimerTests::bazir, this); timer.SetArguments (3); - timer.SetFunction (&TimerTests::bazcir, this, 1); + timer.SetFunction (&TimerTests::bazcir, this); timer.SetArguments (3); Simulator::Run (); Simulator::Destroy (); - return ok; + return result; } TimerTests g_tests; diff --git a/src/simulator/timer.h b/src/simulator/timer.h index ebd38a306..0c2c2343e 100644 --- a/src/simulator/timer.h +++ b/src/simulator/timer.h @@ -4,6 +4,7 @@ #include "ns3/fatal-error.h" #include "nstime.h" #include "event-id.h" +#include "ns3/int-to-type.h" namespace ns3 { @@ -100,39 +101,8 @@ public: * * Store this function in this Timer for later use by Timer::Schedule. */ - void SetFunction (void (*fn) (void)); - /** - * \param fn the function - * \param a1 the first argument - * - * Store this function and this argument in this Timer for later use by - * Timer::Schedule. - */ - template - void SetFunction (void (*fn) (U1), T1 a1); - /** - * \param fn the function - * \param a1 the first argument - * \param a2 the second argument - * - * Store this function and these arguments in this Timer for later use by - * Timer::Schedule. - */ - template - void SetFunction (void (*fn) (U1, U2), T1 a1, T2 a2); - /** - * \param fn the function - * \param a1 the first argument - * \param a2 the second argument - * \param a3 the third argument - * - * Store this function and these arguments in this Timer for later use by - * Timer::Schedule. - */ - template - void SetFunction (void (*fn) (U1, U2, U3), T1 a1, T2 a2, T3 a3); + template + void SetFunction (FN fn); /** * \param memPtr the member function pointer @@ -142,42 +112,6 @@ public: */ template void SetFunction (MEM_PTR memPtr, OBJ_PTR objPtr); - /** - * \param memPtr the member function pointer - * \param objPtr the pointer to object - * \param a1 the first argument - * - * Store this function and this argument in this Timer for later use by - * Timer::Schedule. - */ - template - void SetFunction (MEM_PTR memPtr, OBJ_PTR objPtr, T1 a1); - /** - * \param memPtr the member function pointer - * \param objPtr the pointer to object - * \param a1 the first argument - * \param a2 the second argument - * - * Store this function and these arguments in this Timer for later use by - * Timer::Schedule. - */ - template - void SetFunction (MEM_PTR memPtr, OBJ_PTR objPtr, T1 a1, T2 a2); - /** - * \param memPtr the member function pointer - * \param objPtr the pointer to object - * \param a1 the first argument - * \param a2 the second argument - * \param a3 the third argument - * - * Store this function and these arguments in this Timer for later use by - * Timer::Schedule. - */ - template - void SetFunction (MEM_PTR memPtr, OBJ_PTR objPtr, T1 a1, T2 a2, T3 a3); /** @@ -241,6 +175,15 @@ public: void Schedule (void); private: + template + void DoSetFunction (IntToType<0>, FN fn); + template + void DoSetFunction (IntToType<1>, FN fn); + template + void DoSetFunction (IntToType<0>, MEM_PTR memPtr, OBJ_PTR objPtr); + template + void DoSetFunction (IntToType<1>, MEM_PTR memPtr, OBJ_PTR objPtr); + int m_flags; Time m_delay; EventId m_event; @@ -256,6 +199,7 @@ private: namespace ns3 { + template struct TimerTraits; template @@ -280,29 +224,57 @@ struct TimerImplOne : public TimerImpl }; -template +template void -Timer::SetFunction (void (*fn) (U1), T1 a1) +Timer::SetFunction (FN fn) { - struct FnTimerImplOne : public TimerImplOne::ParameterType> + NS_ASSERT (TypeTraits::IsFunctionPointer); + DoSetFunction (IntToType::FunctionPointerTraits::nArgs> (), fn); +} + +template +void +Timer::DoSetFunction (IntToType<0>, FN fn) +{ + struct FnTimerImplZero : public TimerImpl + { + FnTimerImplZero (FN fn) + : m_fn (fn) {} + virtual EventId Schedule (const Time &delay) { + return Simulator::Schedule (delay, m_fn); + } + FN m_fn; + } *function = new FnTimerImplZero (fn); + delete m_impl; + m_impl = function; +} + +template +void +Timer::DoSetFunction (IntToType<1>, FN fn) +{ + typedef typename TypeTraits::FunctionPointerTraits::Arg1Type T1; + typedef typename TimerTraits::ParameterType T1Parameter; + typedef typename TimerTraits::StoredType T1Stored; + + struct FnTimerImplOne : public TimerImplOne { - typedef void (*FN) (U1); FnTimerImplOne (FN fn) : m_fn (fn) {} - virtual void SetArguments (typename TimerTraits::ParameterType a1) { + virtual void SetArguments (T1Parameter a1) { m_a1 = a1; } virtual EventId Schedule (const Time &delay) { return Simulator::Schedule (delay, m_fn, m_a1); } FN m_fn; - typename TimerTraits::StoredType m_a1; + T1Stored m_a1; } *function = new FnTimerImplOne (fn); - function->SetArguments (a1); delete m_impl; m_impl = function; } + template void Timer::SetArguments (T1 a1) @@ -322,9 +294,18 @@ Timer::SetArguments (T1 a1) impl->SetArguments (a1); } + template void Timer::SetFunction (MEM_PTR memPtr, OBJ_PTR objPtr) +{ + NS_ASSERT (TypeTraits::IsPointerToMember); + DoSetFunction (IntToType::PointerToMemberTraits::nArgs> () , memPtr, objPtr); +} + +template +void +Timer::DoSetFunction (IntToType<0>, MEM_PTR memPtr, OBJ_PTR objPtr) { struct MemFnTimerImplZero : public TimerImpl { @@ -340,18 +321,19 @@ Timer::SetFunction (MEM_PTR memPtr, OBJ_PTR objPtr) m_impl = function; } - - -template +template void -Timer::SetFunction (MEM_PTR memPtr, OBJ_PTR objPtr, T1 a1) +Timer::DoSetFunction (IntToType<1>, MEM_PTR memPtr, OBJ_PTR objPtr) { - struct MemFnTimerImplOne : public TimerImplOne::ParameterType> + typedef typename TypeTraits::PointerToMemberTraits::Arg1Type T1; + typedef typename TimerTraits::ParameterType T1Parameter; + typedef typename TimerTraits::StoredType T1Stored; + + struct MemFnTimerImplOne : public TimerImplOne { MemFnTimerImplOne (MEM_PTR memPtr, OBJ_PTR objPtr) : m_memPtr (memPtr), m_objPtr (objPtr) {} - virtual void SetArguments (typename TimerTraits::ParameterType a1) { + virtual void SetArguments (T1Parameter a1) { m_a1 = a1; } virtual EventId Schedule (const Time &delay) { @@ -359,16 +341,12 @@ Timer::SetFunction (MEM_PTR memPtr, OBJ_PTR objPtr, T1 a1) } MEM_PTR m_memPtr; OBJ_PTR m_objPtr; - typename TimerTraits::StoredType m_a1; + T1Stored m_a1; } *function = new MemFnTimerImplOne (memPtr, objPtr); - function->SetArguments (a1); delete m_impl; m_impl = function; } - - - } // namespace ns3 #endif /* TIMER_H */ From 82070b999ca5ff360611f5cb9ba83fd7cc8b3b2c Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 28 Sep 2007 10:42:29 +0200 Subject: [PATCH 112/148] simplicy policy handling --- src/simulator/timer.cc | 61 +++++++------------------------ src/simulator/timer.h | 82 +++++++++++++++++++----------------------- 2 files changed, 48 insertions(+), 95 deletions(-) diff --git a/src/simulator/timer.cc b/src/simulator/timer.cc index bd7523807..2bd973ec8 100644 --- a/src/simulator/timer.cc +++ b/src/simulator/timer.cc @@ -12,57 +12,20 @@ Timer::Timer () m_impl (0) {} -Timer::Timer (int flags) - : m_flags (flags), +Timer::Timer (enum SchedulePolicy schedulePolicy, + enum DestroyPolicy destroyPolicy) + : m_flags (schedulePolicy | destroyPolicy), m_delay (FemtoSeconds (0)), m_event (), m_impl (0) -{ - if (flags & GARBAGE_COLLECT) - { - if (flags != GARBAGE_COLLECT) - { - NS_FATAL_ERROR ("You cannot specify another Timer flag with the GARBAGE_COLLECT flag."); - } - } - else - { - int onSchedule = 0; - if (m_flags & CHECK_ON_SCHEDULE) - { - onSchedule++; - } - if (m_flags & CANCEL_ON_SCHEDULE) - { - onSchedule++; - } - if (m_flags & REMOVE_ON_SCHEDULE) - { - onSchedule++; - } - int onDestroy = 0; - if (m_flags & CHECK_ON_DESTROY) - { - onDestroy++; - } - if (m_flags & CANCEL_ON_DESTROY) - { - onDestroy++; - } - if (m_flags & REMOVE_ON_DESTROY) - { - onDestroy++; - } - if (onSchedule > 1) - { - NS_FATAL_ERROR ("You cannot specify more than one ON_SCHEDULE flag on a Timer."); - } - if (onDestroy > 1) - { - NS_FATAL_ERROR ("You cannot specify more than one ON_DESTROY flag on a Timer."); - } - } -} +{} + +Timer::Timer (enum GarbageCollectPolicy policy) + : m_flags (GARBAGE_COLLECT), + m_delay (FemtoSeconds (0)), + m_event (), + m_impl (0) +{} Timer::~Timer () { @@ -190,7 +153,7 @@ TimerTests::RunTests (void) int a = 0; int &b = a; const int &c = a; - Timer timer = Timer (0); + Timer timer; timer.SetFunction (&bari); timer.SetArguments (2); diff --git a/src/simulator/timer.h b/src/simulator/timer.h index 0c2c2343e..6c97ca5c0 100644 --- a/src/simulator/timer.h +++ b/src/simulator/timer.h @@ -24,57 +24,48 @@ class TimerImpl; class Timer { public: - enum { - /** - * This policy enforces a check before each call to Timer::Schedule - * to verify that the timer has already expired. This policy - * is incompatible with CANCEL_ON_SCHEDULE REMOVE_ON_SCHEDULE, and, - * GARBAGE_COLLECT. - */ - CHECK_ON_SCHEDULE = (1<<0), - /** - * This policy enforces a check from the destructor of the Timer - * to verify that the timer has already expired. This policy is - * incompatible with CANCEL_ON_DESTROY, REMOVE_ON_DESTROY, and - * GARBAGE_COLLECT. - */ - CHECK_ON_DESTROY = (1<<1), + enum SchedulePolicy { /** * This policy cancels the event before scheduling a new event - * for each call to Timer::Schedule. This policy - * is incompatible with CHECK_ON_SCHEDULE, REMOVE_ON_SCHEDULE, and, - * GARBAGE_COLLECT. + * for each call to Timer::Schedule. */ - CANCEL_ON_SCHEDULE = (1<<2), - /** - * This policy cancels the event from the destructor of the Timer - * to verify that the event has already expired. This policy is - * incompatible with CHECK_ON_DESTROY, REMOVE_ON_DESTROY, and - * GARBAGE_COLLECT. - */ - CANCEL_ON_DESTROY = (1<<3), + CANCEL_ON_SCHEDULE = (1<<0), /** * This policy removes the event from the simulation event list * before scheduling a new event for each call to Timer::Schedule. - * This policy is incompatible with CHECK_ON_SCHEDULE, - * CANCEL_ON_SCHEDULE, and, GARBAGE_COLLECT. */ - REMOVE_ON_SCHEDULE = (1<<4), + REMOVE_ON_SCHEDULE = (1<<1), + /** + * This policy enforces a check before each call to Timer::Schedule + * to verify that the timer has already expired. + */ + CHECK_ON_SCHEDULE = (1<<2), + }; + enum DestroyPolicy { + /** + * This policy cancels the event from the destructor of the Timer + * to verify that the event has already expired. + */ + CANCEL_ON_DESTROY = (1<<3), /** * This policy removes the event from the simulation event list - * when the destructor of the Timer is invoked. This policy is - * incompatible with CHECK_ON_DESTROY, CANCEL_ON_DESTROY, and - * GARBAGE_COLLECT. + * when the destructor of the Timer is invoked. */ - REMOVE_ON_DESTROY = (1<<5), + REMOVE_ON_DESTROY = (1<<4), /** - * This policy is incompatible with all other policies. Event - * event scheduled with this policy is kept track of by an + * This policy enforces a check from the destructor of the Timer + * to verify that the timer has already expired. + */ + CHECK_ON_DESTROY = (1<<5) + }; + enum GarbageCollectPolicy { + /** + * Every event scheduled with this policy is kept track of by an * event garbage collector which makes sure that all events * of timers with a GARBAGE_COLLECT policy are cancelled at the * end of the simulation. */ - GARBAGE_COLLECT = (1<<6), + GARBAGE_COLLECT = (1<<6) }; /** * create a timer with a default event lifetime management policy: @@ -83,17 +74,16 @@ public: */ Timer (); /** - * \param flags the event lifetime management policies to use - * - * The set of flag combinations allowed is: - * - none - * - GARBAGE_COLLECT - * - one of CANCEL_ON_DESTROY, REMOVE_ON_DESTROY, or, CHECK_ON_DESTROY - * - one of CANCEL_ON_SCHEDULE, REMOVE_ON_SCHEDULE, or, CHECK_ON_SCHEDULE - * - one of CANCEL_ON_DESTROY, REMOVE_ON_DESTROY, or, CHECK_ON_DESTROY ored - * with one of CANCEL_ON_SCHEDULE, REMOVE_ON_SCHEDULE, or, CHECK_ON_SCHEDULE. + * \param scheduleFlags the event lifetime management policies to use for schedule events + * \param destroyFlags the event lifetime management policies to use for destroy events */ - Timer (int flags); + Timer (enum SchedulePolicy schedulePolicy, + enum DestroyPolicy destroyPolicy); + /** + * \param policy the garbage collect policy. Only one + * value is possible. + */ + Timer (enum GarbageCollectPolicy policy); ~Timer (); /** From d953614fe421da145c8a8b1b156443eae0862e40 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 28 Sep 2007 12:46:17 +0200 Subject: [PATCH 113/148] add 6 arg support to pointer to member traits --- src/core/type-traits.h | 114 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 113 insertions(+), 1 deletion(-) diff --git a/src/core/type-traits.h b/src/core/type-traits.h index 2fcb83550..58dbb9e3f 100644 --- a/src/core/type-traits.h +++ b/src/core/type-traits.h @@ -114,7 +114,119 @@ private: typedef W1 Arg1Type; typedef W2 Arg2Type; }; - + template + struct PtrToMemberTraits + { + enum {IsPointerToMember = 1}; + enum {nArgs = 3}; + typedef U ReturnType; + typedef W1 Arg1Type; + typedef W2 Arg2Type; + typedef W3 Arg3Type; + }; + template + struct PtrToMemberTraits + { + enum {IsPointerToMember = 1}; + enum {nArgs = 3}; + typedef U ReturnType; + typedef W1 Arg1Type; + typedef W2 Arg2Type; + typedef W3 Arg3Type; + }; + template + struct PtrToMemberTraits + { + enum {IsPointerToMember = 1}; + enum {nArgs = 4}; + typedef U ReturnType; + typedef W1 Arg1Type; + typedef W2 Arg2Type; + typedef W3 Arg3Type; + typedef W4 Arg4Type; + }; + template + struct PtrToMemberTraits + { + enum {IsPointerToMember = 1}; + enum {nArgs = 4}; + typedef U ReturnType; + typedef W1 Arg1Type; + typedef W2 Arg2Type; + typedef W3 Arg3Type; + typedef W4 Arg4Type; + }; + template + struct PtrToMemberTraits + { + enum {IsPointerToMember = 1}; + enum {nArgs = 5}; + typedef U ReturnType; + typedef W1 Arg1Type; + typedef W2 Arg2Type; + typedef W3 Arg3Type; + typedef W4 Arg4Type; + typedef W5 Arg5Type; + }; + template + struct PtrToMemberTraits + { + enum {IsPointerToMember = 1}; + enum {nArgs = 5}; + typedef U ReturnType; + typedef W1 Arg1Type; + typedef W2 Arg2Type; + typedef W3 Arg3Type; + typedef W4 Arg4Type; + typedef W5 Arg5Type; + }; + template + struct PtrToMemberTraits + { + enum {IsPointerToMember = 1}; + enum {nArgs = 6}; + typedef U ReturnType; + typedef W1 Arg1Type; + typedef W2 Arg2Type; + typedef W3 Arg3Type; + typedef W4 Arg4Type; + typedef W5 Arg5Type; + typedef W6 Arg6Type; + }; + template + struct PtrToMemberTraits + { + enum {IsPointerToMember = 1}; + enum {nArgs = 6}; + typedef U ReturnType; + typedef W1 Arg1Type; + typedef W2 Arg2Type; + typedef W3 Arg3Type; + typedef W4 Arg4Type; + typedef W5 Arg5Type; + typedef W6 Arg6Type; + }; + public: typedef typename UnConst::Result NonConstType; typedef typename ReferenceTraits::ReferencedType ReferencedType; From eec001cad48b98c2bde62667cc66df8f6c097f81 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 28 Sep 2007 12:49:05 +0200 Subject: [PATCH 114/148] add 6 arg support to pointer to function traits --- src/core/type-traits.h | 52 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/src/core/type-traits.h b/src/core/type-traits.h index 58dbb9e3f..54c550e10 100644 --- a/src/core/type-traits.h +++ b/src/core/type-traits.h @@ -62,6 +62,58 @@ private: typedef V1 Arg1Type; typedef V2 Arg2Type; }; + template + struct FunctionPtrTraits + { + enum {IsFunctionPointer = 1}; + enum {nArgs = 3}; + typedef U ReturnType; + typedef V1 Arg1Type; + typedef V2 Arg2Type; + typedef V3 Arg3Type; + }; + template + struct FunctionPtrTraits + { + enum {IsFunctionPointer = 1}; + enum {nArgs = 4}; + typedef U ReturnType; + typedef V1 Arg1Type; + typedef V2 Arg2Type; + typedef V3 Arg3Type; + typedef V4 Arg4Type; + }; + template + struct FunctionPtrTraits + { + enum {IsFunctionPointer = 1}; + enum {nArgs = 5}; + typedef U ReturnType; + typedef V1 Arg1Type; + typedef V2 Arg2Type; + typedef V3 Arg3Type; + typedef V4 Arg4Type; + typedef V5 Arg5Type; + }; + template + struct FunctionPtrTraits + { + enum {IsFunctionPointer = 1}; + enum {nArgs = 6}; + typedef U ReturnType; + typedef V1 Arg1Type; + typedef V2 Arg2Type; + typedef V3 Arg3Type; + typedef V4 Arg4Type; + typedef V5 Arg5Type; + typedef V6 Arg6Type; + }; template struct PtrToMemberTraits { enum {IsPointerToMember = 0}; From 5391aa0fa2a96c556bf5126da5e5962754e620ca Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 28 Sep 2007 13:02:14 +0200 Subject: [PATCH 115/148] add support for 6-arg SetArguments --- src/simulator/timer.h | 240 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 219 insertions(+), 21 deletions(-) diff --git a/src/simulator/timer.h b/src/simulator/timer.h index 6c97ca5c0..d282d8bd2 100644 --- a/src/simulator/timer.h +++ b/src/simulator/timer.h @@ -128,6 +128,39 @@ public: */ template void SetArguments (T1 a1, T2 a2, T3 a3); + /** + * \param a1 the first argument + * \param a2 the second argument + * \param a3 the third argument + * \param a4 the fourth argument + * + * Store these arguments in this Timer for later use by Timer::Schedule. + */ + template + void SetArguments (T1 a1, T2 a2, T3 a3, T4 a4); + /** + * \param a1 the first argument + * \param a2 the second argument + * \param a3 the third argument + * \param a4 the fourth argument + * \param a5 the fifth argument + * + * Store these arguments in this Timer for later use by Timer::Schedule. + */ + template + void SetArguments (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5); + /** + * \param a1 the first argument + * \param a2 the second argument + * \param a3 the third argument + * \param a4 the fourth argument + * \param a5 the fifth argument + * \param a6 the sixth argument + * + * Store these arguments in this Timer for later use by Timer::Schedule. + */ + template + void SetArguments (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6); /** * \param delay the delay @@ -169,10 +202,27 @@ private: void DoSetFunction (IntToType<0>, FN fn); template void DoSetFunction (IntToType<1>, FN fn); + template + void DoSetFunction (IntToType<2>, FN fn); + template + void DoSetFunction (IntToType<3>, FN fn); + template + void DoSetFunction (IntToType<4>, FN fn); + template + void DoSetFunction (IntToType<5>, FN fn); + template void DoSetFunction (IntToType<0>, MEM_PTR memPtr, OBJ_PTR objPtr); template void DoSetFunction (IntToType<1>, MEM_PTR memPtr, OBJ_PTR objPtr); + template + void DoSetFunction (IntToType<2>, MEM_PTR memPtr, OBJ_PTR objPtr); + template + void DoSetFunction (IntToType<3>, MEM_PTR memPtr, OBJ_PTR objPtr); + template + void DoSetFunction (IntToType<4>, MEM_PTR memPtr, OBJ_PTR objPtr); + template + void DoSetFunction (IntToType<5>, MEM_PTR memPtr, OBJ_PTR objPtr); int m_flags; Time m_delay; @@ -212,6 +262,31 @@ struct TimerImplOne : public TimerImpl { virtual void SetArguments (T1 a1) = 0; }; +template +struct TimerImplTwo : public TimerImpl +{ + virtual void SetArguments (T1 a1,T2 a2) = 0; +}; +template +struct TimerImplThree : public TimerImpl +{ + virtual void SetArguments (T1 a1,T2 a2,T3 a3) = 0; +}; +template +struct TimerImplFour : public TimerImpl +{ + virtual void SetArguments (T1 a1,T2 a2,T3 a3, T4 a4) = 0; +}; +template +struct TimerImplFive : public TimerImpl +{ + virtual void SetArguments (T1 a1,T2 a2,T3 a3, T4 a4, T5 a5) = 0; +}; +template +struct TimerImplSix : public TimerImpl +{ + virtual void SetArguments (T1 a1,T2 a2,T3 a3, T4 a4, T5 a5, T6 a6) = 0; +}; template @@ -264,27 +339,6 @@ Timer::DoSetFunction (IntToType<1>, FN fn) m_impl = function; } - -template -void -Timer::SetArguments (T1 a1) -{ - if (m_impl == 0) - { - NS_FATAL_ERROR ("You cannot set the arguments of a Timer before setting its function."); - return; - } - struct TimerImplOne::ParameterType> *impl = - dynamic_cast::ParameterType> *> (m_impl); - if (impl == 0) - { - NS_FATAL_ERROR ("You tried to set Timer arguments incompatible with its function."); - return; - } - impl->SetArguments (a1); -} - - template void Timer::SetFunction (MEM_PTR memPtr, OBJ_PTR objPtr) @@ -337,6 +391,150 @@ Timer::DoSetFunction (IntToType<1>, MEM_PTR memPtr, OBJ_PTR objPtr) m_impl = function; } + +template +void +Timer::SetArguments (T1 a1) +{ + if (m_impl == 0) + { + NS_FATAL_ERROR ("You cannot set the arguments of a Timer before setting its function."); + return; + } + typedef struct TimerImplOne< + typename TimerTraits::ParameterType + > TimerImplBase; + TimerImplBase *impl = dynamic_cast (m_impl); + if (impl == 0) + { + NS_FATAL_ERROR ("You tried to set Timer arguments incompatible with its function."); + return; + } + impl->SetArguments (a1); +} + +template +void +Timer::SetArguments (T1 a1, T2 a2) +{ + if (m_impl == 0) + { + NS_FATAL_ERROR ("You cannot set the arguments of a Timer before setting its function."); + return; + } + typedef struct TimerImplTwo< + typename TimerTraits::ParameterType, + typename TimerTraits::ParameterType + > TimerImplBase; + TimerImplBase *impl = dynamic_cast (m_impl); + if (impl == 0) + { + NS_FATAL_ERROR ("You tried to set Timer arguments incompatible with its function."); + return; + } + impl->SetArguments (a1, a2); +} + +template +void +Timer::SetArguments (T1 a1, T2 a2, T3 a3) +{ + if (m_impl == 0) + { + NS_FATAL_ERROR ("You cannot set the arguments of a Timer before setting its function."); + return; + } + typedef struct TimerImplThree< + typename TimerTraits::ParameterType, + typename TimerTraits::ParameterType, + typename TimerTraits::ParameterType + > TimerImplBase; + TimerImplBase *impl = dynamic_cast (m_impl); + if (impl == 0) + { + NS_FATAL_ERROR ("You tried to set Timer arguments incompatible with its function."); + return; + } + impl->SetArguments (a1, a2, a3); +} + +template +void +Timer::SetArguments (T1 a1, T2 a2, T3 a3, T4 a4) +{ + if (m_impl == 0) + { + NS_FATAL_ERROR ("You cannot set the arguments of a Timer before setting its function."); + return; + } + typedef struct TimerImplFour< + typename TimerTraits::ParameterType, + typename TimerTraits::ParameterType, + typename TimerTraits::ParameterType, + typename TimerTraits::ParameterType + > TimerImplBase; + TimerImplBase *impl = dynamic_cast (m_impl); + if (impl == 0) + { + NS_FATAL_ERROR ("You tried to set Timer arguments incompatible with its function."); + return; + } + impl->SetArguments (a1, a2, a3, a4); +} + +template +void +Timer::SetArguments (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5) +{ + if (m_impl == 0) + { + NS_FATAL_ERROR ("You cannot set the arguments of a Timer before setting its function."); + return; + } + typedef struct TimerImplFive< + typename TimerTraits::ParameterType, + typename TimerTraits::ParameterType, + typename TimerTraits::ParameterType, + typename TimerTraits::ParameterType, + typename TimerTraits::ParameterType + > TimerImplBase; + TimerImplBase *impl = dynamic_cast (m_impl); + if (impl == 0) + { + NS_FATAL_ERROR ("You tried to set Timer arguments incompatible with its function."); + return; + } + impl->SetArguments (a1, a2, a3, a4, a5); +} + +template +void +Timer::SetArguments (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6) +{ + if (m_impl == 0) + { + NS_FATAL_ERROR ("You cannot set the arguments of a Timer before setting its function."); + return; + } + typedef struct TimerImplSix< + typename TimerTraits::ParameterType, + typename TimerTraits::ParameterType, + typename TimerTraits::ParameterType, + typename TimerTraits::ParameterType, + typename TimerTraits::ParameterType, + typename TimerTraits::ParameterType + > TimerImplBase; + TimerImplBase *impl = dynamic_cast (m_impl); + if (impl == 0) + { + NS_FATAL_ERROR ("You tried to set Timer arguments incompatible with its function."); + return; + } + impl->SetArguments (a1, a2, a3, a4, a5, a6); +} + + + } // namespace ns3 #endif /* TIMER_H */ From 7a6385b6143dda22fe8a0ad952d3b61bc930c7fd Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 28 Sep 2007 13:19:43 +0200 Subject: [PATCH 116/148] add support for 6-arg Timer::SetFunction --- src/simulator/timer.h | 409 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 409 insertions(+) diff --git a/src/simulator/timer.h b/src/simulator/timer.h index d282d8bd2..05896c740 100644 --- a/src/simulator/timer.h +++ b/src/simulator/timer.h @@ -210,6 +210,8 @@ private: void DoSetFunction (IntToType<4>, FN fn); template void DoSetFunction (IntToType<5>, FN fn); + template + void DoSetFunction (IntToType<6>, FN fn); template void DoSetFunction (IntToType<0>, MEM_PTR memPtr, OBJ_PTR objPtr); @@ -223,6 +225,8 @@ private: void DoSetFunction (IntToType<4>, MEM_PTR memPtr, OBJ_PTR objPtr); template void DoSetFunction (IntToType<5>, MEM_PTR memPtr, OBJ_PTR objPtr); + template + void DoSetFunction (IntToType<6>, MEM_PTR memPtr, OBJ_PTR objPtr); int m_flags; Time m_delay; @@ -339,6 +343,206 @@ Timer::DoSetFunction (IntToType<1>, FN fn) m_impl = function; } +template +void +Timer::DoSetFunction (IntToType<2>, FN fn) +{ + typedef typename TypeTraits::FunctionPointerTraits::Arg1Type T1; + typedef typename TimerTraits::ParameterType T1Parameter; + typedef typename TimerTraits::StoredType T1Stored; + typedef typename TypeTraits::FunctionPointerTraits::Arg2Type T2; + typedef typename TimerTraits::ParameterType T2Parameter; + typedef typename TimerTraits::StoredType T2Stored; + + struct FnTimerImplTwo : public TimerImplTwo + { + FnTimerImplTwo (FN fn) + : m_fn (fn) {} + virtual void SetArguments (T1Parameter a1, T2Parameter a2) { + m_a1 = a1; + m_a2 = a2; + } + virtual EventId Schedule (const Time &delay) { + return Simulator::Schedule (delay, m_fn, m_a1, m_a2); + } + FN m_fn; + T1Stored m_a1; + T2Stored m_a2; + } *function = new FnTimerImplTwo (fn); + delete m_impl; + m_impl = function; +} + +template +void +Timer::DoSetFunction (IntToType<3>, FN fn) +{ + typedef typename TypeTraits::FunctionPointerTraits::Arg1Type T1; + typedef typename TimerTraits::ParameterType T1Parameter; + typedef typename TimerTraits::StoredType T1Stored; + typedef typename TypeTraits::FunctionPointerTraits::Arg2Type T2; + typedef typename TimerTraits::ParameterType T2Parameter; + typedef typename TimerTraits::StoredType T2Stored; + typedef typename TypeTraits::FunctionPointerTraits::Arg3Type T3; + typedef typename TimerTraits::ParameterType T3Parameter; + typedef typename TimerTraits::StoredType T3Stored; + + struct FnTimerImplThree : public TimerImplThree + { + FnTimerImplThree (FN fn) + : m_fn (fn) {} + virtual void SetArguments (T1Parameter a1, T2Parameter a2, T3Parameter a3) { + m_a1 = a1; + m_a2 = a2; + m_a3 = a3; + } + virtual EventId Schedule (const Time &delay) { + return Simulator::Schedule (delay, m_fn, m_a1, m_a2, m_a3); + } + FN m_fn; + T1Stored m_a1; + T2Stored m_a2; + T3Stored m_a3; + } *function = new FnTimerImplThree (fn); + delete m_impl; + m_impl = function; +} + +template +void +Timer::DoSetFunction (IntToType<4>, FN fn) +{ + typedef typename TypeTraits::FunctionPointerTraits::Arg1Type T1; + typedef typename TimerTraits::ParameterType T1Parameter; + typedef typename TimerTraits::StoredType T1Stored; + typedef typename TypeTraits::FunctionPointerTraits::Arg2Type T2; + typedef typename TimerTraits::ParameterType T2Parameter; + typedef typename TimerTraits::StoredType T2Stored; + typedef typename TypeTraits::FunctionPointerTraits::Arg3Type T3; + typedef typename TimerTraits::ParameterType T3Parameter; + typedef typename TimerTraits::StoredType T3Stored; + typedef typename TypeTraits::FunctionPointerTraits::Arg4Type T4; + typedef typename TimerTraits::ParameterType T4Parameter; + typedef typename TimerTraits::StoredType T4Stored; + + struct FnTimerImplFour : public TimerImplFour + { + FnTimerImplFour (FN fn) + : m_fn (fn) {} + virtual void SetArguments (T1Parameter a1, T2Parameter a2, T3Parameter a3, T4Parameter a4) { + m_a1 = a1; + m_a2 = a2; + m_a3 = a3; + m_a4 = a4; + } + virtual EventId Schedule (const Time &delay) { + return Simulator::Schedule (delay, m_fn, m_a1, m_a2, m_a3, m_a4); + } + FN m_fn; + T1Stored m_a1; + T2Stored m_a2; + T3Stored m_a3; + T4Stored m_a4; + } *function = new FnTimerImplFour (fn); + delete m_impl; + m_impl = function; +} + +template +void +Timer::DoSetFunction (IntToType<5>, FN fn) +{ + typedef typename TypeTraits::FunctionPointerTraits::Arg1Type T1; + typedef typename TimerTraits::ParameterType T1Parameter; + typedef typename TimerTraits::StoredType T1Stored; + typedef typename TypeTraits::FunctionPointerTraits::Arg2Type T2; + typedef typename TimerTraits::ParameterType T2Parameter; + typedef typename TimerTraits::StoredType T2Stored; + typedef typename TypeTraits::FunctionPointerTraits::Arg3Type T3; + typedef typename TimerTraits::ParameterType T3Parameter; + typedef typename TimerTraits::StoredType T3Stored; + typedef typename TypeTraits::FunctionPointerTraits::Arg4Type T4; + typedef typename TimerTraits::ParameterType T4Parameter; + typedef typename TimerTraits::StoredType T4Stored; + typedef typename TypeTraits::FunctionPointerTraits::Arg5Type T5; + typedef typename TimerTraits::ParameterType T5Parameter; + typedef typename TimerTraits::StoredType T5Stored; + + struct FnTimerImplFive : public TimerImplFive + { + FnTimerImplFive (FN fn) + : m_fn (fn) {} + virtual void SetArguments (T1Parameter a1, T2Parameter a2, T3Parameter a3, T4Parameter a4, T5Parameter a5) { + m_a1 = a1; + m_a2 = a2; + m_a3 = a3; + m_a4 = a4; + m_a5 = a5; + } + virtual EventId Schedule (const Time &delay) { + return Simulator::Schedule (delay, m_fn, m_a1, m_a2, m_a3, m_a4, m_a5); + } + FN m_fn; + T1Stored m_a1; + T2Stored m_a2; + T3Stored m_a3; + T4Stored m_a4; + T5Stored m_a5; + } *function = new FnTimerImplFive (fn); + delete m_impl; + m_impl = function; +} + +template +void +Timer::DoSetFunction (IntToType<6>, FN fn) +{ + typedef typename TypeTraits::FunctionPointerTraits::Arg1Type T1; + typedef typename TimerTraits::ParameterType T1Parameter; + typedef typename TimerTraits::StoredType T1Stored; + typedef typename TypeTraits::FunctionPointerTraits::Arg2Type T2; + typedef typename TimerTraits::ParameterType T2Parameter; + typedef typename TimerTraits::StoredType T2Stored; + typedef typename TypeTraits::FunctionPointerTraits::Arg3Type T3; + typedef typename TimerTraits::ParameterType T3Parameter; + typedef typename TimerTraits::StoredType T3Stored; + typedef typename TypeTraits::FunctionPointerTraits::Arg4Type T4; + typedef typename TimerTraits::ParameterType T4Parameter; + typedef typename TimerTraits::StoredType T4Stored; + typedef typename TypeTraits::FunctionPointerTraits::Arg5Type T5; + typedef typename TimerTraits::ParameterType T5Parameter; + typedef typename TimerTraits::StoredType T5Stored; + typedef typename TypeTraits::FunctionPointerTraits::Arg6Type T6; + typedef typename TimerTraits::ParameterType T6Parameter; + typedef typename TimerTraits::StoredType T6Stored; + + struct FnTimerImplSix : public TimerImplSix + { + FnTimerImplSix (FN fn) + : m_fn (fn) {} + virtual void SetArguments (T1Parameter a1, T2Parameter a2, T3Parameter a3, T4Parameter a4, T5Parameter a5, T6Parameter a6) { + m_a1 = a1; + m_a2 = a2; + m_a3 = a3; + m_a4 = a4; + m_a5 = a5; + m_a6 = a6; + } + virtual EventId Schedule (const Time &delay) { + return Simulator::Schedule (delay, m_fn, m_a1, m_a2, m_a3, m_a4, m_a5, m_a6); + } + FN m_fn; + T1Stored m_a1; + T2Stored m_a2; + T3Stored m_a3; + T4Stored m_a4; + T5Stored m_a5; + T6Stored m_a6; + } *function = new FnTimerImplSix (fn); + delete m_impl; + m_impl = function; +} + template void Timer::SetFunction (MEM_PTR memPtr, OBJ_PTR objPtr) @@ -391,6 +595,211 @@ Timer::DoSetFunction (IntToType<1>, MEM_PTR memPtr, OBJ_PTR objPtr) m_impl = function; } +template +void +Timer::DoSetFunction (IntToType<2>, MEM_PTR memPtr, OBJ_PTR objPtr) +{ + typedef typename TypeTraits::PointerToMemberTraits::Arg1Type T1; + typedef typename TimerTraits::ParameterType T1Parameter; + typedef typename TimerTraits::StoredType T1Stored; + typedef typename TypeTraits::PointerToMemberTraits::Arg2Type T2; + typedef typename TimerTraits::ParameterType T2Parameter; + typedef typename TimerTraits::StoredType T2Stored; + + struct MemFnTimerImplTwo : public TimerImplTwo + { + MemFnTimerImplTwo (MEM_PTR memPtr, OBJ_PTR objPtr) + : m_memPtr (memPtr), m_objPtr (objPtr) {} + virtual void SetArguments (T1Parameter a1, T2Parameter a2) { + m_a1 = a1; + m_a2 = a2; + } + virtual EventId Schedule (const Time &delay) { + return Simulator::Schedule (delay, m_memPtr, m_objPtr, m_a1, m_a2); + } + MEM_PTR m_memPtr; + OBJ_PTR m_objPtr; + T1Stored m_a1; + T2Stored m_a2; + } *function = new MemFnTimerImplTwo (memPtr, objPtr); + delete m_impl; + m_impl = function; +} + +template +void +Timer::DoSetFunction (IntToType<3>, MEM_PTR memPtr, OBJ_PTR objPtr) +{ + typedef typename TypeTraits::PointerToMemberTraits::Arg1Type T1; + typedef typename TimerTraits::ParameterType T1Parameter; + typedef typename TimerTraits::StoredType T1Stored; + typedef typename TypeTraits::PointerToMemberTraits::Arg2Type T2; + typedef typename TimerTraits::ParameterType T2Parameter; + typedef typename TimerTraits::StoredType T2Stored; + typedef typename TypeTraits::PointerToMemberTraits::Arg3Type T3; + typedef typename TimerTraits::ParameterType T3Parameter; + typedef typename TimerTraits::StoredType T3Stored; + + struct MemFnTimerImplThree : public TimerImplThree + { + MemFnTimerImplThree (MEM_PTR memPtr, OBJ_PTR objPtr) + : m_memPtr (memPtr), m_objPtr (objPtr) {} + virtual void SetArguments (T1Parameter a1, T2Parameter a2, T3Parameter a3) { + m_a1 = a1; + m_a2 = a2; + m_a3 = a3; + } + virtual EventId Schedule (const Time &delay) { + return Simulator::Schedule (delay, m_memPtr, m_objPtr, m_a1, m_a2, m_a3); + } + MEM_PTR m_memPtr; + OBJ_PTR m_objPtr; + T1Stored m_a1; + T2Stored m_a2; + T3Stored m_a3; + } *function = new MemFnTimerImplThree (memPtr, objPtr); + delete m_impl; + m_impl = function; +} + +template +void +Timer::DoSetFunction (IntToType<4>, MEM_PTR memPtr, OBJ_PTR objPtr) +{ + typedef typename TypeTraits::PointerToMemberTraits::Arg1Type T1; + typedef typename TimerTraits::ParameterType T1Parameter; + typedef typename TimerTraits::StoredType T1Stored; + typedef typename TypeTraits::PointerToMemberTraits::Arg2Type T2; + typedef typename TimerTraits::ParameterType T2Parameter; + typedef typename TimerTraits::StoredType T2Stored; + typedef typename TypeTraits::PointerToMemberTraits::Arg3Type T3; + typedef typename TimerTraits::ParameterType T3Parameter; + typedef typename TimerTraits::StoredType T3Stored; + typedef typename TypeTraits::PointerToMemberTraits::Arg4Type T4; + typedef typename TimerTraits::ParameterType T4Parameter; + typedef typename TimerTraits::StoredType T4Stored; + + struct MemFnTimerImplFour : public TimerImplFour + { + MemFnTimerImplFour (MEM_PTR memPtr, OBJ_PTR objPtr) + : m_memPtr (memPtr), m_objPtr (objPtr) {} + virtual void SetArguments (T1Parameter a1, T2Parameter a2, T3Parameter a3, T4Parameter a4) { + m_a1 = a1; + m_a2 = a2; + m_a3 = a3; + m_a4 = a4; + } + virtual EventId Schedule (const Time &delay) { + return Simulator::Schedule (delay, m_memPtr, m_objPtr, m_a1, m_a2, m_a3, m_a4); + } + MEM_PTR m_memPtr; + OBJ_PTR m_objPtr; + T1Stored m_a1; + T2Stored m_a2; + T3Stored m_a3; + T4Stored m_a4; + } *function = new MemFnTimerImplFour (memPtr, objPtr); + delete m_impl; + m_impl = function; +} + +template +void +Timer::DoSetFunction (IntToType<5>, MEM_PTR memPtr, OBJ_PTR objPtr) +{ + typedef typename TypeTraits::PointerToMemberTraits::Arg1Type T1; + typedef typename TimerTraits::ParameterType T1Parameter; + typedef typename TimerTraits::StoredType T1Stored; + typedef typename TypeTraits::PointerToMemberTraits::Arg2Type T2; + typedef typename TimerTraits::ParameterType T2Parameter; + typedef typename TimerTraits::StoredType T2Stored; + typedef typename TypeTraits::PointerToMemberTraits::Arg3Type T3; + typedef typename TimerTraits::ParameterType T3Parameter; + typedef typename TimerTraits::StoredType T3Stored; + typedef typename TypeTraits::PointerToMemberTraits::Arg4Type T4; + typedef typename TimerTraits::ParameterType T4Parameter; + typedef typename TimerTraits::StoredType T4Stored; + typedef typename TypeTraits::PointerToMemberTraits::Arg5Type T5; + typedef typename TimerTraits::ParameterType T5Parameter; + typedef typename TimerTraits::StoredType T5Stored; + + struct MemFnTimerImplFive : public TimerImplFive + { + MemFnTimerImplFive (MEM_PTR memPtr, OBJ_PTR objPtr) + : m_memPtr (memPtr), m_objPtr (objPtr) {} + virtual void SetArguments (T1Parameter a1, T2Parameter a2, T3Parameter a3, T4Parameter a4,T5Parameter a5) { + m_a1 = a1; + m_a2 = a2; + m_a3 = a3; + m_a4 = a4; + m_a5 = a5; + } + virtual EventId Schedule (const Time &delay) { + return Simulator::Schedule (delay, m_memPtr, m_objPtr, m_a1, m_a2, m_a3, m_a4, m_a5); + } + MEM_PTR m_memPtr; + OBJ_PTR m_objPtr; + T1Stored m_a1; + T2Stored m_a2; + T3Stored m_a3; + T4Stored m_a4; + T5Stored m_a5; + } *function = new MemFnTimerImplFive (memPtr, objPtr); + delete m_impl; + m_impl = function; +} + +template +void +Timer::DoSetFunction (IntToType<6>, MEM_PTR memPtr, OBJ_PTR objPtr) +{ + typedef typename TypeTraits::PointerToMemberTraits::Arg1Type T1; + typedef typename TimerTraits::ParameterType T1Parameter; + typedef typename TimerTraits::StoredType T1Stored; + typedef typename TypeTraits::PointerToMemberTraits::Arg2Type T2; + typedef typename TimerTraits::ParameterType T2Parameter; + typedef typename TimerTraits::StoredType T2Stored; + typedef typename TypeTraits::PointerToMemberTraits::Arg3Type T3; + typedef typename TimerTraits::ParameterType T3Parameter; + typedef typename TimerTraits::StoredType T3Stored; + typedef typename TypeTraits::PointerToMemberTraits::Arg4Type T4; + typedef typename TimerTraits::ParameterType T4Parameter; + typedef typename TimerTraits::StoredType T4Stored; + typedef typename TypeTraits::PointerToMemberTraits::Arg5Type T5; + typedef typename TimerTraits::ParameterType T5Parameter; + typedef typename TimerTraits::StoredType T5Stored; + typedef typename TypeTraits::PointerToMemberTraits::Arg6Type T6; + typedef typename TimerTraits::ParameterType T6Parameter; + typedef typename TimerTraits::StoredType T6Stored; + + struct MemFnTimerImplSix : public TimerImplSix + { + MemFnTimerImplSix (MEM_PTR memPtr, OBJ_PTR objPtr) + : m_memPtr (memPtr), m_objPtr (objPtr) {} + virtual void SetArguments (T1Parameter a1, T2Parameter a2, T3Parameter a3, T4Parameter a4,T5Parameter a5,T6Parameter a6) { + m_a1 = a1; + m_a2 = a2; + m_a3 = a3; + m_a4 = a4; + m_a5 = a5; + m_a6 = a6; + } + virtual EventId Schedule (const Time &delay) { + return Simulator::Schedule (delay, m_memPtr, m_objPtr, m_a1, m_a2, m_a3, m_a4, m_a5, m_a6); + } + MEM_PTR m_memPtr; + OBJ_PTR m_objPtr; + T1Stored m_a1; + T2Stored m_a2; + T3Stored m_a3; + T4Stored m_a4; + T5Stored m_a5; + T6Stored m_a6; + } *function = new MemFnTimerImplSix (memPtr, objPtr); + delete m_impl; + m_impl = function; +} + template void From c5797b916f3b6eec5f884da5f91364980fa2fbd6 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 28 Sep 2007 13:27:37 +0200 Subject: [PATCH 117/148] add tests --- src/simulator/timer.cc | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/simulator/timer.cc b/src/simulator/timer.cc index 2bd973ec8..74969e204 100644 --- a/src/simulator/timer.cc +++ b/src/simulator/timer.cc @@ -117,6 +117,16 @@ Timer::Schedule (void) namespace { void bari (int) {} +void bar2i (int, int) +{} +void bar3i (int, int, int) +{} +void bar4i (int, int, int, int) +{} +void bar5i (int, int, int, int, int) +{} +void bar6i (int, int, int, int, int, int) +{} void barcir (const int &) {} void barir (int &) @@ -135,6 +145,11 @@ public: TimerTests (); virtual bool RunTests (void); void bazi (int) {} + void baz2i (int, int) {} + void baz3i (int, int, int) {} + void baz4i (int, int, int, int) {} + void baz5i (int, int, int, int, int) {} + void baz6i (int, int, int, int, int, int) {} void bazcir (const int&) {} void bazir (int&) {} void bazip (int *) {} @@ -183,6 +198,29 @@ TimerTests::RunTests (void) timer.SetFunction (&TimerTests::bazcir, this); timer.SetArguments (3); + timer.SetFunction (&bar2i); + timer.SetArguments (1, 1); + timer.SetFunction (&bar3i); + timer.SetArguments (1, 1, 1); + timer.SetFunction (&bar4i); + timer.SetArguments (1, 1, 1, 1); + timer.SetFunction (&bar5i); + timer.SetArguments (1, 1, 1, 1, 1); + //timer.SetFunction (&bar6i); + //timer.SetArguments (1, 1, 1, 1, 1, 1); + + timer.SetFunction (&TimerTests::baz2i, this); + timer.SetArguments (1, 1); + timer.SetFunction (&TimerTests::baz3i, this); + timer.SetArguments (1, 1, 1); + timer.SetFunction (&TimerTests::baz4i, this); + timer.SetArguments (1, 1, 1, 1); + timer.SetFunction (&TimerTests::baz5i, this); + timer.SetArguments (1, 1, 1, 1, 1); + //timer.SetFunction (&TimerTests::baz6i, this); + //timer.SetArguments (1, 1, 1, 1, 1, 1); + + Simulator::Run (); Simulator::Destroy (); return result; From 4b00999550701cb413c42a1da76b937d32d7e843 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 28 Sep 2007 16:14:16 +0200 Subject: [PATCH 118/148] add a Timer::Schedule (delay) method --- src/simulator/timer.cc | 8 +++++++- src/simulator/timer.h | 8 +++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/simulator/timer.cc b/src/simulator/timer.cc index 74969e204..677d9335d 100644 --- a/src/simulator/timer.cc +++ b/src/simulator/timer.cc @@ -83,6 +83,12 @@ Timer::IsRunning (void) const void Timer::Schedule (void) +{ + Schedule (m_delay); +} + +void +Timer::Schedule (Time delay) { NS_ASSERT (m_impl != 0); if (m_flags & CHECK_ON_SCHEDULE) @@ -100,7 +106,7 @@ Timer::Schedule (void) { Simulator::Remove (m_event); } - m_event = m_impl->Schedule (m_delay); + m_event = m_impl->Schedule (delay); if (m_flags & GARBAGE_COLLECT) { SimulationSingleton::Get ()->Track (m_event); diff --git a/src/simulator/timer.h b/src/simulator/timer.h index 05896c740..896c901a6 100644 --- a/src/simulator/timer.h +++ b/src/simulator/timer.h @@ -190,12 +190,18 @@ public: * \return true if there is a currently-running event, false otherwise. */ bool IsRunning (void) const; - /** * Schedule a new event using the currently-configured delay, function, * and arguments. */ void Schedule (void); + /** + * \param delay the delay to use + * + * Schedule a new event using the specified delay (ignore the delay set by + * Timer::SetDelay), function, and arguments. + */ + void Schedule (Time delay); private: template From 16e4a3c10e3001c2afb4bca9ce69cc439a1b42e9 Mon Sep 17 00:00:00 2001 From: "Gustavo J. A. M. Carneiro" Date: Fri, 28 Sep 2007 16:50:15 +0100 Subject: [PATCH 119/148] Compile the random-walk-2d-mobility-model sources. --- src/mobility/wscript | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/mobility/wscript b/src/mobility/wscript index 9fee254c7..2dadd0b31 100644 --- a/src/mobility/wscript +++ b/src/mobility/wscript @@ -17,6 +17,7 @@ def build(bld): 'static-speed-helper.cc', 'static-speed-mobility-model.cc', 'random-waypoint-mobility-model.cc', + 'random-walk-2d-mobility-model.cc', ] headers = bld.create_obj('ns3header') @@ -35,4 +36,5 @@ def build(bld): 'static-speed-helper.h', 'static-speed-mobility-model.h', 'random-waypoint-mobility-model.h', + 'random-walk-2d-mobility-model.h', ] From b6d9bf7319d1959bc900d839b89ac3ec23c0c028 Mon Sep 17 00:00:00 2001 From: "Gustavo J. A. M. Carneiro" Date: Fri, 28 Sep 2007 16:51:34 +0100 Subject: [PATCH 120/148] Fix the enumeration value list end marker (0 and (void*)0 are very different things in amd64) --- src/mobility/random-walk-2d-mobility-model.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mobility/random-walk-2d-mobility-model.cc b/src/mobility/random-walk-2d-mobility-model.cc index 158a32244..b6db1b5fa 100644 --- a/src/mobility/random-walk-2d-mobility-model.cc +++ b/src/mobility/random-walk-2d-mobility-model.cc @@ -41,9 +41,9 @@ g_mode ("RandomWalk2dMode", "change the current speed and direction", RandomWalk2dMobilityModelParameters::MODE_DISTANCE, "Distance", RandomWalk2dMobilityModelParameters::MODE_TIME, "Time", - 0, 0); + 0, (void*)0); -static IntegerDefaultValue +static NumericDefaultValue g_modeDistance ("RandomWalk2dDistance", "Change current direction and speed after moving this distance.", 2.0); From c0b11f6eab8990de7b290d01021c53086674b7d6 Mon Sep 17 00:00:00 2001 From: "Gustavo J. A. M. Carneiro" Date: Fri, 28 Sep 2007 16:54:04 +0100 Subject: [PATCH 121/148] Update random mobility sample to new API --- samples/main-random-walk.cc | 23 +++++++++++------------ samples/wscript | 3 +++ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/samples/main-random-walk.cc b/samples/main-random-walk.cc index 1aed4ced9..e1c308359 100644 --- a/samples/main-random-walk.cc +++ b/samples/main-random-walk.cc @@ -5,7 +5,6 @@ #include "ns3/ptr.h" #include "ns3/mobility-model.h" #include "ns3/mobility-model-notifier.h" -#include "ns3/random-walk-2d-mobility-model.h" #include "ns3/random-topology.h" #include "ns3/default-value.h" #include "ns3/command-line.h" @@ -15,7 +14,7 @@ using namespace ns3; static void -CourseChange (Ptr position) +CourseChange (ns3::TraceContext const&, Ptr position) { Position pos = position->Get (); std::cout << Simulator::Now () << ", pos=" << position << ", x=" << pos.x << ", y=" << pos.y @@ -24,17 +23,17 @@ CourseChange (Ptr position) int main (int argc, char *argv[]) { - Bind ("RandomWalk2dMode", "Time"); - Bind ("RandomWalk2dTime", "2s"); - Bind ("RandomWalk2dSpeed", "Constant:1.0"); - Bind ("RandomWalk2dBounds", "0:200:0:100"); + DefaultValue::Bind ("RandomWalk2dMode", "Time"); + DefaultValue::Bind ("RandomWalk2dTime", "2s"); + DefaultValue::Bind ("RandomWalk2dSpeed", "Constant:1.0"); + DefaultValue::Bind ("RandomWalk2dBounds", "0:200:0:100"); - Bind ("RandomDiscPositionX", "100"); - Bind ("RandomDiscPositionY", "50"); - Bind ("RandomDiscPositionRho", "Uniform:0:30"); + DefaultValue::Bind ("RandomDiscPositionX", "100"); + DefaultValue::Bind ("RandomDiscPositionY", "50"); + DefaultValue::Bind ("RandomDiscPositionRho", "Uniform:0:30"); - Bind ("RandomTopologyPositionType", "RandomDiscPosition"); - Bind ("RandomTopologyMobilityType", "RandomWalk2dMobilityModel"); + DefaultValue::Bind ("RandomTopologyPositionType", "RandomDiscPosition"); + DefaultValue::Bind ("RandomTopologyMobilityType", "RandomWalk2dMobilityModel"); CommandLine::Parse (argc, argv); @@ -44,7 +43,7 @@ int main (int argc, char *argv[]) for (uint32_t i = 0; i < 100; i++) { Ptr notifier = Create (); - notifier->RegisterListener (MakeCallback (&CourseChange)); + notifier->TraceConnect ("/course-change", MakeCallback (&CourseChange)); objects.push_back (notifier); } diff --git a/samples/wscript b/samples/wscript index e89fbe9b8..73f694ed1 100644 --- a/samples/wscript +++ b/samples/wscript @@ -41,3 +41,6 @@ def build(bld): ['core', 'simulator', 'mobility']) obj.source = 'main-random-topology.cc' + obj = bld.create_ns3_program('main-random-walk', + ['core', 'simulator', 'mobility']) + obj.source = 'main-random-walk.cc' From 3898752f67df65fabfb672e70d01ce5e3028d818 Mon Sep 17 00:00:00 2001 From: "Gustavo J. A. M. Carneiro" Date: Fri, 28 Sep 2007 17:13:20 +0100 Subject: [PATCH 122/148] Fix infinite loop in CommandLine::Parse when trying to ignore a badly formed parameter. --- src/core/command-line.cc | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/core/command-line.cc b/src/core/command-line.cc index 481c925b4..d66ce53da 100644 --- a/src/core/command-line.cc +++ b/src/core/command-line.cc @@ -85,9 +85,7 @@ CommandLine::PrintHelp (void) void CommandLine::Parse (int argc, char *argv[]) { - argc--; - argv++; - while (argc > 0) + for (argc--, argv++; argc > 0; argc--, argv++) { // remove "--" or "-" heading. std::string param = *argv; @@ -142,8 +140,6 @@ CommandLine::Parse (int argc, char *argv[]) continue; } } - argc--; - argv++; } } From b7ddc302441063ef90d18bbdba74f9c31cd967be Mon Sep 17 00:00:00 2001 From: "Gustavo J. A. M. Carneiro" Date: Fri, 28 Sep 2007 17:33:18 +0100 Subject: [PATCH 123/148] Fix bad RandomWaypointPosition default value --- src/mobility/random-waypoint-mobility-model.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mobility/random-waypoint-mobility-model.cc b/src/mobility/random-waypoint-mobility-model.cc index 255af7aa6..678273178 100644 --- a/src/mobility/random-waypoint-mobility-model.cc +++ b/src/mobility/random-waypoint-mobility-model.cc @@ -21,7 +21,7 @@ static ClassIdDefaultValue g_position ("RandomWaypointPosition", "A random position model used to pick the next waypoint position.", RandomPosition::iid, - "RandomPositionRectangle"); + "RandomRectanglePosition"); const ClassId RandomWaypointMobilityModel::cid = MakeClassId ("RandomWaypointMobilityModel", MobilityModel::iid); From 35ed9727d3528b84eb551cbf0784c92e0e9abac0 Mon Sep 17 00:00:00 2001 From: "Gustavo J. A. M. Carneiro" Date: Fri, 28 Sep 2007 18:17:25 +0100 Subject: [PATCH 124/148] Add NotifyCourseChange support to RandomWaypointMobilityModel, and fix a bug in GetSpeed. --- .../random-waypoint-mobility-model.cc | 24 +++++++++++++------ src/mobility/random-waypoint-mobility-model.h | 2 ++ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/mobility/random-waypoint-mobility-model.cc b/src/mobility/random-waypoint-mobility-model.cc index 678273178..4db4f2e11 100644 --- a/src/mobility/random-waypoint-mobility-model.cc +++ b/src/mobility/random-waypoint-mobility-model.cc @@ -1,3 +1,4 @@ +#include #include "ns3/simulator.h" #include "ns3/random-variable.h" #include "ns3/random-variable-default-value.h" @@ -91,23 +92,32 @@ RandomWaypointMobilityModel::RandomWaypointMobilityModel (Ptrm_pause->GetValue ()); 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; + double dx = (destination.x - m_current.x); + double dy = (destination.y - m_current.y); + double dz = (destination.z - m_current.z); + double k = speed / std::sqrt (dx*dx + dy*dy + dz*dz); - m_helper.Reset (Speed (dx,dy,dz), pause); + m_helper.Reset (Speed (k*dx, k*dy, k*dz), m_pause); Time travelDelay = Seconds (CalculateDistance (destination, m_current) / speed); - m_event = Simulator::Schedule (travelDelay + pause, - &RandomWaypointMobilityModel::Start, this); + m_event = Simulator::Schedule (travelDelay, + &RandomWaypointMobilityModel::BeginPause, this); + NotifyCourseChange (); } Position diff --git a/src/mobility/random-waypoint-mobility-model.h b/src/mobility/random-waypoint-mobility-model.h index aef1c3d86..147302383 100644 --- a/src/mobility/random-waypoint-mobility-model.h +++ b/src/mobility/random-waypoint-mobility-model.h @@ -97,6 +97,7 @@ public: RandomWaypointMobilityModel (Ptr parameters); private: void Start (void); + void BeginPause (void); virtual Position DoGet (void) const; virtual void DoSet (const Position &position); virtual Speed DoGetSpeed (void) const; @@ -104,6 +105,7 @@ private: StaticSpeedHelper m_helper; Ptr m_parameters; EventId m_event; + Time m_pause; }; } // namespace ns3 From ea76a9f88ee7634105318b26ccb33145b2267dc3 Mon Sep 17 00:00:00 2001 From: "Gustavo J. A. M. Carneiro" Date: Fri, 28 Sep 2007 19:04:11 +0100 Subject: [PATCH 125/148] Create real nodes, not just mobility models. --- samples/main-random-walk.cc | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/samples/main-random-walk.cc b/samples/main-random-walk.cc index e1c308359..b1b542c17 100644 --- a/samples/main-random-walk.cc +++ b/samples/main-random-walk.cc @@ -10,6 +10,8 @@ #include "ns3/command-line.h" #include "ns3/simulator.h" #include "ns3/nstime.h" +#include "ns3/node.h" +#include "ns3/node-list.h" using namespace ns3; @@ -39,15 +41,15 @@ int main (int argc, char *argv[]) RandomTopology topology; - std::vector > objects; for (uint32_t i = 0; i < 100; i++) { - Ptr notifier = Create (); - notifier->TraceConnect ("/course-change", MakeCallback (&CourseChange)); - objects.push_back (notifier); + Ptr node = Create (); + node->AddInterface (Create ()); } - topology.Layout (objects.begin (), objects.end ()); + topology.Layout (NodeList::Begin (), NodeList::End ()); + NodeList::Connect ("/nodes/*/$MobilityModelNotifier/course-change", + MakeCallback (&CourseChange)); Simulator::StopAt (Seconds (100.0)); From 6951f13ce4fee55cd9241681d0a7caee7c91fe2c Mon Sep 17 00:00:00 2001 From: "Gustavo J. A. M. Carneiro" Date: Fri, 28 Sep 2007 19:13:19 +0100 Subject: [PATCH 126/148] On course-change, print velocity in addition to position. --- samples/main-random-walk.cc | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/samples/main-random-walk.cc b/samples/main-random-walk.cc index b1b542c17..cccd64936 100644 --- a/samples/main-random-walk.cc +++ b/samples/main-random-walk.cc @@ -16,11 +16,13 @@ using namespace ns3; static void -CourseChange (ns3::TraceContext const&, Ptr position) +CourseChange (ns3::TraceContext const&, Ptr mobility) { - Position pos = position->Get (); - std::cout << Simulator::Now () << ", pos=" << position << ", x=" << pos.x << ", y=" << pos.y - << ", z=" << pos.z << std::endl; + Position pos = mobility->Get (); + Speed vel = mobility->GetSpeed (); + std::cout << Simulator::Now () << ", model=" << mobility << ", POS: x=" << pos.x << ", y=" << pos.y + << ", z=" << pos.z << "; VEL:" << vel.dx << ", y=" << vel.dy + << ", z=" << vel.dz << std::endl; } int main (int argc, char *argv[]) From 5099bebba5f31f585fa47b5f44c334a37fb702cd Mon Sep 17 00:00:00 2001 From: "Gustavo J. A. M. Carneiro" Date: Mon, 1 Oct 2007 11:42:41 +0100 Subject: [PATCH 127/148] Put back an assertion in Object::DoQueryInterface, erroneously removed during branch merging. --- src/core/object.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/src/core/object.cc b/src/core/object.cc index 4a9e66218..1c6d58772 100644 --- a/src/core/object.cc +++ b/src/core/object.cc @@ -192,6 +192,7 @@ Object::~Object () Ptr Object::DoQueryInterface (InterfaceId iid) const { + NS_ASSERT (Check ()); const Object *currentObject = this; do { NS_ASSERT (currentObject != 0); From 27ba7608db6b97a1ec4a241043098b938bc71a15 Mon Sep 17 00:00:00 2001 From: "Gustavo J. A. M. Carneiro" Date: Mon, 1 Oct 2007 12:34:32 +0100 Subject: [PATCH 128/148] Use this-> to access member variables of Rectangle, for clarity. --- src/mobility/rectangle.cc | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/mobility/rectangle.cc b/src/mobility/rectangle.cc index 5ae589b8c..438f9ba91 100644 --- a/src/mobility/rectangle.cc +++ b/src/mobility/rectangle.cc @@ -45,17 +45,17 @@ bool Rectangle::IsInside (const Position &position) const { return - position.x <= xMax && position.x >= xMin && - position.y <= yMax && position.y >= yMin; + position.x <= this->xMax && position.x >= this->xMin && + position.y <= this->yMax && position.y >= this->yMin; } Rectangle::Side Rectangle::GetClosestSide (const Position &position) const { - double xMinDist = std::abs (position.x - xMin); - double xMaxDist = std::abs (xMax - position.x); - double yMinDist = std::abs (position.y - yMin); - double yMaxDist = std::abs (yMax - position.y); + double xMinDist = std::abs (position.x - this->xMin); + double xMaxDist = std::abs (this->xMax - position.x); + double yMinDist = std::abs (position.y - this->yMin); + double yMaxDist = std::abs (this->yMax - position.y); double minX = std::min (xMinDist, xMaxDist); double minY = std::min (yMinDist, yMaxDist); if (minX < minY) @@ -85,29 +85,29 @@ Rectangle::GetClosestSide (const Position &position) const Position Rectangle::CalculateIntersection (const Position ¤t, const Speed &speed) const { - double xMaxY = current.y + (xMax - current.x) / speed.dx * speed.dy; - double xMinY = current.y + (xMin - current.x) / speed.dx * speed.dy; - double yMaxX = current.x + (yMax - current.y) / speed.dy * speed.dx; - double yMinX = current.x + (yMin - current.y) / speed.dy * speed.dx; - bool xMaxOk = xMaxY <= yMax && xMaxY >= yMin; - bool xMinOk = xMinY <= yMax && xMinY >= yMin; - bool yMaxOk = yMaxX <= xMax && yMaxX >= xMin; - bool yMinOk = yMinX <= xMax && yMinX >= xMin; + double xMaxY = current.y + (this->xMax - current.x) / speed.dx * speed.dy; + double xMinY = current.y + (this->xMin - current.x) / speed.dx * speed.dy; + double yMaxX = current.x + (this->yMax - current.y) / speed.dy * speed.dx; + double yMinX = current.x + (this->yMin - current.y) / speed.dy * speed.dx; + bool xMaxOk = xMaxY <= this->yMax && xMaxY >= this->yMin; + bool xMinOk = xMinY <= this->yMax && xMinY >= this->yMin; + bool yMaxOk = yMaxX <= this->xMax && yMaxX >= this->xMin; + bool yMinOk = yMinX <= this->xMax && yMinX >= this->xMin; if (xMaxOk && speed.dx >= 0) { - return Position (xMax, xMaxY, 0.0); + return Position (this->xMax, xMaxY, 0.0); } else if (xMinOk && speed.dx <= 0) { - return Position (xMin, xMinY, 0.0); + return Position (this->xMin, xMinY, 0.0); } else if (yMaxOk && speed.dy >= 0) { - return Position (yMaxX, yMax, 0.0); + return Position (yMaxX, this->yMax, 0.0); } else if (yMinOk && speed.dy <= 0) { - return Position (yMinX, yMin, 0.0); + return Position (yMinX, this->yMin, 0.0); } else { From 8f30f628a09d443d6a8de16540ad0260fa1819a3 Mon Sep 17 00:00:00 2001 From: "Gustavo J. A. M. Carneiro" Date: Mon, 1 Oct 2007 12:45:27 +0100 Subject: [PATCH 129/148] Minor code readability improvements. --- src/mobility/rectangle.cc | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/mobility/rectangle.cc b/src/mobility/rectangle.cc index 438f9ba91..2f10a7d43 100644 --- a/src/mobility/rectangle.cc +++ b/src/mobility/rectangle.cc @@ -89,23 +89,23 @@ Rectangle::CalculateIntersection (const Position ¤t, const Speed &speed) c double xMinY = current.y + (this->xMin - current.x) / speed.dx * speed.dy; double yMaxX = current.x + (this->yMax - current.y) / speed.dy * speed.dx; double yMinX = current.x + (this->yMin - current.y) / speed.dy * speed.dx; - bool xMaxOk = xMaxY <= this->yMax && xMaxY >= this->yMin; - bool xMinOk = xMinY <= this->yMax && xMinY >= this->yMin; - bool yMaxOk = yMaxX <= this->xMax && yMaxX >= this->xMin; - bool yMinOk = yMinX <= this->xMax && yMinX >= this->xMin; - if (xMaxOk && speed.dx >= 0) + bool xMaxYOk = (xMaxY <= this->yMax && xMaxY >= this->yMin); + bool xMinYOk = (xMinY <= this->yMax && xMinY >= this->yMin); + bool yMaxXOk = (yMaxX <= this->xMax && yMaxX >= this->xMin); + bool yMinXOk = (yMinX <= this->xMax && yMinX >= this->xMin); + if (xMaxYOk && speed.dx >= 0) { return Position (this->xMax, xMaxY, 0.0); } - else if (xMinOk && speed.dx <= 0) + else if (xMinYOk && speed.dx <= 0) { return Position (this->xMin, xMinY, 0.0); } - else if (yMaxOk && speed.dy >= 0) + else if (yMaxXOk && speed.dy >= 0) { return Position (yMaxX, this->yMax, 0.0); } - else if (yMinOk && speed.dy <= 0) + else if (yMinXOk && speed.dy <= 0) { return Position (yMinX, this->yMin, 0.0); } From b504a88459fc543b37ac3733310c02a113d96fcd Mon Sep 17 00:00:00 2001 From: "Gustavo J. A. M. Carneiro" Date: Mon, 1 Oct 2007 15:05:03 +0100 Subject: [PATCH 130/148] Add a bit of function name logging. --- src/mobility/random-direction-2d-mobility-model.cc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/mobility/random-direction-2d-mobility-model.cc b/src/mobility/random-direction-2d-mobility-model.cc index 63c4a7d51..6c32e57aa 100644 --- a/src/mobility/random-direction-2d-mobility-model.cc +++ b/src/mobility/random-direction-2d-mobility-model.cc @@ -24,6 +24,9 @@ #include #include #include "random-direction-2d-mobility-model.h" +#include "ns3/log.h" + +NS_LOG_COMPONENT_DEFINE ("RandomDirection2dMobilityModel"); namespace ns3 { @@ -138,6 +141,7 @@ RandomDirection2dMobilityModel::Start (void) void RandomDirection2dMobilityModel::SetDirectionAndSpeed (double direction) { + NS_LOG_FUNCTION; double speed = m_parameters->m_speedVariable->GetValue (); const Speed vector (std::cos (direction) * speed, std::sin (direction) * speed, From d8e5697bfea13366a1cb2f032cfa80e53691b0da Mon Sep 17 00:00:00 2001 From: "Gustavo J. A. M. Carneiro" Date: Mon, 1 Oct 2007 15:05:43 +0100 Subject: [PATCH 131/148] Add the random-direction-2d-mobility-model.cc,h sources to the build. --- src/mobility/wscript | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/mobility/wscript b/src/mobility/wscript index 2dadd0b31..748f7c55d 100644 --- a/src/mobility/wscript +++ b/src/mobility/wscript @@ -18,6 +18,7 @@ def build(bld): 'static-speed-mobility-model.cc', 'random-waypoint-mobility-model.cc', 'random-walk-2d-mobility-model.cc', + 'random-direction-2d-mobility-model.cc', ] headers = bld.create_obj('ns3header') @@ -37,4 +38,5 @@ def build(bld): 'static-speed-mobility-model.h', 'random-waypoint-mobility-model.h', 'random-walk-2d-mobility-model.h', + 'random-direction-2d-mobility-model.h', ] From 99da16b8342cc89c21351559e75cdcf88e4fd64a Mon Sep 17 00:00:00 2001 From: "Gustavo J. A. M. Carneiro" Date: Mon, 1 Oct 2007 15:11:16 +0100 Subject: [PATCH 132/148] Improve the precision of the RandomDirection2dMobilityModel::PI constant (value copied from glibc's math.h header file). --- src/mobility/random-direction-2d-mobility-model.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mobility/random-direction-2d-mobility-model.cc b/src/mobility/random-direction-2d-mobility-model.cc index 6c32e57aa..0dde51ee8 100644 --- a/src/mobility/random-direction-2d-mobility-model.cc +++ b/src/mobility/random-direction-2d-mobility-model.cc @@ -30,7 +30,7 @@ NS_LOG_COMPONENT_DEFINE ("RandomDirection2dMobilityModel"); namespace ns3 { -const double RandomDirection2dMobilityModel::PI = 3.1415; +const double RandomDirection2dMobilityModel::PI = 3.14159265358979323846; const ClassId RandomDirection2dMobilityModel::cid = MakeClassId ("RandomDirection2dMobilityModel", MobilityModel::iid); From 9c219d647d9107e11932104ab08eb0f707ea8267 Mon Sep 17 00:00:00 2001 From: "Gustavo J. A. M. Carneiro" Date: Mon, 1 Oct 2007 17:10:15 +0100 Subject: [PATCH 133/148] Refactor handling of pauses in StaticSpeedHelper to fix bug (must return null speed when paused) --- src/mobility/static-speed-helper.cc | 48 ++++++++++++++++------------- src/mobility/static-speed-helper.h | 5 +-- 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/src/mobility/static-speed-helper.cc b/src/mobility/static-speed-helper.cc index 82a3dd029..b2d806ab8 100644 --- a/src/mobility/static-speed-helper.cc +++ b/src/mobility/static-speed-helper.cc @@ -31,7 +31,8 @@ StaticSpeedHelper::StaticSpeedHelper (const Position &position) StaticSpeedHelper::StaticSpeedHelper (const Position &position, const Speed &speed) : m_position (position), - m_speed (speed) + m_speed (speed), + m_paused (true) {} void StaticSpeedHelper::InitializePosition (const Position &position) @@ -41,14 +42,7 @@ StaticSpeedHelper::InitializePosition (const Position &position) m_speed.dy = 0.0; m_speed.dz = 0.0; m_lastUpdate = Simulator::Now (); - m_pauseEnd = Simulator::Now (); -} - -void -StaticSpeedHelper::Reset (const Speed &speed, const Time &pauseDelay) -{ - Reset (speed); - m_pauseEnd = Simulator::Now () + pauseDelay; + m_paused = true; } Position @@ -61,7 +55,7 @@ StaticSpeedHelper::GetCurrentPosition (void) const Speed StaticSpeedHelper::GetSpeed (void) const { - return m_speed; + return m_paused? Speed (0, 0, 0) : m_speed; } void StaticSpeedHelper::SetSpeed (const Speed &speed) @@ -73,17 +67,13 @@ StaticSpeedHelper::SetSpeed (const Speed &speed) void StaticSpeedHelper::Update (void) const { + if (m_paused) + { + return; + } Time now = Simulator::Now (); - if (m_pauseEnd > now) - { - return; - } - Time last = std::max (now, m_pauseEnd); - if (m_lastUpdate >= last) - { - return; - } - Time deltaTime = now - last; + NS_ASSERT (m_lastUpdate <= now); + Time deltaTime = now - m_lastUpdate; m_lastUpdate = now; double deltaS = deltaTime.GetSeconds (); m_position.x += m_speed.dx * deltaS; @@ -96,7 +86,7 @@ StaticSpeedHelper::Reset (const Speed &speed) { Update (); m_speed = speed; - m_pauseEnd = Simulator::Now (); + Unpause (); } void StaticSpeedHelper::UpdateFull (const Rectangle &bounds) const @@ -115,5 +105,21 @@ StaticSpeedHelper::GetCurrentPosition (const Rectangle &bounds) const return m_position; } +void +StaticSpeedHelper::Pause (void) +{ + Update (); + m_paused = true; +} + +void +StaticSpeedHelper::Unpause (void) +{ + if (m_paused) + { + m_lastUpdate = Simulator::Now (); + m_paused = false; + } +} } // namespace ns3 diff --git a/src/mobility/static-speed-helper.h b/src/mobility/static-speed-helper.h index 24e1c6237..343898f22 100644 --- a/src/mobility/static-speed-helper.h +++ b/src/mobility/static-speed-helper.h @@ -37,12 +37,13 @@ class StaticSpeedHelper const Speed &speed); void InitializePosition (const Position &position); - void Reset (const Speed &speed, const Time &pauseDelay); void Reset (const Speed &speed); Position GetCurrentPosition (const Rectangle &bounds) const; Position GetCurrentPosition (void) const; Speed GetSpeed (void) const; void SetSpeed (const Speed &speed); + void Pause (void); + void Unpause (void); private: void Update (void) const; @@ -50,7 +51,7 @@ class StaticSpeedHelper mutable Time m_lastUpdate; mutable Position m_position; Speed m_speed; - Time m_pauseEnd; + bool m_paused; }; } // namespace ns3 From 636e47d2192eacbd6fa296eab01d09ca013aad52 Mon Sep 17 00:00:00 2001 From: "Gustavo J. A. M. Carneiro" Date: Mon, 1 Oct 2007 17:10:46 +0100 Subject: [PATCH 134/148] Adapt to StaticSpeedHelper change --- .../random-waypoint-mobility-model.cc | 27 ++++++++++++++++--- src/mobility/random-waypoint-mobility-model.h | 1 - 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/mobility/random-waypoint-mobility-model.cc b/src/mobility/random-waypoint-mobility-model.cc index 4db4f2e11..5d34abc16 100644 --- a/src/mobility/random-waypoint-mobility-model.cc +++ b/src/mobility/random-waypoint-mobility-model.cc @@ -1,3 +1,22 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 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 #include "ns3/simulator.h" #include "ns3/random-variable.h" @@ -98,13 +117,15 @@ RandomWaypointMobilityModel::RandomWaypointMobilityModel (Ptrm_pause->GetValue ()); + m_helper.Pause (); + NotifyCourseChange (); + m_event = Simulator::Schedule (pause, &RandomWaypointMobilityModel::Start, this); } void RandomWaypointMobilityModel::Start (void) { - m_pause = Seconds (m_parameters->m_pause->GetValue ()); Position m_current = m_helper.GetCurrentPosition (); Position destination = m_parameters->m_position->Get (); double speed = m_parameters->m_speed->GetValue (); @@ -113,7 +134,7 @@ RandomWaypointMobilityModel::Start (void) double dz = (destination.z - m_current.z); double k = speed / std::sqrt (dx*dx + dy*dy + dz*dz); - m_helper.Reset (Speed (k*dx, k*dy, k*dz), m_pause); + m_helper.Reset (Speed (k*dx, k*dy, k*dz)); Time travelDelay = Seconds (CalculateDistance (destination, m_current) / speed); m_event = Simulator::Schedule (travelDelay, &RandomWaypointMobilityModel::BeginPause, this); diff --git a/src/mobility/random-waypoint-mobility-model.h b/src/mobility/random-waypoint-mobility-model.h index 147302383..1b083835b 100644 --- a/src/mobility/random-waypoint-mobility-model.h +++ b/src/mobility/random-waypoint-mobility-model.h @@ -105,7 +105,6 @@ private: StaticSpeedHelper m_helper; Ptr m_parameters; EventId m_event; - Time m_pause; }; } // namespace ns3 From afc46ac1d3276bf142df50a7e3b4bd4ea694a9b0 Mon Sep 17 00:00:00 2001 From: "Gustavo J. A. M. Carneiro" Date: Mon, 1 Oct 2007 17:11:06 +0100 Subject: [PATCH 135/148] Adapt to StaticSpeedHelper change --- .../random-direction-2d-mobility-model.cc | 17 +++++++++++++---- .../random-direction-2d-mobility-model.h | 1 + 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/mobility/random-direction-2d-mobility-model.cc b/src/mobility/random-direction-2d-mobility-model.cc index 0dde51ee8..9df80874c 100644 --- a/src/mobility/random-direction-2d-mobility-model.cc +++ b/src/mobility/random-direction-2d-mobility-model.cc @@ -138,6 +138,16 @@ RandomDirection2dMobilityModel::Start (void) double direction = UniformVariable::GetSingleValue (0, 2 * PI); SetDirectionAndSpeed (direction); } + +void +RandomDirection2dMobilityModel::BeginPause (void) +{ + Time pause = Seconds (m_parameters->m_pauseVariable->GetValue ()); + m_helper.Pause (); + m_event = Simulator::Schedule (pause, &RandomDirection2dMobilityModel::ResetDirectionAndSpeed, this); + NotifyCourseChange (); +} + void RandomDirection2dMobilityModel::SetDirectionAndSpeed (double direction) { @@ -146,13 +156,12 @@ RandomDirection2dMobilityModel::SetDirectionAndSpeed (double direction) const Speed vector (std::cos (direction) * speed, std::sin (direction) * speed, 0.0); - Time pause = Seconds (m_parameters->m_pauseVariable->GetValue ()); Position position = m_helper.GetCurrentPosition (m_parameters->m_bounds); - m_helper.Reset (vector, pause); + m_helper.Reset (vector); Position next = m_parameters->m_bounds.CalculateIntersection (position, vector); Time delay = Seconds (CalculateDistance (position, next) / speed); - m_event = Simulator::Schedule (delay + pause, - &RandomDirection2dMobilityModel::ResetDirectionAndSpeed, this); + m_event = Simulator::Schedule (delay, + &RandomDirection2dMobilityModel::BeginPause, this); NotifyCourseChange (); } void diff --git a/src/mobility/random-direction-2d-mobility-model.h b/src/mobility/random-direction-2d-mobility-model.h index dda1b1173..6dce660a1 100644 --- a/src/mobility/random-direction-2d-mobility-model.h +++ b/src/mobility/random-direction-2d-mobility-model.h @@ -102,6 +102,7 @@ class RandomDirection2dMobilityModel : public MobilityModel private: void Start (void); void ResetDirectionAndSpeed (void); + void BeginPause (void); void SetDirectionAndSpeed (double direction); void InitializeDirectionAndSpeed (void); virtual void DoDispose (void); From 1bc6f6eeda74357da81a6430fdc4dc96d6d075fd Mon Sep 17 00:00:00 2001 From: "Gustavo J. A. M. Carneiro" Date: Mon, 1 Oct 2007 18:01:38 +0100 Subject: [PATCH 136/148] Add ns2-mobility-file-topology sources --- src/mobility/wscript | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/mobility/wscript b/src/mobility/wscript index 748f7c55d..f630a4b71 100644 --- a/src/mobility/wscript +++ b/src/mobility/wscript @@ -19,6 +19,7 @@ def build(bld): 'random-waypoint-mobility-model.cc', 'random-walk-2d-mobility-model.cc', 'random-direction-2d-mobility-model.cc', + 'ns2-mobility-file-topology.cc', ] headers = bld.create_obj('ns3header') @@ -39,4 +40,5 @@ def build(bld): 'random-waypoint-mobility-model.h', 'random-walk-2d-mobility-model.h', 'random-direction-2d-mobility-model.h', + 'ns2-mobility-file-topology.h', ] From e7fb2a2c7c0b5779a27cbf9561210d758b0b306f Mon Sep 17 00:00:00 2001 From: "Gustavo J. A. M. Carneiro" Date: Mon, 1 Oct 2007 18:55:04 +0100 Subject: [PATCH 137/148] According to the literature, in RandomWaypointMobilityModel the pause should come before the walk, not after; fixed. --- .../random-waypoint-mobility-model.cc | 22 +++++++++---------- src/mobility/random-waypoint-mobility-model.h | 2 +- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/mobility/random-waypoint-mobility-model.cc b/src/mobility/random-waypoint-mobility-model.cc index 5d34abc16..0078a35c2 100644 --- a/src/mobility/random-waypoint-mobility-model.cc +++ b/src/mobility/random-waypoint-mobility-model.cc @@ -115,16 +115,7 @@ RandomWaypointMobilityModel::RandomWaypointMobilityModel (Ptrm_pause->GetValue ()); - m_helper.Pause (); - NotifyCourseChange (); - m_event = Simulator::Schedule (pause, &RandomWaypointMobilityModel::Start, this); -} - -void -RandomWaypointMobilityModel::Start (void) +RandomWaypointMobilityModel::BeginWalk (void) { Position m_current = m_helper.GetCurrentPosition (); Position destination = m_parameters->m_position->Get (); @@ -137,10 +128,19 @@ RandomWaypointMobilityModel::Start (void) m_helper.Reset (Speed (k*dx, k*dy, k*dz)); Time travelDelay = Seconds (CalculateDistance (destination, m_current) / speed); m_event = Simulator::Schedule (travelDelay, - &RandomWaypointMobilityModel::BeginPause, this); + &RandomWaypointMobilityModel::Start, this); NotifyCourseChange (); } +void +RandomWaypointMobilityModel::Start (void) +{ + Time pause = Seconds (m_parameters->m_pause->GetValue ()); + m_helper.Pause (); + NotifyCourseChange (); + m_event = Simulator::Schedule (pause, &RandomWaypointMobilityModel::BeginWalk, this); +} + Position RandomWaypointMobilityModel::DoGet (void) const { diff --git a/src/mobility/random-waypoint-mobility-model.h b/src/mobility/random-waypoint-mobility-model.h index 1b083835b..72aef43b3 100644 --- a/src/mobility/random-waypoint-mobility-model.h +++ b/src/mobility/random-waypoint-mobility-model.h @@ -97,7 +97,7 @@ public: RandomWaypointMobilityModel (Ptr parameters); private: void Start (void); - void BeginPause (void); + void BeginWalk (void); virtual Position DoGet (void) const; virtual void DoSet (const Position &position); virtual Speed DoGetSpeed (void) const; From e216675ca13576370872ce7ddb9135a3889a622d Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Tue, 2 Oct 2007 11:32:16 +0200 Subject: [PATCH 138/148] remove uneeded forward declaration --- src/simulator/timer.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/simulator/timer.h b/src/simulator/timer.h index 896c901a6..301e4e6ef 100644 --- a/src/simulator/timer.h +++ b/src/simulator/timer.h @@ -250,8 +250,6 @@ private: namespace ns3 { -template -struct TimerTraits; template struct TimerTraits { From 1440302ab399f6f4d3b00127ec9e4df91c9f2a74 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 3 Oct 2007 13:01:12 +0200 Subject: [PATCH 139/148] add Simulator::GetDelayLeft --- src/simulator/simulator.cc | 24 +++++++++++++++++++++--- src/simulator/simulator.h | 7 +++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/simulator/simulator.cc b/src/simulator/simulator.cc index e00f548b5..5e6d3bad4 100644 --- a/src/simulator/simulator.cc +++ b/src/simulator/simulator.cc @@ -67,9 +67,10 @@ public: EventId ScheduleDestroy (const Ptr &event); void Remove (const EventId &ev); void Cancel (const EventId &ev); - bool IsExpired (const EventId &ev); + bool IsExpired (const EventId &ev) const; void Run (void); Time Now (void) const; + Time GetDelayLeft (const EventId &id) const; private: void ProcessOneEvent (void); @@ -251,6 +252,18 @@ SimulatorPrivate::Now (void) const { return TimeStep (m_currentTs); } +Time +SimulatorPrivate::GetDelayLeft (const EventId &id) const +{ + if (IsExpired (id)) + { + return TimeStep (0); + } + else + { + return TimeStep (id.GetTs () - m_currentTs); + } +} void SimulatorPrivate::Remove (const EventId &ev) @@ -293,12 +306,12 @@ SimulatorPrivate::Cancel (const EventId &id) } bool -SimulatorPrivate::IsExpired (const EventId &ev) +SimulatorPrivate::IsExpired (const EventId &ev) const { if (ev.GetUid () == 2) { // destroy events. - for (DestroyEvents::iterator i = m_destroyEvents.begin (); i != m_destroyEvents.end (); i++) + for (DestroyEvents::const_iterator i = m_destroyEvents.begin (); i != m_destroyEvents.end (); i++) { if (*i == ev) { @@ -411,6 +424,11 @@ Simulator::Now (void) { return GetPriv ()->Now (); } +Time +Simulator::GetDelayLeft (const EventId &id) +{ + return GetPriv ()->GetDelayLeft (id); +} Ptr Simulator::MakeEvent (void (*f) (void)) diff --git a/src/simulator/simulator.h b/src/simulator/simulator.h index cdf2f8b4e..0d48b2990 100644 --- a/src/simulator/simulator.h +++ b/src/simulator/simulator.h @@ -552,6 +552,13 @@ public: * Return the "current simulation time". */ static Time Now (void); + /** + * \param id the event id to analyse + * \returns the delay left until the input event id expires. + * if the event is not running, this method returns + * zero. + */ + static Time GetDelayLeft (const EventId &id); private: Simulator (); ~Simulator (); From 855d52908b274654378900137de3a5e97abfe5f6 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 3 Oct 2007 13:01:29 +0200 Subject: [PATCH 140/148] add Timer::Suspend/Resume/IsSuspended --- src/simulator/timer.cc | 46 ++++++++++++++++++++++++++++++++++++++++-- src/simulator/timer.h | 23 +++++++++++++++++++++ 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/src/simulator/timer.cc b/src/simulator/timer.cc index 677d9335d..b6f8f5929 100644 --- a/src/simulator/timer.cc +++ b/src/simulator/timer.cc @@ -79,7 +79,11 @@ Timer::IsRunning (void) const { return m_event.IsRunning (); } - +bool +Timer::IsSuspended (void) const +{ + return (m_flags & TIMER_SUSPENDED) == TIMER_SUSPENDED; +} void Timer::Schedule (void) @@ -113,6 +117,23 @@ Timer::Schedule (Time delay) } } +void +Timer::Suspend (void) +{ + NS_ASSERT (IsRunning ()); + m_delayLeft = Simulator::GetDelayLeft (m_event); + Simulator::Remove (m_event); + m_flags |= TIMER_SUSPENDED; +} + +void +Timer::Resume (void) +{ + NS_ASSERT (m_flags & TIMER_SUSPENDED); + m_event = m_impl->Schedule (m_delayLeft); + m_flags &= ~TIMER_SUSPENDED; +} + } // namespace ns3 @@ -171,10 +192,30 @@ TimerTests::RunTests (void) { bool result = true; + Timer timer; + + timer.SetFunction (&bari); + timer.SetArguments (1); + timer.SetDelay (Seconds (10.0)); + NS_TEST_ASSERT (!timer.IsRunning ()); + NS_TEST_ASSERT (timer.IsExpired ()); + NS_TEST_ASSERT (!timer.IsSuspended ()); + timer.Schedule (); + NS_TEST_ASSERT (timer.IsRunning ()); + NS_TEST_ASSERT (!timer.IsExpired ()); + NS_TEST_ASSERT (!timer.IsSuspended ()); + timer.Suspend (); + NS_TEST_ASSERT (!timer.IsRunning ()); + NS_TEST_ASSERT (timer.IsExpired ()); + NS_TEST_ASSERT (timer.IsSuspended ()); + timer.Resume (); + NS_TEST_ASSERT (timer.IsRunning ()); + NS_TEST_ASSERT (!timer.IsExpired ()); + NS_TEST_ASSERT (!timer.IsSuspended ()); + int a = 0; int &b = a; const int &c = a; - Timer timer; timer.SetFunction (&bari); timer.SetArguments (2); @@ -229,6 +270,7 @@ TimerTests::RunTests (void) Simulator::Run (); Simulator::Destroy (); + return result; } diff --git a/src/simulator/timer.h b/src/simulator/timer.h index 301e4e6ef..4539626f1 100644 --- a/src/simulator/timer.h +++ b/src/simulator/timer.h @@ -190,6 +190,11 @@ public: * \return true if there is a currently-running event, false otherwise. */ bool IsRunning (void) const; + /** + * \returns true if this timer was suspended and not yet resumed, false + * otherwise. + */ + bool IsSuspended (void) const; /** * Schedule a new event using the currently-configured delay, function, * and arguments. @@ -203,6 +208,19 @@ public: */ void Schedule (Time delay); + /** + * Cancel the timer and save the amount of time left until it was + * set to expire. + * Calling Suspend on a non-running timer is an error. + */ + void Suspend (void); + /** + * Restart the timer to expire within the amount of time left saved + * during Suspend. + * Calling Resume without a prior call to Suspend is an error. + */ + void Resume (void); + private: template void DoSetFunction (IntToType<0>, FN fn); @@ -234,10 +252,15 @@ private: template void DoSetFunction (IntToType<6>, MEM_PTR memPtr, OBJ_PTR objPtr); + enum { + TIMER_SUSPENDED = (1<<7) + }; + int m_flags; Time m_delay; EventId m_event; TimerImpl *m_impl; + Time m_delayLeft; }; } // namespace ns3 From 7fae03e300f60592abb348013a5bc1a8300c6fae Mon Sep 17 00:00:00 2001 From: "Gustavo J. A. M. Carneiro" Date: Wed, 3 Oct 2007 16:38:17 +0100 Subject: [PATCH 141/148] Add a small mobility model visualization tool --- utils/mobility-visualizer-model.cc | 137 ++++++++++++++++++++ utils/mobility-visualizer-view.cc | 193 +++++++++++++++++++++++++++++ utils/mobility-visualizer.h | 23 ++++ utils/wscript | 14 +++ wscript | 1 + 5 files changed, 368 insertions(+) create mode 100644 utils/mobility-visualizer-model.cc create mode 100644 utils/mobility-visualizer-view.cc create mode 100644 utils/mobility-visualizer.h diff --git a/utils/mobility-visualizer-model.cc b/utils/mobility-visualizer-model.cc new file mode 100644 index 000000000..4e699222b --- /dev/null +++ b/utils/mobility-visualizer-model.cc @@ -0,0 +1,137 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ + +#include + +#include "ns3/ptr.h" +#include "ns3/mobility-model.h" +#include "ns3/mobility-model-notifier.h" +#include "ns3/random-topology.h" +#include "ns3/default-value.h" +#include "ns3/command-line.h" +#include "ns3/command-line.h" +#include "ns3/simulator.h" +#include "ns3/nstime.h" +#include "ns3/node.h" +#include "ns3/node-list.h" +#include "ns3/rectangle-default-value.h" + +#include "mobility-visualizer.h" + +using namespace ns3; + +static Time g_sampleInterval = Seconds (SAMPLE_INTERVAL); +static uint32_t g_numNodes = 10; + +template +static const T* DefaultValueListGet (const std::string &name) +{ + for (DefaultValueList::Iterator iter = DefaultValueList::Begin (); + iter != DefaultValueList::End (); iter++) + { + const DefaultValueBase *value = *iter; + if (value->GetName () == name) + { + return dynamic_cast (value); + } + } + return NULL; +} + + + +static void +Sample () +{ + + ViewUpdateData *data = new ViewUpdateData; + for (NodeList::Iterator nodeIter = NodeList::Begin (); nodeIter != NodeList::End (); nodeIter++) + { + Ptr node = *nodeIter; + Ptr mobility = node->QueryInterface (MobilityModel::iid); + Position pos = mobility->Get (); + Speed vel = mobility->GetSpeed (); + + NodeUpdate update; + update.node = PeekPointer (node); + update.x = pos.x; + update.y = pos.y; + update.vx = vel.dx; + update.vy = vel.dy; + data->updateList.push_back (update); + } + data->time = Simulator::Now ().GetSeconds (); + view_update (data); + Simulator::Schedule (g_sampleInterval, Sample); +} + + + +int model_init (int argc, char *argv[], double *x1, double *y1, double *x2, double *y2) +{ + DefaultValue::Bind ("RandomWalk2dMode", "Time"); + DefaultValue::Bind ("RandomWalk2dTime", "5s"); + DefaultValue::Bind ("RandomWalk2dSpeed", "Constant:20.0"); + DefaultValue::Bind ("RandomDirection2dSpeed", "Constant:20.0"); + DefaultValue::Bind ("RandomWalk2dBounds", "0:400:0:300"); + DefaultValue::Bind ("RandomDirection2dArea", "0:400:0:300"); + DefaultValue::Bind ("RandomWaypointSpeed", "Uniform:10:30"); + +// DefaultValue::Bind ("RandomDiscPositionX", "100"); +// DefaultValue::Bind ("RandomDiscPositionY", "50"); +// DefaultValue::Bind ("RandomDiscPositionRho", "Uniform:0:30"); + + DefaultValue::Bind ("RandomTopologyPositionType", "RandomRectanglePosition"); + DefaultValue::Bind ("RandomTopologyMobilityType", "RandomWalk2dMobilityModel"); + +// CommandLine::AddArgValue ("sample-interval","sample interval", g_sampleInterval); +// CommandLine::AddArgValue ("num-nodes","number of nodes", g_numNodes); + + CommandLine::Parse (argc, argv); + + RandomTopology topology; + + for (uint32_t i = 0; i < g_numNodes; i++) + { + Ptr node = Create (); + node->AddInterface (Create ()); + } + + topology.Layout (NodeList::Begin (), NodeList::End ()); + + Simulator::Schedule (g_sampleInterval, Sample); + + ClassId mobType = DefaultValueListGet ("RandomTopologyMobilityType")->GetValue (); + if (mobType.GetName () == "RandomWalk2dMobilityModel") + { + Rectangle bounds = DefaultValueListGet ("RandomWalk2dBounds")->GetValue (); + *x1 = bounds.xMin; + *y1 = bounds.yMin; + *x2 = bounds.xMax; + *y2 = bounds.yMax; + std::cout << "RECT " << bounds.xMin << " " << bounds.xMax << " " + << bounds.yMin << " " << bounds.yMax << std::endl; + } + else if (mobType.GetName () == "RandomDirection2dMobilityModel") + { + Rectangle bounds = DefaultValueListGet ("RandomDirection2dArea")->GetValue (); + *x1 = bounds.xMin; + *y1 = bounds.yMin; + *x2 = bounds.xMax; + *y2 = bounds.yMax; + std::cout << "RECT " << bounds.xMin << " " << bounds.xMax << " " + << bounds.yMin << " " << bounds.yMax << std::endl; + } + else if (mobType.GetName () == "RandomWaypointMobilityModel") + { + std::cerr << "bounds for RandomWaypointMobilityModel not implemented" << std::endl; + //ClassId posType = DefaultValueList::Get ("RandomWaypointPosition")->GetValue (); + std::cout << "?" << std::endl; // too hard to represent an abstract/probabilistic model graphically + } + else + { + NS_FATAL_ERROR ("mobility type " << mobType.GetName () << " not supported"); + } + + std::cerr << g_sampleInterval << std::endl; + return 0; +} diff --git a/utils/mobility-visualizer-view.cc b/utils/mobility-visualizer-view.cc new file mode 100644 index 000000000..70a72440a --- /dev/null +++ b/utils/mobility-visualizer-view.cc @@ -0,0 +1,193 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ + +#include + +#include +#include + +#include "mobility-visualizer.h" +#include +#include "ns3/simulator.h" + +#define MAX_QUEUE_LENGTH 100 +#define MAX_EVENTS 20 +#define LOOKAHEAD_SECONDS 10 + +GtkWidget *g_canvas; + +int model_init (int argc, char *argv[]); + +struct Node +{ + GooCanvasItem *m_item; + void create () + { + GooCanvasItem *root = goo_canvas_get_root_item (GOO_CANVAS (g_canvas)); + m_item = goo_canvas_ellipse_new (root, 0, 0, 2.0, 2.0, + "line_width", 0.5, + "stroke_color", "black", + "fill_color", "red", + NULL); + } + void update (double x, double y, double vx, double vy) + { + g_object_set (m_item, "center_x", x, "center_y", y, NULL); + } +}; + +std::map g_nodes; + +GTimeVal initialTime = {-1, -1}; +gboolean firstTime = TRUE; +double g_lookaheadTime = 0; +G_LOCK_DEFINE (g_lookaheadTimeMux); +ViewUpdateData *g_nextData = NULL; + + +GAsyncQueue *queue; + +double get_current_time () +{ + GTimeVal currTime; + g_get_current_time (&currTime); + GTimeVal relativeTime; + relativeTime.tv_sec = currTime.tv_sec - initialTime.tv_sec + LOOKAHEAD_SECONDS; + relativeTime.tv_usec = currTime.tv_usec; + g_time_val_add (&relativeTime, -initialTime.tv_usec); + return relativeTime.tv_sec + 1.0e-6*relativeTime.tv_usec; +} + +// called from the simulation thread +void view_update (ViewUpdateData *updateData) +{ + while ((G_LOCK (g_lookaheadTimeMux), g_lookaheadTime) != 0 and updateData->time >= g_lookaheadTime) + { + G_UNLOCK (g_lookaheadTimeMux); + g_usleep ((gulong) 10e3); + } + G_UNLOCK (g_lookaheadTimeMux); + g_async_queue_push (queue, updateData); +} + +void view_update_process (ViewUpdateData *updateData) +{ + if (firstTime) + { + firstTime = FALSE; + g_get_current_time (&initialTime); + + for (std::vector::const_iterator update + = updateData->updateList.begin (); + update != updateData->updateList.end (); + update++) + { + g_nodes[update->node].create (); + g_nodes[update->node].update (update->x, update->y, update->vx, update->vy); + } + delete updateData; + } + else + { + for (std::vector::const_iterator update + = updateData->updateList.begin (); + update != updateData->updateList.end (); + update++) + { + g_nodes[update->node].update (update->x, update->y, update->vx, update->vy); + } + delete updateData; + + G_LOCK (g_lookaheadTimeMux); + g_lookaheadTime = get_current_time () + LOOKAHEAD_SECONDS; + G_UNLOCK (g_lookaheadTimeMux); + } +} + +gboolean view_update_consumer () +{ + if (!g_nextData) + g_nextData = (ViewUpdateData *) g_async_queue_try_pop (queue); + + if (!g_nextData) + return TRUE; + + double time = get_current_time (); + if (g_nextData->time > time) + return TRUE; + + do + { + view_update_process (g_nextData); + g_nextData = (ViewUpdateData *) g_async_queue_try_pop (queue); + } + while (g_nextData && g_nextData->time <= time); + + return TRUE; +} + +void zoom_changed (GtkAdjustment *adj) +{ + goo_canvas_set_scale (GOO_CANVAS (g_canvas), gtk_adjustment_get_value (adj)); +} + +int main (int argc, char *argv[]) +{ + g_thread_init (NULL); + gtk_init (&argc, &argv); + double x1 = 0, y1 = 0, x2 = 0, y2 = 0; + + model_init (argc, argv, &x1, &y1, &x2, &y2); + + GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL); + gtk_window_set_default_size (GTK_WINDOW (window), 640, 600); + gtk_widget_show (window); + g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL); + + GtkWidget *scrolled_win = gtk_scrolled_window_new (NULL, NULL); + gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win), GTK_SHADOW_IN); + gtk_widget_show (scrolled_win); + + GtkWidget *vbox = gtk_vbox_new (FALSE, 4); + gtk_widget_show (vbox); + gtk_box_pack_start (GTK_BOX (vbox), scrolled_win, 1, 1, 4); + gtk_container_add (GTK_CONTAINER (window), vbox); + + GtkWidget *hbox = gtk_hbox_new (FALSE, 4); + gtk_widget_show (hbox); + gtk_box_pack_start (GTK_BOX (vbox), hbox, false, false, 4); + + GtkObject *zoom = gtk_adjustment_new (3.0, 0.1, 10.0, 0.2, 1.0, 1.0); + gtk_box_pack_start(GTK_BOX (hbox), + GTK_WIDGET (g_object_new (GTK_TYPE_SPIN_BUTTON, "adjustment", zoom, + "visible", true, "digits", 2, NULL)), + false, false, 4); + + g_canvas = goo_canvas_new (); + gtk_widget_set_size_request (GTK_WIDGET (g_canvas), 600, 450); + goo_canvas_set_bounds (GOO_CANVAS (g_canvas), -500, -500, 500, 500); + g_signal_connect (zoom, "value-changed", G_CALLBACK (zoom_changed), NULL); + gtk_adjustment_value_changed (GTK_ADJUSTMENT (zoom)); + gtk_widget_show (g_canvas); + gtk_container_add (GTK_CONTAINER (scrolled_win), g_canvas); + + goo_canvas_scroll_to (GOO_CANVAS (g_canvas), 0, 0); + + // create the bounds rectangle + if (x1 != x2) + { + GooCanvasItem *item = + goo_canvas_rect_new (goo_canvas_get_root_item (GOO_CANVAS (g_canvas)), + x1, y1, x2-x1, y2-y1, NULL); + g_object_set (item, "line-width", 1.0, "stroke-color", "grey", NULL); + } + + queue = g_async_queue_new (); + + g_timeout_add ((guint) (SAMPLE_INTERVAL*1000), (GSourceFunc) view_update_consumer, NULL); + + g_thread_create (GThreadFunc (ns3::Simulator::Run), NULL, FALSE, NULL); + + gtk_main (); + + return 0; +} diff --git a/utils/mobility-visualizer.h b/utils/mobility-visualizer.h new file mode 100644 index 000000000..1e1719fcc --- /dev/null +++ b/utils/mobility-visualizer.h @@ -0,0 +1,23 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +#include + +int model_init (int argc, char *argv[], double *x1, double *y1, double *x2, double *y2); + +struct NodeUpdate +{ + void *node; + double x; + double y; + double vx; + double vy; +}; + +struct ViewUpdateData +{ + std::vector updateList; + double time; +}; + +void view_update (ViewUpdateData *updateData); + +#define SAMPLE_INTERVAL (1.0/100) // due to some race condition, this is the only value that works diff --git a/utils/wscript b/utils/wscript index 15fbfa512..f127ae7df 100644 --- a/utils/wscript +++ b/utils/wscript @@ -1,5 +1,12 @@ ## -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*- +def configure(conf): + check = conf.create_pkgconfig_configurator() + check.name = 'goocanvas gthread-2.0' + check.uselib = 'MOBILITY_VISUALIZER' + check.mandatory = False + conf.env['ENABLE_MOBILITY_VISUALIZER'] = check.run() + def build(bld): env = bld.env_of_name('default') @@ -23,3 +30,10 @@ def build(bld): obj = bld.create_ns3_program('print-trace-sources', ['internet-node', 'csma-cd', 'point-to-point']) obj.source = 'print-trace-sources.cc' + + if env['ENABLE_MOBILITY_VISUALIZER']: + obj = bld.create_ns3_program('mobility-visualizer', + ['internet-node', 'mobility']) + obj.source = ['mobility-visualizer-model.cc', 'mobility-visualizer-view.cc'] + obj.uselib = 'MOBILITY_VISUALIZER' + diff --git a/wscript b/wscript index a02db75da..f8c1317ae 100644 --- a/wscript +++ b/wscript @@ -143,6 +143,7 @@ def configure(conf): variant_env.append_value("LINKFLAGS", "-Wl,--enable-runtime-pseudo-reloc") conf.sub_config('src') + conf.sub_config('utils') def create_ns3_program(bld, name, dependencies=('simulator',)): From c0ea316f4445d4e35c289e72a94e0e49c120379a Mon Sep 17 00:00:00 2001 From: "Gustavo J. A. M. Carneiro" Date: Wed, 3 Oct 2007 17:29:27 +0100 Subject: [PATCH 142/148] Draw the velocity vector --- utils/mobility-visualizer-model.cc | 2 +- utils/mobility-visualizer-view.cc | 26 +++++++++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/utils/mobility-visualizer-model.cc b/utils/mobility-visualizer-model.cc index 4e699222b..21cb007e4 100644 --- a/utils/mobility-visualizer-model.cc +++ b/utils/mobility-visualizer-model.cc @@ -71,7 +71,7 @@ int model_init (int argc, char *argv[], double *x1, double *y1, double *x2, doub DefaultValue::Bind ("RandomWalk2dMode", "Time"); DefaultValue::Bind ("RandomWalk2dTime", "5s"); DefaultValue::Bind ("RandomWalk2dSpeed", "Constant:20.0"); - DefaultValue::Bind ("RandomDirection2dSpeed", "Constant:20.0"); + DefaultValue::Bind ("RandomDirection2dSpeed", "Uniform:10.0:20.0"); DefaultValue::Bind ("RandomWalk2dBounds", "0:400:0:300"); DefaultValue::Bind ("RandomDirection2dArea", "0:400:0:300"); DefaultValue::Bind ("RandomWaypointSpeed", "Uniform:10:30"); diff --git a/utils/mobility-visualizer-view.cc b/utils/mobility-visualizer-view.cc index 70a72440a..6015a92f1 100644 --- a/utils/mobility-visualizer-view.cc +++ b/utils/mobility-visualizer-view.cc @@ -20,6 +20,7 @@ int model_init (int argc, char *argv[]); struct Node { GooCanvasItem *m_item; + GooCanvasItem *m_vector; void create () { GooCanvasItem *root = goo_canvas_get_root_item (GOO_CANVAS (g_canvas)); @@ -28,10 +29,31 @@ struct Node "stroke_color", "black", "fill_color", "red", NULL); + + m_vector = goo_canvas_polyline_new (root, FALSE, 0, + "line_width", 0.3, + "stroke_color", "black", + "end-arrow", TRUE, + "arrow-length", 10.0, + "arrow-width", 10.0, + NULL); + } + + void update (double x, double y, double vx, double vy) { g_object_set (m_item, "center_x", x, "center_y", y, NULL); + + GooCanvasPoints *points = goo_canvas_points_new (2); + + points->coords[0] = x; + points->coords[1] = y; + points->coords[2] = x + vx; + points->coords[3] = y + vy; + + g_object_set (m_vector, "points", points, NULL); + goo_canvas_points_unref (points); } }; @@ -46,6 +68,8 @@ ViewUpdateData *g_nextData = NULL; GAsyncQueue *queue; +#define TIME_SCALE 1 + double get_current_time () { GTimeVal currTime; @@ -54,7 +78,7 @@ double get_current_time () relativeTime.tv_sec = currTime.tv_sec - initialTime.tv_sec + LOOKAHEAD_SECONDS; relativeTime.tv_usec = currTime.tv_usec; g_time_val_add (&relativeTime, -initialTime.tv_usec); - return relativeTime.tv_sec + 1.0e-6*relativeTime.tv_usec; + return (relativeTime.tv_sec + 1.0e-6*relativeTime.tv_usec)*TIME_SCALE; } // called from the simulation thread From 666850a6bc78aeadb095222e7f3ea54a5785ffc3 Mon Sep 17 00:00:00 2001 From: "Gustavo J. A. M. Carneiro" Date: Wed, 3 Oct 2007 17:38:05 +0100 Subject: [PATCH 143/148] Hide the velocity vector if it is null. --- utils/mobility-visualizer-view.cc | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/utils/mobility-visualizer-view.cc b/utils/mobility-visualizer-view.cc index 6015a92f1..cbf0dca95 100644 --- a/utils/mobility-visualizer-view.cc +++ b/utils/mobility-visualizer-view.cc @@ -45,15 +45,24 @@ struct Node { g_object_set (m_item, "center_x", x, "center_y", y, NULL); - GooCanvasPoints *points = goo_canvas_points_new (2); + if (vx == 0 && vy == 0) + { + GooCanvasPoints *points = goo_canvas_points_new (0); + g_object_set (m_vector, "points", points, NULL); + goo_canvas_points_unref (points); + } + else + { + GooCanvasPoints *points = goo_canvas_points_new (2); - points->coords[0] = x; - points->coords[1] = y; - points->coords[2] = x + vx; - points->coords[3] = y + vy; + points->coords[0] = x; + points->coords[1] = y; + points->coords[2] = x + vx; + points->coords[3] = y + vy; - g_object_set (m_vector, "points", points, NULL); - goo_canvas_points_unref (points); + g_object_set (m_vector, "points", points, NULL); + goo_canvas_points_unref (points); + } } }; From cf896e8064a298a6af84cc8d0f298f8899d3d028 Mon Sep 17 00:00:00 2001 From: "Gustavo J. A. M. Carneiro" Date: Wed, 3 Oct 2007 19:36:56 +0100 Subject: [PATCH 144/148] Use GStaticMutex instead of G_DEFINE_LOCK --- utils/mobility-visualizer-view.cc | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/utils/mobility-visualizer-view.cc b/utils/mobility-visualizer-view.cc index cbf0dca95..18bc08ce5 100644 --- a/utils/mobility-visualizer-view.cc +++ b/utils/mobility-visualizer-view.cc @@ -71,7 +71,7 @@ std::map g_nodes; GTimeVal initialTime = {-1, -1}; gboolean firstTime = TRUE; double g_lookaheadTime = 0; -G_LOCK_DEFINE (g_lookaheadTimeMux); +GStaticMutex g_lookaheadTimeMux = G_STATIC_MUTEX_INIT; ViewUpdateData *g_nextData = NULL; @@ -93,12 +93,12 @@ double get_current_time () // called from the simulation thread void view_update (ViewUpdateData *updateData) { - while ((G_LOCK (g_lookaheadTimeMux), g_lookaheadTime) != 0 and updateData->time >= g_lookaheadTime) + while ((g_static_mutex_lock (&g_lookaheadTimeMux), g_lookaheadTime) != 0 and updateData->time >= g_lookaheadTime) { - G_UNLOCK (g_lookaheadTimeMux); + g_static_mutex_unlock (&g_lookaheadTimeMux); g_usleep ((gulong) 10e3); } - G_UNLOCK (g_lookaheadTimeMux); + g_static_mutex_unlock (&g_lookaheadTimeMux); g_async_queue_push (queue, updateData); } @@ -130,9 +130,9 @@ void view_update_process (ViewUpdateData *updateData) } delete updateData; - G_LOCK (g_lookaheadTimeMux); + g_static_mutex_lock (&g_lookaheadTimeMux); g_lookaheadTime = get_current_time () + LOOKAHEAD_SECONDS; - G_UNLOCK (g_lookaheadTimeMux); + g_static_mutex_unlock (&g_lookaheadTimeMux); } } From 2d0f6869649164fa4472af5f484409d6ebbd6d28 Mon Sep 17 00:00:00 2001 From: "Gustavo J. A. M. Carneiro" Date: Wed, 3 Oct 2007 19:38:03 +0100 Subject: [PATCH 145/148] Compile mobility-visualizer with -fno-strict-aliasing to avoid GNOME bug #316221 --- utils/wscript | 3 +++ 1 file changed, 3 insertions(+) diff --git a/utils/wscript b/utils/wscript index f127ae7df..390508478 100644 --- a/utils/wscript +++ b/utils/wscript @@ -1,4 +1,5 @@ ## -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*- +import os.path def configure(conf): check = conf.create_pkgconfig_configurator() @@ -36,4 +37,6 @@ def build(bld): ['internet-node', 'mobility']) obj.source = ['mobility-visualizer-model.cc', 'mobility-visualizer-view.cc'] obj.uselib = 'MOBILITY_VISUALIZER' + if os.path.basename(obj.env['CXX']).startswith("g++"): + obj.env.append_value('CXXFLAGS', '-fno-strict-aliasing') From b28727ef8c9dc2f34ba56fcbd4886231cb21c31d Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 4 Oct 2007 09:14:16 +0200 Subject: [PATCH 146/148] make the timer a tristate object --- src/simulator/timer.cc | 32 +++++++++++++++++++++++++++++--- src/simulator/timer.h | 9 +++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/simulator/timer.cc b/src/simulator/timer.cc index b6f8f5929..d17a69c11 100644 --- a/src/simulator/timer.cc +++ b/src/simulator/timer.cc @@ -72,18 +72,35 @@ Timer::Remove (void) bool Timer::IsExpired (void) const { - return m_event.IsExpired (); + return !IsSuspended () && m_event.IsExpired (); } bool Timer::IsRunning (void) const { - return m_event.IsRunning (); + return !IsSuspended () && m_event.IsRunning (); } bool Timer::IsSuspended (void) const { return (m_flags & TIMER_SUSPENDED) == TIMER_SUSPENDED; } +enum Timer::State +Timer::GetState (void) const +{ + if (IsRunning ()) + { + return Timer::RUNNING; + } + else if (IsExpired ()) + { + return Timer::EXPIRED; + } + else + { + NS_ASSERT (IsSuspended ()); + return Timer::SUSPENDED; + } +} void Timer::Schedule (void) @@ -200,18 +217,27 @@ TimerTests::RunTests (void) NS_TEST_ASSERT (!timer.IsRunning ()); NS_TEST_ASSERT (timer.IsExpired ()); NS_TEST_ASSERT (!timer.IsSuspended ()); + NS_TEST_ASSERT_EQUAL (timer.GetState (), Timer::EXPIRED); timer.Schedule (); NS_TEST_ASSERT (timer.IsRunning ()); NS_TEST_ASSERT (!timer.IsExpired ()); NS_TEST_ASSERT (!timer.IsSuspended ()); + NS_TEST_ASSERT_EQUAL (timer.GetState (), Timer::RUNNING); timer.Suspend (); NS_TEST_ASSERT (!timer.IsRunning ()); - NS_TEST_ASSERT (timer.IsExpired ()); + NS_TEST_ASSERT (!timer.IsExpired ()); NS_TEST_ASSERT (timer.IsSuspended ()); + NS_TEST_ASSERT_EQUAL (timer.GetState (), Timer::SUSPENDED); timer.Resume (); NS_TEST_ASSERT (timer.IsRunning ()); NS_TEST_ASSERT (!timer.IsExpired ()); NS_TEST_ASSERT (!timer.IsSuspended ()); + NS_TEST_ASSERT_EQUAL (timer.GetState (), Timer::RUNNING); + timer.Cancel (); + NS_TEST_ASSERT (!timer.IsRunning ()); + NS_TEST_ASSERT (timer.IsExpired ()); + NS_TEST_ASSERT (!timer.IsSuspended ()); + NS_TEST_ASSERT_EQUAL (timer.GetState (), Timer::EXPIRED); int a = 0; int &b = a; diff --git a/src/simulator/timer.h b/src/simulator/timer.h index 4539626f1..2d573c3df 100644 --- a/src/simulator/timer.h +++ b/src/simulator/timer.h @@ -67,6 +67,11 @@ public: */ GARBAGE_COLLECT = (1<<6) }; + enum State { + RUNNING, + EXPIRED, + SUSPENDED, + }; /** * create a timer with a default event lifetime management policy: * - CHECK_ON_SCHEDULE @@ -195,6 +200,10 @@ public: * otherwise. */ bool IsSuspended (void) const; + /** + * \returns the current state of the timer. + */ + enum Timer::State GetState (void) const; /** * Schedule a new event using the currently-configured delay, function, * and arguments. From f70700c3300384c023efedf2ebf9452c3759e536 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 4 Oct 2007 09:22:05 +0200 Subject: [PATCH 147/148] add Timer::GetDelayLeft --- src/simulator/timer.cc | 20 +++++++++++++++++++- src/simulator/timer.h | 6 ++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/simulator/timer.cc b/src/simulator/timer.cc index d17a69c11..d3c09a0c8 100644 --- a/src/simulator/timer.cc +++ b/src/simulator/timer.cc @@ -57,7 +57,25 @@ Timer::GetDelay (void) const { return m_delay; } - +Time +Timer::GetDelayLeft (void) const +{ + switch (GetState ()) { + case Timer::RUNNING: + return Simulator::GetDelayLeft (m_event); + break; + case Timer::EXPIRED: + return TimeStep (0); + break; + case Timer::SUSPENDED: + return m_delayLeft; + break; + default: + NS_ASSERT (false); + return TimeStep (0); + break; + } +} void Timer::Cancel (void) diff --git a/src/simulator/timer.h b/src/simulator/timer.h index 2d573c3df..5087db294 100644 --- a/src/simulator/timer.h +++ b/src/simulator/timer.h @@ -177,6 +177,12 @@ public: * \returns the currently-configured delay for the next Schedule. */ Time GetDelay (void) const; + /** + * \returns the amount of time left until this timer expires. + * + * This method returns zero if the timer is in EXPIRED state. + */ + Time GetDelayLeft (void) const; /** * Cancel the currently-running event if there is one. Do nothing * otherwise. From 3457fcb48baa8a5c547697845321e48970ccda75 Mon Sep 17 00:00:00 2001 From: "Gustavo J. A. M. Carneiro" Date: Thu, 4 Oct 2007 11:24:05 +0100 Subject: [PATCH 148/148] Fix race condition in mobility-visualizer-view; reduce refresh rate to 30fps. --- utils/mobility-visualizer-view.cc | 48 +++++++++++++------------------ utils/mobility-visualizer.h | 2 +- 2 files changed, 21 insertions(+), 29 deletions(-) diff --git a/utils/mobility-visualizer-view.cc b/utils/mobility-visualizer-view.cc index 18bc08ce5..c1370856b 100644 --- a/utils/mobility-visualizer-view.cc +++ b/utils/mobility-visualizer-view.cc @@ -104,48 +104,40 @@ void view_update (ViewUpdateData *updateData) void view_update_process (ViewUpdateData *updateData) { - if (firstTime) + for (std::vector::const_iterator update + = updateData->updateList.begin (); + update != updateData->updateList.end (); + update++) { - firstTime = FALSE; - g_get_current_time (&initialTime); - - for (std::vector::const_iterator update - = updateData->updateList.begin (); - update != updateData->updateList.end (); - update++) + if (g_nodes.find (update->node) == g_nodes.end ()) { g_nodes[update->node].create (); - g_nodes[update->node].update (update->x, update->y, update->vx, update->vy); } - delete updateData; - } - else - { - for (std::vector::const_iterator update - = updateData->updateList.begin (); - update != updateData->updateList.end (); - update++) - { - g_nodes[update->node].update (update->x, update->y, update->vx, update->vy); - } - delete updateData; - - g_static_mutex_lock (&g_lookaheadTimeMux); - g_lookaheadTime = get_current_time () + LOOKAHEAD_SECONDS; - g_static_mutex_unlock (&g_lookaheadTimeMux); + g_nodes[update->node].update (update->x, update->y, update->vx, update->vy); } + delete updateData; } gboolean view_update_consumer () { + if (firstTime) + { + firstTime = FALSE; + g_get_current_time (&initialTime); + } + + double now = get_current_time (); + g_static_mutex_lock (&g_lookaheadTimeMux); + g_lookaheadTime = now + LOOKAHEAD_SECONDS; + g_static_mutex_unlock (&g_lookaheadTimeMux); + if (!g_nextData) g_nextData = (ViewUpdateData *) g_async_queue_try_pop (queue); if (!g_nextData) return TRUE; - double time = get_current_time (); - if (g_nextData->time > time) + if (g_nextData->time > now) return TRUE; do @@ -153,7 +145,7 @@ gboolean view_update_consumer () view_update_process (g_nextData); g_nextData = (ViewUpdateData *) g_async_queue_try_pop (queue); } - while (g_nextData && g_nextData->time <= time); + while (g_nextData && g_nextData->time <= now); return TRUE; } diff --git a/utils/mobility-visualizer.h b/utils/mobility-visualizer.h index 1e1719fcc..634847c3c 100644 --- a/utils/mobility-visualizer.h +++ b/utils/mobility-visualizer.h @@ -20,4 +20,4 @@ struct ViewUpdateData void view_update (ViewUpdateData *updateData); -#define SAMPLE_INTERVAL (1.0/100) // due to some race condition, this is the only value that works +#define SAMPLE_INTERVAL (1.0/30)