30except ModuleNotFoundError:
32 "Error: ns3 Python module not found;"
33 " Python bindings may not be enabled"
34 " or your PYTHONPATH might not be properly configured"
41 print(
"RandomFunction received event at", ns.Simulator.Now().GetSeconds(),
"s")
47 print(
"I should never be called... ")
55 /** Simple model object to illustrate event handling. */
59 /** Start model execution by scheduling a HandleEvent. */
64 * Simple event handler.
66 * \param [
in] eventValue Event argument.
68 void HandleEvent (double eventValue);
74 Simulator::Schedule (Seconds (10.0),
75 &MyModel::HandleEvent,
76 this, Simulator::Now ().GetSeconds ());
79 MyModel::HandleEvent (double value)
81 std::cout <<
"Member method received event at "
82 << Simulator::Now ().GetSeconds ()
83 <<
"s started at " << value <<
"s" << std::endl;
86 void ExampleFunction(MyModel& model){
87 std::cout <<
"ExampleFunction received event at " << Simulator::Now().GetSeconds() <<
"s" << std::endl;
91 EventImpl* ExampleFunctionEvent(MyModel& model)
93 return MakeEvent(&ExampleFunction, model);
96 void RandomFunctionCpp(MyModel& model) {
97 CPyCppyy::Eval(
"RandomFunction()");
100 EventImpl* RandomFunctionEvent(MyModel& model)
102 return MakeEvent(&RandomFunctionCpp, model);
105 void CancelledFunctionCpp() {
106 CPyCppyy::Eval(
"CancelledEvent()");
109 EventImpl* CancelledFunctionEvent()
111 return MakeEvent(&CancelledFunctionCpp);
118 cmd = ns.CommandLine(__file__)
121 model = ns.cppyy.gbl.MyModel()
122 v = ns.CreateObject[ns.UniformRandomVariable]()
123 v.SetAttribute("Min", ns.DoubleValue(10))
124 v.SetAttribute(
"Max", ns.DoubleValue(20))
126 ev = ns.cppyy.gbl.ExampleFunctionEvent(model)
127 ns.Simulator.Schedule(ns.Seconds(10.0), ev)
129 ev2 = ns.cppyy.gbl.RandomFunctionEvent(model)
130 ns.Simulator.Schedule(ns.Seconds(v.GetValue()), ev2)
132 ev3 = ns.cppyy.gbl.CancelledFunctionEvent()
133 id = ns.Simulator.Schedule(ns.Seconds(30.0), ev3)
134 ns.Simulator.Cancel(id)
138 ns.Simulator.Destroy()
141if __name__ ==
"__main__":
def CancelledEvent()
Example function - triggered if an event is canceled (should not be called).
def RandomFunction()
Example function - triggered at a random time.