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(double eventValue);
43};
44
45void
50
51void
53{
54 std::cout << "Member method received event at " << Simulator::Now().GetSeconds()
55 << "s started at " << value << "s" << std::endl;
56}
57
58/**
59 * Simple function event handler which Starts a MyModel object.
60 *
61 * \param [in] model The MyModel object to start.
62 */
63void
65{
66 std::cout << "ExampleFunction received event at " << Simulator::Now().GetSeconds() << "s"
67 << std::endl;
68 model->Start();
69}
70
71/**
72 * Simple function event handler; this function is called randomly.
73 */
74void
76{
77 std::cout << "RandomFunction received event at " << Simulator::Now().GetSeconds() << "s"
78 << std::endl;
79}
80
81/** Simple function event handler; the corresponding event is cancelled. */
82void
84{
85 std::cout << "I should never be called... " << std::endl;
86}
87
88} // unnamed namespace
89
90int
91main(int argc, char* argv[])
92{
93 CommandLine cmd(__FILE__);
94 cmd.Parse(argc, argv);
95
96 MyModel model;
98 v->SetAttribute("Min", DoubleValue(10));
99 v->SetAttribute("Max", DoubleValue(20));
100
102
104
107
108 Simulator::Schedule(Seconds(25.0), []() {
109 std::cout << "Code within a lambda expression at time " << Simulator::Now().As(Time::S)
110 << std::endl;
111 });
112
114
116
117 return 0;
118}
Simple model object to illustrate event handling.
void Start()
Start model execution by scheduling a HandleEvent.
void HandleEvent(double 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.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
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:274
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
static void Run()
Run the simulation.
Definition simulator.cc:167
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition time.cc:404
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:392
@ S
second
Definition nstime.h:105
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
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.