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 
35 using namespace ns3;
36 
37 namespace
38 {
39 
41 class MyModel
42 {
43  public:
45  void Start();
46 
47  private:
53  void HandleEvent(double eventValue);
54 };
55 
56 void
57 MyModel::Start()
58 {
59  Simulator::Schedule(Seconds(10.0), &MyModel::HandleEvent, this, Simulator::Now().GetSeconds());
60 }
61 
62 void
63 MyModel::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 
74 void
76 {
77  std::cout << "ExampleFunction received event at " << Simulator::Now().GetSeconds() << "s"
78  << std::endl;
79  model->Start();
80 }
81 
85 void
87 {
88  std::cout << "RandomFunction received event at " << Simulator::Now().GetSeconds() << "s"
89  << std::endl;
90 }
91 
93 void
95 {
96  std::cout << "I should never be called... " << std::endl;
97 }
98 
99 } // unnamed namespace
100 
101 int
102 main(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 
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 
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:204
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
double GetValue(double min, double max)
Get the next random value drawn from the distribution.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
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.
cmd
Definition: second.py:40