Skip to content

Commit 87b171a

Browse files
animeshsri14edalm
authored andcommitted
core:(fixes #1167) Add scheduled lambda example inside sample-simulator
1 parent fadbdfb commit 87b171a

File tree

3 files changed

+33
-20
lines changed

3 files changed

+33
-20
lines changed

src/core/examples/sample-simulator.cc

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,25 @@ class MyModel
3939
*
4040
* @param [in] eventValue Event argument.
4141
*/
42-
void HandleEvent(double eventValue);
42+
void HandleEvent(Time eventValue);
4343
};
4444

4545
void
4646
MyModel::Start()
4747
{
48-
Simulator::Schedule(Seconds(10), &MyModel::HandleEvent, this, Simulator::Now().GetSeconds());
48+
Simulator::Schedule(Seconds(10), &MyModel::HandleEvent, this, Simulator::Now());
49+
50+
Simulator::Schedule(Seconds(11), []() {
51+
std::cout << "Lambda scheduled from within a class method at "
52+
<< Simulator::Now().As(Time::S) << std::endl;
53+
});
4954
}
5055

5156
void
52-
MyModel::HandleEvent(double value)
57+
MyModel::HandleEvent(Time value)
5358
{
54-
std::cout << "Member method received event at " << Simulator::Now().GetSeconds()
55-
<< "s started at " << value << "s" << std::endl;
59+
std::cout << "Member method received event at " << Simulator::Now().As(Time::S)
60+
<< " started at " << value.As(Time::S) << std::endl;
5661
}
5762

5863
/**
@@ -63,8 +68,7 @@ MyModel::HandleEvent(double value)
6368
void
6469
ExampleFunction(MyModel* model)
6570
{
66-
std::cout << "ExampleFunction received event at " << Simulator::Now().GetSeconds() << "s"
67-
<< std::endl;
71+
std::cout << "ExampleFunction received event at " << Simulator::Now().As(Time::S) << std::endl;
6872
model->Start();
6973
}
7074

@@ -74,8 +78,7 @@ ExampleFunction(MyModel* model)
7478
void
7579
RandomFunction()
7680
{
77-
std::cout << "RandomFunction received event at " << Simulator::Now().GetSeconds() << "s"
78-
<< std::endl;
81+
std::cout << "RandomFunction received event at " << Simulator::Now().As(Time::S) << std::endl;
7982
}
8083

8184
/** Simple function event handler; the corresponding event is cancelled. */

src/core/examples/sample-simulator.py

100755100644
Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ def CancelledEvent():
3939
ns.cppyy.cppdef("""
4040
#include "CPyCppyy/API.h"
4141
42+
#include <iostream>
43+
4244
using namespace ns3;
4345
/** Simple model object to illustrate event handling. */
4446
class MyModel
@@ -51,28 +53,35 @@ class MyModel
5153
/**
5254
* Simple event handler.
5355
*
54-
* \param [in] eventValue Event argument.
56+
* @param [in] eventValue Event argument.
5557
*/
56-
void HandleEvent (double eventValue);
58+
void HandleEvent (Time eventValue);
5759
};
5860
5961
void
6062
MyModel::Start ()
6163
{
6264
Simulator::Schedule (Seconds (10.0),
6365
&MyModel::HandleEvent,
64-
this, Simulator::Now ().GetSeconds ());
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+
});
6573
}
74+
6675
void
67-
MyModel::HandleEvent (double value)
76+
MyModel::HandleEvent (Time value)
6877
{
6978
std::cout << "Member method received event at "
70-
<< Simulator::Now ().GetSeconds ()
71-
<< "s started at " << value << "s" << std::endl;
79+
<< Simulator::Now().As(Time::S)
80+
<< " started at " << value.As(Time::S) << std::endl;
7281
}
7382
7483
void ExampleFunction(MyModel& model){
75-
std::cout << "ExampleFunction received event at " << Simulator::Now().GetSeconds() << "s" << std::endl;
84+
std::cout << "ExampleFunction received event at " << Simulator::Now().As(Time::S) << std::endl;
7685
model.Start();
7786
};
7887
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
ExampleFunction received event at 10s
2-
RandomFunction received event at 18.1653s
3-
Member method received event at 20s started at 10s
4-
Code within a lambda expression at time +25s
1+
ExampleFunction received event at +10s
2+
RandomFunction received event at +18.1653s
3+
Member method received event at +20s started at +10s
4+
Lambda scheduled from within a class method at +21s
5+
Code within a lambda expression at time +25s

0 commit comments

Comments
 (0)