diff --git a/route/NamePrefixTable.go b/route/NamePrefixTable.go index 7fd2c3b..5324867 100644 --- a/route/NamePrefixTable.go +++ b/route/NamePrefixTable.go @@ -274,6 +274,28 @@ func (t *NamePrefixTable) RemoveEntry(name *component.Identifier, destRouter *co } } +// +// isNextHopsEqual +// @Description: 遍历查看两个下一跳列表是否完全一致 +// @param hops1 +// @param hops2 +// @return bool +// +func isNextHopsEqual(hops1 []*NextHop, hops2 []*NextHop) bool { + // 长度不等,则false + if len(hops1) != len(hops2) { + return false + } + // 遍历,如果有一个不相等,则false + for i := 0; i < len(hops1); i++ { + if hops1[i].LogicFaceUri() != hops2[i].LogicFaceUri() || + hops1[i].RouteCost.RouteCost() != hops2[i].RouteCost.RouteCost() { + return false + } + } + return true +} + // // UpdateWithNewRoute // @Description: 接收假定为详尽的条目列表,并使用条目中相应条目中包含的下一跳信息更新每个池条目。 @@ -282,7 +304,43 @@ func (t *NamePrefixTable) RemoveEntry(name *component.Identifier, destRouter *co // @param entries // func (t *NamePrefixTable) UpdateWithNewRoute(entries []*RoutingTableEntry) { + common2.LogDebug("Updating table with newly calculated routes") + // 遍历名称前缀表中的路由表缓存池 + for _, rtpe := range t.m_rtpool { + // 查看当前缓存表项的目的路由器,是否在传入的真路由表项里可以找到对应的 + sameIndex := -1 // 默认没找到 + for i := 0; i < len(entries); i++ { + if entries[i].GetDestination().ToUri() == rtpe.GetDestination().ToUri() { + sameIndex = i + } + } + + // 根据缓存池与原路由表项的差异,将缓存池更新 + if sameIndex >= 0 && !isNextHopsEqual(rtpe.GetNextHops(), entries[sameIndex].GetNextHops()) { + // 如果找到了对应条目,且两个路由表条目的下一跳列表不一致 + common2.LogDebug("Routing entry: " + rtpe.GetDestination().ToUri() + " has changed next-hops.") + // 更新rtpe的下一跳列表 + rtpe.SetNexthopList(entries[sameIndex].NextHopList) + // 更新rtpe的每个名称前缀表项 + for _, nameEntry := range rtpe.NamePrefixTableEntries { + t.AddEntry(nameEntry.GetNamePrefix(), rtpe.GetDestination()) + } + } else if sameIndex < 0 { + // 没有找到对应条目,说明到该路由器的路径已经无了无了无了~ + common2.LogDebug("Routing entry: " + rtpe.GetDestination().ToUri() + " now has no next-hops.") + // 更新rtpe的下一跳列表:清空 + rtpe.Clear() + // 更新rtpe的每个名称前缀表项 + for _, nameEntry := range rtpe.NamePrefixTableEntries { + t.AddEntry(nameEntry.GetNamePrefix(), rtpe.GetDestination()) + } + } else { + // 有找到对应条目,但是两个条目的下一跳列表完全一致 + common2.LogDebug("No change in routing entry:" + rtpe.GetDestination().ToUri() + + ", no action necessary.") + } + } } //