Simulation of AODV in MANET with Random waypoint mobility (TC)
#include<stdio.h>
#include<graphics.h>
#include<math.h>
#include<conio.h>
#include<time.h>
#include<dos.h>
#include<stdlib.h>
int packlost=0;
double txdist=0.0,rxdist=0.0;
double dist(int x1,int y1,int x2,int y2)
{
double d=0.0;
d=((double)((x2-x1)*(x2-x1))+(double)((y1-y2)*(y1-y2)));
if(d<0)
d=d*(double)(-1);
d=sqrt(d);
return d;
}
void main()
{
double d;
int count=0;
int n=20,a[20],l;
char mobno[20][3]={"0","1","2","3","4","5","6","7","8","9","10","11","12",
"13","14","15","16","17","18","19"};
int server,rec,tx,k;
int gd=DETECT,gm,i,j,x,y;
int x1,x2,y1,y2,pos;
initgraph(&gd,&gm,"d:\\tc\\bgi");
randomize();
line(20,20,420,20);
line(20,420,420,420);
i=20;
j=20;
up:n=rand()%20;
if(n<3)
goto up;
for(j=0;j<=10;j++)
{
line(i,20,i,420);
i=i+40;
}
i=20;
for(j=0;j<=10;j++)
{
line(20,i,420,i);
i=i+40;
}
for(l=0;l<=n;l++)
{
a[l]=rand()%100;
pos=a[l];
x1=40+(pos%10)*40;
pos=pos/10;
y1=(pos)*40+40;
setcolor(RED);
circle(x1,y1,13);
outtextxy(x1,y1,mobno[l]);
}
/**********/
server=rand()%n;
printf("server=%d ",server);
top:tx=rand()%n;
if(tx==server)
goto top;
top1: rec=rand()%n;
if(rec==server)
goto top1;
if(rec==tx)
goto top1;
/*transmitter*/
printf("rec=%d",rec);
printf(" tx=%d ",tx);
setcolor(GREEN);
pos=a[tx];
x1=40+(pos%10)*40;
pos=pos/10;
y1=(pos)*40+40;
pos=a[server];
x2=40+(pos%10)*40;
pos=pos/10;
y2=(pos)*40+40;
d=dist(x2,y2,x1,y1);
txdist=d;
delay(4500);
setcolor(YELLOW);
outtextxy(x2,y2,"S");
setcolor(GREEN);
line(x1,y1,x2,y2);
setcolor(GREEN);
/*reciever*/
pos=a[rec];
x1=40+(pos%10)*40;
pos=pos/10;
y1=(pos)*40+40;
delay(1500);
d=dist(x2,y2,x1,y1);
rxdist=d;
line(x1,y1,x2,y2);
delay(1500);
/* simulate it thrice*/
for(k=0;k<3;k++)
{
cleardevice();
i=20;
setcolor(WHITE);
for(j=0;j<=10;j++)
{
line(i,20,i,420);
i=i+40;
}
i=20;
for(j=0;j<=10;j++)
{
line(20,i,420,i);
i=i+40;
}
for(l=0;l<=n;l++)
{
a[l]=rand()%100;
pos=a[l];
x1=40+(pos%10)*40;
pos=pos/10;
y1=(pos)*40+40;
setcolor(RED);
circle(x1,y1,13);
outtextxy(x1,y1,mobno[l]);
}
delay(1400);
pos=a[tx];
x1=40+(pos%10)*40;
pos=pos/10;
y1=(pos)*40+40;
pos=a[server];
x2=40+(pos%10)*40;
pos=pos/10;
y2=(pos)*40+40;
d=dist(x1,y1,x2,y2);
count=0;
while(d>txdist)
{
printf("RREQ the rerequest for router");
do
{
count++;
if(count==2)
{
count=-1;
break;
}
server=rand()%n;
}while(server==tx);
pos=a[server];
x2=40+(pos%10)*40;
pos=pos/10;
y2=(pos)*40+40;
d=dist(x1,y1,x2,y2);
if(count==-1)
break;
}
delay(4500);
setcolor(YELLOW);
outtextxy(x2,y2,"S");
setcolor(GREEN);
line(x1,y1,x2,y2);
setcolor(GREEN);
pos=a[rec];
x1=40+(pos%10)*40;
pos=pos/10;
y1=(pos)*40+40;
delay(1500);
d=dist(x1,y1,x2,y2);
if(d>rxdist)
{
packlost++;
outtextxy(100,10,"packet loss");
delay(1500);
}
else
{
line(x1,y1,x2,y2);
delay(1500);
}
}
getch();
closegraph();
textcolor(RED);
gotoxy(10,10);
cprintf("Packet lost=%d",packlost);
getch();
}