do not use DefaultValues in simulator
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Mon, 03 Mar 2008 20:47:12 +0100
changeset 2559 ab98bf436a17
parent 2558 e03d9fefa80b
child 2560 627fef42e280
do not use DefaultValues in simulator
samples/main-adhoc-wifi.cc
samples/main-ap-wifi.cc
src/simulator/scheduler.cc
src/simulator/scheduler.h
src/simulator/simulator.cc
src/simulator/simulator.h
src/simulator/time-default-value.cc
src/simulator/time-default-value.h
src/simulator/wscript
utils/bench-simulator.cc
utils/replay-simulation.cc
--- a/samples/main-adhoc-wifi.cc	Mon Mar 03 20:01:24 2008 +0100
+++ b/samples/main-adhoc-wifi.cc	Mon Mar 03 20:47:12 2008 +0100
@@ -173,8 +173,6 @@
 
 int main (int argc, char *argv[])
 {
-  Simulator::SetLinkedList ();
-
   // disable fragmentation
   Config::SetDefault ("WifiRemoteStationManager::FragmentationThreshold", String ("2200"));
   Config::SetDefault ("WifiRemoteStationManager::RtsCtsThreshold", String ("2200"));
--- a/samples/main-ap-wifi.cc	Mon Mar 03 20:01:24 2008 +0100
+++ b/samples/main-ap-wifi.cc	Mon Mar 03 20:47:12 2008 +0100
@@ -103,8 +103,6 @@
 
 int main (int argc, char *argv[])
 {
-  Simulator::SetLinkedList ();
-
   Packet::EnableMetadata ();
 
   // enable rts cts all the time.
--- a/src/simulator/scheduler.cc	Mon Mar 03 20:01:24 2008 +0100
+++ b/src/simulator/scheduler.cc	Mon Mar 03 20:47:12 2008 +0100
@@ -26,4 +26,13 @@
 Scheduler::~Scheduler () 
 {}
 
-}; // namespace ns3
+TypeId 
+Scheduler::GetTypeId (void)
+{
+  static TypeId tid = TypeId ("Scheduler")
+    .SetParent<Object> ()
+    ;
+  return tid;
+}
+
+} // namespace ns3
--- a/src/simulator/scheduler.h	Mon Mar 03 20:01:24 2008 +0100
+++ b/src/simulator/scheduler.h	Mon Mar 03 20:47:12 2008 +0100
@@ -23,6 +23,7 @@
 
 #include <stdint.h>
 #include "event-id.h"
+#include "ns3/object.h"
 
 namespace ns3 {
 
@@ -47,8 +48,11 @@
  * 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;
--- a/src/simulator/simulator.cc	Mon Mar 03 20:01:24 2008 +0100
+++ b/src/simulator/simulator.cc	Mon Mar 03 20:47:12 2008 +0100
@@ -24,7 +24,6 @@
 
 #include "ns3/ptr.h"
 #include "ns3/assert.h"
-#include "ns3/default-value.h"
 
 
 #include <math.h>
@@ -50,9 +49,12 @@
 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 @@
   Time GetDelayLeft (const EventId &id) const;
   Time GetMaximumSimulationTime (void) const;
 
+  void SetScheduler (Ptr<Scheduler> scheduler);
+  Ptr<Scheduler> GetScheduler (void) const;
+
 private:
   void ProcessOneEvent (void);
   uint64_t NextTs (void) const;
@@ -80,7 +85,7 @@
   DestroyEvents m_destroyEvents;
   uint64_t m_stopAt;
   bool m_stop;
-  Scheduler *m_events;
+  Ptr<Scheduler> m_events;
   uint32_t m_uid;
   uint32_t m_currentUid;
   uint64_t m_currentTs;
@@ -93,13 +98,26 @@
 };
 
 
-
+TypeId
+SimulatorPrivate::GetTypeId (void)
+{
+  static TypeId tid = TypeId ("SimulatorPrivate")
+    .SetParent<Object> ()
+    .AddConstructor<SimulatorPrivate> ()
+    .AddAttribute ("Scheduler",
+                   "XXX",
+                   Ptr<Scheduler> (0),
+                   // XXX: allow getting the scheduler too.
+                   MakePtrAccessor (&SimulatorPrivate::SetScheduler),
+                   MakePtrChecker<Scheduler> ())
+    ;
+  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 @@
     {
       EventId next = m_events->RemoveNext ();
     }
-  delete m_events;
-  m_events = (Scheduler *)0xdeadbeaf;
+  m_events = 0;
 }
 
+void
+SimulatorPrivate::SetScheduler (Ptr<Scheduler> scheduler)
+{
+  if (m_events != 0)
+    {
+      while (!m_events->IsEmpty ())
+        {
+          EventId next = m_events->RemoveNext ();
+          scheduler->Insert (next);
+        }
+    }
+  m_events = scheduler;
+}
+
+Ptr<Scheduler>
+SimulatorPrivate::GetScheduler (void) const
+{
+  return m_events;
+}
 
 void
 SimulatorPrivate::EnableLogTo (char const *filename)
@@ -361,22 +397,9 @@
 
 SimulatorPrivate *Simulator::m_priv = 0;
 
-void Simulator::SetLinkedList (void)
-{
-  DefaultValue::Bind ("Scheduler", "List");
-}
-void Simulator::SetBinaryHeap (void)
+void Simulator::SetScheduler (Ptr<Scheduler> scheduler)
 {
-  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 @@
 {
   if (m_priv == 0) 
     {
-      Scheduler *events = SchedulerFactory::CreateDefault ();
-      m_priv = new SimulatorPrivate (events);
+      m_priv = new SimulatorPrivate ();
+      Ptr<Scheduler> scheduler = CreateObject<SchedulerMap> ();
+      m_priv->SetScheduler (scheduler);
     }
   TRACE_S ("priv " << m_priv);
   return m_priv;
@@ -900,19 +924,19 @@
   bool result = true;
 
   Simulator::Destroy ();
-  Simulator::SetLinkedList ();
+  Simulator::SetScheduler (CreateObject<SchedulerList> ());
   if (!RunOneTest ()) 
     {
       result = false;
     }
   Simulator::Destroy ();
-  Simulator::SetBinaryHeap ();
+  Simulator::SetScheduler (CreateObject<SchedulerHeap> ());
   if (!RunOneTest ()) 
     {
       result = false;
     }
   Simulator::Destroy ();
-  Simulator::SetStdMap ();
+  Simulator::SetScheduler (CreateObject<SchedulerMap> ());
   if (!RunOneTest ()) 
     {
       result = false;
--- a/src/simulator/simulator.h	Mon Mar 03 20:01:24 2008 +0100
+++ b/src/simulator/simulator.h	Mon Mar 03 20:47:12 2008 +0100
@@ -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 @@
    */
   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> scheduler);
 
   /**
    * Force the use of a user-provided event scheduler.
--- a/src/simulator/time-default-value.cc	Mon Mar 03 20:01:24 2008 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -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 <mathieu.lacage@sophia.inria.fr>
- */
-
-#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
--- a/src/simulator/time-default-value.h	Mon Mar 03 20:01:24 2008 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -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 <mathieu.lacage@sophia.inria.fr>
- */
-#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 */
--- a/src/simulator/wscript	Mon Mar 03 20:01:24 2008 +0100
+++ b/src/simulator/wscript	Mon Mar 03 20:47:12 2008 +0100
@@ -59,7 +59,6 @@
         'scheduler-map.cc',
         'event-impl.cc',
         'simulator.cc',
-        'time-default-value.cc',
         'timer.cc',
         'watchdog.cc',
         ]
@@ -72,9 +71,10 @@
         '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',
--- a/utils/bench-simulator.cc	Mon Mar 03 20:01:24 2008 +0100
+++ b/utils/bench-simulator.cc	Mon Mar 03 20:47:12 2008 +0100
@@ -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 <iostream>
 #include <fstream>
@@ -159,15 +162,15 @@
     {
       if (strcmp ("--list", argv[0]) == 0) 
         {
-          Simulator::SetLinkedList ();
+          Simulator::SetScheduler (CreateObject<SchedulerList> ());
         } 
       else if (strcmp ("--heap", argv[0]) == 0) 
         {
-          Simulator::SetBinaryHeap ();
+          Simulator::SetScheduler (CreateObject<SchedulerHeap> ());
         } 
       else if (strcmp ("--map", argv[0]) == 0) 
         {
-          Simulator::SetStdMap ();
+          Simulator::SetScheduler (CreateObject<SchedulerMap> ());
         } 
       else if (strcmp ("--debug", argv[0]) == 0) 
         {
--- a/utils/replay-simulation.cc	Mon Mar 03 20:01:24 2008 +0100
+++ b/utils/replay-simulation.cc	Mon Mar 03 20:47:12 2008 +0100
@@ -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 <vector>
@@ -315,15 +318,15 @@
     {
       if (is_map)
         {
-          Simulator::SetStdMap ();
+          Simulator::SetScheduler (CreateObject<SchedulerMap> ());
         }
       else if (is_list)
         {
-          Simulator::SetLinkedList ();
+          Simulator::SetScheduler (CreateObject<SchedulerList> ());
         }
       else if (is_heap)
         {
-          Simulator::SetBinaryHeap ();
+          Simulator::SetScheduler (CreateObject<SchedulerHeap> ());
         }
       log.Run ();
       Simulator::Destroy ();