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 """
41 #include "CPyCppyy/API.h"
42
43 using namespace ns3;
44 /** Simple model object to illustrate event handling. */
45 class MyModel
46 {
47 public:
48 /** Start model execution by scheduling a HandleEvent. */
49 void Start ();
50
51 private:
52 /**
53 * Simple event handler.
54 *
55 * \param [in] eventValue Event argument.
56 */
57 void HandleEvent (double eventValue);
58 };
59
60 void
61 MyModel::Start ()
62 {
63 Simulator::Schedule (Seconds (10.0),
64 &MyModel::HandleEvent,
65 this, Simulator::Now ().GetSeconds ());
66 }
67 void
68 MyModel::HandleEvent (double value)
69 {
70 std::cout << "Member method received event at "
71 << Simulator::Now ().GetSeconds ()
72 << "s started at " << value << "s" << std::endl;
73 }
74
75 void ExampleFunction(MyModel& model){
76 std::cout << "ExampleFunction received event at " << Simulator::Now().GetSeconds() << "s" << std::endl;
77 model.Start();
78 };
79
80 EventImpl* ExampleFunctionEvent(MyModel& model)
81 {
82 return MakeEvent(&ExampleFunction, model);
83 }
84
85 void RandomFunctionCpp(MyModel& model) {
86 CPyCppyy::Eval("RandomFunction()");
87 }
88
89 EventImpl* RandomFunctionEvent(MyModel& model)
90 {
91 return MakeEvent(&RandomFunctionCpp, model);
92 }
93
94 void CancelledFunctionCpp() {
95 CPyCppyy::Eval("CancelledEvent()");
96 }
97
98 EventImpl* CancelledFunctionEvent()
99 {
100 return MakeEvent(&CancelledFunctionCpp);
101 }
102 """
103)
104
105
106def main(argv):
107 cmd = ns.CommandLine(__file__)
108 cmd.Parse(argv)
109
110 model = ns.cppyy.gbl.MyModel()
111 v = ns.CreateObject[ns.UniformRandomVariable]()
112 v.SetAttribute("Min", ns.DoubleValue(10))
113 v.SetAttribute("Max", ns.DoubleValue(20))
114
115 ev = ns.cppyy.gbl.ExampleFunctionEvent(model)
116 ns.Simulator.Schedule(ns.Seconds(10.0), ev)
117
118 ev2 = ns.cppyy.gbl.RandomFunctionEvent(model)
119 ns.Simulator.Schedule(ns.Seconds(v.GetValue()), ev2)
120
121 ev3 = ns.cppyy.gbl.CancelledFunctionEvent()
122 id = ns.Simulator.Schedule(ns.Seconds(30.0), ev3)
123 ns.Simulator.Cancel(id)
124
125 ns.Simulator.Run()
126
127 ns.Simulator.Destroy()
128
129
130if __name__ == "__main__":
131 import sys
132
133 main(sys.argv)
RandomFunction()
Example function - triggered at a random time.
CancelledEvent()
Example function - triggered if an event is canceled (should not be called).