From ec747d1e91306f12bfefb75add513c392758a852 Mon Sep 17 00:00:00 2001 From: free will <2647778488@qq.com> Date: Fri, 24 Jun 2022 21:26:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0Lsdb=E4=B8=AD=E5=AF=B9?= =?UTF-8?q?=E4=BA=8E=E8=B7=AF=E7=94=B1=E8=A1=A8=E8=A2=AB=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E7=9A=84=E4=BA=8B=E4=BB=B6=EF=BC=9B=E5=A2=9E=E5=8A=A0=E4=BA=8B?= =?UTF-8?q?=E4=BB=B6=E8=A7=A6=E5=8F=91=E6=8E=A5=E6=94=B6=E5=8A=A8=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lsdb/Lsdb.go | 14 ++++---- route/NamePrefixTable.go | 46 +++++++++++++++++++++++++ route/RoutingTable.go | 74 ++++++++++++++++++++++++++++++++++------ 3 files changed, 117 insertions(+), 17 deletions(-) diff --git a/lsdb/Lsdb.go b/lsdb/Lsdb.go index 59feb74..4227210 100644 --- a/lsdb/Lsdb.go +++ b/lsdb/Lsdb.go @@ -63,7 +63,7 @@ type Lsdb struct { adjBuildCount uint64 // 邻接LSA的构建的统计数目 // 信号:用以通知“Lsdb被修改”这个事件的发生 - onLsdbModified *utils.Signal + OnLsdbModified *utils.Signal } func (l *Lsdb) Init(mc *common.MlsrConfig, ms *MlsrScheduler, @@ -108,7 +108,7 @@ func (l *Lsdb) Init(mc *common.MlsrConfig, ms *MlsrScheduler, l.BuildAndInstallOwnNameLsa() // 信号初始化 - l.onLsdbModified = utils.NewSignal() + l.OnLsdbModified = utils.NewSignal() } // @@ -310,8 +310,8 @@ func (l *Lsdb) InstallLsa(lsa lsa.ILsa) { common2.LogError("Add Lsa error", lsa.ToString()) return } - // 2. 触发LSDB安装LSA的动作信号 onLsdbModified(lsa, LsdbUpdate::INSTALLED, {}, {}); - l.onLsdbModified.Emit(lsa, INSTALLED) + // 2. 触发LSDB安装LSA的动作信号 OnLsdbModified(lsa, LsdbUpdate::INSTALLED, {}, {}); + l.OnLsdbModified.Emit(lsa, INSTALLED) // 3. 设置LSA超期时间事件 l.ScheduleLsaExpiration(lsa, timeToExpire) } else if lsaInDb.GetSeqNo() < lsa.GetSeqNo() { @@ -323,11 +323,11 @@ func (l *Lsdb) InstallLsa(lsa lsa.ILsa) { common2.LogError("Add Lsa error", lsa.ToString()) return } - // 2. 触发LSDB更新LSA的动作信号 onLsdbModified(lsa, LsdbUpdate::UPDATED, namesToAdd, namesToRemove); + // 2. 触发LSDB更新LSA的动作信号 OnLsdbModified(lsa, LsdbUpdate::UPDATED, namesToAdd, namesToRemove); // todo 实现该方法:对比两个lsa,将其不同的部分取出来 updated, namesToAdd, namesToRemove := l.compareTwoLsa(lsaInDb, lsa) if updated { - l.onLsdbModified.Emit(lsa, UPDATED, namesToAdd, namesToRemove) + l.OnLsdbModified.Emit(lsa, UPDATED, namesToAdd, namesToRemove) } // 3. 设置LSA超期时间事件 l.ScheduleLsaExpiration(lsa, timeToExpire) @@ -361,7 +361,7 @@ func (l *Lsdb) RemoveLsa(lsa lsa.ILsa) { common2.LogError("") return } - l.onLsdbModified.Emit(lsa, REMOVED) + l.OnLsdbModified.Emit(lsa, REMOVED) common2.LogDebug("Removed LSA: ", lsa.ToString()) } diff --git a/route/NamePrefixTable.go b/route/NamePrefixTable.go index 09fd953..63cb50a 100644 --- a/route/NamePrefixTable.go +++ b/route/NamePrefixTable.go @@ -13,6 +13,7 @@ import ( "minlib/component" "mlsr/lsa" "mlsr/lsdb" + "mlsr/utils" ) // @@ -29,9 +30,54 @@ type NamePrefixTable struct { m_ownRouterName *component.Identifier // 路由表。包含路由计算器计算出来的每个路由表标识及其对应的下一跳列表 m_routingTable *RoutingTable + + // 信号接收器 + m_afterRoutingChangeConnection *utils.Connection + m_afterLsdbModified *utils.Connection + // todo fib } +// +// Init +// @Description: 名称前缀表初始化 +// @receiver t +// @param ownRouterName +// @param routingTable +// @param afterRoutingChangeSignal +// @param afterLsdbModifiedSignal +// +func (t *NamePrefixTable) Init(ownRouterName *component.Identifier, + routingTable *RoutingTable, afterRoutingChangeSignal *utils.Signal, + afterLsdbModifiedSignal *utils.Signal) { + // 赋值变量 + t.m_ownRouterName = ownRouterName + t.m_routingTable = routingTable + + // 监听路由表的修改,触发NPT表修改 + bindRoutingChangeFunc := func(args ...interface{}) { + entrys := args[0].([]*RoutingTableEntry) + t.UpdateWithNewRoute(entrys) + } + t.m_afterRoutingChangeConnection = afterRoutingChangeSignal.Connect(bindRoutingChangeFunc) + + // 监听LSDB修改,触发NPT表修改 + bindLsdbModifiedFunc := func(args ...interface{}) { + // 获取四个参数 + ilsa := args[0].(lsa.ILsa) + updateType := args[1].(lsdb.LsdbUpdate) + var namesToAdd []*component.Identifier + var namesToRemove []*component.Identifier + if updateType == lsdb.REMOVED { + namesToAdd = args[2].([]*component.Identifier) + namesToRemove = args[3].([]*component.Identifier) + } + // 执行更新操作 + t.UpdateFromLsdb(ilsa, updateType, namesToAdd, namesToRemove) + } + t.m_afterLsdbModified = afterLsdbModifiedSignal.Connect(bindLsdbModifiedFunc) +} + // // UpdateFromLsdb // @Description: 根据lsdb的更新,对名称进行增加、更新或移除 diff --git a/route/RoutingTable.go b/route/RoutingTable.go index 1958963..36d1978 100644 --- a/route/RoutingTable.go +++ b/route/RoutingTable.go @@ -17,6 +17,7 @@ import ( "mlsr/extensions" "mlsr/lsa" "mlsr/lsdb" + "mlsr/utils" "time" ) @@ -39,6 +40,11 @@ type RoutingTable struct { // 当前路由器的邻接LSA是否存在 m_ownAdjLsaExist bool + // 通知路由表更改的信号 + afterRoutingChange *utils.Signal + // 监听Lsdb被修改的信号接收器 + m_afterLsdbModified *utils.Connection + // 双曲坐标状态 => 暂时用不到 m_hyperbolicState uint32 } @@ -66,16 +72,64 @@ func (t *RoutingTable) Init(mc *common.MlsrConfig, t.m_isRoutingTableCalculating = false t.m_isRouteCalculationScheduled = false - // todo 事件监听:如果监听到了传来的删除本地邻接LSA操作 + // 初始化信号 + t.afterRoutingChange = utils.NewSignal() - // todo 事件监听:如果监听到了传来的安装本地邻接LSA操作 - t.m_ownAdjLsaExist = true + bindFunc := func(args ...interface{}) { + // 获取四个参数 + ilsa := args[0].(lsa.ILsa) + updateType := args[1].(lsdb.LsdbUpdate) + //var namesToAdd []*component.Identifier + //var namesToRemove []*component.Identifier + //if updateType == lsdb.REMOVED { + // namesToAdd = args[2].([]*component.Identifier) + // namesToRemove = args[3].([]*component.Identifier) + //} - // todo 事件监听:如果监听到了传来的安装或更新本地邻接LSA操作 - //err := t.ScheduleRoutingTableCalculation() - //if err != nil { - // common2.LogError("RoutingTable.Init ScheduleRoutingTableCalculation error.") - //} + // 根据四个参数做进一步的信息获取 + lsaType := ilsa.GetType() + updateForOwnAdjacencyLsa := false + if ilsa.GetOriginRouter().ToUri() == t.mlsrConfig.GetRouterPrefix().ToUri() && + lsaType == lsa.LsaADJACENCYType { + updateForOwnAdjacencyLsa = true + } + + // 事件监听:如果监听到了传来的删除本地邻接LSA操作 + if updateType == lsdb.REMOVED && updateForOwnAdjacencyLsa { + // 如果删除了自己的邻接LSA,那么我们就没有活动邻居。 + //(永远不会删除自身坐标LSA。但路由表计算在HelloProtocol中安排。HR的路由表计算器会考虑链路的非活动状态) + common2.LogDebug("No Adj LSA of router itself, routing table can not be calculated :(") + t.clearRoutingTable() + common2.LogDebug("Calling Update NPT With new Route") + // todo 触发改变 afterRoutingChange(m_rTable); + t.afterRoutingChange.Emit(t.rTable) + t.m_ownAdjLsaExist = false + } + + // 事件监听:如果监听到了传来的安装本地邻接LSA操作 + if updateType == lsdb.INSTALLED && updateForOwnAdjacencyLsa { + t.m_ownAdjLsaExist = true + } + + scheduleCalculation := false + + // 事件监听:如果监听到了传来的安装或更新本地邻接LSA操作 + // 不要在移除时做任何事情,等待Hello协议去确认,然后做出响应 + if updateType == lsdb.INSTALLED || updateType == lsdb.UPDATED { + if lsaType == lsa.LsaADJACENCYType { + scheduleCalculation = true + } + } + + if scheduleCalculation { + err := t.ScheduleRoutingTableCalculation() + if err != nil { + common2.LogError("RoutingTable.Init ScheduleRoutingTableCalculation error.") + } + } + } + + t.m_afterLsdbModified = t.lsdb.OnLsdbModified.Connect(bindFunc) } // @@ -208,9 +262,9 @@ func (t *RoutingTable) calculateLsRoutingTable() { t.AddNextHop(routers[i], nexthops[i]) } - // todo: 开始根据新计算的路由去更新NPT表 + // 开始根据新计算的路由去更新NPT表 common2.LogInfo("Calling Update NPT With new Route") - //todo + t.afterRoutingChange.Emit(t.rTable) } //