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.HandleEvent, 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 def ExampleFunction(model):
48  print ("ExampleFunction received event at", ns.core.Simulator.Now().GetSeconds(), "s")
49  model.Start()
50 
51 def RandomFunction(model):
52  print ("RandomFunction received event at", ns.core.Simulator.Now().GetSeconds(), "s")
53 
55  print ("I should never be called... ")
56 
57 def main(dummy_argv):
58  ns.core.CommandLine().Parse(dummy_argv)
59 
60  model = MyModel()
61  v = ns.core.UniformRandomVariable()
62  v.SetAttribute("Min", ns.core.DoubleValue (10))
63  v.SetAttribute("Max", ns.core.DoubleValue (20))
64 
65  ns.core.Simulator.Schedule(ns.core.Seconds(10.0), ExampleFunction, model)
66 
67  ns.core.Simulator.Schedule(ns.core.Seconds(v.GetValue()), RandomFunction, model)
68 
69  id = ns.core.Simulator.Schedule(ns.core.Seconds(30.0), CancelledEvent)
70  ns.core.Simulator.Cancel(id)
71 
72  ns.core.Simulator.Run()
73 
74  ns.core.Simulator.Destroy()
75 
76 if __name__ == '__main__':
77  import sys
78  main(sys.argv)
def RandomFunction(model)
def HandleEvent(self, value)
def ExampleFunction(model)