A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
sample-simulator.py
Go to the documentation of this file.
2# Copyright (c) 2010 INRIA
3#
4# SPDX-License-Identifier: GPL-2.0-only
5#
6# Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7#
8#
9# Python version of sample-simulator.cc
10
11## \file
12# \ingroup core-examples
13# \ingroup simulator
14# Python example program demonstrating use of various Schedule functions.
15
16## Import ns-3
17try:
18 from ns import ns
19except ModuleNotFoundError:
20 raise SystemExit(
21 "Error: ns3 Python module not found;"
22 " Python bindings may not be enabled"
23 " or your PYTHONPATH might not be properly configured"
24 )
25
26
27## Example function - triggered at a random time.
28## \return None.
30 print("RandomFunction received event at", ns.Simulator.Now().GetSeconds(), "s")
31
32
33## Example function - triggered if an event is canceled (should not be called).
34## \return None.
36 print("I should never be called... ")
37
38
39ns.cppyy.cppdef("""
40 #include "CPyCppyy/API.h"
41
42 using namespace ns3;
43 /** Simple model object to illustrate event handling. */
44 class MyModel
45 {
46 public:
47 /** Start model execution by scheduling a HandleEvent. */
48 void Start ();
49
50 private:
51 /**
52 * Simple event handler.
53 *
54 * \param [in] eventValue Event argument.
55 */
56 void HandleEvent (double eventValue);
57 };
58
59 void
60 MyModel::Start ()
61 {
62 Simulator::Schedule (Seconds (10.0),
63 &MyModel::HandleEvent,
64 this, Simulator::Now ().GetSeconds ());
65 }
66 void
67 MyModel::HandleEvent (double value)
68 {
69 std::cout << "Member method received event at "
70 << Simulator::Now ().GetSeconds ()
71 << "s started at " << value << "s" << std::endl;
72 }
73
74 void ExampleFunction(MyModel& model){
75 std::cout << "ExampleFunction received event at " << Simulator::Now().GetSeconds() << "s" << std::endl;
76 model.Start();
77 };
78
79 EventImpl* ExampleFunctionEvent(MyModel& model)
80 {
81 return MakeEvent(&ExampleFunction, model);
82 }
83
84 void RandomFunctionCpp(MyModel& model) {
85 CPyCppyy::Eval("RandomFunction()");
86 }
87
88 EventImpl* RandomFunctionEvent(MyModel& model)
89 {
90 return MakeEvent(&RandomFunctionCpp, model);
91 }
92
93 void CancelledFunctionCpp() {
94 CPyCppyy::Eval("CancelledEvent()");
95 }
96
97 EventImpl* CancelledFunctionEvent()
98 {
99 return MakeEvent(&CancelledFunctionCpp);
100 }
101 """)
102
103
104def main(argv):
105 cmd = ns.CommandLine(__file__)
106 cmd.Parse(argv)
107
108 model = ns.cppyy.gbl.MyModel()
109 v = ns.CreateObject[ns.UniformRandomVariable]()
110 v.SetAttribute("Min", ns.DoubleValue(10))
111 v.SetAttribute("Max", ns.DoubleValue(20))
112
113 ev = ns.cppyy.gbl.ExampleFunctionEvent(model)
114 ns.Simulator.Schedule(ns.Seconds(10), ev)
115
116 ev2 = ns.cppyy.gbl.RandomFunctionEvent(model)
117 ns.Simulator.Schedule(ns.Seconds(v.GetValue()), ev2)
118
119 ev3 = ns.cppyy.gbl.CancelledFunctionEvent()
120 id = ns.Simulator.Schedule(ns.Seconds(30), ev3)
121 ns.Simulator.Cancel(id)
122
123 ns.Simulator.Run()
124
125 ns.Simulator.Destroy()
126
127
128if __name__ == "__main__":
129 import sys
130
131 main(sys.argv)
RandomFunction()
Example function - triggered at a random time.
CancelledEvent()
Example function - triggered if an event is canceled (should not be called).