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
# -*- Mode:Python; -*-
2
# /*
3
# * Copyright (c) 2010 INRIA
4
# *
5
# * This program is free software; you can redistribute it and/or modify
6
# * it under the terms of the GNU General Public License version 2 as
7
# * published by the Free Software Foundation;
8
# *
9
# * This program is distributed in the hope that it will be useful,
10
# * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# * GNU General Public License for more details.
13
# *
14
# * You should have received a copy of the GNU General Public License
15
# * along with this program; if not, write to the Free Software
16
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
# *
18
# * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19
# */
20
#
21
# Python version of sample-simulator.cc
22
23
27
28
29
from
ns
import
ns
30
31
33
def
RandomFunction
():
34
print
(
"RandomFunction received event at"
, ns.core.Simulator.Now().GetSeconds(),
"s"
)
35
36
38
def
CancelledEvent
():
39
print
(
"I should never be called... "
)
40
41
ns.cppyy.cppdef(
"""
42
#include "CPyCppyy/API.h"
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 (double eventValue);
59
};
60
61
void
62
MyModel::Start ()
63
{
64
Simulator::Schedule (Seconds (10.0),
65
&MyModel::HandleEvent,
66
this, Simulator::Now ().GetSeconds ());
67
}
68
void
69
MyModel::HandleEvent (double value)
70
{
71
std::cout << "Member method received event at "
72
<< Simulator::Now ().GetSeconds ()
73
<< "s started at " << value << "s" << std::endl;
74
}
75
76
void ExampleFunction(MyModel& model){
77
std::cout << "ExampleFunction received event at " << Simulator::Now().GetSeconds() << "s" << std::endl;
78
model.Start();
79
};
80
81
EventImpl* ExampleFunctionEvent(MyModel& model)
82
{
83
return MakeEvent(&ExampleFunction, model);
84
}
85
86
void RandomFunctionCpp(MyModel& model) {
87
CPyCppyy::Eval("RandomFunction()");
88
}
89
90
EventImpl* RandomFunctionEvent(MyModel& model)
91
{
92
return MakeEvent(&RandomFunctionCpp, model);
93
}
94
95
void CancelledFunctionCpp() {
96
CPyCppyy::Eval("CancelledEvent()");
97
}
98
99
EventImpl* CancelledFunctionEvent()
100
{
101
return MakeEvent(&CancelledFunctionCpp);
102
}
103
"""
)
104
105
106
def
main(argv):
107
cmd = ns.CommandLine(__file__)
108
cmd.Parse(argv)
109
110
model = ns.cppyy.gbl.MyModel()
111
v = ns.CreateObject(
"UniformRandomVariable"
)
112
v.SetAttribute(
"Min"
, ns.core.DoubleValue(10))
113
v.SetAttribute(
"Max"
, ns.core.DoubleValue(20))
114
115
ev = ns.cppyy.gbl.ExampleFunctionEvent(model)
116
ns.core.Simulator.Schedule(ns.core.Seconds(10.0), ev)
117
118
ev2 = ns.cppyy.gbl.RandomFunctionEvent(model)
119
ns.core.Simulator.Schedule(ns.core.Seconds(v.GetValue()), ev2)
120
121
ev3 = ns.cppyy.gbl.CancelledFunctionEvent()
122
id = ns.core.Simulator.Schedule(ns.core.Seconds(30.0), ev3)
123
ns.core.Simulator.Cancel(id)
124
125
ns.core.Simulator.Run()
126
127
ns.core.Simulator.Destroy()
128
129
if
__name__ ==
'__main__'
:
130
import
sys
131
main(sys.argv)
sample-simulator.CancelledEvent
def CancelledEvent()
Example function - triggered if an event is canceled (should not be called).
Definition:
sample-simulator.py:38
sample-simulator.RandomFunction
def RandomFunction()
Example function - triggered at a random time.
Definition:
sample-simulator.py:33
src
core
examples
sample-simulator.py
Generated on Fri Mar 31 2023 13:30:39 for ns-3 by
1.9.1