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 * 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#include "ns3/command-line.h"
21#include "ns3/double.h"
22#include "ns3/nstime.h"
23#include "ns3/random-variable-stream.h"
24#include "ns3/simulator.h"
25
26#include <iostream>
27
28/**
29 * \file
30 * \ingroup core-examples
31 * \ingroup simulator
32 * Example program demonstrating use of various Schedule functions.
33 */
34
35using namespace ns3;
36
37namespace
38{
39
40/** Simple model object to illustrate event handling. */
42{
43 public:
44 /** Start model execution by scheduling a HandleEvent. */
45 void Start();
46
47 private:
48 /**
49 * Simple event handler.
50 *
51 * \param [in] eventValue Event argument.
52 */
53 void HandleEvent(double eventValue);
54};
55
56void
58{
60}
61
62void
64{
65 std::cout << "Member method received event at " << Simulator::Now().GetSeconds()
66 << "s started at " << value << "s" << std::endl;
67}
68
69/**
70 * Simple function event handler which Starts a MyModel object.
71 *
72 * \param [in] model The MyModel object to start.
73 */
74void
76{
77 std::cout << "ExampleFunction received event at " << Simulator::Now().GetSeconds() << "s"
78 << std::endl;
79 model->Start();
80}
81
82/**
83 * Simple function event handler; this function is called randomly.
84 */
85void
87{
88 std::cout << "RandomFunction received event at " << Simulator::Now().GetSeconds() << "s"
89 << std::endl;
90}
91
92/** Simple function event handler; the corresponding event is cancelled. */
93void
95{
96 std::cout << "I should never be called... " << std::endl;
97}
98
99} // unnamed namespace
100
101int
102main(int argc, char* argv[])
103{
104 CommandLine cmd(__FILE__);
105 cmd.Parse(argc, argv);
106
107 MyModel model;
108 Ptr<UniformRandomVariable> v = CreateObject<UniformRandomVariable>();
109 v->SetAttribute("Min", DoubleValue(10));
110 v->SetAttribute("Max", DoubleValue(20));
111
113
115
118
119 Simulator::Schedule(Seconds(25.0), []() {
120 std::cout << "Code within a lambda expression at time " << Simulator::Now().As(Time::S)
121 << std::endl;
122 });
123
125
127
128 return 0;
129}
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.
Definition: command-line.h:232
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
An identifier for simulation events.
Definition: event-id.h:55
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
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:285
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
static void Run()
Run the simulation.
Definition: simulator.cc:178
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition: time.cc:415
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:403
@ S
second
Definition: nstime.h:116
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
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.
ns cmd
Definition: second.py:40