2. Events
Dictionary: A thing that happens
NS2: A thing which we are interested in
simulation
NS2 has two types of events
1. Packet reception event
2. At event: T execution
cl
www.ns2ultimate.com
3. When NS2 Uses Events
Things to happen in future
Packet reception event at future time
At event: T execution in future
cl
Examples:
A packet will arrive at 0.9 sec.
Execute “$ftp start” at 6.8 sec.
$ns at 0.05 “$ftp start”
$ns at 60.0 “$ftp stop”
$ns 61 “finish”
www.ns2ultimate.com
4. 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
Chain of 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.,
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
NS2 lines up events in a sequential manner
and Handler, class Scheduler, and class Simulator, respectively. Finally, we
summarize this chapter in Section 4.6.
(like a chain)
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
At run time, it executes discrete-event in the chain con-
Fig. 4.1. A sample chain of events in a events simulation. Each event
tains execution time and a reference to the next event. In this figure, Event1 creates
one by one, sequentially
and inserts Event5 after Event2 (the execution time of Event 5 is at 3.7 second).
www.ns2ultimate.com
5. (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.,
Example: Chain of Events
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
and Handler, class Scheduler, and class Simulator, respectively. Finally, we
summarize this chapter in Section 4.6.
insert Packet
Event5
create
event
event
time 1 3.7
=
departs
Action5
Event1
Packet Event2
Packet Event3 Event4
Packet
time 1= 0.9 time 2 2.2
= time “…”
print = 5 time 3 6.8
=
arrives
Action1 arrives
Action2 Action3 arrives
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
and inserts Event5 after Event2 (the execution time of Event 5 is at 3.7 second).
www.ns2ultimate.com
8. Events and Handlers
Event Handler
Indicate What happened What to do
- Pointer to the next
event Default actions
Main components - Pointer to the handler to do when the
- Time when the event event occurs
occurs
www.ns2ultimate.com
9. Classes Event and Handler
//~/ns/common/scheduler.h
class Event {
public:
Event* next_; /* a pointer to the next event*/
Event* prev_; /* a pointer to the previous event*/
Handler* handler_; /* handler which contains associated action */
double time_; /* time at which event is execute */
scheduler_uid_t uid_; /* unique ID of the event */
Event() : time_(0), uid_(0) {}
};
//~/ns/common/scheduler.h
class Handler {
public:
virtual ~Handler () {}
virtual void handle(Event* e) = 0; /* default action is defined
here */
};
www.ns2ultimate.com
10. Class Event
Class Event has 4 main variables
- next_: A pointer to the next event
- handler_: A pointer to the Handler
- time_: Time
- uid_: Unique ID
Class Handler defines the default action
associated wit the event in its function handle(e)
www.ns2ultimate.com
12. Handler
What kind of actions?
//~/ns/common/scheduler.h
class Handler {
public:
virtual ~Handler () {} Pure virtual function
virtual void handle(Event* e) = 0; /* default action is defined
here */
};
Default action
- Pure virtual
- T be defined in the derived classes
o
www.ns2ultimate.com
13. 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
NS2 Hierarchy
Chapter 15.
5.1.2 C++ Class Hierarchy
This section gives an overview of C++ class hierarchies. The entire hierarchy
consistsClass 100 C++ classes thestruct data types. of all Network
of over Handler is and based class Here, we only show
a part of the hierarchy (in Fig. 5.1). The readers are referred to [18] for the
complete class Object, e.g., queue, agent, link delay.
hierarchy.
OTcl Interface Default Action
TclObject Handler
Simulator PacketQueue NsObject AtHandler QueueHandler
RoutingModule
Network Component
Classifier Connector LanRouter
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
14. Chapter 15.
5.1.2 C++ Class Hierarchy
NS2 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
complete class hierarchy.
OTcl Interface Default Action
TclObject Handler
Simulator PacketQueue NsObject AtHandler QueueHandler
RoutingModule
Network Component
Let’s focus on
class NsObject (all Classifier Connector LanRouter
network objects) 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
15. NsObject
So the default action (i.e., function handle(e)) is
also defined here:
//~/ns/common/object.cc
void NsObject::handle(Event* e)
{
recv((Packet*)e);
}
- Take a pointer to a packet as an input
- Default action = receive the packet.
- See [ here ] for the details of packet reception
www.ns2ultimate.com
16. Next Step?
Let’s talk about how NS2 form a chain of
events ➠ Scheduler
(In The Next Post)
17. For more
information
about NS2
Please see
this book
from Springer
T. Issaraiyakul and E. Hossain, “Introduction to Network Simulator NS2”, Springer 2009
or visit www.ns2ultimate.com