A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
API
sample-simulator.py
Go to the documentation of this file.
1
#
2
# Copyright (c) 2010 INRIA
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License version 2 as
6
# published by the Free Software Foundation;
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
#
17
# Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18
#
19
#
20
# Python version of sample-simulator.cc
21
22
26
27
28
try
:
29
from
ns
import
ns
30
except
ModuleNotFoundError:
31
raise
SystemExit(
32
"Error: ns3 Python module not found;"
33
" Python bindings may not be enabled"
34
" or your PYTHONPATH might not be properly configured"
35
)
36
37
38
40
def
RandomFunction
():
41
print(
"RandomFunction received event at"
, ns.core.Simulator.Now().GetSeconds(),
"s"
)
42
43
44
46
def
CancelledEvent
():
47
print(
"I should never be called... "
)
48
49
50
ns.cppyy.cppdef(
51
"""
52
#include "CPyCppyy/API.h"
53
54
using namespace ns3;
55
/** Simple model object to illustrate event handling. */
56
class MyModel
57
{
58
public:
59
/** Start model execution by scheduling a HandleEvent. */
60
void Start ();
61
62
private:
63
/**
64
* Simple event handler.
65
*
66
* \param [in] eventValue Event argument.
67
*/
68
void HandleEvent (double eventValue);
69
};
70
71
void
72
MyModel::Start ()
73
{
74
Simulator::Schedule (Seconds (10.0),
75
&MyModel::HandleEvent,
76
this, Simulator::Now ().GetSeconds ());
77
}
78
void
79
MyModel::HandleEvent (double value)
80
{
81
std::cout << "Member method received event at "
82
<< Simulator::Now ().GetSeconds ()
83
<< "s started at " << value << "s" << std::endl;
84
}
85
86
void ExampleFunction(MyModel& model){
87
std::cout << "ExampleFunction received event at " << Simulator::Now().GetSeconds() << "s" << std::endl;
88
model.Start();
89
};
90
91
EventImpl* ExampleFunctionEvent(MyModel& model)
92
{
93
return MakeEvent(&ExampleFunction, model);
94
}
95
96
void RandomFunctionCpp(MyModel& model) {
97
CPyCppyy::Eval("RandomFunction()");
98
}
99
100
EventImpl* RandomFunctionEvent(MyModel& model)
101
{
102
return MakeEvent(&RandomFunctionCpp, model);
103
}
104
105
void CancelledFunctionCpp() {
106
CPyCppyy::Eval("CancelledEvent()");
107
}
108
109
EventImpl* CancelledFunctionEvent()
110
{
111
return MakeEvent(&CancelledFunctionCpp);
112
}
113
"""
114
)
115
116
117
def
main(argv):
118
cmd = ns.CommandLine(__file__)
119
cmd.Parse(argv)
120
121
model = ns.cppyy.gbl.MyModel()
122
v = ns.CreateObject(
"UniformRandomVariable"
)
123
v.SetAttribute(
"Min"
, ns.core.DoubleValue(10))
124
v.SetAttribute(
"Max"
, ns.core.DoubleValue(20))
125
126
ev = ns.cppyy.gbl.ExampleFunctionEvent(model)
127
ns.core.Simulator.Schedule(ns.core.Seconds(10.0), ev)
128
129
ev2 = ns.cppyy.gbl.RandomFunctionEvent(model)
130
ns.core.Simulator.Schedule(ns.core.Seconds(v.GetValue()), ev2)
131
132
ev3 = ns.cppyy.gbl.CancelledFunctionEvent()
133
id = ns.core.Simulator.Schedule(ns.core.Seconds(30.0), ev3)
134
ns.core.Simulator.Cancel(id)
135
136
ns.core.Simulator.Run()
137
138
ns.core.Simulator.Destroy()
139
140
141
if
__name__ ==
"__main__"
:
142
import
sys
143
144
main(sys.argv)
sample-simulator.CancelledEvent
def CancelledEvent()
Example function - triggered if an event is canceled (should not be called).
Definition:
sample-simulator.py:46
sample-simulator.RandomFunction
def RandomFunction()
Example function - triggered at a random time.
Definition:
sample-simulator.py:40
src
core
examples
sample-simulator.py
Generated on Sun Mar 3 2024 17:10:55 for ns-3 by
1.9.1