diff --git a/src/node/onoff-application.cc b/src/node/onoff-application.cc index a62b9d639..a4f00621d 100644 --- a/src/node/onoff-application.cc +++ b/src/node/onoff-application.cc @@ -60,7 +60,7 @@ uint32_t OnOffApplication::g_defaultSize = 512; m_cbrRate(rate), m_pktSize(size), m_residualBits(0), - m_lastStartTime(0), + m_lastStartTime((HighPrecision)0), m_maxBytes(0xffffffff), m_totBytes(0), m_startStopScheduled(false), diff --git a/src/simulator/nstime.h b/src/simulator/nstime.h index 464fe58ce..cfa07f0eb 100644 --- a/src/simulator/nstime.h +++ b/src/simulator/nstime.h @@ -308,6 +308,21 @@ class TimeUnit<1> // -*- New methods -*- public: + /** + * \brief String constructor + * Construct TimeUnit<1> object from common time expressions like " + * 1ms" or "10s". Supported units include: + * - s (seconds) + * - ms (milliseconds) + * - us (microseconds) + * - ns (nanoseconds) + * + * There can be no white space between the numerical portion + * and the units. Any otherwise malformed string causes a fatal error to + * occur. + * \param s The string to parse into a TimeUnit<1> + */ + TimeUnit<1>(const std::string& s); /** * \returns an approximation in seconds of the time stored in this * instance. diff --git a/src/simulator/time.cc b/src/simulator/time.cc index 7ad27c48b..184e57c99 100644 --- a/src/simulator/time.cc +++ b/src/simulator/time.cc @@ -20,9 +20,43 @@ */ #include "time.h" #include "simulator.h" +#include "ns3/fatal-error.h" namespace ns3 { +TimeUnit<1>::TimeUnit(const std::string& s) +{ + std::string::size_type n = s.find_first_not_of("0123456789."); + if (n != std::string::npos) + { // Found non-numeric + double r = atof(s.substr(0, n).c_str()); + std::string trailer = s.substr(n, std::string::npos); + if (trailer == std::string("s")) + { + m_data = HighPrecision (r * 1000000000.0); + return; + } + if (trailer == std::string("ms")) + { + m_data = HighPrecision ((int64_t)(r * 1000000), false); + return; + } + if (trailer == std::string("us")) + { + m_data = HighPrecision ((int64_t)(r * 1000), false); + return; + } + if (trailer == std::string("ns")) + { + m_data = HighPrecision ((int64_t)r, false); + return; + } + NS_FATAL_ERROR("Can't Parse Time "<::GetSeconds (void) const {