A Discrete-Event Network Simulator
API
sample-simulator.py
Go to the documentation of this file.
1 # -*- Mode:Python; -*-
2 # /*
3 # * Copyright (c) 2010 INRIA
4 # *
5 # * This program is free software; you can redistribute it and/or modify
6 # * it under the terms of the GNU General Public License version 2 as
7 # * published by the Free Software Foundation;
8 # *
9 # * This program is distributed in the hope that it will be useful,
10 # * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # * GNU General Public License for more details.
13 # *
14 # * You should have received a copy of the GNU General Public License
15 # * along with this program; if not, write to the Free Software
16 # * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 # *
18 # * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19 # */
20 #
21 # Python version of sample-simulator.cc
22 
23 
27 
28 
29 import ns.core
30 
31 class MyModel(object):
32  """Simple model object to illustrate event handling."""
33 
34 
35  def Start(self):
36  """Start model execution by scheduling a HandleEvent."""
37  ns.core.Simulator.Schedule(ns.core.Seconds(10.0), self.HandleEventHandleEvent, ns.core.Simulator.Now().GetSeconds())
38 
39 
42  def HandleEvent(self, value):
43  """Simple event handler."""
44  print ("Member method received event at", ns.core.Simulator.Now().GetSeconds(), \
45  "s started at", value, "s")
46 
47 
50 def ExampleFunction(model):
51  print ("ExampleFunction received event at", ns.core.Simulator.Now().GetSeconds(), "s")
52  model.Start()
53 
54 
57 def RandomFunction(model):
58  print ("RandomFunction received event at", ns.core.Simulator.Now().GetSeconds(), "s")
59 
60 
63  print ("I should never be called... ")
64 
65 def main(dummy_argv):
66  ns.core.CommandLine().Parse(dummy_argv)
67 
68  model = MyModel()
69  v = ns.core.UniformRandomVariable()
70  v.SetAttribute("Min", ns.core.DoubleValue (10))
71  v.SetAttribute("Max", ns.core.DoubleValue (20))
72 
73  ns.core.Simulator.Schedule(ns.core.Seconds(10.0), ExampleFunction, model)
74 
75  ns.core.Simulator.Schedule(ns.core.Seconds(v.GetValue()), RandomFunction, model)
76 
77  id = ns.core.Simulator.Schedule(ns.core.Seconds(30.0), CancelledEvent)
78  ns.core.Simulator.Cancel(id)
79 
80  ns.core.Simulator.Run()
81 
82  ns.core.Simulator.Destroy()
83 
84 if __name__ == '__main__':
85  import sys
86  main(sys.argv)
def HandleEvent(self, value)
def CancelledEvent()
Example function - triggered if an event is canceled (should not be called).
def RandomFunction(model)
Example function - triggered at a random time.
def ExampleFunction(model)
Example function - starts MyModel.