mirror of
https://gitee.com/willfree/mlsr.git
synced 2026-06-15 19:24:47 +08:00
使用五节点拓扑来测试链路状态路由计算器,执行单路径计算,测试通过
This commit is contained in:
@@ -15,6 +15,7 @@ import (
|
||||
"mlsr/common"
|
||||
"mlsr/lsa"
|
||||
"mlsr/lsdb"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
//
|
||||
@@ -23,8 +24,6 @@ import (
|
||||
//
|
||||
type LinkStateRoutingTableCalculator struct {
|
||||
RoutingTableCalculator
|
||||
NO_NEXT_HOP int
|
||||
|
||||
// 私有变量区
|
||||
// 表示源路由器想要到达某路由器,其最短路径上经过的上一个路由器。
|
||||
m_parent []int
|
||||
@@ -32,6 +31,33 @@ type LinkStateRoutingTableCalculator struct {
|
||||
m_distance []uint64
|
||||
}
|
||||
|
||||
//
|
||||
// ToString
|
||||
// @Description: 转string
|
||||
// @receiver c
|
||||
// @return string
|
||||
//
|
||||
func (c *LinkStateRoutingTableCalculator) ToString() string {
|
||||
str := ""
|
||||
str += "每个节点最佳路径的上一跳:["
|
||||
for i := 0; i < len(c.m_parent); i++ {
|
||||
str += "(" + strconv.Itoa(i)
|
||||
str += ", "
|
||||
str += strconv.Itoa(c.m_parent[i])
|
||||
str += ")"
|
||||
}
|
||||
str += "\n"
|
||||
str += "每个节点最佳路径的总开销:["
|
||||
for i := 0; i < len(c.m_distance); i++ {
|
||||
str += "(" + strconv.Itoa(i)
|
||||
str += ", "
|
||||
str += strconv.FormatUint(c.m_distance[i], 10)
|
||||
str += ")"
|
||||
}
|
||||
str += "\n"
|
||||
return str
|
||||
}
|
||||
|
||||
// 一些在最短路径计算时会用到的常量
|
||||
const (
|
||||
EMPTY_PARENT = -12345 // 没有父节点 -12345
|
||||
@@ -61,6 +87,9 @@ func (c *LinkStateRoutingTableCalculator) CalculatePath(routerMap *RouterMap,
|
||||
c.initMatrix()
|
||||
// 根据Lsdb邻接信息构造邻接矩阵
|
||||
_ = c.makeAdjMatrix(lsdb, routerMap)
|
||||
// 为路由计算中记录路径和距离的两个数组分配空间
|
||||
c.allocateParent()
|
||||
c.allocateDistance()
|
||||
// 从配置文件中获取源路由器前缀,并根据routerMap获取其映射号
|
||||
sourceRouter, b := routerMap.GetMappingNoByRouterName(mConfig.GetRouterPrefix())
|
||||
if !b {
|
||||
@@ -74,7 +103,7 @@ func (c *LinkStateRoutingTableCalculator) CalculatePath(routerMap *RouterMap,
|
||||
if mConfig.GetMaxFacesPerPrefix() == 1 {
|
||||
// 在单路径的情况下,我们可以简单地运行Dijkstra算法。
|
||||
c.doDijkstraPathCalculation(sourceRouter)
|
||||
// 将新的下一跳通知路由表。// todo: 注意,当前版本中,本机的邻接信息是从配置文件中读取出来的。
|
||||
// 将新的下一跳通知路由表。// 注意,当前版本中,本机的邻接信息是从配置文件中读取出来的。
|
||||
rs, ns := c.addAllLsNextHopsToRoutingTable(mConfig.GetAdjacencys(), routerMap, sourceRouter)
|
||||
return rs, ns, nil
|
||||
} else {
|
||||
@@ -169,9 +198,10 @@ func (c *LinkStateRoutingTableCalculator) doDijkstraPathCalculation(sourceRouter
|
||||
|
||||
//
|
||||
// sortQueueByDistance
|
||||
// @Description: 对列表中的元素进行交换排序
|
||||
// @Description: 对切片中的元素进行交换排序
|
||||
// 注意,切片是引用传递,因此这里实际修改了Q的值。
|
||||
// @receiver c
|
||||
// @param Q 要排序元素的数组
|
||||
// @param Q 要排序元素的切片
|
||||
// @param dist 包含距离的数组
|
||||
// @param start 列表中要排序的第一个元素
|
||||
// @param element 列表元素数目
|
||||
|
||||
@@ -11,16 +11,54 @@ package route
|
||||
import (
|
||||
"fmt"
|
||||
common2 "minlib/common"
|
||||
"minlib/component"
|
||||
"minlib/logicface"
|
||||
"mlsr/common"
|
||||
"mlsr/lsa"
|
||||
"mlsr/lsdb"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
//
|
||||
// testCreateAdjLsa
|
||||
// @Description: 构造邻接lsa
|
||||
// @param uri
|
||||
// @return *lsa.AdjLsa
|
||||
//
|
||||
func testCreateAdjLsa(uri string) *lsa.AdjLsa {
|
||||
base := new(lsa.AdjLsa)
|
||||
timeExpire := time.Now()
|
||||
timeExpire = timeExpire.Add(30 * time.Minute)
|
||||
base.SetLsaExpirationTime(uint64(timeExpire.UnixMilli()))
|
||||
base.SetLsaSequenceNumber(1234)
|
||||
base.LsaOriginRouterIdentifier, _ = component.CreateIdentifierByString(uri)
|
||||
return base
|
||||
}
|
||||
|
||||
//
|
||||
// testCreateAdjInfo
|
||||
// @Description: 构造邻接信息
|
||||
// @param cost
|
||||
// @param uri
|
||||
// @return *lsa.AdjLsaAdjacencyInfo
|
||||
//
|
||||
func testCreateAdjInfo(cost uint64, uri string) *lsa.AdjLsaAdjacencyInfo {
|
||||
linkCost := lsa.AdjLsaLinkCost{}
|
||||
linkCost.SetLinkCost(cost)
|
||||
faceUri := lsa.AdjLsaLogicFaceUri{}
|
||||
faceUri.SetLogicFaceUri("uri")
|
||||
faceId := lsa.AdjLsaLogicFaceId{}
|
||||
faceId.SetLogicFaceId(77)
|
||||
iden, _ := component.CreateIdentifierByString(uri)
|
||||
adjInfo := lsa.NewAdjLsaAdjacencyInfo(linkCost, faceUri, faceId, iden, 123, 1)
|
||||
return adjInfo
|
||||
}
|
||||
|
||||
//
|
||||
// TestLinkStateRoutingTableCalculator_CalculateOnePath
|
||||
// @Description: 测试普通的路径计算
|
||||
// @Description: 测试普通的最短路径计算。测试已通过。
|
||||
// @param t
|
||||
//
|
||||
func TestLinkStateRoutingTableCalculator_CalculateOnePath(t *testing.T) {
|
||||
@@ -43,8 +81,41 @@ func TestLinkStateRoutingTableCalculator_CalculateOnePath(t *testing.T) {
|
||||
mlsdb.Init(mlsrConfig, sche, face)
|
||||
// 安装本机adjlsa
|
||||
mlsdb.BuildAndInstallOwnAdjLsa()
|
||||
// todo 传入一些其它虚拟lsa,这些lsa与本机lsa共同形成一个小型路由拓扑
|
||||
|
||||
// 传入一些其它虚拟lsa,这些lsa与本机lsa共同形成一个小型路由拓扑
|
||||
// 路由器B
|
||||
adjLsaB := testCreateAdjLsa("/routerB")
|
||||
adjInfoBA := testCreateAdjInfo(10, "/min/pkusz/routerA")
|
||||
adjInfoBE := testCreateAdjInfo(50, "/routerE")
|
||||
adjLsaB.Insert(adjInfoBA)
|
||||
adjLsaB.Insert(adjInfoBE)
|
||||
// 路由器C
|
||||
adjLsaC := testCreateAdjLsa("/routerC")
|
||||
adjInfoCA := testCreateAdjInfo(100, "/min/pkusz/routerA")
|
||||
adjInfoCE := testCreateAdjInfo(10, "/routerE")
|
||||
adjInfoCD := testCreateAdjInfo(60, "/routerD")
|
||||
adjLsaC.Insert(adjInfoCA)
|
||||
adjLsaC.Insert(adjInfoCD)
|
||||
adjLsaC.Insert(adjInfoCE)
|
||||
// 路由器E
|
||||
adjLsaE := testCreateAdjLsa("/routerE")
|
||||
adjInfoEB := testCreateAdjInfo(50, "/routerB")
|
||||
adjInfoEC := testCreateAdjInfo(10, "/routerC")
|
||||
adjInfoED := testCreateAdjInfo(20, "/routerD")
|
||||
adjLsaE.Insert(adjInfoEB)
|
||||
adjLsaE.Insert(adjInfoEC)
|
||||
adjLsaE.Insert(adjInfoED)
|
||||
// 路由器D
|
||||
adjLsaD := testCreateAdjLsa("/routerD")
|
||||
adjInfoDA := testCreateAdjInfo(30, "/min/pkusz/routerA")
|
||||
adjInfoDC := testCreateAdjInfo(60, "/routerC")
|
||||
adjInfoDE := testCreateAdjInfo(20, "/routerE")
|
||||
adjLsaD.Insert(adjInfoDA)
|
||||
adjLsaD.Insert(adjInfoDC)
|
||||
adjLsaD.Insert(adjInfoDE)
|
||||
mlsdb.InstallLsa(adjLsaB)
|
||||
mlsdb.InstallLsa(adjLsaC)
|
||||
mlsdb.InstallLsa(adjLsaD)
|
||||
mlsdb.InstallLsa(adjLsaE)
|
||||
// 1.2 构造RouterMap
|
||||
lsas, err := mlsdb.GetLSAsByType(lsa.LsaADJACENCYType)
|
||||
if err != nil {
|
||||
@@ -71,14 +142,28 @@ func TestLinkStateRoutingTableCalculator_CalculateOnePath(t *testing.T) {
|
||||
}
|
||||
|
||||
// 1.4 打印输出路由表计算器
|
||||
fmt.Println(rMap.ToString())
|
||||
fmt.Println(rtc.ToString())
|
||||
fmt.Println("源路由器的邻接数目:", rtc.getNumOfLinkfromAdjMatrix(0))
|
||||
fmt.Println("另一个路由器的邻接数目:", rtc.getNumOfLinkfromAdjMatrix(1))
|
||||
|
||||
// 2. 执行单源最短路径算法
|
||||
rtc.allocateParent()
|
||||
rtc.allocateDistance()
|
||||
rtc.doDijkstraPathCalculation(0)
|
||||
// todo 查看执行结果
|
||||
|
||||
// 查看执行结果
|
||||
// 计算器中的上一跳路径,以及开销
|
||||
fmt.Println(rtc.ToString())
|
||||
// 计算器获取的针对每个目的路由器的下一跳,及开销。
|
||||
names, nexthops := rtc.addAllLsNextHopsToRoutingTable(mlsrConfig.GetAdjacencys(), rMap, 0)
|
||||
for i := 0; i < len(names); i++ {
|
||||
fmt.Println(names[i].ToUri() + " ")
|
||||
}
|
||||
fmt.Println("")
|
||||
for i := 0; i < len(nexthops); i++ {
|
||||
fmt.Println(nexthops[i].LogicFaceUri() + "-" +
|
||||
strconv.FormatUint(nexthops[i].RouteCost.RouteCost(), 10) + " ")
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
+1
-1
@@ -50,7 +50,7 @@ func (m *RouterMap) ToString() string {
|
||||
dataString := string(dataType)
|
||||
s += dataString + " "
|
||||
// 第二个map
|
||||
dataType, _ = json.Marshal(m.routerToNumberMap)
|
||||
dataType, _ = json.Marshal(m.numberToRouterMap)
|
||||
dataString = string(dataType)
|
||||
s += dataString + " "
|
||||
return s
|
||||
|
||||
Reference in New Issue
Block a user