From e2803e573acc727053a442720697742e65dfeed6 Mon Sep 17 00:00:00 2001 From: free will <2647778488@qq.com> Date: Wed, 6 Jul 2022 16:18:47 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=90=8D=E7=A7=B0=E5=89=8D?= =?UTF-8?q?=E7=BC=80=E8=A1=A8=E7=9A=84=E6=89=93=E5=8D=B0=E7=8A=B6=E6=80=81?= =?UTF-8?q?=E5=87=BD=E6=95=B0=EF=BC=9B=E6=9B=B4=E6=96=B0Lsdb=E4=B8=AD?= =?UTF-8?q?=E5=85=B3=E4=BA=8E=E8=A7=A6=E5=8F=91=E6=9B=B4=E6=96=B0=E7=9A=84?= =?UTF-8?q?=E8=AF=AD=E5=8F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lsdb/Lsdb.go | 46 ++++++++++++++++++++++++++++++++--- route/NamePrefixTable.go | 25 ++++++++++++++++++- route/NamePrefixTable_test.go | 35 ++++++++++++++++++++++++++ route/RoutingTable.go | 4 +-- 4 files changed, 103 insertions(+), 7 deletions(-) diff --git a/lsdb/Lsdb.go b/lsdb/Lsdb.go index ed74ef5..fe26c28 100644 --- a/lsdb/Lsdb.go +++ b/lsdb/Lsdb.go @@ -326,7 +326,6 @@ func (l *Lsdb) InstallLsa(lsa lsa.ILsa) { return } // 2. 触发LSDB更新LSA的动作信号 OnLsdbModified(lsa, LsdbUpdate::UPDATED, namesToAdd, namesToRemove); - // todo 实现该方法:对比两个lsa,将其不同的部分取出来 updated, namesToAdd, namesToRemove := l.compareTwoLsa(lsaInDb, lsa) if updated && !l.OnLsdbModified.IsEmpty() { l.OnLsdbModified.Emit(lsa, UPDATED, namesToAdd, namesToRemove) @@ -343,12 +342,51 @@ func (l *Lsdb) InstallLsa(lsa lsa.ILsa) { // @receiver l // @param lsaA // @param lsaB -// @return bool +// @return bool 如果有不同,就返回true // @return []*component.Identifier // @return []component.Identifier // -func (l *Lsdb) compareTwoLsa(lsaA lsa.ILsa, lsaB lsa.ILsa) (bool, []*component.Identifier, []component.Identifier) { - return true, nil, nil +func (l *Lsdb) compareTwoLsa(lsaA lsa.ILsa, lsaB lsa.ILsa) (bool, []*component.Identifier, []*component.Identifier) { + namesToAdd := []*component.Identifier{} + namesToRemove := []*component.Identifier{} + // 前置判断,确定两个都是名称Lsa + if lsaA.GetType() != lsa.LsaNAMEType || lsaB.GetType() != lsa.LsaNAMEType { + return false, namesToAdd, namesToRemove + } + // 转换为名称Lsa + lsa1 := lsaA.(*lsa.NameLsa) + lsa2 := lsaB.(*lsa.NameLsa) + lsaPrefixs1 := lsa1.GetNamePrefixes() + lsaPrefixs2 := lsa2.GetNamePrefixes() + // lsa2有而lsa1没的,则为要添加的 + for i := 0; i < len(lsaPrefixs2); i++ { + if !existsIdentifier(lsaPrefixs2[i], lsaPrefixs1) { + namesToAdd = append(namesToAdd, lsaPrefixs2[i]) + } + } + // lsa1有而lsa2没的,则为要删除的 + for i := 0; i < len(lsaPrefixs1); i++ { + if !existsIdentifier(lsaPrefixs1[i], lsaPrefixs2) { + namesToRemove = append(namesToRemove, lsaPrefixs1[i]) + } + } + return true, namesToAdd, namesToRemove +} + +// +// existsIdentifier +// @Description: 判断是否有某个identifier +// @param target +// @param identifiers +// @return bool +// +func existsIdentifier(target *component.Identifier, identifiers []*component.Identifier) bool { + for i := 0; i < len(identifiers); i++ { + if identifiers[i].ToUri() == target.ToUri() { + return true + } + } + return false } // diff --git a/route/NamePrefixTable.go b/route/NamePrefixTable.go index fbcb221..1d5d649 100644 --- a/route/NamePrefixTable.go +++ b/route/NamePrefixTable.go @@ -9,6 +9,7 @@ package route import ( + "fmt" common2 "minlib/common" "minlib/component" "mlsr/lsa" @@ -38,6 +39,28 @@ type NamePrefixTable struct { // todo fib } +// +// PrintSelf +// @Description: 打印名称前缀表的状态。用于测试。 +// @receiver t +// +func (t *NamePrefixTable) PrintSelf() { + fmt.Println("------------------- Name Prefix Table begin: " + t.m_ownRouterName.ToUri() + " -----------------") + fmt.Println("-----------------" + " 1.路由表项池 begin " + "-----------------") + fmt.Println("路由表项池大小:", len(t.m_rtpool)) + for _, rtpe := range t.m_rtpool { + fmt.Println(rtpe.ToString()) + } + fmt.Println("-----------------" + " 1.路由表项池 end " + "-----------------") + fmt.Println("----------------" + " 2.名称前缀列表 start " + "----------------") + for i := 0; i < len(t.nptEntryList); i++ { + fmt.Println(t.nptEntryList[i].ToString()) + } + fmt.Println("----------------" + " 2.名称前缀列表 end " + "----------------") + t.m_routingTable.PrintSelf() + fmt.Println("------------------- Name Prefix Table end: " + t.m_ownRouterName.ToUri() + " -----------------") +} + // // Init // @Description: 名称前缀表初始化 @@ -73,7 +96,7 @@ func (t *NamePrefixTable) Init(ownRouterName *component.Identifier, common2.LogInfo("NamePrefixTable bindLsdbModifiedFunc updateType: ", updateType) namesToAdd := []*component.Identifier{} namesToRemove := []*component.Identifier{} - if updateType == lsdb.REMOVED { + if updateType == lsdb.UPDATED { namesToAdd = args[2].([]*component.Identifier) namesToRemove = args[3].([]*component.Identifier) } diff --git a/route/NamePrefixTable_test.go b/route/NamePrefixTable_test.go index eb06a7f..973054d 100644 --- a/route/NamePrefixTable_test.go +++ b/route/NamePrefixTable_test.go @@ -120,7 +120,42 @@ func TestNamePrefixTable_FinalTest(t *testing.T) { // @param t // func TestNamePrefixTable_AddEntry(t *testing.T) { + // 1. 建立前置结构体(lsdb等) + // 配置文件初始化 + mlsrConfig, err := common.ParseConfig(testConfigPath) + if err != nil { + common2.LogInfo("配置文件解析错误") + } + // 调度器初始化 + sche := new(lsdb.MlsrScheduler) + sche.Init() + // LogicFace初始化(假设初始化,暂时用不到) + face := new(logicface.LogicFace) + // LSDB初始化 + mlsdb := new(lsdb.Lsdb) + mlsdb.Init(mlsrConfig, sche, face) + // 2. 打印lsdb中所有本机lsa + lsas, _ := mlsdb.LsaContainer.GetLSAsByType(lsa.LsaADJACENCYType) + for i := 0; i < len(lsas); i++ { + common2.LogInfo(lsas[i].ToString()) + } + lsas, _ = mlsdb.LsaContainer.GetLSAsByType(lsa.LsaNAMEType) + for i := 0; i < len(lsas); i++ { + common2.LogInfo(lsas[i].ToString()) + } + fmt.Println("lsdb是否正在忙于构建lsdb: ", mlsdb.GetIsBuildAdjLsaScheduled()) + + // 3. 路由表初始化 + rt := new(RoutingTable) + rt.Init(mlsrConfig, sche, mlsdb) + + // 4. 名称前缀表初始化 + npt := new(NamePrefixTable) + npt.Init(mlsrConfig.GetRouterPrefix(), rt, rt.AfterRoutingChange, mlsdb.OnLsdbModified) + + // 5. 安装本机adjlsa => 将触发两个事件:A. 安排15s后做路由表计算;B.触发名称前缀表更新(本机lsa不做更新) + mlsdb.BuildAndInstallOwnAdjLsa() } // diff --git a/route/RoutingTable.go b/route/RoutingTable.go index 65c1bfa..4c4db2b 100644 --- a/route/RoutingTable.go +++ b/route/RoutingTable.go @@ -56,16 +56,16 @@ type RoutingTable struct { // @receiver t // func (t *RoutingTable) PrintSelf() { - fmt.Println("----------- Table beg ---------") + fmt.Println("----------- Routing Table begin ---------") fmt.Println("len: ", len(t.rTable)) for i := 0; i < len(t.rTable); i++ { fmt.Println(t.rTable[i].ToString()) } - fmt.Println("----------- Table end ---------") fmt.Println("路由计算间隔: ", t.m_routingCalcInterval.String()) fmt.Println("是否正在计算: ", t.m_isRoutingTableCalculating) fmt.Println("是否安排计算: ", t.m_isRouteCalculationScheduled) fmt.Println("本机路由器邻接lsa存在状态: ", t.m_ownAdjLsaExist) + fmt.Println("----------- Routing Table end ---------") } //