small modifications to the samples showing event scheduling
authorTom Henderson <tomh@tomh.org>
Wed, 22 Dec 2010 16:35:11 -0800
changeset 6721 e85ecc17279a
parent 6720 16271e640e04
child 6722 cc6b9c4fc9e8
small modifications to the samples showing event scheduling
samples/main-simulator.cc
samples/sample-simulator.cc
samples/sample-simulator.py
samples/wscript
--- a/samples/main-simulator.cc	Wed Dec 22 16:19:42 2010 -0800
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,51 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-#include "ns3/simulator.h"
-#include "ns3/nstime.h"
-#include "ns3/command-line.h"
-#include <iostream>
-
-using namespace ns3;
-
-class MyModel {
-public:
-  void Start (void);
-private:
-  void DealWithEvent (double eventValue);
-};
-
-void 
-MyModel::Start (void)
-{
-  Simulator::Schedule (Seconds (10.0), 
-                       &MyModel::DealWithEvent, 
-                       this, Simulator::Now ().GetSeconds ());
-}
-void
-MyModel::DealWithEvent (double value)
-{
-  std::cout << "Member method received event at " << Simulator::Now ().GetSeconds () 
-            << "s started at " << value << "s" << std::endl;
-}
-
-static void 
-random_function (MyModel *model)
-{
-  std::cout << "random function received event at " << 
-      Simulator::Now ().GetSeconds () << "s" << std::endl;
-  model->Start ();
-}
-
-
-int main (int argc, char *argv[])
-{
-  CommandLine cmd;
-  cmd.Parse (argc, argv);
-
-  MyModel model;
-
-  Simulator::Schedule (Seconds (10.0), &random_function, &model);
-
-  Simulator::Run ();
-
-  Simulator::Destroy ();
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/samples/sample-simulator.cc	Wed Dec 22 16:35:11 2010 -0800
@@ -0,0 +1,91 @@
+/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2010 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
+ *
+ * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
+ */
+
+#include <iostream>
+#include "ns3/simulator.h"
+#include "ns3/nstime.h"
+#include "ns3/command-line.h"
+#include "ns3/random-variable.h"
+
+using namespace ns3;
+
+class MyModel
+{
+public:
+  void Start (void);
+private:
+  void HandleEvent (double eventValue);
+};
+
+void
+MyModel::Start (void)
+{
+  Simulator::Schedule (Seconds (10.0),
+                       &MyModel::HandleEvent,
+                       this, Simulator::Now ().GetSeconds ());
+}
+void
+MyModel::HandleEvent (double value)
+{
+  std::cout << "Member method received event at "
+            << Simulator::Now ().GetSeconds ()
+            << "s started at " << value << "s" << std::endl;
+}
+
+static void
+ExampleFunction (MyModel *model)
+{
+  std::cout << "ExampleFunction received event at "
+            << Simulator::Now ().GetSeconds () << "s" << std::endl;
+  model->Start ();
+}
+
+static void
+RandomFunction (void)
+{
+  std::cout << "RandomFunction received event at "
+            << Simulator::Now ().GetSeconds () << "s" << std::endl;
+}
+
+static void
+CancelledEvent (void)
+{
+  std::cout << "I should never be called... " << std::endl;
+}
+
+int main (int argc, char *argv[])
+{
+  CommandLine cmd;
+  cmd.Parse (argc, argv);
+
+  MyModel model;
+  UniformVariable v = UniformVariable (10, 20);
+
+  Simulator::Schedule (Seconds (10.0), &ExampleFunction, &model);
+
+  Simulator::Schedule (Seconds (v.GetValue ()), &RandomFunction);
+
+  EventId id = Simulator::Schedule (Seconds (30.0), &CancelledEvent);
+  Simulator::Cancel (id);
+  
+  Simulator::Run ();
+
+  Simulator::Destroy ();
+}
--- a/samples/sample-simulator.py	Wed Dec 22 16:19:42 2010 -0800
+++ b/samples/sample-simulator.py	Wed Dec 22 16:35:11 2010 -0800
@@ -1,26 +1,60 @@
 # -*- Mode:Python; -*-
+# /*
+#  * Copyright (c) 2010 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
+#  *
+#  * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
+#  */
+# 
+# Python version of sample-simulator.cc
 
 import ns3 as ns
 
-
 class MyModel(object):
 
     def Start(self):
-        ns.Simulator.Schedule(ns.Seconds(10.0), self.DealWithEvent, ns.Simulator.Now().GetSeconds())
+        ns.Simulator.Schedule(ns.Seconds(10.0), self.HandleEvent, ns.Simulator.Now().GetSeconds())
 
-    def DealWithEvent(self, value):
-        print "Member method received event at ", ns.Simulator.Now().GetSeconds(), \
-            "s started at ", value, "s"
+    def HandleEvent(self, value):
+        print "Member method received event at", ns.Simulator.Now().GetSeconds(), \
+            "s started at", value, "s"
 
-def random_function(model):
-    print "random function received event at ", ns.Simulator.Now().GetSeconds(), "s"
+def ExampleFunction(model):
+    print "ExampleFunction received event at", ns.Simulator.Now().GetSeconds(), "s"
     model.Start()
 
+def RandomFunction(model):
+    print "RandomFunction received event at", ns.Simulator.Now().GetSeconds(), "s"
+
+def CancelledEvent():
+    print "I should never be called... "
 
 def main(dummy_argv):
+
     model = MyModel()
-    ns.Simulator.Schedule(ns.Seconds(10.0), random_function, model)
+    v = ns.UniformVariable(10,20)
+
+    ns.Simulator.Schedule(ns.Seconds(10.0), ExampleFunction, model)
+
+    ns.Simulator.Schedule(ns.Seconds(v.GetValue()), RandomFunction, model)
+
+    id = ns.Simulator.Schedule(ns.Seconds(30.0), CancelledEvent)
+    ns.Simulator.Cancel(id)
+
     ns.Simulator.Run()
+
     ns.Simulator.Destroy()
 
 if __name__ == '__main__':
--- a/samples/wscript	Wed Dec 22 16:19:42 2010 -0800
+++ b/samples/wscript	Wed Dec 22 16:35:11 2010 -0800
@@ -11,8 +11,8 @@
     obj = bld.create_ns3_program('main-callback', ['core'])
     obj.source = 'main-callback.cc'
 
-    obj = bld.create_ns3_program('main-simulator', ['core'])
-    obj.source = 'main-simulator.cc'
+    obj = bld.create_ns3_program('sample-simulator', ['core'])
+    obj.source = 'sample-simulator.cc'
 
     obj = bld.create_ns3_program('main-ptr', ['core'] )
     obj.source = 'main-ptr.cc'