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 
34 using namespace ns3;
35 
37 class MyModel
38 {
39 public:
41  void Start (void);
42 private:
48  void HandleEvent (double eventValue);
49 };
50 
51 void
53 {
56  this, Simulator::Now ().GetSeconds ());
57 }
58 void
59 MyModel::HandleEvent (double value)
60 {
61  std::cout << "Member method received event at "
62  << Simulator::Now ().GetSeconds ()
63  << "s started at " << value << "s" << std::endl;
64 }
65 
71 static void
73 {
74  std::cout << "ExampleFunction received event at "
75  << Simulator::Now ().GetSeconds () << "s" << std::endl;
76  model->Start ();
77 }
78 
82 static void
84 {
85  std::cout << "RandomFunction received event at "
86  << Simulator::Now ().GetSeconds () << "s" << std::endl;
87 }
88 
90 static void
92 {
93  std::cout << "I should never be called... " << std::endl;
94 }
95 
96 int main (int argc, char *argv[])
97 {
99  cmd.Parse (argc, argv);
100 
101  MyModel model;
102  Ptr<UniformRandomVariable> v = CreateObject<UniformRandomVariable> ();
103  v->SetAttribute ("Min", DoubleValue (10));
104  v->SetAttribute ("Max", DoubleValue (20));
105 
106  Simulator::Schedule (Seconds (10.0), &ExampleFunction, &model);
107 
109 
111  Simulator::Cancel (id);
112 
113  Simulator::Run ();
114 
116 }
static void Run(void)
Run the simulation.
Definition: simulator.cc:201
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:321
void Start(void)
Start model execution by scheduling a HandleEvent.
tuple cmd
Definition: second.py:35
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:341
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1238
static void CancelledEvent(void)
Simple function event handler; the corresponding event is cancelled.
Parse command-line arguments.
Definition: command-line.h:205
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:165
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void HandleEvent(double eventValue)
Simple event handler.
double GetValue(double min, double max)
Get the next random value, as a double in the specified range .
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:224
static void RandomFunction(void)
Simple function event handler; this function is called randomly.
An identifier for simulation events.
Definition: event-id.h:53
Simple model object to illustrate event handling.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
void Parse(int argc, char *argv[])
Parse the program arguments.
static void ExampleFunction(MyModel *model)
Simple function event handler which Starts a MyModel object.
This class can be used to hold variables of floating point type such as 'double' or 'float'...
Definition: double.h:41
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:191