1
0
mirror of https://gitee.com/willfree/mlsr.git synced 2026-06-17 15:40:24 +08:00

实现路由表类中的计算路由表函数

This commit is contained in:
free will
2022-05-18 17:01:33 +08:00
parent 853c6d679d
commit 0da380c22f
3 changed files with 125 additions and 61 deletions
+42 -27
View File
@@ -11,6 +11,7 @@ package Calculator
import (
"minlib/component"
"mlsr/lsa"
"sync"
)
@@ -39,11 +40,11 @@ type RouterMap struct {
// @receiver m
// @param routerName
//
func (m *RouterMap) Store(routerName *component.Identifier) {
func (m *RouterMap) Store(routerName *component.Identifier) {
m.mapLock.Lock()
uri := routerName.ToUri()
_,ok := m.uriToIdentifierMap[uri]
if !ok{
_, ok := m.uriToIdentifierMap[uri]
if !ok {
// 如果不存在,则将其存入三张map
m.uriToIdentifierMap[uri] = routerName
m.routerToNumberMap[uri] = m.mappingIndex
@@ -53,21 +54,35 @@ func (m *RouterMap) Store(routerName *component.Identifier) {
m.mapLock.Unlock()
}
func (m *RouterMap) CreateFromAdjLsdb(lsas []lsa.ILsa) error {
for i := 0; i < len(lsas); i++ {
// 1. 先将LSA解析为邻接LSA
adjLsa := (lsas[i]).(*lsa.AdjLsa)
// 2. 将每个邻接lsa的源路由,以及所有该源路由的邻接路由器,都加入到map中
m.Store(adjLsa.GetOriginRouter())
adjs := adjLsa.GetAdjList()
for j := 0; j < len(adjs); j++ {
m.Store(adjs[j].GetNeighborRouterIdentifier())
}
}
return nil
}
//
// Delete
// @Description: 删除一个路由器标识。在实际使用中用不到,故先小写其字母。
// @receiver m
// @param routerName
//
func (m *RouterMap) delete(routerName *component.Identifier) {
func (m *RouterMap) delete(routerName *component.Identifier) {
m.mapLock.Lock()
uri := routerName.ToUri()
value,ok := m.routerToNumberMap[uri]
if ok{
value, ok := m.routerToNumberMap[uri]
if ok {
// 如果存在,则将其从三张map中删除
delete(m.uriToIdentifierMap,uri)
delete(m.routerToNumberMap,uri)
delete(m.numberToRouterMap,value)
delete(m.uriToIdentifierMap, uri)
delete(m.routerToNumberMap, uri)
delete(m.numberToRouterMap, value)
}
m.mapLock.Unlock()
}
@@ -80,15 +95,15 @@ func (m *RouterMap) delete(routerName *component.Identifier) {
// @return *component.Identifier
// @return bool
//
func (m *RouterMap) GetRouterNameByMappingNo(index int) (*component.Identifier,bool) {
func (m *RouterMap) GetRouterNameByMappingNo(index int) (*component.Identifier, bool) {
m.mapLock.Lock()
defer m.mapLock.Unlock()
value,ok := m.numberToRouterMap[index]
if ok{
value, ok := m.numberToRouterMap[index]
if ok {
idenValue := m.uriToIdentifierMap[value]
return idenValue,true
return idenValue, true
}
return nil,false
return nil, false
}
//
@@ -99,11 +114,11 @@ func (m *RouterMap) GetRouterNameByMappingNo(index int) (*component.Identifier,b
// @return int
// @return bool
//
func (m *RouterMap) GetMappingNoByRouterName(routerName *component.Identifier) (int,bool) {
func (m *RouterMap) GetMappingNoByRouterName(routerName *component.Identifier) (int, bool) {
m.mapLock.Lock()
defer m.mapLock.Unlock()
value,ok := m.routerToNumberMap[routerName.ToUri()]
return value,ok
value, ok := m.routerToNumberMap[routerName.ToUri()]
return value, ok
}
//
@@ -122,17 +137,17 @@ func (m *RouterMap) GetMapSize() int {
// @Description: 必须先执行该函数,以初始化RouterMap
// @receiver m*
//
func (m* RouterMap) Init() {
if m.uriToIdentifierMap==nil{
m.uriToIdentifierMap=make(map[string]*component.Identifier)
func (m *RouterMap) Init() {
if m.uriToIdentifierMap == nil {
m.uriToIdentifierMap = make(map[string]*component.Identifier)
}
if m.routerToNumberMap==nil{
m.routerToNumberMap=make(map[string]int)
if m.routerToNumberMap == nil {
m.routerToNumberMap = make(map[string]int)
}
if m.numberToRouterMap==nil{
m.numberToRouterMap=make(map[int]string)
if m.numberToRouterMap == nil {
m.numberToRouterMap = make(map[int]string)
}
if m.mapLock==nil{
m.mapLock=new(sync.RWMutex)
if m.mapLock == nil {
m.mapLock = new(sync.RWMutex)
}
}
}
+41 -32
View File
@@ -24,21 +24,31 @@ type RoutingTableCalculator struct {
// 路由器数目
m_nRouters int
vNoLink int
links []int
vNoLink int
links []int
linkCosts []uint64
}
//
// SetRouterNum
// @Description: 设置路由器个数
// @receiver c
// @param n
//
func (c *RoutingTableCalculator) SetRouterNum(n int) {
c.m_nRouters = n
}
//
// allocateAdjMatrix
// @Description: 给邻接矩阵分配所需的空间
// @receiver c
//
func (c *RoutingTableCalculator) allocateAdjMatrix() {
c.adjMatrix=make([][]uint64,c.m_nRouters)
func (c *RoutingTableCalculator) allocateAdjMatrix() {
c.adjMatrix = make([][]uint64, c.m_nRouters)
// 根据路由器的数目,确定邻接矩阵的长度
for i := 0; i < c.m_nRouters; i++ {
c.adjMatrix[i]=make([]uint64,c.m_nRouters)
c.adjMatrix[i] = make([]uint64, c.m_nRouters)
}
}
@@ -47,10 +57,10 @@ func (c *RoutingTableCalculator) allocateAdjMatrix() {
// @Description: 为矩阵的每个单元设置非邻接成本(NON_ADJACENT_COST,如-12345),以确保内存安全。
// @receiver c
//
func (c *RoutingTableCalculator) initMatrix() {
func (c *RoutingTableCalculator) initMatrix() {
for i := 0; i < c.m_nRouters; i++ {
for j := 0; j < c.m_nRouters; j++ {
c.adjMatrix[0][0]=route.NON_ADJACENT_COST
c.adjMatrix[0][0] = route.NON_ADJACENT_COST
}
}
}
@@ -66,33 +76,33 @@ func (c *RoutingTableCalculator) makeAdjMatrix(lsdb *lsdb.Lsdb,
rp *RouterMap) error {
// 1. 在Map中表示每个LSA
// 先从Lsdb中取出邻接LSA
lsas,err := lsdb.GetLSAsByType(lsa.LsaADJACENCYType)
if err!=nil {
lsas, err := lsdb.GetLSAsByType(lsa.LsaADJACENCYType)
if err != nil {
return errors.New("No Found adj lsa in LSDB. ")
}
// 2. 再对每个lsa进行操作
for i := 0; i < len(lsas); i++ {
// 2.1 取出源路由器,找其索引值,作为邻接矩阵的行号
row,b := rp.GetMappingNoByRouterName(lsas[i].GetOriginRouter())
row, b := rp.GetMappingNoByRouterName(lsas[i].GetOriginRouter())
if !b {
return errors.New("No Found origin router in routerMap. ")
}
// 2.2 依次取出该源路由器对应的所有邻接路由器标识,以及链路开销,来构建邻接矩阵
adjlsa,ok := (lsas[i]).(*lsa.AdjLsa)
if !ok{
adjlsa, ok := (lsas[i]).(*lsa.AdjLsa)
if !ok {
return errors.New("Lsa from LSDB is not adjlsa. ")
}
adjs := adjlsa.GetAdjList()
for j := 0; j < len(adjs); j++ {
// 2.2.1 取列数
col,b := rp.GetMappingNoByRouterName(adjs[j].GetNeighborRouterIdentifier())
col, b := rp.GetMappingNoByRouterName(adjs[j].GetNeighborRouterIdentifier())
if !b {
return errors.New("No Found neighbor router in routerMap. ")
}
// 2.2.2 取开销
linkCost := adjs[j].AdjLsaLinkCost.LinkCost()
// 2.2.3 将开销放入邻接矩阵
if (row < c.m_nRouters) && (col < c.m_nRouters){
if (row < c.m_nRouters) && (col < c.m_nRouters) {
c.adjMatrix[row][col] = linkCost
}
}
@@ -109,7 +119,7 @@ func (c *RoutingTableCalculator) makeAdjMatrix(lsdb *lsdb.Lsdb,
// @receiver c 包含邻接矩阵数据的映射
// @param p
//
func (c *RoutingTableCalculator) writeAdjMatrixLog(p *sync.Map) {
func (c *RoutingTableCalculator) writeAdjMatrixLog(p *sync.Map) {
// todo 输出调试信息,验证功能是否正常运行
}
@@ -122,7 +132,7 @@ func (c *RoutingTableCalculator) writeAdjMatrixLog(p *sync.Map) {
func (c *RoutingTableCalculator) getNumOfLinkfromAdjMatrix(sRouter int) int {
num := 0
for i := 0; i < c.m_nRouters; i++ {
if (c.adjMatrix[sRouter][i]!=route.NON_ADJACENT_COST) && (i!=sRouter){
if (c.adjMatrix[sRouter][i] != route.NON_ADJACENT_COST) && (i != sRouter) {
num++
}
}
@@ -134,7 +144,7 @@ func (c *RoutingTableCalculator) getNumOfLinkfromAdjMatrix(sRouter int) int {
// @Description: 释放内存
// @receiver c
//
func (c *RoutingTableCalculator) freeAdjMatrix() {
func (c *RoutingTableCalculator) freeAdjMatrix() {
// todo 寄托于golang自己的GC机制
}
@@ -146,12 +156,12 @@ func (c *RoutingTableCalculator) freeAdjMatrix() {
// @param link 要调整的源的邻接
// @param linkCost 更改为的成本。
//
func (c *RoutingTableCalculator) adjustAdMatrix(source int,link int,linkCost uint64) {
func (c *RoutingTableCalculator) adjustAdMatrix(source int, link int, linkCost uint64) {
for i := 0; i < c.m_nRouters; i++ {
if i==link{
c.adjMatrix[source][i]=linkCost
}else{
c.adjMatrix[source][i]=route.NON_ADJACENT_COST
if i == link {
c.adjMatrix[source][i] = linkCost
} else {
c.adjMatrix[source][i] = route.NON_ADJACENT_COST
}
}
}
@@ -167,10 +177,10 @@ func (c *RoutingTableCalculator) adjustAdMatrix(source int,link int,linkCost uin
// @param linkCosts 存储链路开销的一个双指针数组
// @param source 被调整值的路由器
//
func (c *RoutingTableCalculator) getLinksFromAdjMatrix(source int) {
func (c *RoutingTableCalculator) getLinksFromAdjMatrix(source int) {
index := 0
for i := 0; i < c.m_nRouters; i++ {
if (c.adjMatrix[source][i]!=route.NON_ADJACENT_COST) && (i!=source){
if (c.adjMatrix[source][i] != route.NON_ADJACENT_COST) && (i != source) {
c.links[index] = i
c.linkCosts[index] = c.adjMatrix[source][i]
index++
@@ -183,8 +193,8 @@ func (c *RoutingTableCalculator) getLinksFromAdjMatrix(source int) {
// @Description: 分配一个足以容纳多路径计算临时值的数组
// @receiver c
//
func (c *RoutingTableCalculator) allocateLinks() {
c.links = make([]int,c.vNoLink)
func (c *RoutingTableCalculator) allocateLinks() {
c.links = make([]int, c.vNoLink)
}
//
@@ -192,8 +202,8 @@ func (c *RoutingTableCalculator) allocateLinks() {
// @Description: 分配内存
// @receiver c
//
func (c *RoutingTableCalculator) allocateLinkCosts() {
c.linkCosts = make([]uint64,c.vNoLink)
func (c *RoutingTableCalculator) allocateLinkCosts() {
c.linkCosts = make([]uint64, c.vNoLink)
}
//
@@ -201,7 +211,7 @@ func (c *RoutingTableCalculator) allocateLinkCosts() {
// @Description: 释放内存
// @receiver c
//
func (c *RoutingTableCalculator) freeLinks() {
func (c *RoutingTableCalculator) freeLinks() {
// todo 寄托于golang自己的GC机制
}
@@ -210,7 +220,7 @@ func (c *RoutingTableCalculator) freeLinks() {
// @Description: 释放内存
// @receiver c
//
func (c *RoutingTableCalculator) freeLinksCosts() {
func (c *RoutingTableCalculator) freeLinksCosts() {
// todo 寄托于golang自己的GC机制
}
@@ -220,7 +230,6 @@ func (c *RoutingTableCalculator) freeLinksCosts() {
// @receiver c
// @param nl
//
func (c *RoutingTableCalculator) setNoLink(nl int) {
func (c *RoutingTableCalculator) setNoLink(nl int) {
c.vNoLink = nl
}
+42 -2
View File
@@ -10,11 +10,14 @@ package route
import (
"errors"
common2 "minlib/common"
"minlib/component"
"minlib/encoding"
"mlsr/common"
"mlsr/extensions"
"mlsr/lsa"
"mlsr/lsdb"
"mlsr/route/Calculator"
"time"
)
@@ -83,7 +86,9 @@ func (t *RoutingTable) FindRoutingTableEntry(destRouter *component.Identifier) *
// @receiver t
//
func (t *RoutingTable) ScheduleRoutingTableCalculation() {
if !t.m_isRouteCalculationScheduled {
}
}
//
@@ -93,8 +98,43 @@ func (t *RoutingTable) ScheduleRoutingTableCalculation() {
//
func (t *RoutingTable) calculateLsRoutingTable() {
if t.lsdb.GetIsBuildAdjLsaScheduled() {
common2.LogFatal("Adjacency build is scheduled, routing table can not be calculated :(")
return
}
// 我们只在LS中检查这一点,因为我们从不移除自己的坐标LSA。而如果没有任何邻居,则移除自己的邻接LSA
if !t.m_ownAdjLsaExist {
return
}
t.clearRoutingTable()
// 构造路由映射表
lsas, err := t.lsdb.GetLSAsByType(lsa.LsaADJACENCYType)
if err != nil {
common2.LogFatal("Get Lsa in calculateLsRoutingTable error, ", err)
}
rMap := new(Calculator.RouterMap)
rMap.Init()
err = rMap.CreateFromAdjLsdb(lsas)
if err != nil {
common2.LogFatal("create RouterMap from lsdb error in calculateLsRoutingTable error, ", err)
}
nRouters := rMap.GetMapSize()
// 构造路由计算器
calculator := new(Calculator.LinkStateRoutingTableCalculator)
calculator.SetRouterNum(nRouters)
// 使用计算器去计算路由
err = calculator.CalculatePath(rMap, t, t.mlsrConfig, t.lsdb)
if err != nil {
common2.LogFatal("CalculatePath in calculateLsRoutingTable error, ", err)
}
// todo: 开始根据新计算的路由去更新NPT表
common2.LogInfo("Calling Update NPT With new Route")
//todo
}
//