Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 1



Similar documents
Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer

Network Simulator: ns-2

ns-2 Tutorial (1) Multimedia Networking Group, The Department of Computer Science, UVA Jianping Wang Jianping Wang, 2004 cs757 1 Today

Help on the Embedded Software Block

CS170 Lab 11 Abstract Data Types & Objects

An Incomplete C++ Primer. University of Wyoming MA 5310

ns-2 Tutorial Exercise

Channel Access Client Programming. Andrew Johnson Computer Scientist, AES-SSG

Implementing Rexx Handlers in NetRexx/Java/Rexx

SQLITE C/C++ TUTORIAL

5 Arrays and Pointers

Variable Base Interface

: provid.ir

Virtuozzo Virtualization SDK

INTRODUCTION TO OBJECTIVE-C CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 12 09/29/2011

How To Understand How A Process Works In Unix (Shell) (Shell Shell) (Program) (Unix) (For A Non-Program) And (Shell).Orgode) (Powerpoint) (Permanent) (Processes

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

The C Programming Language course syllabus associate level

MUSIC Multi-Simulation Coordinator. Users Manual. Örjan Ekeberg and Mikael Djurfeldt

An API for Reading the MySQL Binary Log

Network Programming. Writing network and internet applications.

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Exercises on ns-2. Chadi BARAKAT. INRIA, PLANETE research group 2004, route des Lucioles Sophia Antipolis, France

Moving from CS 61A Scheme to CS 61B Java

CS506 Web Design and Development Solved Online Quiz No. 01

Polymorphism. Problems with switch statement. Solution - use virtual functions (polymorphism) Polymorphism

Sources: On the Web: Slides will be available on:

J a v a Quiz (Unit 3, Test 0 Practice)

IVI Configuration Store

AP Computer Science Java Subset

Tutorial. Reference for more thorough Mininet walkthrough if desired

SMTP-32 Library. Simple Mail Transfer Protocol Dynamic Link Library for Microsoft Windows. Version 5.2

Computer Systems II. Unix system calls. fork( ) wait( ) exit( ) How To Create New Processes? Creating and Executing Processes

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Software Engineering Concepts: Testing. Pointers & Dynamic Allocation. CS 311 Data Structures and Algorithms Lecture Slides Monday, September 14, 2009

Design Patterns in C++

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Facebook Twitter YouTube Google Plus Website

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Daniele Messina, Ilenia Tinnirello

iphone SDK Enrolled students will be invited to developer program Login to Program Portal Request a Certificate Download and install the SDK

CISC 181 Project 3 Designing Classes for Bank Accounts

Introduction to Object-Oriented Programming

3.5. cmsg Developer s Guide. Data Acquisition Group JEFFERSON LAB. Version

Operating System Manual. Realtime Communication System for netx. Kernel API Function Reference.

# Constructors $smtp = Net::SMTP->new('mailhost'); $smtp = Net::SMTP->new('mailhost', Timeout => 60);

Netscape Internet Service Broker for C++ Programmer's Guide. Contents

Chapter 13 Storage classes

IMPLEMENTATION OF A SECURITY PROTOCOL FOR BLUETOOTH AND WI-FI

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

JavaScript. JavaScript: fundamentals, concepts, object model. Document Object Model. The Web Page. The window object (1/2) The document object

New Implementations into Simulation Software NS-2 for Routing in Wireless Ad-Hoc Networks

It has a parameter list Account(String n, double b) in the creation of an instance of this class.

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Object Oriented Software Design II

WASv6_Scheduler.ppt Page 1 of 18

StreamServe Persuasion SP4 Service Broker

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

PostgreSQL Functions By Example

Specific Simple Network Management Tools

C++ INTERVIEW QUESTIONS

Chapter 2 Quality of Service (QoS)

CIA405.lib. Contents. WAGO-I/O-PRO 32 Library

µtasker Document FTP Client

Part I. Multiple Choice Questions (2 points each):

Introduction to Java

Illustration 1: Diagram of program function and data flow

Web development... the server side (of the force)

Enhanced Connector Applications SupportPac VP01 for IBM WebSphere Business Events 3.0.0

Getting off the ground when creating an RVM test-bench

CpSc212 Goddard Notes Chapter 6. Yet More on Classes. We discuss the problems of comparing, copying, passing, outputting, and destructing

Analysis of Denial of Service Attack Using Proposed Model

An Introduction To Simple Scheduling (Primarily targeted at Arduino Platform)

El Dorado Union High School District Educational Services

HPCC - Hrothgar Getting Started User Guide MPI Programming

STORM. Simulation TOol for Real-time Multiprocessor scheduling. Designer Guide V3.3.1 September 2009

Compile-time type versus run-time type. Consider the parameter to this function:

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Project 2: Bejeweled

Configuration Manager

Developing Task Model Applications

Programming Languages CIS 443

6.088 Intro to C/C++ Day 4: Object-oriented programming in C++ Eunsuk Kang and Jean Yang

MPLAB TM C30 Managed PSV Pointers. Beta support included with MPLAB C30 V3.00

Networks and Protocols Course: International University Bremen Date: Dr. Jürgen Schönwälder Deadline:

Design and Implementation of Distributed Process Execution Environment

ECE 122. Engineering Problem Solving with Java

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

V850E2/ML4 APPLICATION NOTE. Performance Evaluation Software. Abstract. Products. R01AN1228EJ0100 Rev Aug. 24, 2012

Introduction to the BackgroundWorker Component in WPF

Keil C51 Cross Compiler

Data Management Applications with Drupal as Your Framework

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Job Reference Guide. SLAMD Distributed Load Generation Engine. Version 1.8.2

Embedded Event Manager Commands

Installing Java (Windows) and Writing your First Program

Sample CSE8A midterm Multiple Choice (circle one)

Business Application

Wave Analytics Data Integration

ESPResSo Summer School 2012

Transcription:

Event Driven Simulation in NS2 Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 1

Outline Recap: Discrete Event v.s. Time Driven Events and Handlers The Scheduler The Simulator Summary Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 2

Event-Driven v.s.. Time-Driven Q: Time Driven = ( Move from one time slot to another ) Q: Event Driven = ( Move from one event to another ) Time Driven or Discrete Time Simulation Example: Packet arrivals and departures Arrivals Departures Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 3

Time-Driven Simulation Observe the buffer for every FIXED period (e.g., 1 second) No. of Packets in the Buffer 3 2 1 Time (s) 1 2 3 4 5 6 7 8 9 10 11 12 Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 4

Time-Driven Simulation Simulation event for every time slot (fixed interval) Example Psudo Codes: For t = 1 to sim_time { if (arrival) buffer = buffer + 1; if (departure) buffer = buffer -1; print(buffer); } Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 5

Event-Driven Simulation Go from one event to another Same Example No. of Packets in the Buffer 3 2 1 Time (s) () 0.8 1.5 2.4 5.2 5.5 7.4 9.4 10.5 11.5 Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 6

Event-Driven Di Simulation i Use a Scheduler Maintain a set of events Example CreateEvent(); Run (); Psudo Codes CreateEvent(){ Pkt1.arr(0.8) Pkt2.arr(1.5) } Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 7

Event-Driven Simulation CreateEvent(); Event ID 1 2 3 4 5 6 Type Arrival Arrival Arrival Arrival Arrival Arrival Time 0.8 1.5 5.2 7.4 9.4 11.5 Run(); Create departure Create departure Create departure Event ID 7 8 9 Type Departure Departure Departure Time 2.4 5.5 10.5 Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 8

NS2 Simulation Concept Event-Driven Simulation Recap: Simulation Main Steps Design Simulation Network Configuration i Phase CreateEvent() Simulation Phase Run() Result Compilation Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 9

Simulation Network Configuration Phase Create topology Schedule event (e.g., CreateEvent()) t()) Simulation Phase Simulator::run() (e.g., Run()) ) Execute the scheduled events Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 10

Outline Recap: Discrete Event v.s. Time Driven Events and Handlers The Scheduler The Simulator Summary Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 11

Event and Handler: Outline Overview C++ Classes Event and Handler Two Main Types of Events AtEvent Packet Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 12

Concepts of Events and Handlers Event-driven simulation Put events on the simulation timeline Move forward in time When finding an event, take associated actions (i.e., execute the event) Main components Events C++ class Event Actions C++ class Handler Q: Give examples of events. Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 13

Event and Handler Examples of Events Packet Arrivals/Departures Start/Stop Application Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 14

Event and Handler: C++ Classes Class Event: Define events (e.g., packet arrival) Class Handler: Define (default) actions associated with an event (tell the node to receive the packet) Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 15

C++ Class Event //~/ns/common/scheduler.h class Event { public: }; Event* next_; /* event list */ Event* prev_; Handler* handler _; /* handler to call when event ready */ double time_; /* time at which event is ready */ scheduler_uid_t uid_; /* unique ID */ Event() : time _(0), uid _(0) {} Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 16

Class Event Main variables: next_: Next event - uid_: Unique ID time_: Time - handler_: Handler handler handle(){ handle(){ <actions> <actions> } } handler handler_ next_ handler_ next_ event uid_ time_ uid_ time_ event Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 17

Declaration Class Handler handler What is this? What is the purpose? handle(){ <actions> } Define Default Actions C++ function handle(event*) Associated with an Event handler_ uid_ next_ time_ event Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 18

Handlers: Example Class NsObject (derived from class Handler) ) As we shall see, all network objects (e.g., Connector, TcpAgent) derived from class NsObject. Default action of all network objects is to receive (using function recv( )) a packet (cast from an event e) Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 19

Events and Handlers: Example $ftp start $cbr start 0.0505 0.1 Simulation time handle(){ } send FTP packets handle(){ } send CBR packets handler handler handler_ next_ handler_ next_ 1 0.05 2 0.1 event uid_ time_ uid_ time_ event Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 20

Events and Handlers: Example When hitting an event e, a Scheduler 1. Extract the handler_ associated with the event e 2. Execute handler_->handle(e) (i.e., tell the handler_ to take the default action) handler handle(){ <actions> } The default action is defined in in the handler, NOT in the event handler_ uid_ next_ time_ Simulation time event Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 21

Question What is the main purpose of events? What happen if NS2 does not define classes Event, Handler, and Scheduler? Some events have not occurred; Every event occurs at the same time. Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 22

Event and Handler: Outline Overview C++ Classes Event and Handler Two Main Types of Events AtEvent Packet (Discussed Later) Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 23

Two Types of Events 1. At Event: (Derives from Class Event) Action: Execute an OTcl command Examples: C++ Class AtEvent Placed on the simulation timeline by instproc at with syntax $ns at <time> <Tcl command> Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 24

C++ Class AtEvent class AtEvent : public Event { public: AtEvent() : proc_(0) {} char* proc_; }; time_ uid_ next _ handler_ handle(event *e){ AtEvent* at = (AtEvent*)e; puts this is test Tcl::instance().eval(at->proc_); delete at; proc_ } AtEvent AtHandler Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 25

C++ Class AtEvent OTcl command: $ns at <time> <Tcl command> Implementation: Scheduler::command(int argc, const char*const* argv) { Tcl& tcl = Tcl::instance(); if (argc == 4) { Q: argv[0] =? ( cmd ) if (strcmp(argv[1], "at") == 0) { double delay, t = atof(argv[2]); const char* proc = argv[3]; AtEvent* e = new AtEvent;int n = strlen(proc); e->proc_ = new char[n + 1]; strcpy(e->proc_, proc); delay = t - clock(); schedule(&at_handler, e, delay); return (TCL_OK); } } return (TclObject::command(argc, argv)); } Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 26

Two Types of Events 2. Packet: (Derives from Class Event) Action: Receive a packet C++ Class Packet (will be discussed later) NsObject?? handle(){ <actions>?? } Type casting: Packet is a derived class of class Event handler_ uid_ next_ time_ Event Packet Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 27

Questions Q: How do we put an AtEvent on the $ns at <time> <Tcl command> simulation timeline? ( ) Q: Is it possible to put a Packet on the simulation timeline? Why or why not? ( Yes; Packet derives from class Event ) How do we put events on the simulation timeline? Use THE SCHEDULER Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 28

Outline Recap: Discrete Event v.s. Time Driven Events and Handlers The Scheduler The Simulator Summary Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 29

The Scheduler: Outline Overview C++ Class Scheduler Unique ID and Its Mechanism Scheduling and Dispatching Mechansim Null Events and Dummy Events Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 30

Event Handling: Recap 1. Put events on the simulation timeline 2. Take the default action assoc. with (i.e., handle) event Handler - Also called fire or dispatch - function handle() of class Handler 3. Move to the next event Scheduler - Through the pointer next_ _ of an Event object How do we PUT, TAKE, and MOVE? Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 31

Recap Event e = An indication of future event Handler defines the default action (i.e., how to execute the event e; handler(e)) NS2 moves forwards in time and tell the relevant handler to execute default actions. Execute = Fire = Dispatch What s more? How to put an event on the simulation timeline? Who should execute the actions assoc. with the event? THE SCHEDULER Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 32

The Scheduler 1. Put events on the simulation timeline function schedule( ) 2. Take the default action function dispatch( ) 3. Move forward in time function run( ) event handler_ uid_ time_ handler handle(){ } Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 33

C++ Class Scheduler Current virtual time?? ( uniqueness ) Unique ID: incremented for every new event Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 34

Task 1: Put Event on the Simulation Timeline Use function n schedule(h,e,delay), Associate Event e with a handler h Indicate the dispatching time handler Assign unique ID Put the Event e on the simulation time with delay delay handle(){ <actions> } handler_ next_ uid_ time_ event Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 35

Functions schedule(.) () < Checking for Error > New unique ID Bind e and h Update time Put e on the time line Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 36

Function schedule(.) () 4 Possible errors 1. Null handler (i.e., h = 0) if (!h) { /* error: Do not feed in NULL handler */ }; We will talk about this error later 2. uid_ of the event > 0 Something wrong if (e->uid_ > 0) { } printf("scheduler: Event UID not valid!\n\n"); abort(); This is a very common error message!! Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 37

Function schedule(.) () 4 Possible errors 3. delay < 0 Go back in time if (delay < 0) { /* error: negative delay */ }; 4. uid_ < 0 Use up the uid_ if (uid_ < 0) { fprintf(stderr, "Scheduler: UID space exhausted!\n") abort(); } Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 38

Task 2: Take Default Actions NS2 dispatches a relevant handler to take default actions. event handler_ handler *e uid_ time_ *h Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. Why put negative? We will discuss about the sign of uid_ later. 39

Task 3: Move from One Event to the Next Function run() starts the simulation Take the next event from the queue of events Simulation time Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 40

The Scheduler: Outline Overview C++ Class Scheduler Unique ID and Its Mechanism Scheduling and Dispatching Mechanism Null Events and Dummy Events Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 41

Two types of Unique ID (UID) 1. Scheduler: Global UID Track the number of created UID 2. Event: Individual UID Event ID Assigned by the Scheduler Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 42

Global l UID A member variable of class Scheduler Always Positive Incremented for every new event (fn schedule(.)) Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 43

Individual id UID Unique to each event Set by the Scheduler Assigned by the Scheduler within fn schedule(.) Negated by the invocation n of fn dispatch(.) Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 44

Individual id UID Unique to each event Positive: assigned by fn schedule(.) Negative: dispatched fn dispatch(.) Dynamics: uid_ is switching between +/- values schedule(.) If negative ( Event UID not valid )? dispatch(.) Negative uid_ Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 45

Positive UID Individual id UID The event is on the simulation time line. It is waiting to be executed. Rescheduling the (undispatched) event here would result in an error uid_ of the event > 0 Something wrong: if (e->uid_ > 0) { printf("scheduler: Event UID not valid!\n\n"); abort(); } Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 46

Positive UID Individual id UID The event is on the simulation time line. It is waiting to be executed. Rescheduling the (undispatched) event here would result in an error Negative UID The event has been executed. It is ready to be rescheduled. Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 47

The Scheduler: Outline Overview C++ Class Scheduler Unique ID and Its Mechanism Scheduling and Dispatching Mechanism Null Events and Dummy Events Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 48

The Scheduler: Outline Overview C++ Class Scheduler Unique ID and Its Mechanism Scheduling and Dispatching Mechanism Null Events and Dummy Events Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 49

Scheduling-Dispatching Mechanism Example: set ns [new Simulator] $ns at 10 [puts "An event is dispatched"] $ns run AtEvent AtHandler time_ uid_ next_ handler_ puts An even is dispatched proc_ handle(){ AtEvent* at = (AtEvent*)e; Tcl::instance().eval(at->proc_); delete at; } Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 50

Scheduling-Dispatching Mechanism Scheduler::command(int argc, const char*const* argv) { } Tcl& tcl = Tcl::instance(); if (argc == 4) { if (strcmp(argv[1], "at") == 0) { } double delay, t = atof(argv[2]); const char* proc = argv[3]; AtEvent* e = new AtEvent; int n = strlen(proc); e->proc_ = new char[n + 1]; strcpy(e->proc_, proc); delay = t - clock(); schedule(&at_handler, handler e, delay); return (TCL_OK); } return (TclObject::command(argc, argv)); Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 51

Scheduling-Dispatching Mechanism command (argv = [ x, at, time, str] ) AtEvent e dispatch( p, t ) clock_=t p->uid_ = -p->uid_ p->handler_->handle(p) e->proc_ = str schedule( &at_handler, e, delay ) AtHandler at_handler handle(e) schedule( h, e, delay ) uid_++ invoke OTcl command stored in e->proc_ Event uid_ handler_ time_ Clock_+( ) insert(e) Scheduling Dispatching Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 52

The Scheduler: Outline Overview C++ Class Scheduler Unique ID and Its Mechanism Scheduling and Dispatching Mechanism Null Events and Dummy Events Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 53

Null and Dummy Events Scheduling In general, we feed the event into the Scheduler. The event contains Time where the event occurs, and Ref. to an action taker (i.e., the handler) Example Event = Packet Time = Time where the packet is received Default action = Receive a packet Action taker = NsObject In some case, we the default action involves no event. E.g., Print a string after a certain delay What event would we feed to the function Scheduler::schedule(handler, event,delay)? Q: delay =?; handler =?; event =? Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 54

Null and Dummy Events Scheduling Null Event: set event = 0 Scheduler::schedule(handler,0,delay) Dummy Event: A member variable whose type is Event It does nothing but being placed in function schedule(handler,dummy_event,delay) Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 55

Null and Dummy Events Scheduling Dummy event example: class LinkDelay //~ns/link/delay.h class LinkDelay : public Connector { };... Event intr_; //~ns/link/delay.cc void LinkDelay::recv(Packet* p, Handler* h) {... } s.schedule(h, &intr_, txt); Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 56

Null and Dummy Events Scheduling Which one should we use? Null or Dummy? Null events Simple, but no mechanism to preserve uid_ conformance You lose the scheduling-dispatching protection mechanism. Suitable for simple cases Dummy events Require a declaration in a class. A bit more complicated, but will conform with NS2 scheduling-dispatching dispatching mechanism Suitable for more complicated cases Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 57

Outline Recap: Discrete Event v.s. Time Driven Events and Handlers The Scheduler The Simulator Summary Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 58

The Simulator Maintain assets which are shared among simulation objects The schedulers Event scheduling The null agent Packet destruction Node reference All nodes Link reference All links Ref.to the routing component Routing It does not do the above functionalities. It only yprovide the ref. to the obj which does the above functionalities Q:What is an advantage of putting the ref. to the Simulator? A: For convenient; The Simulator will provide single point of access to these objects. Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 59

The Simulator OTcl and C++ Classes Simulator OTcl Instvar scheduler_: The schduler nullagent_: The packet destruction object Node_(<nodeid>): stores node objects link_(sid:did): id did) stores link objects connecting two nodes routingtable_: stores the routing component sid did Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 60

C++ Class Simulator //~ns/common/simulator.h class Simulator : public TclObject { public: static Simulator& instance() { return (*instance_); } Simulator() : nodelist _(NULL), rtobject_(null), nn_(0), size_(0) {}... private: ParentNode **nodelist_;; RouteLogic *rtobject_; int nn_; int size_; static Simulator* instance_; }; Function instance(): Retrieve the static Simulator instance_. Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 61

Retrieving the Simulator Instance Instproc instance{} //~ns/tcl/lib/ns-lib.tcl Simulator proc instance {} { set ns [Simulator info instances] if { $ns!= "" } { return $ns } }... Retrieve all instantiated instances of a given class Q: What does info instances do? Q: Can it return more than one Simulator instance? Why? If so, which one do we choose? No!, the simulator is declared as static static Simulator* instance_; Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 62

Running Simulation Creating a Simulator object set $ns [new Simulator] ] OTcl constructor: //~ns/tcl/lib/ns-lib.tcl Simulator instproc init args { $self create_packetformat $self use-scheduler Calendar $self set nullagent_ [new Agent/Null] $self set-address-format def eval $self next $args } $ns is now a Simulator instance Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 63

Running Simulation Main instproc run{}: Start simulation //~/ns/tcl/lib/ns-lib.tcl Simulator instproc run { [$self get-routelogic] configure $self instvar scheduler_ Node_ link_ started_ set started_ 1 foreach nn [array names Node_] { $Node_($nn) reset foreach qn [array names link_] { set q [$link_($qn) queue] $q reset } return [$scheduler_ run] } Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 64

Running Simulation Scheduler::run{} Keep executing events until no more event or the simulation is halted Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 65

Instprocs of Class Simulator Instproc Meaning now{} Retrieve the current simulation time. nullagent{} Retrieve the shared null agent. use-scheduler{type} Set the type of the Scheduler to be <type>. at{time stm} Execute the statement <stm> at <time> second. run{} Start the simulation. halt{} Terminate the simulation. cancel{e} Cancel the scheduled event <e>. Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 66

Outline Recap: Discrete Event v.s. Time Driven Events and Handlers The Scheduler The Simulator Summary Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 67

Summary NS2 Simulator is Event Driven Event Unique ID + Time + Handler Two derived classes: ( AtEvent and Packet ) Handlers ( default actions ) ( function handle(e) ) Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 68

Summary Scheduler schedule(.): ( Put an event in the list ) dispatch(.): () ( Executes an event ) run(): ( Start simulation ) Event UID Dynamics schedule() +, dispatch() - Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 69

Summary Null event and Dummy Event Purpose: ( Delay actions requiring no event) Differences: Null Event = ( 0 ) Dummy Event = ( member m variable of a class ) Simulator Maintain all common objects: Scheduler, null agent, nodes, links, and routing table Start the simulation (e.g., $ns run ) Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 70