A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
sample-simulator.cc
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#include "ns3/command-line.h"
10#include "ns3/double.h"
11#include "ns3/nstime.h"
12#include "ns3/random-variable-stream.h"
13#include "ns3/simulator.h"
14
15#include <iostream>
16
17/**
18 * @file
19 * @ingroup core-examples
20 * @ingroup simulator
21 * Example program demonstrating use of various Schedule functions.
22 */
23
24using namespace ns3;
25
26namespace
27{
28
29/** Simple model object to illustrate event handling. */
31{
32 public:
33 /** Start model execution by scheduling a HandleEvent. */
34 void Start();
35
36 private:
37 /**
38 * Simple event handler.
39 *
40 * @param [in] eventValue Event argument.
41 */
42 void HandleEvent(Time eventValue);
43};
44
45void
47{
49
51 std::cout << "Lambda scheduled from within a class method at "
52 << Simulator::Now().As(Time::S) << std::endl;
53 });
54}
55
56void
58{
59 std::cout << "Member method received event at " << Simulator::Now().As(Time::S)
60 << " started at " << value.As(Time::S) << std::endl;
61}
62
63/**
64 * Simple function event handler which Starts a MyModel object.
65 *
66 * @param [in] model The MyModel object to start.
67 */
68void
70{
71 std::cout << "ExampleFunction received event at " << Simulator::Now().As(Time::S) << std::endl;
72 model->Start();
73}
74
75/**
76 * Simple function event handler; this function is called randomly.
77 */
78void
80{
81 std::cout << "RandomFunction received event at " << Simulator::Now().As(Time::S) << std::endl;
82}
83
84/** Simple function event handler; the corresponding event is cancelled. */
85void
87{
88 std::cout << "I should never be called... " << std::endl;
89}
90
91} // unnamed namespace
92
93int
94main(int argc, char* argv[])
95{
96 CommandLine cmd(__FILE__);
97 cmd.Parse(argc, argv);
98
99 MyModel model;
101 v->SetAttribute("Min", DoubleValue(10));
102 v->SetAttribute("Max", DoubleValue(20));
103
105
107
110
111 Simulator::Schedule(Seconds(25), []() {
112 std::cout << "Code within a lambda expression at time " << Simulator::Now().As(Time::S)
113 << std::endl;
114 });
115
117
119
120 return 0;
121}
uint32_t v
Simple model object to illustrate event handling.
void Start()
Start model execution by scheduling a HandleEvent.
void HandleEvent(Time eventValue)
Simple event handler.
Parse command-line arguments.
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
An identifier for simulation events.
Definition event-id.h:45
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:580
static void Cancel(const EventId &id)
Set the cancel bit on this event: the event's associated function will not be invoked when it expires...
Definition simulator.cc:268
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:125
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:191
static void Run()
Run the simulation.
Definition simulator.cc:161
Simulation virtual time values and global simulation resolution.
Definition nstime.h:95
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition time.cc:408
@ S
second
Definition nstime.h:106
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:627
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1273
void CancelledEvent()
Simple function event handler; the corresponding event is cancelled.
void ExampleFunction(MyModel *model)
Simple function event handler which Starts a MyModel object.
void RandomFunction()
Simple function event handler; this function is called randomly.
Every class exported by the ns3 library is enclosed in the ns3 namespace.