A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
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
40 {
41  Simulator::Schedule (Seconds (10.0),
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
55 {
56  std::cout << "ExampleFunction received event at "
57  << Simulator::Now ().GetSeconds () << "s" << std::endl;
58  model->Start ();
59 }
60 
61 static void
63 {
64  std::cout << "RandomFunction received event at "
65  << Simulator::Now ().GetSeconds () << "s" << std::endl;
66 }
67 
68 static 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)
Run the simulation until one of:
Definition: simulator.cc:157
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:268
void Start(void)
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event to expire at the relative time "time" is reached.
Definition: simulator.h:825
double GetSeconds(void) const
Definition: nstime.h:272
static void CancelledEvent(void)
Parse command-line arguments.
Definition: command-line.h:177
static void Destroy(void)
Every event scheduled by the Simulator::insertAtDestroy method is invoked.
Definition: simulator.cc:121
void HandleEvent(double eventValue)
double GetValue(double min, double max)
Returns a random double from the uniform distribution with the specified range.
static Time Now(void)
Return the "current simulation time".
Definition: simulator.cc:180
int main(int argc, char *argv[])
static void RandomFunction(void)
an identifier for simulation events.
Definition: event-id.h:46
void Parse(int argc, char *argv[])
Parse the program arguments.
static void ExampleFunction(MyModel *model)
Hold a floating point type.
Definition: double.h:41
void SetAttribute(std::string name, const AttributeValue &value)
Definition: object-base.cc:176