bug 133: automate RandomVariable memory management.

This commit is contained in:
Mathieu Lacage
2008-02-07 19:57:21 +01:00
parent bf9d59fe21
commit f03f1c237b
21 changed files with 1091 additions and 600 deletions

View File

@@ -91,8 +91,8 @@ OnOffApplication::Construct (Ptr<Node> n,
m_socket = 0;
m_peer = remote;
m_connected = false;
m_onTime = onTime.Copy ();
m_offTime = offTime.Copy ();
m_onTime = onTime;
m_offTime = offTime;
m_pktSize = size;
m_residualBits = 0;
m_lastStartTime = Seconds (0);
@@ -136,12 +136,6 @@ OnOffApplication::DoDispose (void)
NS_LOG_FUNCTION;
m_socket = 0;
delete m_onTime;
delete m_offTime;
m_onTime = 0;
m_offTime = 0;
// chain up
Application::DoDispose ();
}
@@ -222,7 +216,7 @@ void OnOffApplication::ScheduleStartEvent()
{ // Schedules the event to start sending data (switch to the "On" state)
NS_LOG_FUNCTION;
Time offInterval = Seconds(m_offTime->GetValue());
Time offInterval = Seconds(m_offTime.GetValue());
NS_LOG_LOGIC ("start at " << offInterval);
m_startStopEvent = Simulator::Schedule(offInterval, &OnOffApplication::StartSending, this);
}
@@ -231,7 +225,7 @@ void OnOffApplication::ScheduleStopEvent()
{ // Schedules the event to stop sending data (switch to "Off" state)
NS_LOG_FUNCTION;
Time onInterval = Seconds(m_onTime->GetValue());
Time onInterval = Seconds(m_onTime.GetValue());
Simulator::Schedule(onInterval, &OnOffApplication::StopSending, this);
}

View File

@@ -30,6 +30,7 @@
#include "ns3/ptr.h"
#include "ns3/data-rate.h"
#include "ns3/callback-trace-source.h"
#include "ns3/random-variable.h"
namespace ns3 {
@@ -124,8 +125,8 @@ private:
Ptr<Socket> m_socket; // Associated socket
Address m_peer; // Peer address
bool m_connected; // True if connected
RandomVariable* m_onTime; // rng for On Time
RandomVariable* m_offTime; // rng for Off Time
RandomVariable m_onTime; // rng for On Time
RandomVariable m_offTime; // rng for Off Time
DataRate m_cbrRate; // Rate that data is generated
uint32_t m_pktSize; // Size of packets
uint32_t m_residualBits; // Number of generated, but not sent, bits

View File

@@ -140,13 +140,12 @@ RateErrorModel::RateErrorModel () :
{
NS_LOG_FUNCTION;
// Assume a uniform random variable if user does not specify
m_ranvar = new UniformVariable ();
m_ranvar = UniformVariable ();
}
RateErrorModel::~RateErrorModel ()
{
NS_LOG_FUNCTION;
delete m_ranvar;
}
enum ErrorUnit
@@ -181,8 +180,7 @@ void
RateErrorModel::SetRandomVariable (const RandomVariable &ranvar)
{
NS_LOG_FUNCTION;
delete m_ranvar;
m_ranvar = ranvar.Copy ();
m_ranvar = ranvar;
}
bool
@@ -212,7 +210,7 @@ bool
RateErrorModel::DoCorruptPkt (Ptr<Packet> p)
{
NS_LOG_FUNCTION;
return (m_ranvar->GetValue () < m_rate);
return (m_ranvar.GetValue () < m_rate);
}
bool
@@ -221,7 +219,7 @@ RateErrorModel::DoCorruptByte (Ptr<Packet> p)
NS_LOG_FUNCTION;
// compute pkt error rate, assume uniformly distributed byte error
double per = 1 - pow (1.0 - m_rate, p->GetSize ());
return (m_ranvar->GetValue () < per);
return (m_ranvar.GetValue () < per);
}
bool
@@ -230,7 +228,7 @@ RateErrorModel::DoCorruptBit(Ptr<Packet> p)
NS_LOG_FUNCTION;
// compute pkt error rate, assume uniformly distributed bit error
double per = 1 - pow (1.0 - m_rate, (8 * p->GetSize ()) );
return (m_ranvar->GetValue () < per);
return (m_ranvar.GetValue () < per);
}
void

View File

@@ -23,11 +23,11 @@
#include <list>
#include "ns3/object.h"
#include "ns3/random-variable.h"
namespace ns3 {
class Packet;
class RandomVariable;
/**
* \brief General error model that can be used to corrupt packets
@@ -174,7 +174,7 @@ private:
enum ErrorUnit m_unit;
double m_rate;
RandomVariable* m_ranvar;
RandomVariable m_ranvar;
};
/**

View File

@@ -39,17 +39,17 @@ RandomVariableDefaultValue::RandomVariableDefaultValue (std::string name,
DefaultValueList::Add (this);
}
RandomVariable *
RandomVariableDefaultValue::GetCopy (void)
RandomVariable
RandomVariableDefaultValue::Get (void) const
{
RandomVariable *variable;
RandomVariable variable;
bool ok;
ok = Parse (m_value, true, &variable);
NS_ASSERT (ok);
return variable;
}
double
RandomVariableDefaultValue::ReadAsDouble (std::string value, bool &ok)
RandomVariableDefaultValue::ReadAsDouble (std::string value, bool &ok) const
{
double v;
std::istringstream iss;
@@ -60,7 +60,7 @@ RandomVariableDefaultValue::ReadAsDouble (std::string value, bool &ok)
}
bool
RandomVariableDefaultValue::Parse (const std::string &value,
bool mustCreate, RandomVariable **pVariable)
bool mustCreate, RandomVariable *pVariable) const
{
std::string::size_type pos = value.find_first_of(":");
if (pos == std::string::npos)
@@ -76,7 +76,7 @@ RandomVariableDefaultValue::Parse (const std::string &value,
if (mustCreate)
{
NS_LOG_LOGIC ("create Constant constant=" << constant);
*pVariable = new ConstantVariable (constant);
*pVariable = ConstantVariable (constant);
}
else
{
@@ -100,7 +100,7 @@ RandomVariableDefaultValue::Parse (const std::string &value,
if (mustCreate)
{
NS_LOG_LOGIC ("create Uniform min=" << min << ", max=" << max);
*pVariable = new UniformVariable (minVal, maxVal);
*pVariable = UniformVariable (minVal, maxVal);
}
else
{

View File

@@ -33,10 +33,10 @@ class RandomVariableDefaultValue : public DefaultValueBase
std::string help,
std::string defaultValue);
RandomVariable *GetCopy (void);
RandomVariable Get (void) const;
private:
bool Parse (const std::string &value, bool mustCreate, RandomVariable **pVariable);
double ReadAsDouble (const std::string value, bool &ok);
bool Parse (const std::string &value, bool mustCreate, RandomVariable *pVariable) const;
double ReadAsDouble (const std::string value, bool &ok) const;
virtual bool DoParseValue (const std::string &value);
virtual std::string DoGetType (void) const;
virtual std::string DoGetDefaultValue (void) const;

File diff suppressed because it is too large Load Diff

View File

@@ -33,7 +33,7 @@
namespace ns3{
class RngStream;
class RandomVariableBase;
/**
* \brief The basic RNG for NS-3.
@@ -44,46 +44,31 @@ class RngStream;
* the University of Montreal.
*
* NS-3 has a rich set of random number generators.
* Class RandomVariable defines the base class functionalty
* Class RandomVariableBase defines the base class functionalty
* required for all random number generators. By default, the underlying
* generator is seeded with the time of day, and then deterministically
* creates a sequence of seeds for each subsequent generator that is created.
* The rest of the documentation outlines how to change this behavior.
*/
class RandomVariable {
class RandomVariable
{
public:
/**
* \brief Constructor for a random number generator with a random seed.
*/
RandomVariable();
/**
* \brief Copy constructor
*/
RandomVariable(const RandomVariable&);
/**
* \brief Destructor for a random number generator with a random seed.
*/
virtual ~RandomVariable();
RandomVariable(const RandomVariable&o);
RandomVariable &operator = (const RandomVariable &o);
~RandomVariable();
/**
* \brief Returns a random double from the underlying distribution
* \return A floating point random value
*/
virtual double GetValue() = 0;
double GetValue (void) const;
/**
* \brief Returns a random integer integer from the underlying distribution
* \return Integer cast of ::GetValue()
*/
virtual uint32_t GetIntValue();
/**
* \return A copy of this object
*/
virtual RandomVariable* Copy() const = 0;
uint32_t GetIntValue (void) const;
/**
* \brief Get the internal state of the RNG
@@ -94,7 +79,7 @@ public:
* \param seed Output parameter; gets overwritten with the internal state of
* of the RNG.
*/
virtual void GetSeed(uint32_t seed[6]);
void GetSeed(uint32_t seed[6]) const;
/**
* \brief Set seeding behavior
@@ -104,7 +89,7 @@ public:
* generator is seeded with data from /dev/random instead of
* being seeded based upon the time of day. For this to be effective,
* it must be called before the creation of the first instance of a
* RandomVariable or subclass. Example:
* RandomVariableBase or subclass. Example:
* \code
* RandomVariable::UseDevRandom();
* UniformVariable x(2,3); //these are seeded randomly
@@ -174,23 +159,12 @@ public:
*/
static void SetRunNumber(uint32_t n);
private:
static void GetRandomSeeds(uint32_t seeds[6]);
private:
static bool useDevRandom; // True if using /dev/random desired
static bool globalSeedSet; // True if global seed has been specified
static int devRandom; // File handle for /dev/random
static uint32_t globalSeed[6]; // The global seed to use
friend class RandomVariableInitializer;
RandomVariableBase *m_variable;
protected:
static unsigned long heuristic_sequence;
static RngStream* m_static_generator;
static uint32_t runNumber;
static void Initialize(); // Initialize the RNG system
static bool initialized; // True if package seed is set
RngStream* m_generator; //underlying generator being wrapped
RandomVariable (const RandomVariableBase &variable);
RandomVariableBase *Peek (void);
};
/**
* \brief The uniform distribution RNG for NS-3.
* \ingroup randomvariable
@@ -207,7 +181,8 @@ protected:
* UniformVariable::GetSingleValue(100,1000); //returns a value [100,1000)
* \endcode
*/
class UniformVariable : public RandomVariable {
class UniformVariable : public RandomVariable
{
public:
/**
* Creates a uniform random number generator in the
@@ -221,14 +196,6 @@ public:
* \param l High end of the range
*/
UniformVariable(double s, double l);
UniformVariable(const UniformVariable& c);
/**
* \return A value between low and high values specified by the constructor
*/
virtual double GetValue();
virtual RandomVariable* Copy() const;
public:
/**
* \param s Low end of the range
@@ -236,50 +203,36 @@ public:
* \return A uniformly distributed random number between s and l
*/
static double GetSingleValue(double s, double l);
private:
double m_min;
double m_max;
};
/**
* \brief A random variable that returns a constant
* \ingroup randomvariable
*
* Class ConstantVariable defines a random number generator that
* Class ConstantVariableImpl defines a random number generator that
* returns the same value every sample.
*/
class ConstantVariable : public RandomVariable {
public:
/**
* Construct a ConstantVariable RNG that returns zero every sample
* Construct a ConstantVariableImpl RNG that returns zero every sample
*/
ConstantVariable();
/**
* Construct a ConstantVariable RNG that returns the specified value
* Construct a ConstantVariableImpl RNG that returns the specified value
* every sample.
* \param c Unchanging value for this RNG.
*/
ConstantVariable(double c);
ConstantVariable(const ConstantVariable& c) ;
/**
* \brief Specify a new constant RNG for this generator.
* \param c New constant value for this RNG.
*/
void NewConstant(double c);
void SetConstant(double c);
/**
* \return The constant value specified
*/
virtual double GetValue();
virtual uint32_t GetIntValue();
virtual RandomVariable* Copy() const;
private:
double m_const;
};
/**
@@ -291,14 +244,14 @@ private:
* increases for a period, then wraps around to the low value
* and begins monotonicaly increasing again.
*/
class SequentialVariable : public RandomVariable {
class SequentialVariable : public RandomVariable
{
public:
/**
* \brief Constructor for the SequentialVariable RNG.
* \brief Constructor for the SequentialVariableImpl RNG.
*
* The four parameters define the sequence. For example
* SequentialVariable(0,5,1,2) creates a RNG that has the sequence
* SequentialVariableImpl(0,5,1,2) creates a RNG that has the sequence
* 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 0, 0 ...
* \param f First value of the sequence.
* \param l One more than the last value of the sequence.
@@ -308,32 +261,17 @@ public:
SequentialVariable(double f, double l, double i = 1, uint32_t c = 1);
/**
* \brief Constructor for the SequentialVariable RNG.
* \brief Constructor for the SequentialVariableImpl RNG.
*
* Differs from the first only in that the increment parameter is a
* random variable
* \param f First value of the sequence.
* \param l One more than the last value of the sequence.
* \param i Reference to a RandomVariable for the sequence increment
* \param i Reference to a RandomVariableBase for the sequence increment
* \param c Number of times each member of the sequence is repeated
*/
SequentialVariable(double f, double l, const RandomVariable& i, uint32_t c = 1);
SequentialVariable(const SequentialVariable& c);
~SequentialVariable();
/**
* \return The next value in the Sequence
*/
virtual double GetValue();
virtual RandomVariable* Copy() const;
private:
double m_min;
double m_max;
RandomVariable* m_increment;
uint32_t m_consecutive;
double m_current;
uint32_t m_currentConsecutive;
};
/**
@@ -353,14 +291,15 @@ private:
* \f$ \left\{ \begin{array}{cl} \alpha e^{-\alpha x} & x < bound \\ bound & x > bound \end{array}\right. \f$
*
* \code
* ExponentialVariable x(3.14);
* ExponentialVariableImpl x(3.14);
* x.GetValue(); //will always return with mean 3.14
* ExponentialVariable::GetSingleValue(20.1); //returns with mean 20.1
* ExponentialVariable::GetSingleValue(108); //returns with mean 108
* ExponentialVariableImpl::GetSingleValue(20.1); //returns with mean 20.1
* ExponentialVariableImpl::GetSingleValue(108); //returns with mean 108
* \endcode
*
*/
class ExponentialVariable : public RandomVariable {
class ExponentialVariable : public RandomVariable
{
public:
/**
* Constructs an exponential random variable with a mean
@@ -387,27 +326,16 @@ public:
*/
ExponentialVariable(double m, double b);
ExponentialVariable(const ExponentialVariable& c);
/**
* \return A random value from this exponential distribution
*/
virtual double GetValue();
virtual RandomVariable* Copy() const;
public:
/**
* \param m The mean of the distribution from which the return value is drawn
* \param b The upper bound value desired, beyond which values get clipped
* \return A random number from an exponential distribution with mean m
*/
static double GetSingleValue(double m, double b=0);
private:
double m_mean; // Mean value of RV
double m_bound; // Upper bound on value (if non-zero)
};
/**
* \brief ParetoVariable distributed random var
* \brief ParetoVariableImpl distributed random var
* \ingroup randomvariable
*
* This class supports the creation of objects that return random numbers
@@ -422,19 +350,20 @@ private:
* with the equation \f$ x_m = mean \frac{k-1}{k}, k > 1\f$.
*
* \code
* ParetoVariable x(3.14);
* ParetoVariableImpl x(3.14);
* x.GetValue(); //will always return with mean 3.14
* ParetoVariable::GetSingleValue(20.1); //returns with mean 20.1
* ParetoVariable::GetSingleValue(108); //returns with mean 108
* ParetoVariableImpl::GetSingleValue(20.1); //returns with mean 20.1
* ParetoVariableImpl::GetSingleValue(108); //returns with mean 108
* \endcode
*/
class ParetoVariable : public RandomVariable {
class ParetoVariable : public RandomVariable
{
public:
/**
* Constructs a pareto random variable with a mean of 1 and a shape
* parameter of 1.5
*/
ParetoVariable();
ParetoVariable ();
/**
* Constructs a pareto random variable with specified mean and shape
@@ -465,14 +394,6 @@ public:
*/
ParetoVariable(double m, double s, double b);
ParetoVariable(const ParetoVariable& c);
/**
* \return A random value from this Pareto distribution
*/
virtual double GetValue();
virtual RandomVariable* Copy() const;
public:
/**
* \param m The mean value of the distribution from which the return value
* is drawn.
@@ -483,14 +404,10 @@ public:
* parameter s.
*/
static double GetSingleValue(double m, double s, double b=0);
private:
double m_mean; // Mean value of RV
double m_shape; // Shape parameter
double m_bound; // Upper bound on value (if non-zero)
};
/**
* \brief WeibullVariable distributed random var
* \brief WeibullVariableImpl distributed random var
* \ingroup randomvariable
*
* This class supports the creation of objects that return random numbers
@@ -530,7 +447,7 @@ public:
/**
* \brief Constructs a weibull random variable with the specified mean
* \brief value, shape (alpha), and upper bound.
* Since WeibullVariable distributions can theoretically return unbounded values,
* Since WeibullVariableImpl distributions can theoretically return unbounded values,
* it is sometimes usefull to specify a fixed upper limit. Note however
* that when the upper limit is specified, the true mean of the distribution
* is slightly smaller than the mean value specified.
@@ -539,15 +456,6 @@ public:
* \param b Upper limit on returned values
*/
WeibullVariable(double m, double s, double b);
WeibullVariable(const WeibullVariable& c);
/**
* \return A random value from this Weibull distribution
*/
virtual double GetValue();
virtual RandomVariable* Copy() const;
public:
/**
* \param m Mean value for the distribution.
* \param s Shape (alpha) parameter for the distribution.
@@ -555,14 +463,10 @@ public:
* \return Random number from a distribution specified by m,s, and b
*/
static double GetSingleValue(double m, double s, double b=0);
private:
double m_mean; // Mean value of RV
double m_alpha; // Shape parameter
double m_bound; // Upper bound on value (if non-zero)
};
/**
* \brief Class NormalVariable defines a random variable with a
* \brief Class NormalVariableImpl defines a random variable with a
* normal (Gaussian) distribution.
* \ingroup randomvariable
*
@@ -575,8 +479,8 @@ private:
* where \f$ mean = \mu \f$ and \f$ variance = \sigma^2 \f$
*
*/
class NormalVariable : public RandomVariable { // Normally Distributed random var
class NormalVariable : public RandomVariable
{
public:
static const double INFINITE_VALUE;
/**
@@ -589,37 +493,20 @@ public:
* \brief Construct a normal random variable with specified mean and variance
* \param m Mean value
* \param v Variance
* \param b Bound. The NormalVariable is bounded within +-bound.
* \param b Bound. The NormalVariableImpl is bounded within +-bound.
*/
NormalVariable(double m, double v, double b = INFINITE_VALUE);
NormalVariable(const NormalVariable& c);
/**
* \return A value from this normal distribution
*/
virtual double GetValue();
virtual RandomVariable* Copy() const;
public:
/**
* \param m Mean value
* \param v Variance
* \param b Bound. The NormalVariable is bounded within +-bound.
* \param b Bound. The NormalVariableImpl is bounded within +-bound.
* \return A random number from a distribution specified by m,v, and b.
*/
static double GetSingleValue(double m, double v, double b = INFINITE_VALUE);
private:
double m_mean; // Mean value of RV
double m_variance; // Mean value of RV
double m_bound; // Bound on value (absolute value)
bool m_nextValid; // True if next valid
double m_next; // The algorithm produces two values at a time
static bool m_static_nextValid;
static double m_static_next;
};
/**
* \brief EmpiricalVariable distribution random var
* \brief EmpiricalVariableImpl distribution random var
* \ingroup randomvariable
*
* Defines a random variable that has a specified, empirical
@@ -634,37 +521,18 @@ private:
class EmpiricalVariable : public RandomVariable {
public:
/**
* Constructor for the EmpiricalVariable random variables.
* Constructor for the EmpiricalVariableImpl random variables.
*/
explicit EmpiricalVariable();
virtual ~EmpiricalVariable();
EmpiricalVariable(const EmpiricalVariable& c);
/**
* \return A value from this empirical distribution
*/
virtual double GetValue();
virtual RandomVariable* Copy() const;
/**
* \brief Specifies a point in the empirical distribution
* \param v The function value for this point
* \param c Probability that the function is less than or equal to v
*/
virtual void CDF(double v, double c); // Value, prob <= Value
private:
class ValueCDF {
public:
ValueCDF();
ValueCDF(double v, double c);
ValueCDF(const ValueCDF& c);
double value;
double cdf;
};
virtual void Validate(); // Insure non-decreasing emiprical values
virtual double Interpolate(double, double, double, double, double);
bool validated; // True if non-decreasing validated
std::vector<ValueCDF> emp; // Empicical CDF
void CDF(double v, double c); // Value, prob <= Value
protected:
EmpiricalVariable (const RandomVariableBase &variable);
};
/**
@@ -672,20 +540,13 @@ private:
* \ingroup randomvariable
*
* Defines an empirical distribution where all values are integers.
* Indentical to EmpiricalVariable, but with slightly different
* Indentical to EmpiricalVariableImpl, but with slightly different
* interpolation between points.
*/
class IntEmpiricalVariable : public EmpiricalVariable {
class IntEmpiricalVariable : public EmpiricalVariable
{
public:
IntEmpiricalVariable();
virtual RandomVariable* Copy() const;
/**
* \return An integer value from this empirical distribution
*/
virtual uint32_t GetIntValue();
virtual double Interpolate(double, double, double, double, double);
};
/**
@@ -697,8 +558,8 @@ public:
* the RNG to return a known sequence, perhaps to
* compare NS-3 to some other simulator
*/
class DeterministicVariable : public RandomVariable {
class DeterministicVariable : public RandomVariable
{
public:
/**
* \brief Constructor
@@ -707,22 +568,11 @@ public:
* on successive calls to ::Value(). Note that the d pointer is copied
* for use by the generator (shallow-copy), not its contents, so the
* contents of the array d points to have to remain unchanged for the use
* of DeterministicVariable to be meaningful.
* of DeterministicVariableImpl to be meaningful.
* \param d Pointer to array of random values to return in sequence
* \param c Number of values in the array
*/
explicit DeterministicVariable(double* d, uint32_t c);
virtual ~DeterministicVariable();
/**
* \return The next value in the deterministic sequence
*/
virtual double GetValue();
virtual RandomVariable* Copy() const;
private:
uint32_t count;
uint32_t next;
double* data;
};
@@ -730,7 +580,7 @@ private:
* \brief Log-normal Distributed random var
* \ingroup randomvariable
*
* LogNormalVariable defines a random variable with log-normal
* LogNormalVariableImpl defines a random variable with log-normal
* distribution. If one takes the natural logarithm of random
* variable following the log-normal distribution, the obtained values
* follow a normal distribution.
@@ -748,7 +598,8 @@ private:
* \f$ \mu = ln(mean) - \frac{1}{2}ln\left(1+\frac{stddev}{mean^2}\right)\f$, and,
* \f$ \sigma = \sqrt{ln\left(1+\frac{stddev}{mean^2}\right)}\f$
*/
class LogNormalVariable : public RandomVariable {
class LogNormalVariable : public RandomVariable
{
public:
/**
* \param mu mu parameter of the lognormal distribution
@@ -756,21 +607,12 @@ public:
*/
LogNormalVariable (double mu, double sigma);
/**
* \return A random value from this distribution
*/
virtual double GetValue ();
virtual RandomVariable* Copy() const;
public:
/**
* \param mu mu parameter of the underlying normal distribution
* \param sigma sigma parameter of the underlying normal distribution
* \return A random number from the distribution specified by mu and sigma
*/
static double GetSingleValue(double mu, double sigma);
private:
double m_mu;
double m_sigma;
};
/**
@@ -780,7 +622,8 @@ private:
* This distribution is a triangular distribution. The probablility density
* is in the shape of a triangle.
*/
class TriangularVariable : public RandomVariable {
class TriangularVariable : public RandomVariable
{
public:
/**
* Creates a triangle distribution random number generator in the
@@ -796,15 +639,6 @@ public:
* \param mean mean of the distribution
*/
TriangularVariable(double s, double l, double mean);
TriangularVariable(const TriangularVariable& c);
/**
* \return A value from this distribution
*/
virtual double GetValue();
virtual RandomVariable* Copy() const;
public:
/**
* \param s Low end of the range
* \param l High end of the range
@@ -812,11 +646,6 @@ public:
* \return A triangularly distributed random number between s and l
*/
static double GetSingleValue(double s, double l, double mean);
private:
double m_min;
double m_max;
double m_mode; //easier to work with the mode internally instead of the mean
//they are related by the simple: mean = (min+max+mode)/3
};
}//namespace ns3

View File

@@ -69,20 +69,18 @@ PropagationDelayModel::CreateDefault (void)
}
RandomPropagationDelayModel::RandomPropagationDelayModel ()
: m_variable (g_random.GetCopy ())
: m_variable (g_random.Get ())
{}
RandomPropagationDelayModel::RandomPropagationDelayModel (const RandomVariable &variable)
: m_variable (variable.Copy ())
: m_variable (variable)
{}
RandomPropagationDelayModel::~RandomPropagationDelayModel ()
{
delete m_variable;
}
{}
Time
RandomPropagationDelayModel::GetDelay (Ptr<MobilityModel> a, Ptr<MobilityModel> b) const
{
return Seconds (m_variable->GetValue ());
return Seconds (m_variable.GetValue ());
}
ConstantSpeedPropagationDelayModel::ConstantSpeedPropagationDelayModel (double speed)

View File

@@ -23,13 +23,12 @@
#include "ns3/ptr.h"
#include "ns3/object.h"
#include "ns3/nstime.h"
#include "ns3/random-variable.h"
namespace ns3 {
class MobilityModel;
class RandomVariable;
/**
* \brief calculate a propagation delay.
*/
@@ -71,7 +70,7 @@ public:
virtual ~RandomPropagationDelayModel ();
virtual Time GetDelay (Ptr<MobilityModel> a, Ptr<MobilityModel> b) const;
private:
RandomVariable *m_variable;
RandomVariable m_variable;
};
/**

View File

@@ -104,23 +104,21 @@ PropagationLossModel::CreateDefault (void)
}
}
RandomPropagationLossModel::RandomPropagationLossModel ()
: m_variable (g_random.GetCopy ())
: m_variable (g_random.Get ())
{}
RandomPropagationLossModel::RandomPropagationLossModel (const RandomVariable &variable)
: m_variable (variable.Copy ())
: m_variable (variable)
{}
RandomPropagationLossModel::~RandomPropagationLossModel ()
{
delete m_variable;
}
{}
double
RandomPropagationLossModel::GetRxPower (double txPowerDbm,
Ptr<MobilityModel> a,
Ptr<MobilityModel> b) const
{
double rxPower = txPowerDbm - m_variable->GetValue ();
double rxPower = txPowerDbm - m_variable.GetValue ();
NS_LOG_DEBUG ("tx power="<<txPowerDbm<<"dbm, rx power="<<rxPower<<"Dbm");
return rxPower;
}

View File

@@ -21,13 +21,12 @@
#define PROPAGATION_LOSS_MODEL_H
#include "ns3/object.h"
#include "ns3/random-variable.h"
namespace ns3 {
class MobilityModel;
class RandomVariable;
/**
* \brief Modelize the propagation loss through a transmission medium
*
@@ -76,7 +75,7 @@ public:
Ptr<MobilityModel> a,
Ptr<MobilityModel> b) const;
private:
RandomVariable *m_variable;
RandomVariable m_variable;
};
/**

View File

@@ -54,8 +54,8 @@ static RectangleDefaultValue
RandomDirection2dMobilityModelParameters::RandomDirection2dMobilityModelParameters ()
: m_bounds (g_bounds.GetValue ()),
m_speedVariable (g_speedVariable.GetCopy ()),
m_pauseVariable (g_pauseVariable.GetCopy ())
m_speedVariable (g_speedVariable.Get ()),
m_pauseVariable (g_pauseVariable.Get ())
{}
RandomDirection2dMobilityModelParameters::RandomDirection2dMobilityModelParameters
@@ -63,29 +63,22 @@ RandomDirection2dMobilityModelParameters::RandomDirection2dMobilityModelParamete
const RandomVariable &speedVariable,
const RandomVariable &pauseVariable)
: m_bounds (bounds),
m_speedVariable (speedVariable.Copy ()),
m_pauseVariable (pauseVariable.Copy ())
m_speedVariable (speedVariable),
m_pauseVariable (pauseVariable)
{}
RandomDirection2dMobilityModelParameters::~RandomDirection2dMobilityModelParameters ()
{
delete m_speedVariable;
delete m_pauseVariable;
m_speedVariable = 0;
m_pauseVariable = 0;
}
{}
void
RandomDirection2dMobilityModelParameters::SetSpeed (const RandomVariable &speedVariable)
{
delete m_speedVariable;
m_speedVariable = speedVariable.Copy ();
m_speedVariable = speedVariable;
}
void
RandomDirection2dMobilityModelParameters::SetPause (const RandomVariable &pauseVariable)
{
delete m_pauseVariable;
m_pauseVariable = pauseVariable.Copy ();
m_pauseVariable = pauseVariable;
}
void
RandomDirection2dMobilityModelParameters::SetBounds (const Rectangle &bounds)
@@ -149,7 +142,7 @@ RandomDirection2dMobilityModel::Start (void)
void
RandomDirection2dMobilityModel::BeginPause (void)
{
Time pause = Seconds (m_parameters->m_pauseVariable->GetValue ());
Time pause = Seconds (m_parameters->m_pauseVariable.GetValue ());
m_helper.Pause ();
m_event = Simulator::Schedule (pause, &RandomDirection2dMobilityModel::ResetDirectionAndSpeed, this);
NotifyCourseChange ();
@@ -159,7 +152,7 @@ void
RandomDirection2dMobilityModel::SetDirectionAndSpeed (double direction)
{
NS_LOG_FUNCTION;
double speed = m_parameters->m_speedVariable->GetValue ();
double speed = m_parameters->m_speedVariable.GetValue ();
const Vector vector (std::cos (direction) * speed,
std::sin (direction) * speed,
0.0);

View File

@@ -26,13 +26,12 @@
#include "ns3/nstime.h"
#include "ns3/event-id.h"
#include "ns3/rectangle.h"
#include "ns3/random-variable.h"
#include "mobility-model.h"
#include "static-speed-helper.h"
namespace ns3 {
class RandomVariable;
/**
* \brief the parameters to control a RandomDirection mobility model.
*/
@@ -73,8 +72,8 @@ class RandomDirection2dMobilityModelParameters : public Object
static Ptr<RandomDirection2dMobilityModelParameters> GetCurrent (void);
Rectangle m_bounds;
RandomVariable *m_speedVariable;
RandomVariable *m_pauseVariable;
RandomVariable m_speedVariable;
RandomVariable m_pauseVariable;
};
/**

View File

@@ -88,26 +88,21 @@ RandomRectanglePosition::GetTypeId (void)
}
RandomRectanglePosition::RandomRectanglePosition ()
: m_x (g_rectangleX.GetCopy ()),
m_y (g_rectangleY.GetCopy ())
: m_x (g_rectangleX.Get ()),
m_y (g_rectangleY.Get ())
{}
RandomRectanglePosition::RandomRectanglePosition (const RandomVariable &x,
const RandomVariable &y)
: m_x (x.Copy ()),
m_y (y.Copy ())
: m_x (x),
m_y (y)
{}
RandomRectanglePosition::~RandomRectanglePosition ()
{
delete m_x;
delete m_y;
m_x = 0;
m_y = 0;
}
{}
Vector
RandomRectanglePosition::Get (void) const
{
double x = m_x->GetValue ();
double y = m_y->GetValue ();
double x = m_x.GetValue ();
double y = m_y.GetValue ();
return Vector (x, y, 0.0);
}
@@ -123,31 +118,26 @@ RandomDiscPosition::GetTypeId (void)
}
RandomDiscPosition::RandomDiscPosition ()
: m_theta (g_discTheta.GetCopy ()),
m_rho (g_discRho.GetCopy ()),
: m_theta (g_discTheta.Get ()),
m_rho (g_discRho.Get ()),
m_x (g_discX.GetValue ()),
m_y (g_discY.GetValue ())
{}
RandomDiscPosition::RandomDiscPosition (const RandomVariable &theta,
const RandomVariable &rho,
double x, double y)
: m_theta (theta.Copy ()),
m_rho (rho.Copy ()),
: m_theta (theta),
m_rho (rho),
m_x (0.0),
m_y (0.0)
{}
RandomDiscPosition::~RandomDiscPosition ()
{
delete m_theta;
delete m_rho;
m_theta = 0;
m_rho = 0;
}
{}
Vector
RandomDiscPosition::Get (void) const
{
double theta = m_theta->GetValue ();
double rho = m_rho->GetValue ();
double theta = m_theta.GetValue ();
double rho = m_rho.GetValue ();
double x = m_x + std::cos (theta) * rho;
double y = m_y + std::sin (theta) * rho;
NS_LOG_DEBUG ("Disc position x=" << x << ", y=" << y);

View File

@@ -21,12 +21,11 @@
#define RANDOM_POSITION_H
#include "ns3/object.h"
#include "ns3/random-variable.h"
#include "vector.h"
namespace ns3 {
class RandomVariable;
/**
* \brief choose a position at random.
*
@@ -69,8 +68,8 @@ public:
virtual ~RandomRectanglePosition ();
virtual Vector Get (void) const;
private:
RandomVariable *m_x;
RandomVariable *m_y;
RandomVariable m_x;
RandomVariable m_y;
};
/**
@@ -107,8 +106,8 @@ public:
virtual ~RandomDiscPosition ();
virtual Vector Get (void) const;
private:
RandomVariable *m_theta;
RandomVariable *m_rho;
RandomVariable m_theta;
RandomVariable m_rho;
double m_x;
double m_y;
};

View File

@@ -69,30 +69,23 @@ 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_speed (g_speed.Get ()),
m_direction (g_direction.Get ()),
m_bounds (g_rectangle.GetValue ())
{}
RandomWalk2dMobilityModelParameters::~RandomWalk2dMobilityModelParameters ()
{
delete m_speed;
delete m_direction;
m_speed = 0;
m_direction = 0;
}
{}
void
RandomWalk2dMobilityModelParameters::SetSpeed (const RandomVariable &speed)
{
delete m_speed;
m_speed = speed.Copy ();
m_speed = speed;
}
void
RandomWalk2dMobilityModelParameters::SetDirection (const RandomVariable &direction)
{
delete m_direction;
m_direction = direction.Copy ();
m_direction = direction;
}
void
RandomWalk2dMobilityModelParameters::SetModeDistance (double distance)
@@ -154,8 +147,8 @@ RandomWalk2dMobilityModel::RandomWalk2dMobilityModel (Ptr<RandomWalk2dMobilityMo
void
RandomWalk2dMobilityModel::Start (void)
{
double speed = m_parameters->m_speed->GetValue ();
double direction = m_parameters->m_direction->GetValue ();
double speed = m_parameters->m_speed.GetValue ();
double direction = m_parameters->m_direction.GetValue ();
Vector vector (std::cos (direction) * speed,
std::sin (direction) * speed,
0.0);

View File

@@ -25,12 +25,12 @@
#include "ns3/nstime.h"
#include "ns3/event-id.h"
#include "ns3/rectangle.h"
#include "ns3/random-variable.h"
#include "mobility-model.h"
#include "static-speed-helper.h"
namespace ns3 {
class RandomVariable;
/**
* \brief parameters to control a random walk 2d model
@@ -93,8 +93,8 @@ class RandomWalk2dMobilityModelParameters : public Object
enum Mode m_mode;
double m_modeDistance;
Time m_modeTime;
RandomVariable *m_speed;
RandomVariable *m_direction;
RandomVariable m_speed;
RandomVariable m_direction;
Rectangle m_bounds;
};

View File

@@ -47,16 +47,16 @@ g_position ("RandomWaypointPosition",
RandomWaypointMobilityModelParameters::RandomWaypointMobilityModelParameters ()
: m_speed (g_speed.GetCopy ()),
m_pause (g_pause.GetCopy ())
: m_speed (g_speed.Get ()),
m_pause (g_pause.Get ())
{
m_position = g_position.GetValue ().CreateObject ()->QueryInterface<RandomPosition> ();
}
RandomWaypointMobilityModelParameters::RandomWaypointMobilityModelParameters (Ptr<RandomPosition> randomPosition,
const RandomVariable &speed,
const RandomVariable &pause)
: m_speed (speed.Copy ()),
m_pause (pause.Copy ()),
: m_speed (speed),
m_pause (pause),
m_position (randomPosition)
{}
void
@@ -67,23 +67,17 @@ RandomWaypointMobilityModelParameters::SetWaypointPositionModel (Ptr<RandomPosit
void
RandomWaypointMobilityModelParameters::SetSpeed (const RandomVariable &speed)
{
delete m_speed;
m_speed = speed.Copy ();
m_speed = speed;
}
void
RandomWaypointMobilityModelParameters::SetPause (const RandomVariable &pause)
{
delete m_pause;
m_pause = pause.Copy ();
m_pause = pause;
}
void
RandomWaypointMobilityModelParameters::DoDispose (void)
{
m_position = 0;
delete m_pause;
delete m_speed;
m_pause = 0;
m_speed = 0;
}
Ptr<RandomWaypointMobilityModelParameters>
@@ -128,7 +122,7 @@ RandomWaypointMobilityModel::BeginWalk (void)
{
Vector m_current = m_helper.GetCurrentPosition ();
Vector destination = m_parameters->m_position->Get ();
double speed = m_parameters->m_speed->GetValue ();
double speed = m_parameters->m_speed.GetValue ();
double dx = (destination.x - m_current.x);
double dy = (destination.y - m_current.y);
double dz = (destination.z - m_current.z);
@@ -144,7 +138,7 @@ RandomWaypointMobilityModel::BeginWalk (void)
void
RandomWaypointMobilityModel::Start (void)
{
Time pause = Seconds (m_parameters->m_pause->GetValue ());
Time pause = Seconds (m_parameters->m_pause.GetValue ());
m_helper.Pause ();
NotifyCourseChange ();
m_event = Simulator::Schedule (pause, &RandomWaypointMobilityModel::BeginWalk, this);

View File

@@ -25,11 +25,10 @@
#include "mobility-model.h"
#include "random-position.h"
#include "ns3/ptr.h"
#include "ns3/random-variable.h"
namespace ns3 {
class RandomVariable;
/**
* \brief the parameters which control the behavior of a random waypoint
* mobility model.
@@ -66,8 +65,8 @@ private:
friend class RandomWaypointMobilityModel;
static Ptr<RandomWaypointMobilityModelParameters> GetCurrent (void);
virtual void DoDispose (void);
RandomVariable *m_speed;
RandomVariable *m_pause;
RandomVariable m_speed;
RandomVariable m_pause;
Ptr<RandomPosition> m_position;
};

View File

@@ -59,9 +59,8 @@ void Application::Start(const Time& startTime)
void Application::Start(const RandomVariable& startVar)
{
RandomVariable *v = startVar.Copy ();
ScheduleStart (Seconds (v->GetValue ()));
delete v;
RandomVariable v = startVar;
ScheduleStart (Seconds (v.GetValue ()));
}
@@ -72,9 +71,8 @@ void Application::Stop(const Time& stopTime)
void Application::Stop(const RandomVariable& stopVar)
{
RandomVariable *v = stopVar.Copy ();
ScheduleStop (Seconds (v->GetValue ()));
delete v;
RandomVariable v = stopVar;
ScheduleStop (Seconds (v.GetValue ()));
}
Ptr<Node> Application::GetNode() const