From a25c524c68adb0f6d935235e7eed124ea3202e5a Mon Sep 17 00:00:00 2001 From: free will <2647778488@qq.com> Date: Thu, 26 May 2022 21:13:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=94=AF=E6=8C=81=E9=82=BB?= =?UTF-8?q?=E6=8E=A5LSA=E4=B8=8Ejson=E5=AD=97=E7=AC=A6=E4=B8=B2=E4=BA=92?= =?UTF-8?q?=E7=9B=B8=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lsa/AdjLsa.go | 164 +++++++++++++++++++++++---- lsa/AdjLsaAdjacencyInfo.go | 66 +++++------ lsa/AdjLsaAdjacencyInfo_test.go | 46 ++++---- lsa/AdjLsaAdjacencyList_test.go | 74 ++++++------ lsa/AdjLsaLogicFaceUri.go | 8 +- lsa/AdjLsaLogicFaceUri_test.go | 6 +- lsa/AdjLsa_test.go | 86 +++++++++++--- lsa/NameLsa.go | 91 ++++++++------- route/RoutingTableCalculator_test.go | 9 ++ 9 files changed, 374 insertions(+), 176 deletions(-) create mode 100644 route/RoutingTableCalculator_test.go diff --git a/lsa/AdjLsa.go b/lsa/AdjLsa.go index 4ebd8dc..bd38ef1 100644 --- a/lsa/AdjLsa.go +++ b/lsa/AdjLsa.go @@ -9,30 +9,152 @@ package lsa import ( + "encoding/json" + "minlib/component" "minlib/encoding" "mlsr/extensions" ) +// +// AdjLsa +// @Description: 邻接LSA +// type AdjLsa struct { encoding.SelfEncodingBase LsaBase AdjLsaAdjacenctList } +// +// innerAdjLsa +// @Description: 内置结构体之:邻接LSA +// +type innerAdjLsa struct { + LsaTypeInt int + LsaOriginRouter string + LsaSequenceNumber uint64 + LsaExpirationTime uint64 + AdjacencyInfoList []innerAdjacencyInfo +} + +// +// innerAdjacencyInfo +// @Description: 内置结构体之:邻接信息 +// +type innerAdjacencyInfo struct { + LinkCost uint64 + LogicFaceUri string + LogicFaceId uint64 + NeighborRouter string + InterestTimedOutNo uint32 + Status int +} + +// +// parseAdjLsaToInner +// @Description: 邻接LSA转内置结构体 +// @param aLsa +// @return innerAdjLsa +// @return error +// +func parseAdjLsaToInner(aLsa *AdjLsa) (innerAdjLsa, error) { + innerALSA := innerAdjLsa{} + innerALSA.LsaTypeInt = int(aLsa.GetType()) + innerALSA.LsaOriginRouter = aLsa.LsaOriginRouterIdentifier.ToUri() + innerALSA.LsaSequenceNumber = aLsa.GetSeqNo() + innerALSA.LsaExpirationTime = aLsa.GetExpirationTime() + for i := 0; i < len(aLsa.lsaAdjacencys); i++ { + info := innerAdjacencyInfo{} + info.LinkCost = aLsa.lsaAdjacencys[i].LinkCost() + info.LogicFaceUri = aLsa.lsaAdjacencys[i].LogicFaceUri() + info.LogicFaceId = aLsa.lsaAdjacencys[i].LogicFaceId() + info.NeighborRouter = aLsa.lsaAdjacencys[i].lsaNeighborRouterIdentifier.ToUri() + info.InterestTimedOutNo = aLsa.lsaAdjacencys[i].InterestTimedOutNo + info.Status = aLsa.lsaAdjacencys[i].Status + innerALSA.AdjacencyInfoList = append(innerALSA.AdjacencyInfoList, info) + } + return innerALSA, nil +} + +// +// parseInnerToAdjLsa +// @Description: 内置结构体转邻接LSA +// @param inner +// @param alsa +// @return error +// +func parseInnerToAdjLsa(inner *innerAdjLsa, alsa *AdjLsa) error { + routerIdent, err := component.CreateIdentifierByString(inner.LsaOriginRouter) + if err != nil { + return err + } + alsa.SetOriginRouter(routerIdent) + alsa.SetSeqNo(inner.LsaSequenceNumber) + alsa.SetExpirationTime(inner.LsaExpirationTime) + for i := 0; i < len(inner.AdjacencyInfoList); i++ { + realInfo := new(AdjLsaAdjacencyInfo) + realInfo.SetLinkCost(inner.AdjacencyInfoList[i].LinkCost) + realInfo.SetLogicFaceUri(inner.AdjacencyInfoList[i].LogicFaceUri) + realInfo.SetLogicFaceId(inner.AdjacencyInfoList[i].LogicFaceId) + iden, err2 := component.CreateIdentifierByString( + inner.AdjacencyInfoList[i].NeighborRouter) + if err2 != nil { + return err2 + } + realInfo.SetNeighborRouterIdentifier(iden) + realInfo.InterestTimedOutNo = inner.AdjacencyInfoList[i].InterestTimedOutNo + realInfo.Status = inner.AdjacencyInfoList[i].Status + alsa.lsaAdjacencys = append(alsa.lsaAdjacencys, realInfo) + } + return nil +} + +// +// LoadFromJsonString +// @Description: 从json字符串中加载AdjLsa +// @receiver a +// @param jsonBytes +// @return error +// +func (a *AdjLsa) LoadFromJsonString(jsonBytes []byte) error { + // 转中间结构体 + inner := innerAdjLsa{} + err := json.Unmarshal(jsonBytes, &inner) + if err != nil { + return err + } + // 转实际结构体 + err = parseInnerToAdjLsa(&inner, a) + if err != nil { + return err + } + + return nil +} + +// +// GetJsonString +// @Description: AdjLsa转json +// @receiver a +// @return []byte +// @return error +// +func (a *AdjLsa) GetJsonString() ([]byte, error) { + innerAdjLsa, err := parseAdjLsaToInner(a) + if err != nil { + return nil, err + } + b, err := json.Marshal(innerAdjLsa) + if err != nil { + return nil, err + } + return b, nil +} + func (a *AdjLsa) GetType() LsaType { return LsaADJACENCYType } -func (a *AdjLsa) GetJsonString() ([]byte,error) { - // todo - return nil,nil -} - -func (a *AdjLsa) LoadFromJsonString(jsonBytes []byte) error{ - //todo - return nil -} - // // GetAdjList // @Description: 获取邻接信息表 @@ -89,22 +211,22 @@ func (a *AdjLsa) ToString() string { // @return int // @return error // -func (a *AdjLsa) WireEncode(encoder *encoding.Encoder) (int,error) { +func (a *AdjLsa) WireEncode(encoder *encoding.Encoder) (int, error) { totalLength := 0 // TLV-Value - tmpLen,err := a.AdjLsaAdjacenctList.WireEncode(encoder) + tmpLen, err := a.AdjLsaAdjacenctList.WireEncode(encoder) if err != nil { return 0, err } - totalLength+=tmpLen - tmpLen,err = a.LsaBase.WireEncode(encoder) + totalLength += tmpLen + tmpLen, err = a.LsaBase.WireEncode(encoder) if err != nil { return 0, err } - totalLength+=tmpLen + totalLength += tmpLen // TLV-Length - tmpLen,err = encoder.PrependVarNumber(encoding.VlInt(totalLength)) + tmpLen, err = encoder.PrependVarNumber(encoding.VlInt(totalLength)) if err != nil { return 0, err } @@ -117,7 +239,7 @@ func (a *AdjLsa) WireEncode(encoder *encoding.Encoder) (int,error) { } totalLength += tmpLen - return totalLength,nil + return totalLength, nil } // @@ -139,17 +261,17 @@ func (a *AdjLsa) WireDecode(block *encoding.Block) error { } // 提取参数 - for _, parameter := range block.GetSubElements(){ + for _, parameter := range block.GetSubElements() { switch parameter.GetType() { case extensions.TlvMlsrLsaBase: - if err:= a.LsaBase.WireDecode(parameter); err != nil { + if err := a.LsaBase.WireDecode(parameter); err != nil { return err } case extensions.TlvMlsrAdjLsaAdjacencyList: - if err:= a.AdjLsaAdjacenctList.WireDecode(parameter); err != nil { + if err := a.AdjLsaAdjacenctList.WireDecode(parameter); err != nil { return err } } } return nil -} \ No newline at end of file +} diff --git a/lsa/AdjLsaAdjacencyInfo.go b/lsa/AdjLsaAdjacencyInfo.go index 332f8bb..4a3eef5 100644 --- a/lsa/AdjLsaAdjacencyInfo.go +++ b/lsa/AdjLsaAdjacencyInfo.go @@ -16,19 +16,19 @@ import ( ) const ( - STATUS_UNKNOWN = -1 + STATUS_UNKNOWN = -1 STATUS_INACTIVE = 0 - STATUS_ACTIVE = 1 + STATUS_ACTIVE = 1 ) type AdjLsaAdjacencyInfo struct { - AdjLsaLinkCost // 两个路由器之间的链路成本 - AdjLsaLogicFaceUri // MIR层级对LogicFace的字符串描述,如udp://1.0.0.2,或ether:aa.aa.aa.aa... - AdjLsaLogicFaceId // MIR给LogicFace分配的uint64类型的id,如182828,nlsr可通过该id定位到哪个logicface负责传该链路数据 + AdjLsaLinkCost // 两个路由器之间的链路成本 + AdjLsaLogicFaceUri // MIR层级对LogicFace的字符串描述,如udp://1.0.0.2,或ether:aa.aa.aa.aa... + AdjLsaLogicFaceId // MIR给LogicFace分配的uint64类型的id,如182828,nlsr可通过该id定位到哪个logicface负责传该链路数据 lsaNeighborRouterIdentifier *component.Identifier // NLSR已配置的邻居路由器标识符,如/min/pkusz/group1/router1 - InterestTimedOutNo uint32 // 自从收到最后一个回复之后,记录已经失败的hello兴趣包的数目 - Status int // 表示邻居路由器的状态:未知-1、未激活0、激活1 + InterestTimedOutNo uint32 // 自从收到最后一个回复之后,记录已经失败的hello兴趣包的数目 + Status int // 表示邻居路由器的状态:未知-1、未激活0、激活1 } // @@ -45,7 +45,7 @@ func NewAdjLsaAdjacencyInfo(adjLsaLinkCost AdjLsaLinkCost, adjLsaLogicFaceUri Ad faceId AdjLsaLogicFaceId, m_lsaNeighborRouterIdentifier *component.Identifier, m_interestTimedOutNo uint32, m_status int) *AdjLsaAdjacencyInfo { return &AdjLsaAdjacencyInfo{AdjLsaLinkCost: adjLsaLinkCost, AdjLsaLogicFaceUri: adjLsaLogicFaceUri, - AdjLsaLogicFaceId: faceId,lsaNeighborRouterIdentifier: m_lsaNeighborRouterIdentifier, + AdjLsaLogicFaceId: faceId, lsaNeighborRouterIdentifier: m_lsaNeighborRouterIdentifier, InterestTimedOutNo: m_interestTimedOutNo, Status: m_status} } @@ -65,7 +65,7 @@ func NewAdjLsaAdjacencyInfoByInstance(adjLsaLinkCost *AdjLsaLinkCost, adjLsaLogi m_interestTimedOutNo uint32, m_status int) *AdjLsaAdjacencyInfo { adjRes := AdjLsaAdjacencyInfo{} adjRes.SetLinkCost(adjLsaLinkCost.LinkCost()) - adjRes.setLogicFaceUri(adjLsaLogicFaceUri.LogicFaceUri()) + adjRes.SetLogicFaceUri(adjLsaLogicFaceUri.LogicFaceUri()) adjRes.SetLogicFaceId(faceId.LogicFaceId()) adjRes.SetNeighborRouterIdentifier(m_lsaNeighborRouterIdentifier) adjRes.InterestTimedOutNo = m_interestTimedOutNo @@ -91,7 +91,7 @@ func (l *AdjLsaAdjacencyInfo) GetNeighborRouterIdentifier() *component.Identifie // func (l *AdjLsaAdjacencyInfo) SetNeighborRouterIdentifier(routerIdentifier *component.Identifier) { // todo: 验证identifier,如类型、字段是否为空等 - l.lsaNeighborRouterIdentifier= routerIdentifier + l.lsaNeighborRouterIdentifier = routerIdentifier } // @@ -103,7 +103,7 @@ func (l *AdjLsaAdjacencyInfo) SetNeighborRouterIdentifier(routerIdentifier *comp // func (l *AdjLsaAdjacencyInfo) Compare(routerIdentifier *component.Identifier) bool { // todo: 验证identifier,如类型、字段是否为空等 - if routerIdentifier.ToUri()==l.lsaNeighborRouterIdentifier.ToUri() { + if routerIdentifier.ToUri() == l.lsaNeighborRouterIdentifier.ToUri() { return true } return false @@ -117,7 +117,7 @@ func (l *AdjLsaAdjacencyInfo) Compare(routerIdentifier *component.Identifier) bo // @return bool // func (l *AdjLsaAdjacencyInfo) CompareLogicFaceUri(faceUri *AdjLsaLogicFaceUri) bool { - if faceUri.LogicFaceUri()==l.LogicFaceUri(){ + if faceUri.LogicFaceUri() == l.LogicFaceUri() { return true } return false @@ -131,7 +131,7 @@ func (l *AdjLsaAdjacencyInfo) CompareLogicFaceUri(faceUri *AdjLsaLogicFaceUri) b // @return bool // func (l *AdjLsaAdjacencyInfo) CompareLogicFaceUriString(faceUriString string) bool { - if faceUriString==l.LogicFaceUri(){ + if faceUriString == l.LogicFaceUri() { return true } return false @@ -145,7 +145,7 @@ func (l *AdjLsaAdjacencyInfo) CompareLogicFaceUriString(faceUriString string) bo // @return bool // func (l *AdjLsaAdjacencyInfo) CompareLogicFaceId(faceId uint64) bool { - if faceId==l.LogicFaceId(){ + if faceId == l.LogicFaceId() { return true } return false @@ -163,7 +163,7 @@ func (l *AdjLsaAdjacencyInfo) ToString() string { resString += ", LogicFaceUri: " resString += l.LogicFaceUri() resString += ", LogicFaceId: " - resString += strconv.FormatUint(l.LogicFaceId(),10) + resString += strconv.FormatUint(l.LogicFaceId(), 10) resString += ", NeighborRouterIdentifier: " resString += l.lsaNeighborRouterIdentifier.ToUri() resString += ", InterestTimedOutNo: " @@ -182,41 +182,41 @@ func (l *AdjLsaAdjacencyInfo) ToString() string { // @return int // @return error // -func (l *AdjLsaAdjacencyInfo) WireEncode(encoder *encoding.Encoder) (int,error) { +func (l *AdjLsaAdjacencyInfo) WireEncode(encoder *encoding.Encoder) (int, error) { totalLength := 0 // TLV-Value - if tmpLen,err := l.AdjLsaLinkCost.WireEncode(encoder); err!=nil{ + if tmpLen, err := l.AdjLsaLinkCost.WireEncode(encoder); err != nil { return 0, err - }else{ + } else { totalLength += tmpLen } - if tmpLen,err := l.AdjLsaLogicFaceUri.WireEncode(encoder); err!=nil{ + if tmpLen, err := l.AdjLsaLogicFaceUri.WireEncode(encoder); err != nil { return 0, err - }else{ + } else { totalLength += tmpLen } - if tmpLen,err := l.lsaNeighborRouterIdentifier.WireEncode(encoder); err!=nil{ + if tmpLen, err := l.lsaNeighborRouterIdentifier.WireEncode(encoder); err != nil { return 0, err - }else{ + } else { totalLength += tmpLen } // TLV-Length - if tmpLen,err := encoder.PrependVarNumber(encoding.VlInt(totalLength)); err!=nil{ + if tmpLen, err := encoder.PrependVarNumber(encoding.VlInt(totalLength)); err != nil { return 0, err - }else{ + } else { totalLength += tmpLen } // TLV-Type - if tmpLen,err := encoder.PrependVarNumber(extensions.TlvMlsrAdjLsaAdjacencyInfo); err!=nil{ + if tmpLen, err := encoder.PrependVarNumber(extensions.TlvMlsrAdjLsaAdjacencyInfo); err != nil { return 0, err - }else{ + } else { totalLength += tmpLen } - return totalLength,nil + return totalLength, nil } // @@ -238,23 +238,23 @@ func (l *AdjLsaAdjacencyInfo) WireDecode(block *encoding.Block) error { } // 提取参数 - for _, parameter := range block.GetSubElements(){ + for _, parameter := range block.GetSubElements() { switch parameter.GetType() { case encoding.TlvIdentifier: - iden:=component.Identifier{} - if err:= iden.WireDecode(parameter); err != nil { + iden := component.Identifier{} + if err := iden.WireDecode(parameter); err != nil { return err } l.lsaNeighborRouterIdentifier = &iden case extensions.TlvMlsrAdjLsaLogicFaceUri: - if err:= l.AdjLsaLogicFaceUri.WireDecode(parameter); err != nil { + if err := l.AdjLsaLogicFaceUri.WireDecode(parameter); err != nil { return err } case extensions.TlvMlsrAdjLsaLinkCost: - if err:= l.AdjLsaLinkCost.WireDecode(parameter); err != nil { + if err := l.AdjLsaLinkCost.WireDecode(parameter); err != nil { return err } } } return nil -} \ No newline at end of file +} diff --git a/lsa/AdjLsaAdjacencyInfo_test.go b/lsa/AdjLsaAdjacencyInfo_test.go index cf7dc21..8e8b77d 100644 --- a/lsa/AdjLsaAdjacencyInfo_test.go +++ b/lsa/AdjLsaAdjacencyInfo_test.go @@ -21,18 +21,18 @@ import ( // @param t // func TestNewAdjLsaAdjacencyInfo(t *testing.T) { - linkCost :=AdjLsaLinkCost{} + linkCost := AdjLsaLinkCost{} linkCost.SetLinkCost(100) faceUri := AdjLsaLogicFaceUri{} - faceUri.setLogicFaceUri("uri") - faceId :=AdjLsaLogicFaceId{} + faceUri.SetLogicFaceUri("uri") + faceId := AdjLsaLogicFaceId{} faceId.SetLogicFaceId(77) - iden,_ := component.CreateIdentifierByString("/pku/router") - adjInfo := NewAdjLsaAdjacencyInfo(linkCost,faceUri,faceId,iden,123,1) - fmt.Println("cost: ",adjInfo.LinkCost()) - fmt.Println("uri: ",adjInfo.LogicFaceUri()) - fmt.Println("id: ",adjInfo.LogicFaceId()) - fmt.Println("identifier: ",iden.ToUri()) + iden, _ := component.CreateIdentifierByString("/pku/router") + adjInfo := NewAdjLsaAdjacencyInfo(linkCost, faceUri, faceId, iden, 123, 1) + fmt.Println("cost: ", adjInfo.LinkCost()) + fmt.Println("uri: ", adjInfo.LogicFaceUri()) + fmt.Println("id: ", adjInfo.LogicFaceId()) + fmt.Println("identifier: ", iden.ToUri()) fmt.Println(adjInfo.ToString()) } @@ -43,16 +43,16 @@ func TestNewAdjLsaAdjacencyInfo(t *testing.T) { // func TestAdjLsaAdjacencyInfo_Compare(t *testing.T) { // 先构造 - linkCost :=AdjLsaLinkCost{} + linkCost := AdjLsaLinkCost{} linkCost.SetLinkCost(100) faceUri := AdjLsaLogicFaceUri{} - faceUri.setLogicFaceUri("uri") - faceId :=AdjLsaLogicFaceId{} + faceUri.SetLogicFaceUri("uri") + faceId := AdjLsaLogicFaceId{} faceId.SetLogicFaceId(77) - iden,_ := component.CreateIdentifierByString("/pku/router") - adjInfo := NewAdjLsaAdjacencyInfo(linkCost,faceUri,faceId,iden,123,1) + iden, _ := component.CreateIdentifierByString("/pku/router") + adjInfo := NewAdjLsaAdjacencyInfo(linkCost, faceUri, faceId, iden, 123, 1) // 再测试:全部为true则pass - iden2,_ := component.CreateIdentifierByString("/pku/router") + iden2, _ := component.CreateIdentifierByString("/pku/router") fmt.Println(adjInfo.Compare(iden2)) fmt.Println(adjInfo.CompareLogicFaceId(77)) fmt.Println(adjInfo.CompareLogicFaceUriString("uri")) @@ -66,14 +66,14 @@ func TestAdjLsaAdjacencyInfo_Compare(t *testing.T) { // func TestAdjLsaAdjacencyInfo_WireEncode(t *testing.T) { // 先构造 - linkCost :=AdjLsaLinkCost{} + linkCost := AdjLsaLinkCost{} linkCost.SetLinkCost(100) faceUri := AdjLsaLogicFaceUri{} - faceUri.setLogicFaceUri("uri") - faceId :=AdjLsaLogicFaceId{} + faceUri.SetLogicFaceUri("uri") + faceId := AdjLsaLogicFaceId{} faceId.SetLogicFaceId(77) - iden,_ := component.CreateIdentifierByString("/pku/router") - adjInfo := NewAdjLsaAdjacencyInfo(linkCost,faceUri,faceId,iden,123,1) + iden, _ := component.CreateIdentifierByString("/pku/router") + adjInfo := NewAdjLsaAdjacencyInfo(linkCost, faceUri, faceId, iden, 123, 1) // 构造编码器 encoder := encoding.Encoder{} @@ -95,8 +95,8 @@ func TestAdjLsaAdjacencyInfo_WireEncode(t *testing.T) { return } fmt.Println(cnt) - for i:=0;i < len(res);i++{ - fmt.Print(res[i],", ") + for i := 0; i < len(res); i++ { + fmt.Print(res[i], ", ") } fmt.Println() // 241, 1, 169, 33, 101, 19, 100, 2, 0, 47, 100, 4, 0, 112, 107, 117, 100, 7, 0, 114, 111, 117, 116, 101, 114, 241, 1, 168, 3, 117, 114, 105, 241, 1, 167, 1, 100, @@ -127,4 +127,4 @@ func TestAdjLsaAdjacencyInfo_WireDecode(t *testing.T) { return } fmt.Println(adjInfo.ToString()) -} \ No newline at end of file +} diff --git a/lsa/AdjLsaAdjacencyList_test.go b/lsa/AdjLsaAdjacencyList_test.go index df3bdcd..debe648 100644 --- a/lsa/AdjLsaAdjacencyList_test.go +++ b/lsa/AdjLsaAdjacencyList_test.go @@ -22,21 +22,21 @@ import ( // func TestAdjLsaAdjacenctList_GetAdjList(t *testing.T) { // 先构造一个info - linkCost :=AdjLsaLinkCost{} + linkCost := AdjLsaLinkCost{} linkCost.SetLinkCost(100) faceUri := AdjLsaLogicFaceUri{} - faceUri.setLogicFaceUri("uri") - faceId :=AdjLsaLogicFaceId{} + faceUri.SetLogicFaceUri("uri") + faceId := AdjLsaLogicFaceId{} faceId.SetLogicFaceId(77) - iden,_ := component.CreateIdentifierByString("/pku/router1") - adjInfo := NewAdjLsaAdjacencyInfo(linkCost,faceUri,faceId,iden,123,1) + iden, _ := component.CreateIdentifierByString("/pku/router1") + adjInfo := NewAdjLsaAdjacencyInfo(linkCost, faceUri, faceId, iden, 123, 1) // 构造Info列表 adjInfoList := new(AdjLsaAdjacenctList) adjInfoList.Insert(adjInfo) // 再加第二个info - iden2,_ := component.CreateIdentifierByString("/pku/router2") + iden2, _ := component.CreateIdentifierByString("/pku/router2") adjInfo.SetNeighborRouterIdentifier(iden2) adjInfoList.Insert(adjInfo) @@ -51,20 +51,20 @@ func TestAdjLsaAdjacenctList_GetAdjList(t *testing.T) { // func TestAdjLsaAdjacenctList_IsNeighbor(t *testing.T) { // 先构造一个info - linkCost :=AdjLsaLinkCost{} + linkCost := AdjLsaLinkCost{} linkCost.SetLinkCost(100) faceUri := AdjLsaLogicFaceUri{} - faceUri.setLogicFaceUri("uri") - faceId :=AdjLsaLogicFaceId{} + faceUri.SetLogicFaceUri("uri") + faceId := AdjLsaLogicFaceId{} faceId.SetLogicFaceId(77) - iden,_ := component.CreateIdentifierByString("/pku/router1") - adjInfo := NewAdjLsaAdjacencyInfo(linkCost,faceUri,faceId,iden,123,1) + iden, _ := component.CreateIdentifierByString("/pku/router1") + adjInfo := NewAdjLsaAdjacencyInfo(linkCost, faceUri, faceId, iden, 123, 1) adjInfoList := new(AdjLsaAdjacenctList) adjInfoList.Insert(adjInfo) // 测试某路由器是否是邻居节点 fmt.Println(adjInfoList.IsNeighbor(iden)) - iden2,_ := component.CreateIdentifierByString("/pku/router2") + iden2, _ := component.CreateIdentifierByString("/pku/router2") fmt.Println(adjInfoList.IsNeighbor(iden2)) } @@ -75,14 +75,14 @@ func TestAdjLsaAdjacenctList_IsNeighbor(t *testing.T) { // func TestAdjLsaAdjacenctList_IncrementTimedOutInterestCount(t *testing.T) { // 先构造一个info - linkCost :=AdjLsaLinkCost{} + linkCost := AdjLsaLinkCost{} linkCost.SetLinkCost(100) faceUri := AdjLsaLogicFaceUri{} - faceUri.setLogicFaceUri("uri") - faceId :=AdjLsaLogicFaceId{} + faceUri.SetLogicFaceUri("uri") + faceId := AdjLsaLogicFaceId{} faceId.SetLogicFaceId(77) - iden,_ := component.CreateIdentifierByString("/pku/router1") - adjInfo := NewAdjLsaAdjacencyInfo(linkCost,faceUri,faceId,iden,123,1) + iden, _ := component.CreateIdentifierByString("/pku/router1") + adjInfo := NewAdjLsaAdjacencyInfo(linkCost, faceUri, faceId, iden, 123, 1) adjInfoList := new(AdjLsaAdjacenctList) adjInfoList.Insert(adjInfo) @@ -105,14 +105,14 @@ func TestAdjLsaAdjacenctList_IncrementTimedOutInterestCount(t *testing.T) { // func TestAdjLsaAdjacenctList_GetStatusOfNeighbor(t *testing.T) { // 先构造一个info - linkCost :=AdjLsaLinkCost{} + linkCost := AdjLsaLinkCost{} linkCost.SetLinkCost(100) faceUri := AdjLsaLogicFaceUri{} - faceUri.setLogicFaceUri("uri") - faceId :=AdjLsaLogicFaceId{} + faceUri.SetLogicFaceUri("uri") + faceId := AdjLsaLogicFaceId{} faceId.SetLogicFaceId(77) - iden,_ := component.CreateIdentifierByString("/pku/router1") - adjInfo := NewAdjLsaAdjacencyInfo(linkCost,faceUri,faceId,iden,123,1) + iden, _ := component.CreateIdentifierByString("/pku/router1") + adjInfo := NewAdjLsaAdjacencyInfo(linkCost, faceUri, faceId, iden, 123, 1) adjInfoList := new(AdjLsaAdjacenctList) adjInfoList.Insert(adjInfo) @@ -120,7 +120,7 @@ func TestAdjLsaAdjacenctList_GetStatusOfNeighbor(t *testing.T) { fmt.Println(adjInfoList.GetStatusOfNeighbor(iden)) // 设置再获取 - adjInfoList.SetStatusOfNeighbor(iden,2) + adjInfoList.SetStatusOfNeighbor(iden, 2) fmt.Println(adjInfoList.GetStatusOfNeighbor(iden)) } @@ -131,14 +131,14 @@ func TestAdjLsaAdjacenctList_GetStatusOfNeighbor(t *testing.T) { // func TestAdjLsaAdjacenctList_FindAdjacencyByRouterIdentifier(t *testing.T) { // 先构造一个info - linkCost :=AdjLsaLinkCost{} + linkCost := AdjLsaLinkCost{} linkCost.SetLinkCost(100) faceUri := AdjLsaLogicFaceUri{} - faceUri.setLogicFaceUri("uri") - faceId :=AdjLsaLogicFaceId{} + faceUri.SetLogicFaceUri("uri") + faceId := AdjLsaLogicFaceId{} faceId.SetLogicFaceId(77) - iden,_ := component.CreateIdentifierByString("/pku/router1") - adjInfo := NewAdjLsaAdjacencyInfo(linkCost,faceUri,faceId,iden,123,1) + iden, _ := component.CreateIdentifierByString("/pku/router1") + adjInfo := NewAdjLsaAdjacencyInfo(linkCost, faceUri, faceId, iden, 123, 1) adjInfoList := new(AdjLsaAdjacenctList) adjInfoList.Insert(adjInfo) @@ -156,19 +156,19 @@ func TestAdjLsaAdjacenctList_FindAdjacencyByRouterIdentifier(t *testing.T) { // func TestAdjLsaAdjacenctList_WireEncode(t *testing.T) { // 先构造一个info - linkCost :=AdjLsaLinkCost{} + linkCost := AdjLsaLinkCost{} linkCost.SetLinkCost(100) faceUri := AdjLsaLogicFaceUri{} - faceUri.setLogicFaceUri("uri") - faceId :=AdjLsaLogicFaceId{} + faceUri.SetLogicFaceUri("uri") + faceId := AdjLsaLogicFaceId{} faceId.SetLogicFaceId(77) - iden,_ := component.CreateIdentifierByString("/pku/router1") - adjInfo := NewAdjLsaAdjacencyInfo(linkCost,faceUri,faceId,iden,123,1) + iden, _ := component.CreateIdentifierByString("/pku/router1") + adjInfo := NewAdjLsaAdjacencyInfo(linkCost, faceUri, faceId, iden, 123, 1) // 构造Info列表 adjInfoList := new(AdjLsaAdjacenctList) adjInfoList.Insert(adjInfo) // 再加第二个info - iden2,_ := component.CreateIdentifierByString("/pku/router2") + iden2, _ := component.CreateIdentifierByString("/pku/router2") adjInfo2 := AdjLsaAdjacencyInfo{ AdjLsaLinkCost: adjInfo.AdjLsaLinkCost, AdjLsaLogicFaceUri: adjInfo.AdjLsaLogicFaceUri, @@ -200,8 +200,8 @@ func TestAdjLsaAdjacenctList_WireEncode(t *testing.T) { return } fmt.Println(cnt) - for i:=0;i < len(res);i++{ - fmt.Print(res[i],", ") + for i := 0; i < len(res); i++ { + fmt.Print(res[i], ", ") } fmt.Println() } @@ -233,4 +233,4 @@ func TestAdjLsaAdjacenctList_WireDecode(t *testing.T) { return } fmt.Println(base.ToString()) -} \ No newline at end of file +} diff --git a/lsa/AdjLsaLogicFaceUri.go b/lsa/AdjLsaLogicFaceUri.go index 546247f..928fc97 100644 --- a/lsa/AdjLsaLogicFaceUri.go +++ b/lsa/AdjLsaLogicFaceUri.go @@ -33,12 +33,12 @@ func (l *AdjLsaLogicFaceUri) LogicFaceUri() string { } // -// setLogicFaceUri +// SetLogicFaceUri // @Description: 设置LogicFaceUri // @receiver l // @param logicFaceUri // -func (l *AdjLsaLogicFaceUri) setLogicFaceUri(logicFaceUri string) { +func (l *AdjLsaLogicFaceUri) SetLogicFaceUri(logicFaceUri string) { l.CommonString.SetValue(logicFaceUri) } @@ -50,7 +50,7 @@ func (l *AdjLsaLogicFaceUri) setLogicFaceUri(logicFaceUri string) { // @return int // @return error // -func (l *AdjLsaLogicFaceUri) WireEncode(encoder *encoding.Encoder) (int,error) { +func (l *AdjLsaLogicFaceUri) WireEncode(encoder *encoding.Encoder) (int, error) { l.CommonString.SetTlvType(extensions.TlvMlsrAdjLsaLogicFaceUri) return l.CommonString.WireEncode(encoder) } @@ -64,7 +64,7 @@ func (l *AdjLsaLogicFaceUri) WireEncode(encoder *encoding.Encoder) (int,error) { // func (l *AdjLsaLogicFaceUri) WireDecode(block *encoding.Block) error { // 检查type是否正确 - if err := encoding.ExpectType(block.GetType(),extensions.TlvMlsrAdjLsaLogicFaceUri); err!=nil { + if err := encoding.ExpectType(block.GetType(), extensions.TlvMlsrAdjLsaLogicFaceUri); err != nil { return err } return l.CommonString.WireDecode(block) diff --git a/lsa/AdjLsaLogicFaceUri_test.go b/lsa/AdjLsaLogicFaceUri_test.go index 1f7762a..24d347f 100644 --- a/lsa/AdjLsaLogicFaceUri_test.go +++ b/lsa/AdjLsaLogicFaceUri_test.go @@ -21,7 +21,7 @@ import ( // func TestAdjLsaLogicFaceUri_LogicFaceUri(t *testing.T) { testCPU := AdjLsaLogicFaceUri{} - testCPU.setLogicFaceUri("url") + testCPU.SetLogicFaceUri("url") fmt.Println(testCPU.LogicFaceUri()) } @@ -33,7 +33,7 @@ func TestAdjLsaLogicFaceUri_LogicFaceUri(t *testing.T) { func TestAdjLsaLogicFaceUri_WireEncode(t *testing.T) { testCPU := new(AdjLsaLogicFaceUri) fmt.Println(testCPU.LogicFaceUri()) - testCPU.setLogicFaceUri("url") + testCPU.SetLogicFaceUri("url") fmt.Println(testCPU.LogicFaceUri()) //testCPC.SetTlvType(5) fmt.Println(testCPU.Value()) @@ -72,7 +72,7 @@ func TestAdjLsaLogicFaceUri_WireDecode(t *testing.T) { fmt.Println("interest", testCPU.Value()) //testCPC.SetTlvType(5) fmt.Println(testCPU.TlvType()) - testCPU.setLogicFaceUri("url") + testCPU.SetLogicFaceUri("url") fmt.Println("interest", testCPU.Value()) fmt.Println("interest", testCPU.IsInitial()) var encoder encoding.Encoder diff --git a/lsa/AdjLsa_test.go b/lsa/AdjLsa_test.go index 6a36a6d..ee00f49 100644 --- a/lsa/AdjLsa_test.go +++ b/lsa/AdjLsa_test.go @@ -9,12 +9,64 @@ package lsa import ( + "encoding/json" "fmt" + common2 "minlib/common" "minlib/component" "minlib/encoding" "testing" ) +func TestAdjLsa_JsonConv(t *testing.T) { + // 构造基本部分 + base := new(AdjLsa) + base.SetLsaExpirationTime(2000) + base.SetLsaSequenceNumber(1234) + base.LsaOriginRouterIdentifier, _ = component.CreateIdentifierByString("/min/pku") + // 构造邻接信息部分 + // 先构造一个info + linkCost := AdjLsaLinkCost{} + linkCost.SetLinkCost(100) + faceUri := AdjLsaLogicFaceUri{} + faceUri.SetLogicFaceUri("uri") + faceId := AdjLsaLogicFaceId{} + faceId.SetLogicFaceId(77) + iden, _ := component.CreateIdentifierByString("/pku/router1") + adjInfo := NewAdjLsaAdjacencyInfo(linkCost, faceUri, faceId, iden, 123, 1) + base.Insert(adjInfo) + // 再加第二个info + iden2, _ := component.CreateIdentifierByString("/pku/router2") + adjInfo2 := AdjLsaAdjacencyInfo{ + AdjLsaLinkCost: adjInfo.AdjLsaLinkCost, + AdjLsaLogicFaceUri: adjInfo.AdjLsaLogicFaceUri, + AdjLsaLogicFaceId: adjInfo.AdjLsaLogicFaceId, + lsaNeighborRouterIdentifier: iden2, + InterestTimedOutNo: 111, + Status: 0, + } + base.Insert(&adjInfo2) + // 打印该adjlsa + common2.LogInfo(base.ToString()) + + // 转byte[]类型的json + jsonBytes, err := json.Marshal(&base) + if err != nil { + common2.LogInfo("邻接LSA json 转换失败") + } + common2.LogInfo("直接用Marshal将邻接LSA为Json: " + string(jsonBytes)) + + jsonBytes, err = base.GetJsonString() + common2.LogInfo("通过中间结构体将邻接LSA转为json: " + string(jsonBytes)) + + // 从Json转回来 + newAdjLsa := new(AdjLsa) + err = newAdjLsa.LoadFromJsonString(jsonBytes) + if err != nil { + common2.LogInfo("邻接LSA json 转换失败") + } + common2.LogInfo("通过中间结构体将json转为邻接LSA: " + newAdjLsa.ToString()) +} + // // TestAdjLsa_New // @Description: 构造 @@ -25,20 +77,20 @@ func TestAdjLsa_New(t *testing.T) { base := new(AdjLsa) base.SetLsaExpirationTime(2000) base.SetLsaSequenceNumber(1234) - base.LsaOriginRouterIdentifier,_ = component.CreateIdentifierByString("/min/pku") + base.LsaOriginRouterIdentifier, _ = component.CreateIdentifierByString("/min/pku") // 构造邻接信息部分 // 先构造一个info - linkCost :=AdjLsaLinkCost{} + linkCost := AdjLsaLinkCost{} linkCost.SetLinkCost(100) faceUri := AdjLsaLogicFaceUri{} - faceUri.setLogicFaceUri("uri") - faceId :=AdjLsaLogicFaceId{} + faceUri.SetLogicFaceUri("uri") + faceId := AdjLsaLogicFaceId{} faceId.SetLogicFaceId(77) - iden,_ := component.CreateIdentifierByString("/pku/router1") - adjInfo := NewAdjLsaAdjacencyInfo(linkCost,faceUri,faceId,iden,123,1) + iden, _ := component.CreateIdentifierByString("/pku/router1") + adjInfo := NewAdjLsaAdjacencyInfo(linkCost, faceUri, faceId, iden, 123, 1) base.Insert(adjInfo) // 再加第二个info - iden2,_ := component.CreateIdentifierByString("/pku/router2") + iden2, _ := component.CreateIdentifierByString("/pku/router2") adjInfo2 := AdjLsaAdjacencyInfo{ AdjLsaLinkCost: adjInfo.AdjLsaLinkCost, AdjLsaLogicFaceUri: adjInfo.AdjLsaLogicFaceUri, @@ -63,20 +115,20 @@ func TestAdjLsa_WireEncode(t *testing.T) { base := new(AdjLsa) base.SetLsaExpirationTime(2000) base.SetLsaSequenceNumber(1234) - base.LsaOriginRouterIdentifier,_ = component.CreateIdentifierByString("/min/pku") + base.LsaOriginRouterIdentifier, _ = component.CreateIdentifierByString("/min/pku") // 构造邻接信息部分 // 先构造一个info - linkCost :=AdjLsaLinkCost{} + linkCost := AdjLsaLinkCost{} linkCost.SetLinkCost(100) faceUri := AdjLsaLogicFaceUri{} - faceUri.setLogicFaceUri("uri") - faceId :=AdjLsaLogicFaceId{} + faceUri.SetLogicFaceUri("uri") + faceId := AdjLsaLogicFaceId{} faceId.SetLogicFaceId(77) - iden,_ := component.CreateIdentifierByString("/pku/router1") - adjInfo := NewAdjLsaAdjacencyInfo(linkCost,faceUri,faceId,iden,123,1) + iden, _ := component.CreateIdentifierByString("/pku/router1") + adjInfo := NewAdjLsaAdjacencyInfo(linkCost, faceUri, faceId, iden, 123, 1) base.Insert(adjInfo) // 再加第二个info - iden2,_ := component.CreateIdentifierByString("/pku/router2") + iden2, _ := component.CreateIdentifierByString("/pku/router2") adjInfo2 := AdjLsaAdjacencyInfo{ AdjLsaLinkCost: adjInfo.AdjLsaLinkCost, AdjLsaLogicFaceUri: adjInfo.AdjLsaLogicFaceUri, @@ -107,8 +159,8 @@ func TestAdjLsa_WireEncode(t *testing.T) { return } fmt.Println(cnt) - for i:=0;i < len(res);i++{ - fmt.Print(res[i],", ") + for i := 0; i < len(res); i++ { + fmt.Print(res[i], ", ") } fmt.Println() } @@ -143,4 +195,4 @@ func TestAdjLsa_WireDecode(t *testing.T) { return } fmt.Println(base.ToString()) -} \ No newline at end of file +} diff --git a/lsa/NameLsa.go b/lsa/NameLsa.go index ca6a6db..5e17281 100644 --- a/lsa/NameLsa.go +++ b/lsa/NameLsa.go @@ -15,6 +15,10 @@ import ( "mlsr/extensions" ) +// +// NameLsa +// @Description: 名称LSA +// type NameLsa struct { encoding.SelfEncodingBase LsaBase @@ -23,19 +27,23 @@ type NameLsa struct { // // innerNameLsa -// @Description: 转json所用的中间结构体 +// @Description: 内置结构体之:名称LSA // type innerNameLsa struct { - LsaTypeInt int - LsaOriginRouter string + LsaTypeInt int + LsaOriginRouter string LsaSequenceNumber uint64 LsaExpirationTime uint64 - NamePrefixList []innerNamePrefixInfo + NamePrefixList []innerNamePrefixInfo } +// +// innerNamePrefixInfo +// @Description: 内置结构体之:名称前缀信息 +// type innerNamePrefixInfo struct { NamePrefix string - Sources []string + Sources []string } // @@ -45,19 +53,19 @@ type innerNamePrefixInfo struct { // @return innerNameLsa // @return error // -func parseNameLsaToInner(nLsa *NameLsa) (innerNameLsa,error) { +func parseNameLsaToInner(nLsa *NameLsa) (innerNameLsa, error) { innerNLSA := innerNameLsa{} innerNLSA.LsaTypeInt = int(nLsa.GetType()) innerNLSA.LsaOriginRouter = nLsa.LsaOriginRouterIdentifier.ToUri() innerNLSA.LsaSequenceNumber = nLsa.GetSeqNo() innerNLSA.LsaExpirationTime = nLsa.GetExpirationTime() - for i:=0;i