A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
sample-simulator.cc
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 
28 using namespace ns3;
29 
30 class MyModel
31 {
32 public:
33  void Start (void);
34 private:
35  void HandleEvent (double eventValue);
36 };
37 
38 void
39 MyModel::Start (void)
40 {
42  &MyModel::HandleEvent,
43  this, Simulator::Now ().GetSeconds ());
44 }
45 void
46 MyModel::HandleEvent (double value)
47 {
48  std::cout << "Member method received event at "
49  << Simulator::Now ().GetSeconds ()
50  << "s started at " << value << "s" << std::endl;
51 }
52 
53 static void
54 ExampleFunction (MyModel *model)
55 {
56  std::cout << "ExampleFunction received event at "
57  << Simulator::Now ().GetSeconds () << "s" << std::endl;
58  model->Start ();
59 }
60 
61 static void
62 RandomFunction (void)
63 {
64  std::cout << "RandomFunction received event at "
65  << Simulator::Now ().GetSeconds () << "s" << std::endl;
66 }
67 
68 static void
69 CancelledEvent (void)
70 {
71  std::cout << "I should never be called... " << std::endl;
72 }
73 
74 int main (int argc, char *argv[])
75 {
76  CommandLine cmd;
77  cmd.Parse (argc, argv);
78 
79  MyModel model;
80  Ptr<UniformRandomVariable> v = CreateObject<UniformRandomVariable> ();
81  v->SetAttribute ("Min", DoubleValue (10));
82  v->SetAttribute ("Max", DoubleValue (20));
83 
84  Simulator::Schedule (Seconds (10.0), &ExampleFunction, &model);
85 
86  Simulator::Schedule (Seconds (v->GetValue ()), &RandomFunction);
87 
88  EventId id = Simulator::Schedule (Seconds (30.0), &CancelledEvent);
89  Simulator::Cancel (id);
90 
91  Simulator::Run ();
92 
94 }
static void Run(void)
Definition: simulator.cc:157
static void Cancel(const EventId &id)
Definition: simulator.cc:267
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Definition: simulator.h:820
double GetSeconds(void) const
Definition: nstime.h:262
parse command-line argumentsInstances of this class can be used to parse command-line arguments: user...
Definition: command-line.h:50
static void Destroy(void)
Definition: simulator.cc:121
double GetValue(double min, double max)
Returns a random double from the uniform distribution with the specified range.
static Time Now(void)
Definition: simulator.cc:179
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
Definition: nstime.h:586
an identifier for simulation events.
Definition: event-id.h:46
void Parse(int argc, char *argv[]) const
Definition: command-line.cc:84
Hold an floating point type.
Definition: double.h:41
void SetAttribute(std::string name, const AttributeValue &value)
Definition: object-base.cc:160