Wednesday, 5 February 2014

Generate Results in NS using C++:

Generating results through C++ coding:
To extract results through C++ coding in NS, you need to know the architecture of C++ and Tcl and how they are linked. If you are not advanced user, If you are working only with Tcl scripting then you are not advised to use this.

Let us take a simple C++ code to extract value of delay.

Let us take constant delay between nodes.
If number of nodes and delay is given as input, then to calculate total delay, program in C++ is

// test.cc
#include <cstdio>
#include <cstdlib>
using namespace std;
int main(int argc, char* argv[]){
 (void) argc;
float delay = 0;
del = atof(argv[1]);
int i, num_nodes = atoi(argv[2]);
for(i = 1; i <num_nodes; i++)
delay += del;
printf("Overall Packet Delay is %f seconds, where del = %f,num_nodes =%d\n",delay,d_i,num_nodes);
}

Download file here here

From the c++  file, create executable file and run it.
i.e..,
 >> g++ test.cc -o test
>> test 2 20

We get following output from terminal.
>>Overall Packet Delay is 38.000000 seconds, where del = 2.000000,num_nodes = 20.

Here we didn't call Tcl file for simplicity. Even if Tcl is linked, we do same process, but we call the variables which contain required results.

If you want results like, throughput, delay, energy lost, then all you need to do is to implement the formula in the code and request for output. Bottom line is not to try this if you are new to network simulator.