A Discrete-Event Network Simulator
API
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
35using namespace ns3;
36
37namespace
38{
39
42{
43 public:
45 void Start();
46
47 private:
53 void HandleEvent(double eventValue);
54};
55
56void
57MyModel::Start()
58{
59 Simulator::Schedule(Seconds(10.0), &MyModel::HandleEvent, this, Simulator::Now().GetSeconds());
60}
61
62void
63MyModel::HandleEvent(double value)
64{
65 std::cout << "Member method received event at " << Simulator::Now().GetSeconds()
66 << "s started at " << value << "s" << std::endl;
67}
68
74void
76{
77 std::cout << "ExampleFunction received event at " << Simulator::Now().GetSeconds() << "s"
78 << std::endl;
79 model->Start();
80}
81
85void
87{
88 std::cout << "RandomFunction received event at " << Simulator::Now().GetSeconds() << "s"
89 << std::endl;
90}
91
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
112 Simulator::Schedule(Seconds(10.0), &ExampleFunction, &model);
113
114 Simulator::Schedule(Seconds(v->GetValue()), &RandomFunction);
115
116 EventId id = Simulator::Schedule(Seconds(30.0), &CancelledEvent);
117 Simulator::Cancel(id);
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
124 Simulator::Run();
125
126 Simulator::Destroy();
127
128 return 0;
129}
Simple model object to illustrate event handling.
void Start()
Start model execution by scheduling a HandleEvent.
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
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:200
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition: time.cc:417
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:402
double GetValue(double min, double max)
Get the next random value drawn from the distribution.
Time Now()
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:296
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1336
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.
value
Definition: second.py:41
cmd
Definition: second.py:33