Presentation is loading. Please wait.

Presentation is loading. Please wait.

Network Performance modelling and simulation

Similar presentations


Presentation on theme: "Network Performance modelling and simulation"— Presentation transcript:

1 Network Performance modelling and simulation
Chapter 5 Linkage Between OTcl and C++ in NS2

2 NS2 C++/OTcl Architecture Environment binding:
1.1 Ouline NS2 C++/OTcl Architecture Environment binding: – Class Tcl – Class TclCommand – Class EmbeddedTcl Component binding: – Class TclClass, – Class TclObject, and – Class InstVar

3 NS2: C++ and OTcl Composition
Two language architecture: C++ and OTcl C++ – Compiler – Fast to run (run on machine codes) – Slow to change (need compilation) OTcl – Interpreter – Slow to run; Fast to change Hierarchy – C++ = Compiled Hierarchy – OTcl = Interpreted Hierarchy Why two languages? (C++ coding styles)

4 Three C++ Coding Styles
1. Basic C++ – Compile and create “prog.exe” – Recompile for every minor changes (e.g., number of nodes, link speed)

5 Three C++ Coding Styles
2. C++ coding with input arguments – Use parameters input argument (argv,argc) – E.g., “prog <num_node> <link_speed>” – Invocation: “prog 10 3” – What if there are too many parameters?

6 Three C++ Coding Styles
3. C++ coding with input files - Put input parameters in a configuration file - No need to change C++ code – One input argument—the filename – E.G., “prog config.txt” – This is NS2 style!! – The configuration file is called “Tcl Simulation script”

7 NS2 and C++ Coding Styles
C++ coding with input files E.G., “>>ns myfirst_ns.tcl” C++ codes: Located under ~/ns/* C++ executable file: ns The input argument=?: (myfirst_ns.tcl ) A configuration file=?: (myfirst_ns.tcl ) Example: Calculate Delay

8 Why Tcl/OTcl? Problem 1: Problem 2: Problem 3:
– Reading a configuration file from C++ is hard! – Use Tcl Problem 2: – C++ contains classes : Tcl is not OOP – Use OTcl Problem 3: – Some components are easy to implement – C++ make things complicated – Define them in OTcl

9 C++/OTcl Architecture
C++: Internal mechanism OTcl: User interface – Monitor – Keyboard – Tcl Simulation Script/Output file

10 Start from Tcl simulation script For each line:
NS2 Process Start from Tcl simulation script For each line: Execution path:Tcl -> OTcl -> C++ Returning path: C++ -> Otcl -> Tcl (next line) Example:

11 C++/OTcl Components: C++ Classes
Provide an access to OTcl from C++ 6 main classes: – Class Tcl: Access to Tcl/OTcl interpreter – Class InstVar: Define C++ class variables which is bound to OTcl class variables – Class TclObject: C++ object + OTcl interface – Class TclClass: Bind C++ and OTcl Classes – Class TclCommand: Global command – Class EmbeddedTcl: Translate OTcl to C++

12 C++/OTcl Component Functionality

13 Class Tcl Access OTcl from C++ 5 Main Functionalities 1. Obtain the Tcl instance 2. Invoke OTcl statement from C++ 3. Pass/Receive results to/from the OTcl 4. Report error 5. Retrieve the reference to TclObjects

14 Class Tcl: Main Functionalities
Obtain the Tcl instance 2. Invoke OTcl procedures – Tcl::eval(char* str) – Tcl::evalc(const char* str)

15 Class Tcl: Main Functionalities
2. Invoke OTcl procedures (cont.) – Tcl::evalf(const char* format,…) 3. Receive results from the OTcl – Tcl::result()

16 Class Tcl: Main Functionalities
3. Pass results to the Otcl – Tcl::result(const char* str): – Tcl::result(const char* fmt)

17 Reverse of Class Tcl: (Invoke OTcl commands from C++)
Class TclCommand Reverse of Class Tcl: (Invoke OTcl commands from C++) Invoke C++ codes from the OTcl domain Available globally. Not recommended (non OOP). Use command in TclObject Examples – ns-version – ns-random See file ~/ns/common/misc.cc

18 Class TclClass: A Review
Create a “hook” between OTcl and C++ class name When creating an OTcl object, NS2 creates a C++ object with the class name defined in TclClass How to construct such the C++ object ? TclObject !!

19 C++ object & OTcl functionalities In the bound hierarchies
Class TclObject C++ object & OTcl functionalities In the bound hierarchies – C++: Based class = TclObject; see ~/tclcl/tclcl.h – OTcl: Based class = SplitObject see ~/tclcl/tclobject.tcl A user creates a TclObject from the OTcl NS2 automatically creates a “shadow” C++ object Shadow:The same object residing in different class hierarchy

20 Class TclObject: Outline
Object construction: What happen when “new” is executed. OTcl commands: Invoking C++ from the OTcl. C++ and OTcl class variable binding Others – Reference to TclObjects – Object destruction (similar to the construction)

21 TclObject Construction
NS2 creates object using a global OTcl procedure (new <className> [<args>]) E.g., “new Agent/TCP”

22 TclObject Construction
Procedure “create” invokes – alloc{…}: Allocate memory – init{…}: OTcl object construction Example:

23 TclObject Construction
Example: TCP

24 TclObject Construction
Example: Create a TCP object Q: OTcl command = ( new Agent/TCP )? Q: What does it do ? 1. ( alloc ), and 2. ( init )

25 OTcl Commands Invoke C++ statements from Tcl/OTcl environment
Example: $ns connect $tcp $sink The syntax is <object> <command_name> [<args>] <command_name> can be 1. An instproc of <object> 2. OTcl command associated with the <object> Q: From the above example -<object> = ( $ns ) -<command_name> = ( connect ) -<args> = ( $tcp $sink )

26 OTcl Commands command(argc,argv)
The detail of Tcl command is defined in C++ function of class TclObject command(argc,argv) – argc = no. of arguments – argv = an argument vector argv [0] = cmd, argv[1] = <command_name>, argv[n] = <args>, n > 1 Return either Tcl codes (e.g., TCL_OK, TCL_ERROR) Invoke the same function of the parent class

27 OTcl Commands invoke (OTcl ) an OTcl command ? Example:

28 OTcl Commands To define an OTcl command: 1. Define function “command(argc,argv)” 2. Use “if” statement to match <command_name> with argv[1] 3. Return a Tcl code 4. Invoke function “command(…)” of the parent class if the <command_name> is not found.

29 OTcl Commands Q: How do we invoke the command “eventtrace” from the OTcl?

30 Reference to TclObjects

31 Component Binding Tclclass Bind classnames TclObject Bind objects OTcl command Bind functions Variable binding Bind Variables

32 Variable Binding 2 Tasks 1. Binding variables:
A change in one variable: Auto. change in another variable Class InstVar 2. Setting the default values: The value assigned to instvars upon object construction

33 Binding Variables Syntax Other types
bind(“<ovar>”,&<cvar>); – <ovar> = OTcl class variable name – <cvar> = C++ class variable name – <ovar> and <cvar> need not be the same Other types – Regular: bind(“<ovar>”,&<cvar>); – Bandwidth: bind_bw(“<ovar>”,&<cvar>); – Time: bind_time(“<ovar>”,&<cvar>); – Boolean: bind_bool(“<ovar>”,&<cvar>);

34 Binding Variables: Example

35 Setting the Default Values
File ~/ns/tcl/lib/ns-default.tcl Not doing so warning message Syntax: <Class_Name> set <instvar> <value> E.G.,

36 Class InstVar Recall: Variable binding 5 derived classes
– bind (“<ovar>”,&<cvar>); type = (regular,integer,real ) – bind_bw (“<ovar>”,&<cvar>); type = ( bandwidth ) – bind_time (“<ovar>”,&<cvar>); type = ( time ) – bind_bool (“<ovar>”,&<cvar>); type = ( boolean ) 5 derived classes – Real Class InstVarReal – Integer Class InstVarInt – Boolean Class InstVarBool – Bandwidth Class InstVarBandwidth – Time Class InstVarTime


Download ppt "Network Performance modelling and simulation"

Similar presentations


Ads by Google