您的位置:首页 > 编程语言 > Java开发

java实现基于距离向量算法 路由协议

2016-11-23 09:28 429 查看
算法就不多说了,该程序实现的功能:首先你要将网络结构转化成邻接矩阵,每个路由器对应一行,若路由器m与网络n直接相连,则m行n列为1,否则为0
该程序的路由器和网络都从0开始编号

首先创建Table类,代表路由器的路由表
public class Table {

public int hostId;//哪个路由器的路由表
public int netId;//目的网络的id

int distance;
public int nextJumpId;//下一跳路由器id
}

路由器类Router:
import java.util.ArrayList;
import java.util.List;
import java.util.jar.Attributes.Name;

import javax.swing.text.html.HTMLDocument.Iterator;

public class Router {
public int id;
public List<Table> tableList;

public Router(){
tableList=new ArrayList<Table>();
}

public void printTable(){
System.out.println("------------routTable of No "+id+"router is------------------");
for (Table table : tableList) {
System.out.println(table.hostId+" "+table.netId+"  "+table.distance+"  "+table.nextJumpId);
}
}
public boolean isNearby(Router router){
List<Table> tablesOther= router.tableList;
for (Table table : tablesOther) {
for (Table table2 : tableList) {
if ((table.netId==table2.netId)&&(table.distance==1)&&(table2.distance==1)&&(table.hostId!=table2.hostId)) {
return true;//判断相邻的条件:两个路由器,对于相同的网络,都直接相连(距离为1)
}
}
}
return false;

}
public void init(int[][]near){//初始化问题:输入邻接矩阵,对每一个路由器,循环感知相连的网络
tableList=new ArrayList<Table>();
for (int i=0;i< near[id].length;i++)//对矩阵的第id行做循环,注意net编号是从0开始的
{
if (near[id][i]==1) {//id路由器和i号网络相连
Table temptTable=new Table();
temptTable.hostId=id;
temptTable.netId=i;
temptTable.distance=1;
temptTable.nextJumpId=-1;//下一跳id为-1,表示直接交付

tableList.add(temptTable);
}
}
}

public void send(Router router){//向指定路由器发送table,假设已知道二者相连

router.update(tableList);//直接让对方更新自己的表就行了
}

public void update(List <Table> newList){
java.util.Iterator<Table > iterator1= newList.iterator();

while(iterator1.hasNext()) {//对于对方每一项,检查自己所有项
Table table1= iterator1.next();//取出对方项
List<Table>tempTableList=new ArrayList<Table>();//临时表,存储新增的项目
int flag=0;//标志变量,记录是否找到相同目的地址的项
java.util.Iterator<Table> iterator2=tableList.iterator();
while (iterator2.hasNext()) {
Table table2 = (Table) iterator2.next();//

if (table2.netId==table1.netId) {
flag=1;
if (table2.nextJumpId==table1.hostId) {
table2.distance=table1.distance+1;
}
else {
if (table2.distance>table1.distance+1) {
//table2.hostId=id;
//table2.netId=table1.netId;
table2.nextJumpId=table1.hostId;
table2.distance=table1.distance+1;
}
}
}

}
//自己的表遍历完了,如果flag还是0,才说明表中没有该项目
if (flag==0) {
flag=1;
Table tempTable=new Table();
tempTable.hostId=id;
tempTable.netId=table1.netId;
tempTable.nextJumpId=table1.hostId;
tempTable.distance=table1.distance+1;

tableList.add(tempTable);
}

}
//打印当前路由表
printTable();
}
}

Main函数:
import java.util.*;

public class M {

public static void main(String[] args){
System.out.println("请输入网络的个数");
Scanner scanner=new Scanner(System.in);
int netCount= scanner.nextInt();
System.out.println("请输入路由器个数");
Scanner scanner2 =new Scanner(System.in);
int routerCount=scanner2.nextInt();

Router []routerList=new Router[netCount];
int [][] Matrix=new int[routerCount][netCount];

System.out.println("请输入"+routerCount+"行"+netCount+"列的链接矩阵,行代表路由器序号,列代表网络序号,输入quit结束");

Scanner sc=new Scanner(System.in);

//建立一个可变型的Str
4000
ingBuffer,记录用户输入的字符
StringBuffer sb=new StringBuffer(sc.nextLine());

int i=0;
while(!(("quit").equals(sb.toString().trim())))//判定是否为约定的终止输入字符串
{
//StringBuffer转化为String数组
String[] ss=sb.toString().split(" ");
for(int j=0;j<5;j++){
Matrix[i][j]=Integer.parseInt(ss[j]);
}
i++;
sb.setLength(0);
sb.append(sc.nextLine());
}

//得到了m*n的矩阵matrix
//根据链接矩阵初始化路由器
scanner.close();
scanner2.close();
sc.close();
for (int j = 0; j < Matrix.length; j++) {
routerList[j]=new Router();
routerList[j].id=j;
routerList[j].init(Matrix);
}

while(true){
for (int j1 = 0; j1 < Matrix.length; j1++) {
for (int j2 = 0; j2 < Matrix.length; j2++) {
if (routerList[j2].isNearby(routerList[j1])) {
routerList[j2].send(routerList[j1]);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}
}

}
}


水平有限,欢迎指正
如有帮助,请不吝点赞
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: