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  """Simple model object to illustrate event handling."""
27 
28  ## \returns None.
29  def Start(self):
30  """Start model execution by scheduling a HandleEvent."""
31  ns.core.Simulator.Schedule(ns.core.Seconds(10.0), self.HandleEvent, ns.core.Simulator.Now().GetSeconds())
32 
33  ## \param [in] value Event argument.
34  ## \return None.
35  def HandleEvent(self, value):
36  """Simple event handler."""
37  print "Member method received event at", ns.core.Simulator.Now().GetSeconds(), \
38  "s started at", value, "s"
39 
40 def ExampleFunction(model):
41  print "ExampleFunction received event at", ns.core.Simulator.Now().GetSeconds(), "s"
42  model.Start()
43 
44 def RandomFunction(model):
45  print "RandomFunction received event at", ns.core.Simulator.Now().GetSeconds(), "s"
46 
48  print "I should never be called... "
49 
50 def main(dummy_argv):
51 
52  model = MyModel()
53  v = ns.core.UniformRandomVariable()
54  v.SetAttribute("Min", ns.core.DoubleValue (10))
55  v.SetAttribute("Max", ns.core.DoubleValue (20))
56 
57  ns.core.Simulator.Schedule(ns.core.Seconds(10.0), ExampleFunction, model)
58 
59  ns.core.Simulator.Schedule(ns.core.Seconds(v.GetValue()), RandomFunction, model)
60 
61  id = ns.core.Simulator.Schedule(ns.core.Seconds(30.0), CancelledEvent)
62  ns.core.Simulator.Cancel(id)
63 
64  ns.core.Simulator.Run()
65 
66  ns.core.Simulator.Destroy()
67 
68 if __name__ == '__main__':
69  import sys
70  main(sys.argv)