diff --git a/common/MlsrConfigParameters.go b/common/MlsrConfigParameters.go index 0fed91c..acd5d78 100644 --- a/common/MlsrConfigParameters.go +++ b/common/MlsrConfigParameters.go @@ -47,6 +47,11 @@ type MlsrConfigParameters struct { // 时间:FIB更新 m_RoutingCalcInterval time.Duration + + // 邻接信息列表 + m_adjl *lsa.AdjLsaAdjacenctList + // 命名前缀列表 + m_npl *lsa.NameLsaNamePrefixList } // MlsrConfig初始化 @@ -58,6 +63,9 @@ func (mcp *MlsrConfigParameters) Init(config *MlsrConfig) { _ = mcp.buildRouterAndSyncUserPrefix() // 构建时间间隔 mcp.genDurations() + // 构建邻接表和名称前缀表 + mcp.InitAdjacencyList() + mcp.InitNamePrefixList() } // @@ -360,13 +368,12 @@ func (mcp *MlsrConfigParameters) GetMaxFacesPerPrefix() int { } // -// GetAdjacencyList -// @Description: 从配置文件中获取邻接信息列表 +// InitAdjacencyList +// @Description: 从配置文件中初始化邻接信息列表 // @receiver mcp -// @return []*lsa.AdjLsaAdjacencyInfo // -func (mcp *MlsrConfigParameters) GetAdjacencyList() []*lsa.AdjLsaAdjacencyInfo { - adjInfos := []*lsa.AdjLsaAdjacencyInfo{} +func (mcp *MlsrConfigParameters) InitAdjacencyList() { + mcp.m_adjl = new(lsa.AdjLsaAdjacenctList) neighborConfigs := mcp.mlsrconf.neighbors for i := 0; i < len(neighborConfigs); i++ { // 根据配置文件构造每条邻接链路信息 @@ -385,19 +392,12 @@ func (mcp *MlsrConfigParameters) GetAdjacencyList() []*lsa.AdjLsaAdjacencyInfo { faceId.SetLogicFaceId(0) adjInfo := lsa.NewAdjLsaAdjacencyInfo(linkCost, faceUri, faceId, iden, 0, lsa.STATUS_INACTIVE) // 加入信息列表 - adjInfos = append(adjInfos, adjInfo) + mcp.m_adjl.Insert(adjInfo) } - return adjInfos } -// -// GetNamePrefixList -// @Description: 从配置文件中获取名称前缀信息列表 -// @receiver mcp -// @return []*lsa.NameLsaNamePrefixInfo -// -func (mcp *MlsrConfigParameters) GetNamePrefixList() []*lsa.NameLsaNamePrefixInfo { - infos := []*lsa.NameLsaNamePrefixInfo{} +func (mcp *MlsrConfigParameters) InitNamePrefixList() { + mcp.m_npl = new(lsa.NameLsaNamePrefixList) // 从配置文件读取广播前缀 adverPrefixs := mcp.mlsrconf.AdvertisingConfig.Prefixs for i := 0; i < len(adverPrefixs); i++ { @@ -410,10 +410,49 @@ func (mcp *MlsrConfigParameters) GetNamePrefixList() []*lsa.NameLsaNamePrefixInf } namePrefixInfo.NamePrefix = iden namePrefixInfo.InsertSource("") - // 将该名称前缀信息加入切片 - infos = append(infos, namePrefixInfo) + // 将该名称前缀信息加入列表 + mcp.m_npl.InsertNamePrefixInfo(namePrefixInfo) } - return infos +} + +// +// GetAdjacencyList +// @Description: 从配置文件中获取邻接信息列表 +// @receiver mcp +// @return []*lsa.AdjLsaAdjacencyInfo +// +func (mcp *MlsrConfigParameters) GetAdjacencyList() []*lsa.AdjLsaAdjacencyInfo { + return mcp.m_adjl.GetAdjList() +} + +// +// GetAdjacencys +// @Description: 获取整体的邻接信息 +// @receiver mcp +// @return *lsa.AdjLsaAdjacenctList +// +func (mcp *MlsrConfigParameters) GetAdjacencys() *lsa.AdjLsaAdjacenctList { + return mcp.m_adjl +} + +// +// GetNamePrefixList +// @Description: 从配置文件中获取名称前缀信息列表 +// @receiver mcp +// @return []*lsa.NameLsaNamePrefixInfo +// +func (mcp *MlsrConfigParameters) GetNamePrefixList() []*lsa.NameLsaNamePrefixInfo { + return mcp.m_npl.GetNamePrefixList() +} + +// +// GetNamePrefixs +// @Description: 获取整体的名称前缀信息 +// @receiver mcp +// @return *lsa.NameLsaNamePrefixList +// +func (mcp *MlsrConfigParameters) GetNamePrefixs() *lsa.NameLsaNamePrefixList { + return mcp.m_npl } func (mcp *MlsrConfigParameters) GetKeyChain() { diff --git a/lsa/AdjLsaAdjacencyList.go b/lsa/AdjLsaAdjacencyList.go index da495ce..490d5d2 100644 --- a/lsa/AdjLsaAdjacencyList.go +++ b/lsa/AdjLsaAdjacencyList.go @@ -58,7 +58,7 @@ func (l *AdjLsaAdjacenctList) ToString() string { resString += "[ " for i := 0; i < l.Size(); i++ { resString += l.GetAdjList()[i].ToString() - if i < (l.Size()-1){ + if i < (l.Size() - 1) { resString += ", " } } @@ -74,8 +74,8 @@ func (l *AdjLsaAdjacenctList) ToString() string { // @return bool // func (l *AdjLsaAdjacenctList) IsNeighbor(routerIdentifier *component.Identifier) bool { - for i:=0; i= interestRetryNo { + // 如果邻居全部都没有激活,且兴趣包尝试次数超出最大值,就删掉本机的邻接LSA + nTimedOutNeighbors++ } } - return true + if nTimedOutNeighbors == len(l.lsaAdjacencys) { + return true + } else { + // 既没有激活邻居,又还在尝试查看邻居情况,则继续安排构建邻接LSA + return false + } } // @@ -214,11 +218,11 @@ func (l *AdjLsaAdjacenctList) IsAdjLsaBuildable(interestRetryNo uint32) bool { // @return *AdjLsaAdjacencyInfo // @return error // -func (l *AdjLsaAdjacenctList) GetAdjacency(routerIdentifier *component.Identifier) (*AdjLsaAdjacencyInfo,error){ - if index,err:=l.findIndexByRouterIdentifier(routerIdentifier);err!=nil{ - return nil,err - }else{ - return l.lsaAdjacencys[index],nil +func (l *AdjLsaAdjacenctList) GetAdjacency(routerIdentifier *component.Identifier) (*AdjLsaAdjacencyInfo, error) { + if index, err := l.findIndexByRouterIdentifier(routerIdentifier); err != nil { + return nil, err + } else { + return l.lsaAdjacencys[index], nil } } @@ -229,9 +233,9 @@ func (l *AdjLsaAdjacenctList) GetAdjacency(routerIdentifier *component.Identifie // @return int // func (l *AdjLsaAdjacenctList) GetNumOfActiveNeighbor() int { - numbers:=0 - for i:=0; i=0; i-- { - if l.lsaAdjacencys[i]==nil { + for i := len(l.lsaAdjacencys) - 1; i >= 0; i-- { + if l.lsaAdjacencys[i] == nil { continue } - tmpLen,err := l.lsaAdjacencys[i].WireEncode(encoder) + tmpLen, err := l.lsaAdjacencys[i].WireEncode(encoder) if err != nil { return 0, err } @@ -405,7 +409,7 @@ func (l *AdjLsaAdjacenctList) WireEncode(encoder *encoding.Encoder) (int,error) } totalLength += tmpLen - return totalLength,nil + return totalLength, nil } // @@ -436,4 +440,4 @@ func (l *AdjLsaAdjacenctList) WireDecode(block *encoding.Block) error { } l.lsaAdjacencys = adjacencyContainer return nil -} \ No newline at end of file +} diff --git a/lsa/NameLsaNamePrefixList.go b/lsa/NameLsaNamePrefixList.go index 61b45ea..0d00042 100644 --- a/lsa/NameLsaNamePrefixList.go +++ b/lsa/NameLsaNamePrefixList.go @@ -22,6 +22,16 @@ type NameLsaNamePrefixList struct { lsaNamePrefixs LsaNamePrefixContainer } +// +// GetNamePrefixList +// @Description: 获取名称前缀列表 +// @receiver l +// @return []*NameLsaNamePrefixInfo +// +func (l *NameLsaNamePrefixList) GetNamePrefixList() []*NameLsaNamePrefixInfo { + return l.lsaNamePrefixs +} + // // InsertNamePrefixList // @Description: 插入一张名称前缀信息列表 diff --git a/lsdb/LsaContainer/buntdb/LsaContainer_BuntDB.go b/lsdb/LsaContainer/buntdb/LsaContainer_BuntDB.go index 18362be..3bee435 100644 --- a/lsdb/LsaContainer/buntdb/LsaContainer_BuntDB.go +++ b/lsdb/LsaContainer/buntdb/LsaContainer_BuntDB.go @@ -154,6 +154,14 @@ func (bdb *LsaContainer_BuntDB) Exists(lsa lsa.ILsa) bool { return false } +// +// ExistsRouterNameAndType +// @Description: 判断是否存在某个lsa +// @receiver bdb +// @param routerIdentifier +// @param lsaType +// @return bool +// func (bdb *LsaContainer_BuntDB) ExistsRouterNameAndType(routerIdentifier *component.Identifier, lsaType lsa.LsaType) bool { errRes := bdb.db.View(func(tx *buntdb.Tx) error { // 获取key @@ -210,7 +218,7 @@ func (bdb *LsaContainer_BuntDB) Emplace(lsa lsa.ILsa) error { // // Erase -// @Description: 删除一个lsa:根据routerIdentifier和lsaType +// @Description: 删除一个lsa // @receiver bdb // @param lsa // @@ -223,6 +231,24 @@ func (bdb *LsaContainer_BuntDB) EraseLsa(lsa lsa.ILsa) error { return err } +// +// EraseLsaByNameAndType +// @Description: 删除一个lsa:根据routerIdentifier和lsaType +// @receiver bdb +// @param routerIdentifier +// @param lsaType +// @return error +// +func (bdb *LsaContainer_BuntDB) EraseLsaByNameAndType(routerIdentifier *component.Identifier, + lsaType lsa.LsaType) error { + key := getKeyFromNameAndType(routerIdentifier, lsaType) + err := bdb.EraseLsaByKey(key) + if err != nil { + common2.LogError("buntdb EraseLsa error, because of ", err.Error()) + } + return err +} + // // EraseLsaByKey // @Description: 删除一个lsa,根据其在db中的key diff --git a/lsdb/Lsdb.go b/lsdb/Lsdb.go index 3ed9315..a18b0a7 100644 --- a/lsdb/Lsdb.go +++ b/lsdb/Lsdb.go @@ -136,6 +136,7 @@ func (l *Lsdb) BuildAndInstallOwnNameLsa() { l.sequencingManager.IncreaseNameLsaSeq() // todo: 使用sync通知路由更新 + // m_sync.publishRoutingUpdate(Lsa::Type::NAME, m_sequencingManager.getNameLsaSeq()); // 安装lsa到LSDB l.InstallLsa(nlsa) @@ -147,7 +148,7 @@ func (l *Lsdb) BuildAndInstallOwnNameLsa() { // @receiver l // func (l *Lsdb) BuildAndInstallOwnCoordinateLsa() { - // todo + // todo:坐标计算预留接口 } // @@ -176,7 +177,8 @@ func (l *Lsdb) ScheduleAdjLsaBuild() { // // ProcessInterest -// @Description: 处理兴趣包。类似NDN的拉式传输。 +// @Description:收到一个兴趣包,根据兴趣包名称,查找LSA并返回。 +// 处理兴趣包。类似NDN的拉式传输。 // 在NLSR的设计中,由于对兴趣包进行了进一步的分段,因此有以下两种兴趣包: //* 1) 需要从segment fetcher中查找的兴趣包: //* /localhop//nlsr/LSA//// @@ -188,6 +190,10 @@ func (l *Lsdb) ScheduleAdjLsaBuild() { func (l *Lsdb) ProcessInterest(interest *packet.Interest) { } +func (l Lsdb) processInterestForLsa() bool { + return true +} + func (l *Lsdb) ProcessGPPkt(pkt *packet.GPPkt) { } @@ -301,10 +307,24 @@ func (l *Lsdb) RemoveLsa(lsa lsa.ILsa) { common2.LogError("") return } - common2.LogDebug("Removing LSA: ", lsa.ToString()) + common2.LogDebug("Removed LSA: ", lsa.ToString()) } -func (l *Lsdb) RemoveLsaByContainerIndex(index int) { +// +// RemoveLsaByNameAndType +// @Description: 删除lsa +// @receiver l +// @param routerIdentifier +// @param lsaType +// +func (l *Lsdb) RemoveLsaByNameAndType(routerIdentifier *component.Identifier, + lsaType lsa.LsaType) { + err := l.LsaContainer.EraseLsaByNameAndType(routerIdentifier, lsaType) + if err != nil { + common2.LogError("") + return + } + common2.LogDebug("Removed LSA: ", routerIdentifier.ToUri()) } // @@ -314,6 +334,42 @@ func (l *Lsdb) RemoveLsaByContainerIndex(index int) { // @receiver l // func (l *Lsdb) BuildAdjLsa() { + common2.LogDebug("buildAdjLsa called") + + // 构建邻接lsa的任务启动,那么安排构建就变为false + l.isBuildAdjLsaScheduled = false + + // 根据配置文件中邻接表的状态,执行不同的动作 + if l.mlsrConfig.GetAdjacencys().IsAdjLsaBuildable(uint32(l.mlsrConfig.GetHelloRetries())) { + // 只有在有计划的情况下才执行adjLsa构建 + adjBuildCountCache := l.adjBuildCount + if adjBuildCountCache > 0 { + // 只有有安排时,构建邻接lsa + if l.mlsrConfig.GetAdjacencys().GetNumOfActiveNeighbor() > 0 { + // 有激活状态的邻居,则安装邻接LSA + common2.LogDebug("Building and installing own Adj LSA") + l.BuildAndInstallOwnAdjLsa() + } else { + // 我们没有活跃的邻居,这意味着没有人可以穿过我们。 + // 因此,请删除LSDB中的条目。这会阻止此路由器刷新LSA,最终导致其他路由器也将其删除。 + common2.LogDebug("Removing own Adj LSA; no ACTIVE neighbors") + l.RemoveLsaByNameAndType(l.thisRouterPrefix, lsa.LsaADJACENCYType) + } + // 如果在构建adj LSA的过程中,FIB必须等待兴趣响应, + // 那么计划的adj LSA构建的数量可能会发生变化,因此我们不应该只将其设置为0。 + l.adjBuildCount = l.adjBuildCount - adjBuildCountCache + } + } else { + // 我们仍在等待了解某个邻居的邻接状态,因此请安排稍后的构建(当有望完成时) + l.isBuildAdjLsaScheduled = true + scheTime := l.mlsrConfig.GetHelloTimeout() * time.Duration(l.mlsrConfig.GetHelloRetries()) + job, err := l.scheduler.ScheduleTaskAfterDuration(scheTime, 1).Do(l.BuildAdjLsa) + job.LimitRunsTo(1) + l.scheduler.StartAsync() + if err != nil { + common2.LogError("Scheduling BuildAdjLsa error, ", err.Error()) + } + } } // diff --git a/route/LinkStateRoutingTableCalculator.go b/route/LinkStateRoutingTableCalculator.go index 8c0cc75..c552328 100644 --- a/route/LinkStateRoutingTableCalculator.go +++ b/route/LinkStateRoutingTableCalculator.go @@ -70,7 +70,7 @@ func (c *LinkStateRoutingTableCalculator) CalculatePath(routerMap *RouterMap, // 在单路径的情况下,我们可以简单地运行Dijkstra算法。 c.doDijkstraPathCalculation(sourceRouter) // 将新的下一跳通知路由表。// todo: 注意,当前版本中,本机的邻接信息是从配置文件中读取出来的。 - rs, ns := c.addAllLsNextHopsToRoutingTable(mConfig.GetAdjacencyList(), routerMap, sourceRouter) + rs, ns := c.addAllLsNextHopsToRoutingTable(mConfig.GetAdjacencys(), routerMap, sourceRouter) return rs, ns, nil } else { // 多路径计算 @@ -87,7 +87,7 @@ func (c *LinkStateRoutingTableCalculator) CalculatePath(routerMap *RouterMap, // 以当前邻居为起点,执行Dijkstra算法。 c.doDijkstraPathCalculation(sourceRouter) // 用计算结果更新路由表。 - rs, ns := c.addAllLsNextHopsToRoutingTable(mConfig.GetAdjacencyList(), routerMap, sourceRouter) + rs, ns := c.addAllLsNextHopsToRoutingTable(mConfig.GetAdjacencys(), routerMap, sourceRouter) routers = append(routers, rs...) nexthops = append(nexthops, ns...) }