RIP路由选择算法
一、实验目的
模拟 RIP 路由选择算法的工作原理。
二、实验内容
针对上述网络结构,模拟 RIP 路由选择算法的工作原理,包括各个路由表的 建立更新过程,设计简单的图形界面。
三、实验原理
1.路由器刚启动时,对其路由表进行初始化。
2.路由表建立后,路由器周期性地向相邻路由器广播自己的路由表信息。假 设 R1,R2 是一个自治系统中两个相邻的路由器。如果 R1 中没有这项纪录,R1 在 路由表中增加该项,由于要经过 R2 转发,所以距离加 1;如果 R1 路由表中的一 项记录比 R2 发送的一项记录距离值减一还要大,R1 则在路由表中修改该项,距 离根据 R2 提供的值再加 1。 3.RIP 路由更新算法中,每隔 30 秒在相邻的路由器之间减缓依稀路由更新信 息,产生一个随机延迟信息,当记录在 180 秒内没有收到刷新信息时,,路由器 将该记录置为无效,如果无效的记录持续时间超过 120 秒,则请出该项信息。
四、实验代码
1.Client类:
package RIM; public class Client { public static StringBuffer source = new StringBuffer(); public static StringBuffer aim = new StringBuffer(); public static final int SUM = 10; private static Router[] routers = createRouter(); public static void makeAll() { for(Router r : routers){ source.append(r.getInfo()); } for(Router r : routers) r.send(); for(Router r : routers){ aim.append(r.getInfo()); } } public static void refresh(){ source = new StringBuffer(); for(Router r : routers){ source.append(r.getInfo()); } for(Router r : routers) r.send(); aim = new StringBuffer(); for(Router r : routers){ aim.append(r.getInfo()); } } public static Router[] createRouter(){ Router[] routers = new Router[SUM]; for (int i = 0; i < SUM; i++){ routers[i] = new Router("Router"+i); } routers[1].setCon(routers[2],routers[4]); routers[1].initList(new Item("1.2.0.0"),new Item("1.4.0.0")); routers[2].setCon(routers[1],routers[3],routers[5],routers[6]); routers[2].initList(new Item("1.2.0.0"),new Item("2.3.0.0"),new Item("2.5.0.0"),new Item("2.6.0.0")); routers[3].setCon(routers[2],routers[4],routers[6],routers[9]); routers[3].initList(new Item("2.3.0.0"),new Item("3.4.0.0"),new Item("3.6.0.0"),new Item("3.9.0.0")); routers[4].setCon(routers[1],routers[3],routers[7]); routers[4].initList(new Item("1.4.0.0"),new Item("3.4.0.0"),new Item("4.7.0.0")); routers[5].setCon(routers[2],routers[8]); routers[5].initList(new Item("2.5.0.0"),new Item("5.8.0.0")); routers[6].setCon(routers[2],routers[3],routers[8],routers[9]); routers[6].initList(new Item("2.6.0.0"),new Item("3.6.0.0"),new Item("6.8.0.0"),new Item("6.9.0.0")); routers[7].setCon(routers[4],routers[9]); routers[7].initList(new Item("4.7.0.0"),new Item("7.9.0.0")); routers[8].setCon(routers[5],routers[6],routers[9]); routers[8].initList(new Item("5.8.0.0"),new Item("6.8.0.0"),new Item("8.9.0.0")); routers[9].setCon(routers[3],routers[6],routers[7],routers[8]); routers[9].initList(new Item("3.9.0.0"),new Item("6.9.0.0"),new Item("7.9.0.0"),new Item("8.9.0.0")); return routers; } }
2.Item类:
package RIM; public class Item { String aimNet; int distance; String router; int clock; public Item(String aimNet){ this.aimNet = aimNet; this.distance = 0; this.router = "-"; this.clock = 0; } public Item(String aimNet,int distance,String router,int clock){ this.aimNet = aimNet; this.distance = distance; this.router = router; this.clock = clock; } public String getAimNet() { return aimNet; } public void setAimNet(String aimNet) { this.aimNet = aimNet; } public int getDistance() { return distance; } public void setDistance(int distance) { this.distance = distance; } public String getRouter() { return router; } public void setRouter(String router) { this.router = router; } public int getClock() { return clock; } public void setClock(int clock) { this.clock = clock; } @Override public String toString() { return "目的网络: "+ aimNet + "\t距离: " + distance + "\t下一跳路由器: " + router; } }
3.Router类:
package RIM; import java.util.ArrayList; public class Router { public static int time; ArrayList<Item> list = new ArrayList<Item>(); ArrayList<Router> routerList = new ArrayList<Router>(); String name; public Router(String name){ this.name = name; } public void setCon(Router ... router){ for (Router i : router){ routerList.add(i); } } public void initList(Item ... items){ for (Item i : items){ this.list.add(i); } } public void send(){ for (Item item : this.list){ if (item.distance != 0) item.clock += 30; if (item.clock > 180) item.clock = 0; } for (Router router : routerList){ router.recv(this); } } public void recv(Router router){ final ArrayList<Item> list = router.list; for (Item item : list){ boolean flag = true; for (int i = 0; i < this.list.size(); i++){ if (item.aimNet.equals(this.list.get(i).aimNet)){ flag = false; if (item.distance < this.list.get(i).distance - 1){//更新 this.list.get(i).distance = item.distance + 1; this.list.get(i).router = router.name; this.list.get(i).clock = 0; } } } if (flag){ this.list.add(new Item(item.aimNet,item.distance+1,router.name,0)); } } } public void printList(){ if (this.name.equals("Router0")) return; System.out.println(this.name + ":"); for (Item i : this.list) System.out.println(i); } public String getInfo(){ StringBuffer buffer = new StringBuffer(); if (this.name.equals("Router0")) return ""; buffer.append(this.name + " :\n"); for (Item i : this.list) { buffer.append(i.toString() + "\n"); } return buffer.toString(); } }
4.GUI类
package RIM; import javafx.application.Application; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextArea; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class GUI extends Application { private static TextArea source = new TextArea(); private static TextArea aim = new TextArea(); private static Label pre = new Label("原始路由表:"); private static Label after = new Label("更新后的路由表"); private static Button refresh = new Button("更新路由表"); private static VBox vbox = new VBox(15); private static HBox bar = new HBox(15); private static Scene scene = new Scene(vbox); @Override public void start(Stage primaryStage) throws Exception { bar.getChildren().addAll(pre,refresh); vbox.getChildren().addAll(bar,source,after,aim); vbox.setPadding(new Insets(15)); source.setText(Client.source.toString()); aim.setText(Client.aim.toString()); source.setEditable(false); aim.setEditable(false); refresh.setOnAction(action -> { Client.refresh(); pre.setText("更新前的路由表:"); source.setText(Client.source.toString()); aim.setText(Client.aim.toString()); }); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { Client.makeAll(); launch(args); } }