A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 import ns.core
24 
25 class MyModel(object):
26 
27  def Start(self):
28  ns.core.Simulator.Schedule(ns.core.Seconds(10.0), self.HandleEvent, ns.core.Simulator.Now().GetSeconds())
29 
30  def HandleEvent(self, value):
31  print "Member method received event at", ns.core.Simulator.Now().GetSeconds(), \
32  "s started at", value, "s"
33 
34 def ExampleFunction(model):
35  print "ExampleFunction received event at", ns.core.Simulator.Now().GetSeconds(), "s"
36  model.Start()
37 
38 def RandomFunction(model):
39  print "RandomFunction received event at", ns.core.Simulator.Now().GetSeconds(), "s"
40 
42  print "I should never be called... "
43 
44 def main(dummy_argv):
45 
46  model = MyModel()
47  v = ns.core.UniformRandomVariable()
48  v.SetAttribute("Min", ns.core.DoubleValue (10))
49  v.SetAttribute("Max", ns.core.DoubleValue (20))
50 
51  ns.core.Simulator.Schedule(ns.core.Seconds(10.0), ExampleFunction, model)
52 
53  ns.core.Simulator.Schedule(ns.core.Seconds(v.GetValue()), RandomFunction, model)
54 
55  id = ns.core.Simulator.Schedule(ns.core.Seconds(30.0), CancelledEvent)
56  ns.core.Simulator.Cancel(id)
57 
58  ns.core.Simulator.Run()
59 
60  ns.core.Simulator.Destroy()
61 
62 if __name__ == '__main__':
63  import sys
64  main(sys.argv)