A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
API
Loading...
Searching...
No Matches
sample-simulator.py
Go to the documentation of this file.
1
#
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
17
try
:
18
from
ns
import
ns
19
except
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.
29
def
RandomFunction
():
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.
35
def
CancelledEvent
():
36
print(
"I should never be called... "
)
37
38
39
ns.cppyy.cppdef(
"""
40
#include "CPyCppyy/API.h"
41
42
#include <iostream>
43
44
using namespace ns3;
45
/** Simple model object to illustrate event handling. */
46
class MyModel
47
{
48
public:
49
/** Start model execution by scheduling a HandleEvent. */
50
void Start ();
51
52
private:
53
/**
54
* Simple event handler.
55
*
56
* @param [in] eventValue Event argument.
57
*/
58
void HandleEvent (Time eventValue);
59
};
60
61
void
62
MyModel::Start ()
63
{
64
Simulator::Schedule (Seconds (10.0),
65
&MyModel::HandleEvent,
66
this, Simulator::Now());
67
68
Simulator::Schedule (Seconds (11.0), []() {
69
std::cout << "Lambda scheduled from within a class method at "
70
<< Simulator::Now().As(Time::S)
71
<< std::endl;
72
});
73
}
74
75
void
76
MyModel::HandleEvent (Time value)
77
{
78
std::cout << "Member method received event at "
79
<< Simulator::Now().As(Time::S)
80
<< " started at " << value.As(Time::S) << std::endl;
81
}
82
83
void ExampleFunction(MyModel& model){
84
std::cout << "ExampleFunction received event at " << Simulator::Now().As(Time::S) << std::endl;
85
model.Start();
86
};
87
88
EventImpl* ExampleFunctionEvent(MyModel& model)
89
{
90
return MakeEvent(&ExampleFunction, model);
91
}
92
93
void RandomFunctionCpp(MyModel& model) {
94
CPyCppyy::Eval("RandomFunction()");
95
}
96
97
EventImpl* RandomFunctionEvent(MyModel& model)
98
{
99
return MakeEvent(&RandomFunctionCpp, model);
100
}
101
102
void CancelledFunctionCpp() {
103
CPyCppyy::Eval("CancelledEvent()");
104
}
105
106
EventImpl* CancelledFunctionEvent()
107
{
108
return MakeEvent(&CancelledFunctionCpp);
109
}
110
"""
)
111
112
113
def
main(argv):
114
cmd = ns.CommandLine(__file__)
115
cmd.Parse(argv)
116
117
model = ns.cppyy.gbl.MyModel()
118
v = ns.CreateObject[ns.UniformRandomVariable]()
119
v.SetAttribute(
"Min"
, ns.DoubleValue(10))
120
v.SetAttribute(
"Max"
, ns.DoubleValue(20))
121
122
ev = ns.cppyy.gbl.ExampleFunctionEvent(model)
123
ns.Simulator.Schedule(ns.Seconds(10), ev)
124
125
ev2 = ns.cppyy.gbl.RandomFunctionEvent(model)
126
ns.Simulator.Schedule(ns.Seconds(v.GetValue()), ev2)
127
128
ev3 = ns.cppyy.gbl.CancelledFunctionEvent()
129
id = ns.Simulator.Schedule(ns.Seconds(30), ev3)
130
ns.Simulator.Cancel(id)
131
132
ns.Simulator.Run()
133
134
ns.Simulator.Destroy()
135
136
137
if
__name__ ==
"__main__"
:
138
import
sys
139
140
main(sys.argv)
sample-simulator.RandomFunction
RandomFunction()
Example function - triggered at a random time.
Definition
sample-simulator.py:29
sample-simulator.CancelledEvent
CancelledEvent()
Example function - triggered if an event is canceled (should not be called).
Definition
sample-simulator.py:35
src
core
examples
sample-simulator.py
Generated on
for ns-3 by
1.15.0