diff --git a/route/NextHop.go b/route/NextHop.go index 07854d9..f281227 100644 --- a/route/NextHop.go +++ b/route/NextHop.go @@ -41,6 +41,16 @@ func NewNextHop(uri string, cost uint64) (*NextHop, error) { return nextHop, nil } +// +// Init +// @Description: 参数初始化 +// @receiver l +// +func (l *NextHop) Init(faceUri string, cost uint64) { + l.SetLogicFaceUri(faceUri) + l.SetRouteCost(cost) +} + // // WireEncode // @Description: 线速编码 diff --git a/route/RoutingTable.go b/route/RoutingTable.go index 7250cf5..cd5a3f7 100644 --- a/route/RoutingTable.go +++ b/route/RoutingTable.go @@ -10,6 +10,7 @@ package route import ( "errors" + "fmt" common2 "minlib/common" "minlib/component" "minlib/encoding" @@ -49,6 +50,24 @@ type RoutingTable struct { m_hyperbolicState uint32 } +// +// PrintSelf +// @Description: 打印状态,用于测试 +// @receiver t +// +func (t *RoutingTable) PrintSelf() { + fmt.Println("----------- Table beg ---------") + 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) +} + // // Init // @Description: 初始化 @@ -79,6 +98,8 @@ func (t *RoutingTable) Init(mc *common.MlsrConfig, // 获取四个参数 ilsa := args[0].(lsa.ILsa) updateType := args[1].(lsdb.LsdbUpdate) + common2.LogInfo("RoutingTable.bindFunc: ", + ilsa.GetOriginRouter().ToUri(), " ", updateType) //var namesToAdd []*component.Identifier //var namesToRemove []*component.Identifier //if updateType == lsdb.REMOVED { @@ -172,11 +193,11 @@ func (t *RoutingTable) AddNextHop(destRouter *component.Identifier, hop *NextHop // 如果没有这条表项,则构建并加入 if rte == nil { rte = new(RoutingTableEntry) + rte.SetDestination(destRouter) rte.AddNextHop(hop) t.rTable = append(t.rTable, rte) } else { // 如果有,则直接编辑该条目,增加一个下一跳. - // todo: 这块需要测试,是否在go中可以直接根据引用进行表项的修改 rte.AddNextHop(hop) } } diff --git a/route/RoutingTable_test.go b/route/RoutingTable_test.go index 74ec4f6..4007629 100644 --- a/route/RoutingTable_test.go +++ b/route/RoutingTable_test.go @@ -10,6 +10,7 @@ package route import ( common2 "minlib/common" + "minlib/component" "minlib/logicface" "mlsr/common" "mlsr/lsdb" @@ -48,6 +49,43 @@ func TestRoutingTable_Init(t *testing.T) { // @param t // func TestRoutingTable_AddNextHop(t *testing.T) { + // 1. 组件初始化 + // 配置文件初始化 + 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. 路由表初始化 + rt := new(RoutingTable) + rt.Init(mlsrConfig, sche, mlsdb) + + // 加入一个新的条目 + iden, _ := component.CreateIdentifierByString("/pkuiswhat") + hop := new(NextHop) + hop.Init("tcp://1.1.1.1", 77) + rt.AddNextHop(iden, hop) + // 打印 + rt.PrintSelf() + + // 加入一个旧条目 + hopOld := new(NextHop) + hopOld.Init("udp://1.1.1.12", 88) + rt.AddNextHop(iden, hopOld) + // 打印 + rt.PrintSelf() + + // 再加一遍 + rt.AddNextHop(iden, hopOld) + // 打印 + rt.PrintSelf() } //