src/core/examples/sample-simulator.cc
author Tom Henderson <tomh@tomh.org>
Mon, 28 Sep 2015 20:27:25 -0700
changeset 11676 05ea1489e509
parent 11531 5d6b3f94ebf3
permissions -rw-r--r--
bug 2184: Integer overflow in MacLow
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7385
10beb0e53130 standardize emacs c++ mode comments
Vedran Miletić <rivanvx@gmail.com>
parents: 7169
diff changeset
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
6721
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
     2
/*
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
     3
 * Copyright (c) 2010 INRIA
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
     4
 *
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
     6
 * it under the terms of the GNU General Public License version 2 as
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
     7
 * published by the Free Software Foundation;
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
     8
 *
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    12
 * GNU General Public License for more details.
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    13
 *
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    14
 * You should have received a copy of the GNU General Public License
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    15
 * along with this program; if not, write to the Free Software
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    17
 *
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    18
 * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    19
 */
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    20
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    21
#include <iostream>
14
6dd7d31c6fc3 replace yans include with ns3 include
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 12
diff changeset
    22
#include "ns3/simulator.h"
108
6fd2357377ed rename time.h to nstime.h
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 54
diff changeset
    23
#include "ns3/nstime.h"
4309
bf8c338c7820 fix typo
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 163
diff changeset
    24
#include "ns3/command-line.h"
8996
d0e0a162a990 Replace various instances of RandomVariable with RandomVariableStream
Mitch Watrous
parents: 7385
diff changeset
    25
#include "ns3/double.h"
d0e0a162a990 Replace various instances of RandomVariable with RandomVariableStream
Mitch Watrous
parents: 7385
diff changeset
    26
#include "ns3/random-variable-stream.h"
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    27
11140
35c2c969d0b2 [Doxygen] Various in src/core
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 8996
diff changeset
    28
/**
35c2c969d0b2 [Doxygen] Various in src/core
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 8996
diff changeset
    29
 * \file
35c2c969d0b2 [Doxygen] Various in src/core
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 8996
diff changeset
    30
 * \ingroup simulator
35c2c969d0b2 [Doxygen] Various in src/core
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 8996
diff changeset
    31
 * Example program demonstrating use of various Schedule functions.
35c2c969d0b2 [Doxygen] Various in src/core
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 8996
diff changeset
    32
 */
35c2c969d0b2 [Doxygen] Various in src/core
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 8996
diff changeset
    33
16
99e833adbb46 change yans namespace to ns3
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 14
diff changeset
    34
using namespace ns3;
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    35
11531
5d6b3f94ebf3 [Doxygen] Remaining in core
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 11140
diff changeset
    36
/** Simple model object to illustrate event handling. */
6721
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    37
class MyModel
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    38
{
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    39
public:
11531
5d6b3f94ebf3 [Doxygen] Remaining in core
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 11140
diff changeset
    40
  /** Start model execution by scheduling a HandleEvent. */
150
663120712cd9 fix coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 143
diff changeset
    41
  void Start (void);
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    42
private:
11531
5d6b3f94ebf3 [Doxygen] Remaining in core
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 11140
diff changeset
    43
  /**
5d6b3f94ebf3 [Doxygen] Remaining in core
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 11140
diff changeset
    44
   *  Simple event handler.
5d6b3f94ebf3 [Doxygen] Remaining in core
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 11140
diff changeset
    45
   *
5d6b3f94ebf3 [Doxygen] Remaining in core
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 11140
diff changeset
    46
   * \param [in] eventValue Event argument.
5d6b3f94ebf3 [Doxygen] Remaining in core
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 11140
diff changeset
    47
   */
6721
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    48
  void HandleEvent (double eventValue);
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    49
};
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    50
6721
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    51
void
122
6b8f1eda5c57 fix coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 110
diff changeset
    52
MyModel::Start (void)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    53
{
6721
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    54
  Simulator::Schedule (Seconds (10.0),
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    55
                       &MyModel::HandleEvent,
163
2a7e05018eeb rename Time::ApproximateTo methods to Time::Get
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 162
diff changeset
    56
                       this, Simulator::Now ().GetSeconds ());
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    57
}
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    58
void
6721
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    59
MyModel::HandleEvent (double value)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    60
{
6721
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    61
  std::cout << "Member method received event at "
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    62
            << Simulator::Now ().GetSeconds ()
150
663120712cd9 fix coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 143
diff changeset
    63
            << "s started at " << value << "s" << std::endl;
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    64
}
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    65
11531
5d6b3f94ebf3 [Doxygen] Remaining in core
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 11140
diff changeset
    66
/**
5d6b3f94ebf3 [Doxygen] Remaining in core
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 11140
diff changeset
    67
 * Simple function event handler which Starts a MyModel object.
5d6b3f94ebf3 [Doxygen] Remaining in core
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 11140
diff changeset
    68
 *
5d6b3f94ebf3 [Doxygen] Remaining in core
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 11140
diff changeset
    69
 * \param [in] model The MyModel object to start.
5d6b3f94ebf3 [Doxygen] Remaining in core
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 11140
diff changeset
    70
 */
6721
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    71
static void
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    72
ExampleFunction (MyModel *model)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    73
{
6721
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    74
  std::cout << "ExampleFunction received event at "
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    75
            << Simulator::Now ().GetSeconds () << "s" << std::endl;
150
663120712cd9 fix coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 143
diff changeset
    76
  model->Start ();
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    77
}
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    78
11531
5d6b3f94ebf3 [Doxygen] Remaining in core
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 11140
diff changeset
    79
/**
5d6b3f94ebf3 [Doxygen] Remaining in core
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 11140
diff changeset
    80
 * Simple function event handler; this function is called randomly.
5d6b3f94ebf3 [Doxygen] Remaining in core
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 11140
diff changeset
    81
 */
6721
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    82
static void
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    83
RandomFunction (void)
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    84
{
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    85
  std::cout << "RandomFunction received event at "
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    86
            << Simulator::Now ().GetSeconds () << "s" << std::endl;
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    87
}
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    88
11531
5d6b3f94ebf3 [Doxygen] Remaining in core
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 11140
diff changeset
    89
/** Simple function event handler; the corresponding event is cancelled. */
6721
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    90
static void
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    91
CancelledEvent (void)
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    92
{
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    93
  std::cout << "I should never be called... " << std::endl;
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    94
}
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    95
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    96
int main (int argc, char *argv[])
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    97
{
4309
bf8c338c7820 fix typo
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 163
diff changeset
    98
  CommandLine cmd;
bf8c338c7820 fix typo
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 163
diff changeset
    99
  cmd.Parse (argc, argv);
bf8c338c7820 fix typo
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 163
diff changeset
   100
150
663120712cd9 fix coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 143
diff changeset
   101
  MyModel model;
8996
d0e0a162a990 Replace various instances of RandomVariable with RandomVariableStream
Mitch Watrous
parents: 7385
diff changeset
   102
  Ptr<UniformRandomVariable> v = CreateObject<UniformRandomVariable> ();
d0e0a162a990 Replace various instances of RandomVariable with RandomVariableStream
Mitch Watrous
parents: 7385
diff changeset
   103
  v->SetAttribute ("Min", DoubleValue (10));
d0e0a162a990 Replace various instances of RandomVariable with RandomVariableStream
Mitch Watrous
parents: 7385
diff changeset
   104
  v->SetAttribute ("Max", DoubleValue (20));
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   105
6721
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
   106
  Simulator::Schedule (Seconds (10.0), &ExampleFunction, &model);
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
   107
8996
d0e0a162a990 Replace various instances of RandomVariable with RandomVariableStream
Mitch Watrous
parents: 7385
diff changeset
   108
  Simulator::Schedule (Seconds (v->GetValue ()), &RandomFunction);
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   109
6721
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
   110
  EventId id = Simulator::Schedule (Seconds (30.0), &CancelledEvent);
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
   111
  Simulator::Cancel (id);
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 7025
diff changeset
   112
150
663120712cd9 fix coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 143
diff changeset
   113
  Simulator::Run ();
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   114
150
663120712cd9 fix coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 143
diff changeset
   115
  Simulator::Destroy ();
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   116
}