2. Recap
NS2 is a discrete-event simulator, where actions are associated with events
rather than time. An event in a discrete-event simulator consists of execution
time, a set of actions, and a reference to the next event (Fig. 4.1). These events
connect is an event-driven chain of events on the simulation timeline
NS2 to each other and form a simulation
(e.g., that in Fig. 4.1). Unlike a time-driven simulator, in an event-driven
simulator, time between a pair of simulation timeline constant. When
Put eventseventsthe events does executed from left to right (i.e.,
on in the chain are not need to be
the simulation starts,
chronologically).1 In the next section, we will discuss the simulation concept of
NS2. In Sections 4.2, 4.3,the 4.4, we lineexplain take actions when
Move along and time will and the details of classes Event
and Handler, class Scheduler, and class Simulator, respectively. Finally, we
confronting an event
summarize this chapter in Section 4.6.
insert Event5
create event
event time = 3.7
Action5
Event1 Event2 Event3 Event4
time = 0.9 time = 2.2 time = 5 time = 6.8
Action1 Action2 Action3 Action4
Time
(second)
1 2 3 4 5 6 7
Fig. 4.1. A sample chain of events in a discrete-event simulation. Each event con-
tains execution time and a reference to the next event. In this figure, Event1 creates
www.ns2ultimate.com
and inserts Event5 after Event2 (the execution time of Event 5 is at 3.7 second).
3. NS2 is a discrete-event simulator, where actions are associated with events
Recap
rather than time. An event in a discrete-event simulator consists of execution
time, a set of actions, and a reference to the next event (Fig. 4.1). These events
connect to each other and form a chain of events on the simulation timeline
(e.g., that in Fig. 4.1). Unlike a time-driven simulator, in an event-driven
simulator, time between a pair of events does not need to be constant. When
the simulation starts, events in the chain are executed from left to right (i.e.,
• An event indicates what happens
chronologically).1 In the next section, we will discuss the simulation concept of
NS2. In Sections 4.2, 4.3, and 4.4, we will explain the details of classes Event
• A handler indicates what to do
and Handler, class Scheduler, and class Simulator, respectively. Finally, we
summarize this chapter in Section 4.6.
insert Event5
create event
event time = 3.7
Action5
Event1 Event2 Event3 Event4
time = 0.9 time = 2.2 time = 5 time = 6.8
Action1 Action2 Action3 Action4
Time
(second)
1 2 3 4 5 6 7
Fig. 4.1. A sample chain of events in a discrete-event simulation. Each event con-
Handler
Handler Handler Handler Handler
tains execution time and a reference to the next event. In this figure, Event1 creates
1 2 4
and inserts Event5 after Event2 (the execution 3
5 time of Event 5 is at 3.7 second).
www.ns2ultimate.com
4. Recap: Insert an Event
Put an event *e on the timeline at “delay”second in
future, and associated the event with a handler “*h”.
Scheduler s& = Scheduler::instance();
s.schedule(h,e,delay);
Call here h e
associated
Handler Event
Delay
x
current time
www.ns2ultimate.com
5. Recap
T execute actions associated with an event *e
o
which is stored at time “t” seconds in future
h e
dispatch(e,t)
Handler Event
The function dispatch(e,t)
executes default actions defined
in the function handler(e) of x
t
the Handler *h.
www.ns2ultimate.com
7. What’s in This Slideshow
Scheduling-dispatching mechanism
Use an example: Delayed packet forwarding
Handler ➠ NsObject
Event ➠ Packet
How to receive a packet t seconds in the future.
www.ns2ultimate.com
8. objects are events, handlers, the Scheduler, and the Simulator.
• Helper objects do not explicitly participate in packet forwarding. However,
they implicitly help to complete the simulation. For example, a routing
module calculates routes from a source to a destination, while network
address identifies each of the network objects.
Scheduling-Dispatching
In this chapter, we focus only on network objects. Note that, the simulation-
related objects were discussed in Chapter 4. The packet-related objects will
be discussed in Chapter 8. The main helper objects will be discussed in
Chapter 15.
5.1.2 C++ Class Hierarchy
Step 1: Schedule an event
This section gives an overview of C++ class hierarchies. The entire hierarchy
consists of over 100 C++ classes and struct data types. Here, we only show
- Focus on class LinkDelay
a part of the hierarchy (in Fig. 5.1). The readers are referred to [18] for the
complete class hierarchy.
OTcl Interface Default Action
TclObject Handler
Simulator PacketQueue NsObject AtHandler QueueHandler
RoutingModule
Network Component
Classifier Connector LanRouter class LinkDelay
: public Connector {
Uni-directional Point-to- ...
point Object Connector
};
Queue Agent ErrorModel LinkDelay Trace
Fig. 5.1. A part of NS2 C++ class hierarchy (this chapter emphasizes on classes
in boxes with thick solid lines).
www.ns2ultimate.com
9. Scheduling-Dispatching
- From within its function recv(p,h)
void LinkDelay::recv(Packet* p, Handler* h)
{
Scheduler& s = Scheduler::instance();
s.schedule(target_, p, txt + delay_);
}
Call here associated
Handler Event
txt _ + delay_
x
current time
www.ns2ultimate.com
10. 5.1.2 C++ Class Hierarchy
Scheduling-Dispatching
This section gives an overview of C++ class hierarchies. The entire
consists of over 100 C++ classes and struct data types. Here, we o
a part of the hierarchy (in Fig. 5.1). The readers are referred to [1
complete class hierarchy.
The type of target_ is
Function handle(p)OTcl Interface
is Default Action
NsObject*
defined in the class TclObject Handler
NsObject
Simulator PacketQueue NsObject AtHandler QueueH
RoutingModule
target_ is defined at the base Network Component
class (i.e., Connector):
Classifier Connector LanRoute
class Connector : public
NsObject { Uni-directional Point-to-
... point Object Connector
NsObject* target_;
};
Queue Agent ErrorModel LinkDelay Trac
Fig. 5.1. A part of NS2 C++ class hierarchy (this chapter emphasizes
in boxes with thick solid lines). www.ns2ultimate.com
11. Scheduling-Dispatching
NS2 is a discrete-event simulator, where actions are associated with events
rather than time. An event in a discrete-event simulator consists of execution
time, a set of actions, and a reference to the next event (Fig. 4.1). These events
connect to each other and form a chain of events on the simulation timeline
(e.g., that in Fig. 4.1). Unlike a time-driven simulator, in an event-driven
simulator, time between a pair of events does not need to be constant. When
Step 2:
the simulation starts, events in the chain are executed from left to right (i.e.,
chronologically).1 In the next section, we will discuss the simulation concept of
NS2. In Sections 4.2, 4.3, and 4.4, we will explain the details of classes Event
The Scheduler Move forward in time
and Handler, class Scheduler, and class Simulator, respectively. Finally, we
summarize this chapter in Section 4.6.
insert Event5
create event
event time = 3.7
Action5
Event1 Event2 Event3 Event4
time = 0.9 time = 2.2 time = 5 time = 6.8
Action1 Action2 Action3 Action4
Time
(second)
1 2 3 4 5 6 7
MoveA forward events in athe next event. In this figure,Each event con-
Fig. 4.1. sample chain of in time and execute events
tains execution time and a reference to
discrete-event simulation.
Event1 creates
and inserts Event5 after Event2 (the execution time of Event 5 is at 3.7 second).
www.ns2ultimate.com
12. Scheduling-Dispatching
Step 3: Dispatch the event
//~ns/common/scheduler.cc
void Scheduler::run(){ take the next event from
instance_ = this;
Event *p; the timeline and store the
while (!halted_ && (p = deque())) {
dispatch(p, p->time_); pointer to the retrieved
}
} event in the variable p
Event:
time_ 7 NsObject
(LinkDelay::*target_):
handler_
x
www.ns2ultimate.com
13. Scheduling-Dispatching
From within “dispatch(p,p->time)”
//~ns/common/scheduler.cc
void Scheduler::dispatch(Event* p, double t){
clock_ = t; p->uid_ = -p->uid_;
p->handler_->handle(p);
}
Executed handle(p)
associated with this
Event: handler
time_ 7
NsObject
handler_
(LinkDelay::*target_):
x
www.ns2ultimate.com
14. related objects were discussed in Chapter 4. The packet-related objects will
be discussed in Chapter 8. The main helper objects will be discussed in
Chapter 15.
Scheduling-Dispatching
5.1.2 C++ Class Hierarchy
This section gives an overview of C++ class hierarchies. The entire hierarchy
consists of over 100 C++ classes and struct data types. Here, we only show
a part of the hierarchy (in Fig. 5.1). The readers are referred to [18] for the
Function handle( p) is defined in the class
complete class hierarchy.
NsObject OTcl Interface Default Action
TclObject Handler
Simulator PacketQueue NsObject AtHandler QueueHandler
RoutingModule
Network Component
Again, the type of
Classifier Connector LanRouter
NsObject
target_ is *NsObject (LinkDelay::*target_)
Uni-directional Point-to-
point Object Connector
Queue Agent ErrorModel LinkDelay Trace
www.ns2ultimate.com
Fig. 5.1. A part of NS2 C++ class hierarchy (this chapter emphasizes on classes
15. Scheduling-Dispatching
Again, the type of p->handler_ is
NsObject*.
So is the definition of function handle(p) in
this case
//~ns/common/scheduler.cc
void Scheduler::dispatch(Event* p, double t){
clock_ = t; p->uid_ = -p->uid_;
p->handler_->handle(p);
}
www.ns2ultimate.com
16. Scheduling-Dispatching
Here is the definition of function handle(p) in
class NsObject
//~/ns/common/object.cc
void NsObject::handle(Event* e)
{
recv((Packet*)e);
}
which is simply to receive a packet
www.ns2ultimate.com
17. of delayed packet forwarding, while Program 5.6 shows the details of functions
schedule(h,e,delay) as well as dispatch(p,t) of class Scheduler. When
“schedule(target_, p, d)” is invoked, function
schedule (...) casts packet *p and the NsObject *target_ into Event and
When putting together, they become a
Handler objects, respectively (Line 1 of Program 5.6). Line 5 of Program 5.6
associates packet *p with the NsObject *target_. Lines 6-7 insert packet
*p into the simulation timeline at the appropriate time. At the firing time,
delayed packet forwarding mechanism
the event (*p) is dispatched (Lines 9-14). The Scheduler invokes function
handle(p) of the handler associated with event *p. In this case, the associated
Step 1: Schedule
Step 2: Move fw.
Step 3: Dispatch
Fig. 5.4. Delayed packet forwarding
www.ns2ultimate.com
18. For more
information
about NS2
Please see
this book
from Springer
T. Issaraiyakul and E. Hossain, “Introduction to Network Simulator NS2”, Second Edition,
Springer 2011, or visit www.ns2ultimate.com