From 30e5044896ffd510fc455902b91372a903622662 Mon Sep 17 00:00:00 2001 From: free will <2647778488@qq.com> Date: Mon, 6 Jun 2022 16:50:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E9=80=9A=E8=BF=87=E4=BA=86?= =?UTF-8?q?=E5=AE=9A=E6=97=B6=E5=88=A0=E9=99=A4=E5=85=B6=E4=BB=96=E8=B7=AF?= =?UTF-8?q?=E7=94=B1=E5=99=A8=E7=9A=84LSA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/MlsrConfig.go | 2 +- lsdb/Lsdb.go | 42 +++++++++++++++++---- lsdb/Lsdb_test.go | 89 ++++++++++++++++++++++++++++++++++++++++++++ mlsrConf.ini | 2 +- 4 files changed, 126 insertions(+), 9 deletions(-) diff --git a/common/MlsrConfig.go b/common/MlsrConfig.go index e0591c8..1d97932 100644 --- a/common/MlsrConfig.go +++ b/common/MlsrConfig.go @@ -252,7 +252,7 @@ func (c *MlsrConfig) Init() { c.GeneralConfig.SyncInterestLifetime = 60 * 1000 // 1000-120*1000,范围是1秒到2分钟。默认1分钟 // 2. Log - c.LogConfig.LogLevel = "INFO" + c.LogConfig.LogLevel = "ALL" c.LogConfig.ReportCaller = true c.LogConfig.LogFormat = "text" c.LogConfig.LogFilePath = "" diff --git a/lsdb/Lsdb.go b/lsdb/Lsdb.go index 5bcd8c4..f928c97 100644 --- a/lsdb/Lsdb.go +++ b/lsdb/Lsdb.go @@ -70,6 +70,11 @@ func (l *Lsdb) Init(mc *common.MlsrConfig, ms *MlsrScheduler, l.face = _face // 序列号管理器 l.sequencingManager = GetInstance() + // 存储器 + err := l.LsaContainer.Init() + if err != nil { + common2.LogFatal("lsdb.LsaContainer init error, ", err.Error()) + } // todo 构造sync @@ -94,7 +99,8 @@ func (l *Lsdb) Init(mc *common.MlsrConfig, ms *MlsrScheduler, //} // 构建并安装自己的名称LSA - l.BuildAndInstallOwnNameLsa() + // todo 先测试其它项 + //l.BuildAndInstallOwnNameLsa() } // @@ -235,7 +241,7 @@ func (l *Lsdb) FindLsa(routerIdentifier *component.Identifier, } // -// IsLsaNew +// isLsaNew // @Description: 返回来自某个路由器的seq是否表示新的LSA。 // @receiver l // @param routerIdentifier @@ -243,15 +249,37 @@ func (l *Lsdb) FindLsa(routerIdentifier *component.Identifier, // @param seqNo // @return bool // -func (l *Lsdb) IsLsaNew(routerIdentifier *component.Identifier, +func (l *Lsdb) isLsaNew(routerIdentifier *component.Identifier, lsaType lsa.LsaType, seqNo uint64) bool { iLsa := l.FindLsa(routerIdentifier, lsaType) - if iLsa.GetSeqNo() >= seqNo { - return false + // 没找到,说明是最新的 + if iLsa == nil { + return true } - return true + // 找到了,且LSDB中的该lsa序列号小于要查的lsa,说明是最新的 + if iLsa.GetSeqNo() < seqNo { + return true + } + return false } +// +// IsLsaNew +// @Description: 返回来自某个路由器的seq是否表示新的LSA。 +// @receiver l +// @param fLsa +// @return bool +// +func (l *Lsdb) IsLsaNew(fLsa lsa.ILsa) bool { + return l.isLsaNew(fLsa.GetOriginRouter(), fLsa.GetType(), fLsa.GetSeqNo()) +} + +// +// InstallLsa +// @Description: 将LSA安装到lsdb中 +// @receiver l +// @param lsa +// func (l *Lsdb) InstallLsa(lsa lsa.ILsa) { timeToExpire := l.lsaRefreshTime @@ -265,7 +293,7 @@ func (l *Lsdb) InstallLsa(lsa lsa.ILsa) { // 查看数据库中是否已有该LSA源路由器所发出的LSA lsaInDb := l.FindLsa(lsa.GetOriginRouter(), lsa.GetType()) - if lsa == nil { + if lsaInDb == nil { // 如果没有: common2.LogDebug("Adding Lsa ", lsa.ToString()) // 1. 就插入该LSA diff --git a/lsdb/Lsdb_test.go b/lsdb/Lsdb_test.go index 50186c9..8a25ffc 100644 --- a/lsdb/Lsdb_test.go +++ b/lsdb/Lsdb_test.go @@ -10,9 +10,14 @@ package lsdb import ( "fmt" + common2 "minlib/common" + "minlib/component" "minlib/logicface" "mlsr/common" + "mlsr/lsa" + "strconv" "testing" + "time" ) const testConfigPath = "D:\\" + common.DefaultConfFileName @@ -64,3 +69,87 @@ func TestUnixTime(t *testing.T) { timeTemplate := "2006-01-02 15:04:05" // 时间戳常规类型 fmt.Println(lsdb.uintToTime(timeExpire).Format(timeTemplate)) } + +// +// TestLsdb_ScheduleLsaExpiration +// @Description: 测试安排LSA过期事件 +// @param t +// +func TestLsdb_ScheduleLsaExpiration(t *testing.T) { + // 1. 初始化LSDB + // 配置文件初始化 + mlsrConfig, err := common.ParseConfig(testConfigPath) + if err != nil { + common2.LogInfo("配置文件解析错误") + } + // 调度器初始化 + sche := new(MlsrScheduler) + sche.Init() + // LogicFace初始化(假设初始化,暂时用不到) + face := new(logicface.LogicFace) + // LSDB初始化 + lsdb := new(Lsdb) + lsdb.Init(mlsrConfig, sche, face) + + // 2. 生成一个邻接Lsa + adjLsa := new(lsa.AdjLsa) + adjLsa.SetLsaExpirationTime(2000) + adjLsa.SetLsaSequenceNumber(1234) + adjLsa.LsaOriginRouterIdentifier, _ = component.CreateIdentifierByString("/min/pku") + // 构造邻接信息部分 + // 先构造一个info + linkCost := lsa.AdjLsaLinkCost{} + linkCost.SetLinkCost(100) + faceUri := lsa.AdjLsaLogicFaceUri{} + faceUri.SetLogicFaceUri("uri") + faceId := lsa.AdjLsaLogicFaceId{} + faceId.SetLogicFaceId(77) + iden, _ := component.CreateIdentifierByString("/pku/router1") + adjInfo := lsa.NewAdjLsaAdjacencyInfo(linkCost, faceUri, faceId, iden, 123, 1) + adjLsa.Insert(adjInfo) + + // 3. 插入Lsdb + err = lsdb.Emplace(adjLsa) + if err != nil { + common2.LogInfo("插入错误") + } + common2.LogInfo("插入成功") + + // 4. 设置自动删除任务【3s+10s=13s】 + timeDur := 3 * time.Second + lsdb.ScheduleLsaExpiration(adjLsa, timeDur) + common2.LogInfo("???") + + // 5. 不断循环查询lsdb中是否存在该lsa + i := 0 + for true { + b := lsdb.DoesLsaExist(adjLsa) + common2.LogInfo("是否存在:" + strconv.FormatBool(b)) + time.Sleep(time.Second) + i++ + } +} + +// +// TestLsdb_InstallLsa +// @Description: 测试安装lsa +// @param t +// +func TestLsdb_InstallLsa_DoesExist(t *testing.T) { + // 1. 初始化LSDB + // 配置文件初始化 + mlsrConfig, err := common.ParseConfig(testConfigPath) + if err != nil { + fmt.Println("配置文件解析错误") + } + // 调度器初始化 + sche := new(MlsrScheduler) + sche.Init() + // LogicFace初始化(假设初始化,暂时用不到) + face := new(logicface.LogicFace) + // LSDB初始化 + lsdb := new(Lsdb) + lsdb.Init(mlsrConfig, sche, face) + + // 2. +} diff --git a/mlsrConf.ini b/mlsrConf.ini index 7d9d585..3af5564 100644 --- a/mlsrConf.ini +++ b/mlsrConf.ini @@ -9,7 +9,7 @@ SyncProtocol = min-sync SyncInterestLifetime = 60000 [Log] -LogLevel = INFO +LogLevel = ALL ReportCaller = true LogFormat = text LogFilePath =