实现步骤:
-
设计并搭建拓扑图:利用HTML和CSS完成拓扑图的设计和搭建,每个节点使用一个div元素表示,连接则使用SVG图形元素完成,大小、位置均自由设计。
-
数据结构及文件存储:将拓扑图的连接关系及链路开销存储在JSON文件中,使用JavaScript读取文件内容。
-
消息交换模拟:利用JavaScript实现节点之间的消息交换模拟,模拟消息发送和接收过程。
-
解析消息并更新路由表:解析接收到的链路状态消息,更新路由表,使用Dijkstra算法计算新的最短路径。
-
完成路由表输出:根据算法结果完成节点路由表的显示,同时需要支持用户修改链路开销的操作,并实时更新路由表。
-
在可视化界面上呈现整个算法的运行过程,包括拓扑图、消息交换、路由计算和路径的显示。
代码实现:(简化版)
- HTML与CSS代码
<div class="node" id="1"></div>
<div class="node" id="2"></div>
<div class="node" id="3"></div>
<div class="node" id="4"></div>
<svg>
<line id="1-2"></line>
<line id="2-3"></line>
<line id="3-4"></line>
<line id="1-3"></line>
<line id="2-4"></line>
</svg>
<style>
.node {
width: 40px;
height: 40px;
border-radius: 50%;
background-color: #ccc;
}
svg line {
stroke: #ccc;
stroke-width: 2px;
}
</style>
- JS代码
// 读取拓扑结构文件
fetch('topology.json')
.then(res => res.json())
.then(data => {
// 解析节点和链路信息
const nodes = data.nodes;
const links = data.links;
// 初始化路由表
const routingTable = initRoutingTable(nodes);
// 模拟消息交换
setInterval(() => {
const source = Math.ceil(Math.random() * nodes.length);
const target = Math.ceil(Math.random() * nodes.length);
if (source !== target) {
const message = generateMessage(source, target, links, nodes);
updateRoutingTable(routingTable, message);
showRoutingTable(routingTable);
}
}, 5000);
// 界面显示
showTopology(nodes, links);
showRoutingTable(routingTable);
});
// 初始化路由表
function initRoutingTable(nodes) {
const table = {};
for (let i = 1; i <= nodes.length; i++) {
table[i] = {};
for (let j = 1; j <= nodes.length; j++) {
table[i][j] = {
cost: Infinity,
path: null,
}
}
}
return table;
}
// 生成消息
function generateMessage(source, target, links, nodes) {
const message = {
source: source,
target: target,
links: {},
};
for (const link of links) {
if (link.from === source && link.to !== target) {
message.links[link.to] = link.cost;
} else if (link.from !== source && link.to === target) {
message.links[link.from] = link.cost;
}
}
return message;
}
// 更新路由表
function updateRoutingTable(table, message) {
const source = message.source;
const target = message.target;
// 更新开销
table[source][target].cost = 0;
for (const node in message.links) {
table[source][node].cost = message.links[node];
}
// Dijkstra算法
const visited = {};
const unvisited = {};
for (let i = 1; i <= Object.keys(table).length; i++) {
unvisited[i] = table[source][i].cost;
}
delete unvisited[source];
while (Object.keys(unvisited).length > 0) {
const minNode = findMinCostNode(unvisited);
const minCost = unvisited[minNode];
visited[minNode] = minCost;
delete unvisited[minNode];
for (const neighbor in table[minNode]) {
const cost = minCost + table[minNode][neighbor].cost;
if (cost < unvisited[neighbor]) {
table[source][neighbor].cost = cost;
unvisited[neighbor] = cost;
table[source][neighbor].path = minNode;
}
}
}
}
// 寻找未访问的节点中开销最小的节点
function findMinCostNode(unvisited) {
let minNode = null;
let minCost = Infinity;
for (const node in unvisited) {
if (unvisited[node] < minCost) {
minNode = node;
minCost = unvisited[node];
}
}
return minNode;
}
// 显示拓扑结构
function showTopology(nodes, links) {
for (const node of nodes) {
const n = document.getElementById(node.id);
n.style.left = `${node.x}px`;
n.style.top = `${node.y}px`;
}
for (const link of links) {
const line = document.getElementById(`${link.from}-${link.to}`);
line.setAttribute('x1', nodes[link.from - 1].x + 20);
line.setAttribute('y1', nodes[link.from - 1].y + 20);
line.setAttribute('x2', nodes[link.to - 1].x + 20);
line.setAttribute('y2', nodes[link.to - 1].y + 20);
}
}
// 显示路由表
function showRoutingTable(table) {
console.table(table);
}
- JSON文件:拓扑结构及链路开销存储
{
"nodes": [
{"id":1, "x":50, "y":50},
{"id":2, "x":200, "y":50},
{"id":3, "x":350, "y":50},
{"id":4, "x":200, "y":150}
],
"links": [
{"from":1, "to":2, "cost":2},
{"from":2, "to":3, "cost":3},
{"from":3, "to":4, "cost":2},
{"from":1, "to":3, "cost":3},
{"from":2, "to":4, "cost":1}
]
}