samples/sample-simulator.cc
author Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
Sun, 27 Mar 2011 23:35:55 +0100
changeset 6957 5f49d23b4a74
parent 6721 e85ecc17279a
permissions -rw-r--r--
Modular bindings: add support for <module>.py and _<module>.so layout, for extending modules in Python
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6721
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
     1
/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
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"
6721
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    25
#include "ns3/random-variable.h"
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    26
16
99e833adbb46 change yans namespace to ns3
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 14
diff changeset
    27
using namespace ns3;
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    28
6721
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    29
class MyModel
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    30
{
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    31
public:
150
663120712cd9 fix coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 143
diff changeset
    32
  void Start (void);
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    33
private:
6721
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    34
  void HandleEvent (double eventValue);
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    35
};
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    36
6721
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    37
void
122
6b8f1eda5c57 fix coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 110
diff changeset
    38
MyModel::Start (void)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    39
{
6721
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    40
  Simulator::Schedule (Seconds (10.0),
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    41
                       &MyModel::HandleEvent,
163
2a7e05018eeb rename Time::ApproximateTo methods to Time::Get
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 162
diff changeset
    42
                       this, Simulator::Now ().GetSeconds ());
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    43
}
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    44
void
6721
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    45
MyModel::HandleEvent (double value)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    46
{
6721
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    47
  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
    48
            << Simulator::Now ().GetSeconds ()
150
663120712cd9 fix coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 143
diff changeset
    49
            << "s started at " << value << "s" << std::endl;
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    50
}
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    51
6721
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    52
static void
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    53
ExampleFunction (MyModel *model)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    54
{
6721
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    55
  std::cout << "ExampleFunction received event at "
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    56
            << Simulator::Now ().GetSeconds () << "s" << std::endl;
150
663120712cd9 fix coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 143
diff changeset
    57
  model->Start ();
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    58
}
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    59
6721
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    60
static void
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    61
RandomFunction (void)
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    62
{
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    63
  std::cout << "RandomFunction received event at "
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    64
            << Simulator::Now ().GetSeconds () << "s" << std::endl;
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    65
}
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    66
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    67
static void
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    68
CancelledEvent (void)
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    69
{
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    70
  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
    71
}
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    72
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    73
int main (int argc, char *argv[])
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    74
{
4309
bf8c338c7820 fix typo
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 163
diff changeset
    75
  CommandLine cmd;
bf8c338c7820 fix typo
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 163
diff changeset
    76
  cmd.Parse (argc, argv);
bf8c338c7820 fix typo
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 163
diff changeset
    77
150
663120712cd9 fix coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 143
diff changeset
    78
  MyModel model;
6721
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    79
  UniformVariable v = UniformVariable (10, 20);
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    80
6721
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    81
  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
    82
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    83
  Simulator::Schedule (Seconds (v.GetValue ()), &RandomFunction);
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    84
6721
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    85
  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
    86
  Simulator::Cancel (id);
e85ecc17279a small modifications to the samples showing event scheduling
Tom Henderson <tomh@tomh.org>
parents: 4309
diff changeset
    87
  
150
663120712cd9 fix coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 143
diff changeset
    88
  Simulator::Run ();
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    89
150
663120712cd9 fix coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 143
diff changeset
    90
  Simulator::Destroy ();
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    91
}