Distance Vector Routing [Matlab]
clc;
clear
all;
ack=0;
s=input('Enter the
source router no: ');
z=input('Enter the
destination node: ');
while
ack==0
%delay
routing tables for 5 modes
A=randint(1,5,100);%metric
may be delay or no of hops
B=randint(1,5,100);
C=randint(1,5,100);
D=randint(1,5,100);
E=randint(1,5,100);
A(1)=0;B(2)=0;C(3)=0;D(4)=0;E(5)=0;
r=vertcat(A,B,C,D,E);
disp('All routers
are updated with routing tables of neighbouring routers');
fprintf('The routing
tables for router %d is \n',s);
disp(r);
d(1,5)=0;
count=1;
for
i=s
for
j=1:5
if
j~=i
for
k=1:5
if k~=j
&& k~=i
for l =
1:5
if l~=k
&& l~=j && l~=i
for m =
1:5
if m~=k
&& m~=j && m~=i && m~=l
if(m==z)
d(count,:)=[i,j,k,l,m];
count=count+1;
end
end
end
end
end
end
end
end
end
end
count1
=1;
for
i=s
for
j=1:5
if
j~=i
for
k=1:5
if k~=j
&& k~=i
for
l=1:5
if l~=k
&& l~=j && l~=i
if
l==z
c(count1,:)=[i,j,k,l];
count1=
count1+1;
end
end
end
end
end
end
end
end
count2 =
1;
for
i=s
for
j=1:5
if
j~=i
for
k=1:5
if k~=j
&& k~=i
if
k==z
e(count2,:) =
[i,j,k,0];
count2= count2
+ 1;
end
end
end
end
end
end
%finding
the total delay of all possible paths
for
i=1:6
del(i)=0;
for
j=2:5
del(i)=del(i)+r(d(i,j-1),d(i,j));
end
end
count=i+1;
for
i=1:6
del(count)=0;
for
j=2:4
del(count)=del(count)+r(c(i,j-1),c(i,j));
end
count=count+1;
end
for
i=1:3
del(count)=0;
for
j=2:3
del(count)=del(count)+r(e(i,j-1),e(i,j));
end
count=count+1;
end
del(count)=r(s,z);
dc=
sort(del);
%possible
paths and latest delay paths
fprintf('Possble
path delayS');
for
i=1:count
for
j=1:count
if
dc(i)==del(j)
n(i)=j;
if
j<=6
fprintf('\n%d %d %d
%d %d %d %d',d(j,:),dc(i));
ma(i,:)=cellstr(num2str(d(j,:)));
elseif
j<=12
fprintf('\n%d %d %d
%d %d',c(j-6),dc(i));
ma(i,:)=cellstr(num2str(c(j-6)));
elseif
j<=15
fprintf('\n%d %d %d
%d',e((j-12),:),dc(i));
ma(i,:)=cellstr(num2str(e((j-12),:)));
elseif
j==16
fprintf('\n%d %d
%d',s,z,dc(i));
ma(i,:)=cellstr(num2str([s
z]));
end
end
end
end
temp=dc(1);
for
i=2:count
if(dc(i)<=temp)
temp=dc(i);
end
end
for
i=1:count
if(dc(i)==temp)
break;
end
end
disp(sprintf('\n\n\t
Least delay path is %s delay=%d',(char(ma(i))),dc(i)));
m(16,5)=0;
e=char(ma);
for
i=1:length(ma)
m(i,1)=str2double(cellstr(e(i,1)));
m(i,2)=str2double(cellstr(e(i,4)));
m(i,3)=str2double(cellstr(e(i,7)));
m(i,4)=str2double(cellstr(e(i,10)));
m(i,5)=str2double(cellstr(e(i,13)));
end
k=m(1,2);
disp(sprintf('Packet sent
to %d router',k));
disp('Updating
the routing table..');
if
(m(1,1)==s&&m(1,2)==z)
ack=1;
disp(sprintf('Packet sent
to the destination router %d',z));
end
s=m(1,2);
z=m(1,3);
end
No comments:
Post a Comment