A Discrete-Event Network Simulator
API
sample-simulator.cc
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2010 INRIA
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19 */
20
21#include <iostream>
22#include "ns3/simulator.h"
23#include "ns3/nstime.h"
24#include "ns3/command-line.h"
25#include "ns3/double.h"
26#include "ns3/random-variable-stream.h"
27
35using namespace ns3;
36
37namespace {
38
41{
42public:
44 void Start (void);
45
46private:
52 void HandleEvent (double eventValue);
53};
54
55void
56MyModel::Start (void)
57{
58 Simulator::Schedule (Seconds (10.0),
59 &MyModel::HandleEvent,
60 this, Simulator::Now ().GetSeconds ());
61}
62void
63MyModel::HandleEvent (double value)
64{
65 std::cout << "Member method received event at "
67 << "s started at " << value << "s" << std::endl;
68}
69
75static void
77{
78 std::cout << "ExampleFunction received event at "
79 << Simulator::Now ().GetSeconds () << "s" << std::endl;
80 model->Start ();
81}
82
86static void
88{
89 std::cout << "RandomFunction received event at "
90 << Simulator::Now ().GetSeconds () << "s" << std::endl;
91}
92
94static void
96{
97 std::cout << "I should never be called... " << std::endl;
98}
99
100} // unnamed namespace
101
102
103int main (int argc, char *argv[])
104{
105 CommandLine cmd (__FILE__);
106 cmd.Parse (argc, argv);
107
108 MyModel model;
109 Ptr<UniformRandomVariable> v = CreateObject<UniformRandomVariable> ();
110 v->SetAttribute ("Min", DoubleValue (10));
111 v->SetAttribute ("Max", DoubleValue (20));
112
113 Simulator::Schedule (Seconds (10.0), &ExampleFunction, &model);
114
115 Simulator::Schedule (Seconds (v->GetValue ()), &RandomFunction);
116
117 EventId id = Simulator::Schedule (Seconds (30.0), &CancelledEvent);
118 Simulator::Cancel (id);
119
120 Simulator::Schedule (Seconds (25.0),
121 [] ()
122 {
123 std::cout << "Code within a lambda expression at time "
124 << Simulator::Now ().As (Time::S)
125 << std::endl;
126 });
127
128 Simulator::Run ();
129
130 Simulator::Destroy ();
131}
Simple model object to illustrate event handling.
void Start(void)
Start model execution by scheduling a HandleEvent.
Parse command-line arguments.
Definition: command-line.h:229
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:41
An identifier for simulation events.
Definition: event-id.h:54
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:256
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:379
TimeWithUnit As(const enum Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition: time.cc:432
double GetValue(double min, double max)
Get the next random value, as a double in the specified range .
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:287
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
static void RandomFunction(void)
Simple function event handler; this function is called randomly.
static void ExampleFunction(MyModel *model)
Simple function event handler which Starts a MyModel object.
static void CancelledEvent(void)
Simple function event handler; the corresponding event is cancelled.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
cmd
Definition: second.py:35