diff --git a/samples/main-adhoc-wifi.cc b/samples/main-adhoc-wifi.cc index aaa2a6eb8..218b17203 100644 --- a/samples/main-adhoc-wifi.cc +++ b/samples/main-adhoc-wifi.cc @@ -173,8 +173,6 @@ Experiment::Run (const WifiHelper &wifi) int main (int argc, char *argv[]) { - Simulator::SetLinkedList (); - // disable fragmentation Config::SetDefault ("WifiRemoteStationManager::FragmentationThreshold", String ("2200")); Config::SetDefault ("WifiRemoteStationManager::RtsCtsThreshold", String ("2200")); diff --git a/samples/main-ap-wifi.cc b/samples/main-ap-wifi.cc index e0d333f95..5dc54d506 100644 --- a/samples/main-ap-wifi.cc +++ b/samples/main-ap-wifi.cc @@ -103,8 +103,6 @@ AdvancePosition (Ptr node) int main (int argc, char *argv[]) { - Simulator::SetLinkedList (); - Packet::EnableMetadata (); // enable rts cts all the time. diff --git a/src/simulator/scheduler.cc b/src/simulator/scheduler.cc index 03d262e19..8008772cd 100644 --- a/src/simulator/scheduler.cc +++ b/src/simulator/scheduler.cc @@ -26,4 +26,13 @@ namespace ns3 { Scheduler::~Scheduler () {} -}; // namespace ns3 +TypeId +Scheduler::GetTypeId (void) +{ + static TypeId tid = TypeId ("Scheduler") + .SetParent () + ; + return tid; +} + +} // namespace ns3 diff --git a/src/simulator/scheduler.h b/src/simulator/scheduler.h index bb8a7fc23..276c36c96 100644 --- a/src/simulator/scheduler.h +++ b/src/simulator/scheduler.h @@ -23,6 +23,7 @@ #include #include "event-id.h" +#include "ns3/object.h" namespace ns3 { @@ -47,8 +48,11 @@ namespace ns3 { * scheduler included in ns3: see the files * src/simulator/scheduler-list.h and src/simulator/scheduler-list.cc */ -class Scheduler { +class Scheduler : public Object +{ public: + static TypeId GetTypeId (void); + struct EventKey { uint64_t m_ts; uint32_t m_uid; diff --git a/src/simulator/simulator.cc b/src/simulator/simulator.cc index 09ae0e75f..dc887d5cb 100644 --- a/src/simulator/simulator.cc +++ b/src/simulator/simulator.cc @@ -24,7 +24,6 @@ #include "ns3/ptr.h" #include "ns3/assert.h" -#include "ns3/default-value.h" #include @@ -50,9 +49,12 @@ std::cout << "SIMU TRACE " << x << std::endl; namespace ns3 { -class SimulatorPrivate { +class SimulatorPrivate : public Object +{ public: - SimulatorPrivate (Scheduler *events); + static TypeId GetTypeId (void); + + SimulatorPrivate (); ~SimulatorPrivate (); void EnableLogTo (char const *filename); @@ -72,6 +74,9 @@ public: Time GetDelayLeft (const EventId &id) const; Time GetMaximumSimulationTime (void) const; + void SetScheduler (Ptr scheduler); + Ptr GetScheduler (void) const; + private: void ProcessOneEvent (void); uint64_t NextTs (void) const; @@ -80,7 +85,7 @@ private: DestroyEvents m_destroyEvents; uint64_t m_stopAt; bool m_stop; - Scheduler *m_events; + Ptr m_events; uint32_t m_uid; uint32_t m_currentUid; uint64_t m_currentTs; @@ -93,13 +98,26 @@ private: }; +TypeId +SimulatorPrivate::GetTypeId (void) +{ + static TypeId tid = TypeId ("SimulatorPrivate") + .SetParent () + .AddConstructor () + .AddAttribute ("Scheduler", + "XXX", + Ptr (0), + // XXX: allow getting the scheduler too. + MakePtrAccessor (&SimulatorPrivate::SetScheduler), + MakePtrChecker ()) + ; + return tid; +} - -SimulatorPrivate::SimulatorPrivate (Scheduler *events) +SimulatorPrivate::SimulatorPrivate () { m_stop = false; m_stopAt = 0; - m_events = events; // uids are allocated from 4. // uid 0 is "invalid" events // uid 1 is "now" events @@ -128,10 +146,28 @@ SimulatorPrivate::~SimulatorPrivate () { EventId next = m_events->RemoveNext (); } - delete m_events; - m_events = (Scheduler *)0xdeadbeaf; + m_events = 0; } +void +SimulatorPrivate::SetScheduler (Ptr scheduler) +{ + if (m_events != 0) + { + while (!m_events->IsEmpty ()) + { + EventId next = m_events->RemoveNext (); + scheduler->Insert (next); + } + } + m_events = scheduler; +} + +Ptr +SimulatorPrivate::GetScheduler (void) const +{ + return m_events; +} void SimulatorPrivate::EnableLogTo (char const *filename) @@ -361,22 +397,9 @@ namespace ns3 { SimulatorPrivate *Simulator::m_priv = 0; -void Simulator::SetLinkedList (void) +void Simulator::SetScheduler (Ptr scheduler) { - DefaultValue::Bind ("Scheduler", "List"); -} -void Simulator::SetBinaryHeap (void) -{ - DefaultValue::Bind ("Scheduler", "BinaryHeap"); -} -void Simulator::SetStdMap (void) -{ - DefaultValue::Bind ("Scheduler", "Map"); -} -void -Simulator::SetExternal (const std::string &external) -{ - DefaultValue::Bind ("Scheduler", external); + GetPriv ()->SetScheduler (scheduler); } void Simulator::EnableLogTo (char const *filename) { @@ -389,8 +412,9 @@ Simulator::GetPriv (void) { if (m_priv == 0) { - Scheduler *events = SchedulerFactory::CreateDefault (); - m_priv = new SimulatorPrivate (events); + m_priv = new SimulatorPrivate (); + Ptr scheduler = CreateObject (); + m_priv->SetScheduler (scheduler); } TRACE_S ("priv " << m_priv); return m_priv; @@ -900,19 +924,19 @@ SimulatorTests::RunTests (void) bool result = true; Simulator::Destroy (); - Simulator::SetLinkedList (); + Simulator::SetScheduler (CreateObject ()); if (!RunOneTest ()) { result = false; } Simulator::Destroy (); - Simulator::SetBinaryHeap (); + Simulator::SetScheduler (CreateObject ()); if (!RunOneTest ()) { result = false; } Simulator::Destroy (); - Simulator::SetStdMap (); + Simulator::SetScheduler (CreateObject ()); if (!RunOneTest ()) { result = false; diff --git a/src/simulator/simulator.h b/src/simulator/simulator.h index afd9601a5..68b5d9699 100644 --- a/src/simulator/simulator.h +++ b/src/simulator/simulator.h @@ -25,6 +25,7 @@ #include "event-id.h" #include "event-impl.h" #include "nstime.h" +#include "scheduler.h" #include "ns3/type-traits.h" namespace ns3 { @@ -58,29 +59,9 @@ public: */ static void EnableParallelSimulation (void); /** - * Force the use of an event scheduler based on a linked-list. - * This method must be invoked before any other method exported - * by the Simulator class. - * - insert: O(n) - * - remove: O(1) + * XXX */ - static void SetLinkedList (void); - /** - * Force the use of an event scheduler based on a binary heap. - * This method must be invoked before any other method exported - * by the Simulator class. - * - insert: O(log(n)) - * - remove: O(log(n)) - */ - static void SetBinaryHeap (void); - /** - * Force the use of an event scheduler based on a std::map. - * This method must be invoked before any other method exported - * by the Simulator class. - * - insert: O(log(n)) - * - remove: O(log(n)) - */ - static void SetStdMap (void); + static void SetScheduler (Ptr scheduler); /** * Force the use of a user-provided event scheduler. diff --git a/src/simulator/time-default-value.cc b/src/simulator/time-default-value.cc deleted file mode 100644 index 2d2e4a6a6..000000000 --- a/src/simulator/time-default-value.cc +++ /dev/null @@ -1,105 +0,0 @@ -/* -*- 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 "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 deleted file mode 100644 index 0fb64cae1..000000000 --- a/src/simulator/time-default-value.h +++ /dev/null @@ -1,69 +0,0 @@ -/* -*- 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 - */ -#ifndef TIME_DEFAULT_VALUE_H -#define TIME_DEFAULT_VALUE_H - -#include "ns3/default-value.h" -#include "ns3/nstime.h" - -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); - 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 */ diff --git a/src/simulator/wscript b/src/simulator/wscript index a4264b4b5..ea9251d4a 100644 --- a/src/simulator/wscript +++ b/src/simulator/wscript @@ -59,7 +59,6 @@ def build(bld): 'scheduler-map.cc', 'event-impl.cc', 'simulator.cc', - 'time-default-value.cc', 'timer.cc', 'watchdog.cc', ] @@ -72,9 +71,10 @@ def build(bld): 'event-impl.h', 'simulator.h', 'scheduler.h', - 'scheduler-factory.h', + 'scheduler-list.h', + 'scheduler-map.h', + 'scheduler-heap.h', 'simulation-singleton.h', - 'time-default-value.h', 'timer.h', 'timer-impl.h', 'watchdog.h', diff --git a/utils/bench-simulator.cc b/utils/bench-simulator.cc index 957c172fe..92b3ee742 100644 --- a/utils/bench-simulator.cc +++ b/utils/bench-simulator.cc @@ -20,6 +20,9 @@ */ #include "ns3/simulator.h" +#include "ns3/scheduler-list.h" +#include "ns3/scheduler-map.h" +#include "ns3/scheduler-heap.h" #include "ns3/system-wall-clock-ms.h" #include #include @@ -159,15 +162,15 @@ int main (int argc, char *argv[]) { if (strcmp ("--list", argv[0]) == 0) { - Simulator::SetLinkedList (); + Simulator::SetScheduler (CreateObject ()); } else if (strcmp ("--heap", argv[0]) == 0) { - Simulator::SetBinaryHeap (); + Simulator::SetScheduler (CreateObject ()); } else if (strcmp ("--map", argv[0]) == 0) { - Simulator::SetStdMap (); + Simulator::SetScheduler (CreateObject ()); } else if (strcmp ("--debug", argv[0]) == 0) { diff --git a/utils/replay-simulation.cc b/utils/replay-simulation.cc index 9741cf84e..04daa0d64 100644 --- a/utils/replay-simulation.cc +++ b/utils/replay-simulation.cc @@ -20,6 +20,9 @@ */ #include "ns3/simulator.h" +#include "ns3/scheduler-list.h" +#include "ns3/scheduler-map.h" +#include "ns3/scheduler-heap.h" #include "ns3/nstime.h" #include "ns3/system-wall-clock-ms.h" #include @@ -315,15 +318,15 @@ int main (int argc, char *argv[]) { if (is_map) { - Simulator::SetStdMap (); + Simulator::SetScheduler (CreateObject ()); } else if (is_list) { - Simulator::SetLinkedList (); + Simulator::SetScheduler (CreateObject ()); } else if (is_heap) { - Simulator::SetBinaryHeap (); + Simulator::SetScheduler (CreateObject ()); } log.Run (); Simulator::Destroy ();