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 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  ns.core.CommandLine().Parse(dummy_argv)
52 
53  model = MyModel()
54  v = ns.core.UniformRandomVariable()
55  v.SetAttribute("Min", ns.core.DoubleValue (10))
56  v.SetAttribute("Max", ns.core.DoubleValue (20))
57 
58  ns.core.Simulator.Schedule(ns.core.Seconds(10.0), ExampleFunction, model)
59 
60  ns.core.Simulator.Schedule(ns.core.Seconds(v.GetValue()), RandomFunction, model)
61 
62  id = ns.core.Simulator.Schedule(ns.core.Seconds(30.0), CancelledEvent)
63  ns.core.Simulator.Cancel(id)
64 
65  ns.core.Simulator.Run()
66 
67  ns.core.Simulator.Destroy()
68 
69 if __name__ == '__main__':
70  import sys
71  main(sys.argv)
def RandomFunction(model)
def HandleEvent(self, value)
def ExampleFunction(model)