Monday, May 31, 2010

C++ and OTcl Linkage: Why Two Languages?

Note: The content in this series is extracted from the book, Introduction to Network Simulator NS2. You may have to read chapter 3 of the book for better understanding.

Introduction

NS2 consists mainly of two languages: C++ and OTcl. Each of these two languages has its own strengths and weaknesses. NS2 beautifully integrates these two languages to draw out their strengths. For most of the time, you would not need to know the integration mechanism. But you need to know the strength and weaknesses of these two languages in order to apply them properly.

Strengths and Weaknesses of C++ and OTcl
  • C++: C++ is a compiled programming language. A C++ program needs to be compiled (i.e., translated) into the executable machine code. Since the executable is in the form of machine code, C++ program is very fast to run. However, the compilation process can be quite annoying. Every tiny little change like adding "int x = 0;" will take few seconds. 
  • OTcl: OTcl is an interpreted programming language. An OTcl program can run on the fly without the need for compilation. Upon execution, the interpreter translates OTcl instructions to machine code understandable to the operating system line by lin. Therefore, OTcl codes run more slowly than C++ codes do. The upside of OTcl codes is that every change takes effect immediately.
NS2 documentation suggests that C++ is slow to change but fast to run. OTcl is slow to run but fast to change.

Three Programming Style

In order to understand the need of having two languages, let's look at what a C++ programmers would do. Suppose, we program C++ codes in file prog.cc and prog.h. We will need to create an executable file prog.exe. Here are three programing styles:

1. Basic C++ Programming

With this style, you write C++ codes, compile them, and run the executable file. This is quite easy, and is what most programmers would do for small and simple tasks.

But again, as the program gets bigger, you will need to compile the program for every little change. This programming style lacks flexibility.

2. C++ Programming with Input Arguments

C++ allows programmers to supply input arguments: argc (number of input arguments) and argv (vector or array of input arguments).
Suppose you would like to simulate a network with n nodes fully connected with link speed s Mbps. With the first programming style, you will need to recompile the code every time you change the speed and/or the number of nodes.


With the use of input arguments, you may write a program to take the number of nodes and link speed as the first and second input arguments, respectively. You C++ file would look something like this.

#include<iostream>
using namespace std;
void main(int argc, char* argv[]){
    cout<< "The number of node is " <<argv[0]<<endl;
    cout<< "Speed of all links is " <<argv[1]<<endl;
}

The resulting executable program would have the following syntax.

prog <num_node> <link_speed>

For example, if you would like to have 10 nodes each connected with 3 Mbps links, you would run

prog 10 3

This style gives the users flexibility to change system parameters without compilation.

3. C++ Programming with a Configuration File

The problem with the second style is that the invocation can get quite messy as the number of input arguments increases. Imagine a scenario with 10 nodes whose connecting links have various speed. It would be quite difficult to input say different sets of 20 parameters for each run.

The third programming style improves the second one by specifying all the parameters in one file called "configuration file". Basically, it is the second style with the name of the configuration file as its only input parameter. The details of link speed, number of nodes, the topology, and so on, can be specify in the configuration file.

What about NS2?

As you might imagine, NS2 uses the last style, where the configuration file is an OTcl file called "Tcl Simulation Script". This file contains information about what you would like to simulate such as "set ns [new Simulator]", "set n0 [$ns node]", and so on. This Tcl simulation script is just the input configuration file to a C++ program, "~ns-2.35/ns.exe"

If you have a configuration file "simple.tcl", you can run NS2 by executing

ns simple.tcl

where ns is the executable program obtained from compiling the entire NS2 codes using "make" utility [see this link for details], and simple.tcl is an input configuration file of the executable file ns.

So Which on Should I Look into? C++ or OTcl?


Perhaps, this is the question you most want to ask. The above provide the motivation of having two languages. The guideline of selecting the language is provided below:
  • OTcl is a language for creating a network so that you don't have to recompile the codes every time you make changes to your simulation scenarios. It is also used to connect blocks (e.g., classifier, links, agent) within each NS2 components (e.g., node).
  • C++ defines internal mechanism of each block. It does things like packet forwarding, scheduling events, collecting statistic, and so on.
According to the OOP concept, every component should be self-contained and should provide interface to talk to other components. C++ is an OOP. Therefore, it defines these OOP functionalities.

What missing is how the components are linked together. YOU, as a programmer, should be the one who decide what NS2 components are needed and how they are linked together (e.g., Node 1 to Node 3). And, you would tell NS2 to do so (i.e., linking components) by using OTcl.


Summary

By now, you should know what NS2 have two languages. But if you don't or if you think it's too much to remember, just read the following. These two points are the summary of the above.

  • Use C++ for internal mechanism.
  • Use OTcl for configuration.
=======================================================
For more information about C++ and OTcl linkage see Chapter 3 in the following book from Springer:
T. Issaraiyakul and E. Hossain, “Introduction to Network Simulator NS2”, Springer 2009.

You may also find lecture notes and other resource at the following website: http://www.ece.ubc.ca/~teerawat/NS2.htm

No comments: