⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 node.cpp

📁 网络 DV 算法源代码。。。。比较简单。。。。就是模拟路由器分布式选路
💻 CPP
字号:
#include 	"DVsim.h"

/*------------------------
*	rtinit0
*------------------------*/
void rtinit0() 
{
	struct 	 rtpkt rt;
	int i,j;
	//
	for(i=0;i<NODES;i++)
		for(j=0;j<NODES;j++)
			dt[0].costs[j][i]=INF;
	//
	dt[0].costs[0][0]=0;
	dt[0].costs[1][1]=1;
	dt[0].costs[2][2]=2;
	//
	rt.sourceid=0;
	//
	rt.mincost[0]=0;
	rt.mincost[1]=1;
	rt.mincost[2]=2;
	rt.mincost[3]=INF;
	rt.mincost[4]=INF;
	//
	for(j=1;j<3;j++)	
		{
			rt.destid=j;
			tolayer2 (rt);
			send0++;
		}
}
/*------------------------
*	rtinit1
*------------------------*/
void rtinit1() 
{
	struct 	 rtpkt rt;
	int i,j;
 
	for(i=0;i<NODES;i++)
		for(j=0;j<NODES;j++)
			dt[1].costs[j][i]=INF;
	//
	dt[1].costs[0][0]=1;
	dt[1].costs[1][1]=0;
	dt[1].costs[3][3]=15;
	dt[1].costs[4][4]=5;
	//
	rt.sourceid=1;
	//
	rt.mincost[0]=1;
	rt.mincost[1]=0;
	rt.mincost[2]=INF;
	rt.mincost[3]=15;
	rt.mincost[4]=5;
 
	for(j=0;j<NODES;j++)
		if(j!=1&&j!=2)
		{
			rt.destid=j;
			tolayer2 (rt);
			send1++;
		}
}
/*------------------------
*	rtinit2
*------------------------*/
void rtinit2() 
{
	struct 	 rtpkt rt;
	int i,j;
	 
	for(i=0;i<NODES;i++)
		for(j=0;j<NODES;j++)
			dt[2].costs[j][i]=INF;
	//
	dt[2].costs[0][0]=2;
	dt[2].costs[2][2]=0;
	dt[2].costs[3][3]=1;
	dt[2].costs[4][4]=2;

	rt.sourceid=2;
	//
	rt.mincost[0]=2;
	rt.mincost[1]=INF;
	rt.mincost[2]=0;
	rt.mincost[3]=1;
	rt.mincost[4]=2;
	//
	
		
	for(j=0;j<NODES;j++)
		if(j!=1&&j!=2)
		{
			rt.destid=j;
			tolayer2 (rt);
			send2++;
		}
}
/*------------------------
*	rtinit3
*------------------------*/
void rtinit3() 
{
	struct 	 rtpkt rt;
	int i,j;
	 
	for(i=0;i<NODES;i++)
		for(j=0;j<NODES;j++)
			dt[3].costs[j][i]=INF;
	//
	dt[3].costs[4][4]=10;
	dt[3].costs[1][1]=15;
	dt[3].costs[2][2]=1;
	dt[3].costs[3][3]=0;


	rt.sourceid=3;
	//
	rt.mincost[0]=INF;
	rt.mincost[1]=15;
	rt.mincost[2]=1;
	rt.mincost[3]=0;
	rt.mincost[4]=10;
	//
	
		
	for(j=0;j<NODES;j++)
		if(j!=3&&j!=0)
		{
			rt.destid=j;
			tolayer2 (rt);
			send3++;
		}
}
void rtinit4() 
{
	struct 	 rtpkt rt;
	int i,j;
	 
	for(i=0;i<NODES;i++)
		for(j=0;j<NODES;j++)
			dt[4].costs[j][i]=INF;
	//
	dt[4].costs[1][1]=5;
	dt[4].costs[2][2]=2;
	dt[4].costs[3][3]=10;
	dt[4].costs[4][4]=0;


	rt.sourceid=4;
	//
	rt.mincost[0]=INF;
	rt.mincost[1]=5;
	rt.mincost[2]=2;
	rt.mincost[3]=10;
	rt.mincost[4]=0;
	//
	
		
	for(j=0;j<NODES;j++)
		if(j!=0&&j!=4)
		{
			rt.destid=j;
			tolayer2 (rt);
			send4++;
		}
}
 

/*------------------------
*	rtupdate0
*------------------------*/
void rtupdate0 (struct rtpkt *rcvdpkt)
{
	int i,j;
	int NN=0;
	int temp[NODES]={0};
	int flag=1;
	struct 	 rtpkt rt;
	//
	recv0++;
	//

	for( i=0;i<NODES;i++)
		temp[i]=dt[NN].costs[i][rcvdpkt->sourceid];


	///////////////////////////////////////	
	for( i=0;i<NODES;i++)
		if(i!=NN)
			dt[NN].costs[i][rcvdpkt->sourceid] = 
							dt[NN].costs[rcvdpkt->sourceid][rcvdpkt->sourceid]+rcvdpkt->mincost[i]
							<
							dt[NN].costs[i][rcvdpkt->sourceid]?
							dt[NN].costs[rcvdpkt->sourceid][rcvdpkt->sourceid]+rcvdpkt->mincost[i]
							:
							dt[NN].costs[i][rcvdpkt->sourceid];


	////////////////////////////////////////
	
	for(i=0;i<NODES;i++)
		if(temp[i]!=dt[NN].costs[i][rcvdpkt->sourceid])
		{
			flag=0;
			break;
		}
	/////////////////////////////////////////
	if(flag==0)//changed
	{		
		rt.sourceid=0;
		
		for(j=0;j<NODES;j++)
			rt.mincost[j]= dt[NN].costs[j][rcvdpkt->sourceid];

		rt.destid=1;
		tolayer2 (rt);
		
		rt.destid=2;
		tolayer2 (rt);

		send0+=2;
	}

}
/*------------------------
*	rtupdate1
*------------------------*/
void rtupdate1 (struct rtpkt *rcvdpkt)
{
	int i,j;
	int NN=1;
	int temp[NODES]={0};
	int flag=1;
	struct 	 rtpkt rt;
	//
	recv1++;
	//

	for( i=0;i<NODES;i++)
		temp[i]=dt[NN].costs[i][rcvdpkt->sourceid];


	///////////////////////////////////////	
	for( i=0;i<NODES;i++)
		if(i!=NN)
			dt[NN].costs[i][rcvdpkt->sourceid] = 
							dt[NN].costs[rcvdpkt->sourceid][rcvdpkt->sourceid]+rcvdpkt->mincost[i]
							<
							dt[NN].costs[i][rcvdpkt->sourceid]?
							dt[NN].costs[rcvdpkt->sourceid][rcvdpkt->sourceid]+rcvdpkt->mincost[i]
							:
							dt[NN].costs[i][rcvdpkt->sourceid];


	////////////////////////////////////////
	
	for(i=0;i<NODES;i++)
		if(temp[i]!=dt[NN].costs[i][rcvdpkt->sourceid])
		{
			flag=0;
			break;
		}
//////////////

	if(flag==0)//changed
	{		
		rt.sourceid=NN;
		
		for(j=0;j<NODES;j++)
			rt.mincost[j]= dt[NN].costs[j][rcvdpkt->sourceid];

		rt.destid=0;
		tolayer2 (rt);
		
		rt.destid=3;
		tolayer2 (rt);

		rt.destid=4;
		tolayer2 (rt);

		send1+=3;
	}

}
/*------------------------
*	rtupdate2
*------------------------*/
void rtupdate2 (struct rtpkt *rcvdpkt)
{
	int i,j;
	int NN=2;
	int temp[NODES]={0};
	int flag=1;
	struct 	 rtpkt rt;
	//
	recv2++;
	//

	for( i=0;i<NODES;i++)
		temp[i]=dt[NN].costs[i][rcvdpkt->sourceid];


	///////////////////////////////////////	
	for( i=0;i<NODES;i++)
		if(i!=NN)
			dt[NN].costs[i][rcvdpkt->sourceid] = 
							dt[NN].costs[rcvdpkt->sourceid][rcvdpkt->sourceid]+rcvdpkt->mincost[i]
							<
							dt[NN].costs[i][rcvdpkt->sourceid]?
							dt[NN].costs[rcvdpkt->sourceid][rcvdpkt->sourceid]+rcvdpkt->mincost[i]
							:
							dt[NN].costs[i][rcvdpkt->sourceid];


	////////////////////////////////////////
	
	for(i=0;i<NODES;i++)
		if(temp[i]!=dt[NN].costs[i][rcvdpkt->sourceid])
		{
			flag=0;
			break;
		}
		/////
	if(flag==0)//changed
	{		
		rt.sourceid=NN;
		
		for(j=0;j<NODES;j++)
			rt.mincost[j]= dt[NN].costs[j][rcvdpkt->sourceid];

		rt.destid=0;
		tolayer2 (rt);
		
		rt.destid=3;
		tolayer2 (rt);

		rt.destid=4;
		tolayer2 (rt);

		send2+=3;
	}

}
/*------------------------
*	rtupdate3
*------------------------*/
void rtupdate3 (struct rtpkt *rcvdpkt)
{
	int i,j;
	int NN=3;
	int temp[NODES]={0};
	int flag=1;
	struct 	 rtpkt rt;
	//
	recv3++;
	//

	for( i=0;i<NODES;i++)
		temp[i]=dt[NN].costs[i][rcvdpkt->sourceid];


	///////////////////////////////////////	
	for( i=0;i<NODES;i++)
		if(i!=NN)
			dt[NN].costs[i][rcvdpkt->sourceid] = 
							dt[NN].costs[rcvdpkt->sourceid][rcvdpkt->sourceid]+rcvdpkt->mincost[i]
							<
							dt[NN].costs[i][rcvdpkt->sourceid]?
							dt[NN].costs[rcvdpkt->sourceid][rcvdpkt->sourceid]+rcvdpkt->mincost[i]
							:
							dt[NN].costs[i][rcvdpkt->sourceid];


	////////////////////////////////////////
	
	for(i=0;i<NODES;i++)
		if(temp[i]!=dt[NN].costs[i][rcvdpkt->sourceid])
		{
			flag=0;
			break;
		}
		/////
	if(flag==0)//changed
	{		
		rt.sourceid=NN;
		
		for(j=0;j<NODES;j++)
			rt.mincost[j]= dt[NN].costs[j][rcvdpkt->sourceid];

		rt.destid=1;
		tolayer2 (rt);
		
		rt.destid=2;
		tolayer2 (rt);

		rt.destid=4;
		tolayer2 (rt);

		send3 += 3;
	}

}
/*------------------------
*	rtupdate4
*------------------------*/
void rtupdate4 (struct rtpkt *rcvdpkt)
{
	int i,j;
	int NN=4;
	int temp[NODES]={0};
	int flag=1;
	struct 	 rtpkt rt;
	//
	recv4++;
	//

	for( i=0;i<NODES;i++)
		temp[i]=dt[NN].costs[i][rcvdpkt->sourceid];


	///////////////////////////////////////	
	for( i=0;i<NODES;i++)
		if(i!=NN)
			dt[NN].costs[i][rcvdpkt->sourceid] = 
							dt[NN].costs[rcvdpkt->sourceid][rcvdpkt->sourceid]+rcvdpkt->mincost[i]
							<
							dt[NN].costs[i][rcvdpkt->sourceid]?
							dt[NN].costs[rcvdpkt->sourceid][rcvdpkt->sourceid]+rcvdpkt->mincost[i]
							:
							dt[NN].costs[i][rcvdpkt->sourceid];


	////////////////////////////////////////
	
	for(i=0;i<NODES;i++)
		if(temp[i]!=dt[NN].costs[i][rcvdpkt->sourceid])
		{
			flag=0;
			break;
		}
		/////
	if(flag==0)//changed
	{		
		rt.sourceid=NN;
		
		for(j=0;j<NODES;j++)
			rt.mincost[j]= dt[NN].costs[j][rcvdpkt->sourceid];

		rt.destid=1;
		tolayer2 (rt);
		
		rt.destid=2;
		tolayer2 (rt);

		rt.destid=3;
		tolayer2 (rt);

		send4 += 3;
	}

}
//
/*----------------------------------------------------
*	printdt_0 
*
*	Prints distance table at node 0.
*
*	(Format will be different for other nodes.)
*-----------------------------------------------------*/

void printdt_0 (struct distance_table *t)
{
   printf("\t\t  ----------------------\n");
   printf("\t\t      |         via     \n");
   printf("\t\t   D0 |    1          2 \n");
   printf("\t\t  ----|-----------------\n");
   printf("\t\t     1|  %3d         %3d\n", t->costs[1][1], t->costs[1][2]);
   printf("\t\t     2|  %3d         %3d\n",  t->costs[2][1] ,  t->costs[2][2] );
   printf("\t\tdest 3|  %3d         %3d\n",  t->costs[3][1] ,  t->costs[3][2] );
   printf("\t\t     4|  %3d         %3d\n",  t->costs[4][1] ,  t->costs[4][2] );
   printf("\t\t  ----------------------\n");
}
/*----------------------------------------------------
*	printdt_1 
*
*	Prints distance table at node 1.
*
*	(Format will be different for other nodes.)
*-----------------------------------------------------*/
void printdt_1 (struct distance_table *t)
{
   printf("\t\t --------------------------\n");
   printf("\t\t      |         via     \n");
   printf("\t\t   D1 |    0     3     4\n");
   printf("\t\t ----|-----------------\n");
   printf("\t\t     0| %3d   %3d   %3d\n", t->costs[0][0], t->costs[0][3], t->costs[0][4]);
   printf("\t\t     2| %3d   %3d   %3d\n", t->costs[2][0], t->costs[2][3], t->costs[2][4]);
   printf("\t\tdest 3| %3d   %3d   %3d\n", t->costs[3][0], t->costs[3][3], t->costs[3][4]);
   printf("\t\t     4| %3d   %3d   %3d\n", t->costs[4][0], t->costs[4][3], t->costs[4][4]);
   printf("\t\t --------------------------\n");
}
/*----------------------------------------------------
*	printdt_2
*
*	Prints distance table at node 2.
*
*	(Format will be different for other nodes.)
*-----------------------------------------------------*/
void printdt_2 (struct distance_table *t)
{printf("\t\t --------------------------\n");
   printf("\t\t      |         via     \n");
   printf("\t\t   D2 |    0     3     4\n");
   printf("\t\t ----|-----------------\n");
   printf("\t\t     0| %3d   %3d   %3d\n", t->costs[0][0], t->costs[0][3], t->costs[0][4]);
   printf("\t\t     1| %3d   %3d   %3d\n", t->costs[1][0], t->costs[1][3], t->costs[1][4]);
   printf("\t\tdest 3| %3d   %3d   %3d\n", t->costs[3][0], t->costs[3][3], t->costs[3][4]);
   printf("\t\t     4| %3d   %3d   %3d\n", t->costs[4][0], t->costs[4][3], t->costs[4][4]);
   printf("\t\t --------------------------\n");
}
/*----------------------------------------------------
*	printdt_3
*
*	Prints distance table at node 3.
*
*	(Format will be different for other nodes.)
*-----------------------------------------------------*/
void printdt_3 (struct distance_table *t)
{
   printf("\t\t --------------------------\n");
   printf("\t\t      |         via     \n");
   printf("\t\t   D3 |    1     2     4\n");
   printf("\t\t ----|-----------------\n");
   printf("\t\t     0| %3d   %3d   %3d\n", t->costs[0][1], t->costs[0][2], t->costs[0][4]);
   printf("\t\t     1| %3d   %3d   %3d\n", t->costs[1][1], t->costs[1][2], t->costs[1][4]);
   printf("\t\tdest 2| %3d   %3d   %3d\n", t->costs[2][1], t->costs[2][2], t->costs[2][4]);
   printf("\t\t     4| %3d   %3d   %3d\n", t->costs[4][1], t->costs[4][2], t->costs[4][4]);
   printf("\t\t --------------------------\n");
}
/*----------------------------------------------------
*	printdt_4
*
*	Prints distance table at node 4.
*
*	(Format will be different for other nodes.)
*-----------------------------------------------------*/
void printdt_4 (struct distance_table *t)
{
  printf("\t\t --------------------------\n");
   printf("\t\t      |         via     \n");
   printf("\t\t   D4 |    1     2     3\n");
   printf("\t\t ----|-----------------\n");
   printf("\t\t     0| %3d   %3d   %3d\n", t->costs[0][1], t->costs[0][2], t->costs[0][3]);
   printf("\t\t     1| %3d   %3d   %3d\n", t->costs[1][1], t->costs[1][2], t->costs[1][3]);
   printf("\t\tdest 2| %3d   %3d   %3d\n", t->costs[2][1], t->costs[2][2], t->costs[2][3]);
   printf("\t\t     3| %3d   %3d   %3d\n", t->costs[3][1], t->costs[3][2], t->costs[3][3]);
   printf("\t\t --------------------------\n");
}



/*--------------------------------------------------------------
*	linkhandler0
*---------------------------------------------------------------*/
void linkhandler0 (int linkid, int newcost) {}
void linkhandler1 (int linkid, int newcost) {}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -