amost finish adjacencylist

This commit is contained in:
free will committed 2022-03-11 23:16:37 +08:00
1 parent 952b783108
commit da39825c6c
3 files changed
+420 -12

No files matched your search

+11 -8
View File
@@ -12,13 +12,16 @@ package extensions
const (
// TlvMlsrLsa for MLSR
TlvMlsrLsaAdjacency = 401 // 表示MIR之间邻接关系信息的LSA
TlvMlsrLsaContent = 402 // 表示内容标识信息的LSA
TlvMlsrHeader = 410 // 表示LSA最基本信息:源MIR、序列号、过期时间
TlvMlsrLsaExpirationTime = 420 // LSA过期时间
TlvMlsrLsaSequenceNumber = 421 // LSA序列号
TlvMlsrAdjLsaLogicFaceId = 422 // logicfaceId
TlvMlsrAdjLsaLinkCost = 423 // 链路开销
TlvMlsrAdjLsaLogicFaceUri = 424 // LogicFace前缀
TlvMlsrLsaAdjacency = 401 // 表示MIR之间邻接关系信息的LSA
TlvMlsrLsaContent = 402 // 表示内容标识信息的LSA
TlvMlsrHeader = 410 // 表示LSA最基本信息:源MIR、序列号、过期时间
TlvMlsrLsaExpirationTime = 420 // LSA过期时间
TlvMlsrLsaSequenceNumber = 421 // LSA序列号
TlvMlsrAdjLsaLogicFaceId = 422 // logicfaceId
TlvMlsrAdjLsaLinkCost = 423 // 链路开销
TlvMlsrAdjLsaLogicFaceUri = 424 // LogicFace前缀
TlvMlsrAdjLsaAdjacencyInfo = 425 // 单条邻接LSA信息
)
+81 -3
View File
@@ -1,6 +1,6 @@
// Package lsa
// @Author: Wang Feng
// @Description:
// @Description: 单条邻接的链路信息
// @Version: 0.1.0
// @Date: 2022/3/9 22:28
// @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
@@ -8,7 +8,11 @@
package lsa
import "minlib/component"
import (
"minlib/component"
"minlib/encoding"
"mlsr/extensions"
)
const (
STATUS_UNKNOWN = -1
@@ -122,6 +126,80 @@ func (l *AdjLsaAdjacencyInfo) CompareLogicFaceId(faceId uint64) bool {
return false
}
func (l *AdjLsaAdjacencyInfo) WireEncode() {
//
// WireEncode
// @Description: 线速编码
// @receiver l
// @param encoder
// @return int
// @return error
//
func (l *AdjLsaAdjacencyInfo) WireEncode(encoder *encoding.Encoder) (int,error) {
totalLength := 0
// TLV-Value
if tmpLen,err := l.AdjLsaLinkCost.WireEncode(encoder); err!=nil{
return 0, err
}else{
totalLength += tmpLen
}
if tmpLen,err := l.AdjLsaLogicFaceUri.WireEncode(encoder); err!=nil{
return 0, err
}else{
totalLength += tmpLen
}
if tmpLen,err := l.lsaNeighborRouterIdentifier.WireEncode(encoder); err!=nil{
return 0, err
}else{
totalLength += tmpLen
}
// TLV-Length
if tmpLen,err := encoder.PrependVarNumber(encoding.VlInt(totalLength)); err!=nil{
return 0, err
}else{
totalLength += tmpLen
}
// TLV-Type
if tmpLen,err := encoder.PrependVarNumber(extensions.TlvMlsrAdjLsaAdjacencyInfo); err!=nil{
return 0, err
}else{
totalLength += tmpLen
}
return totalLength,nil
}
//
// WireDecode
// @Description: 线速解码
// @receiver l
// @param block
// @return error
//
func (l *AdjLsaAdjacencyInfo) WireDecode(block *encoding.Block) error {
// 首先解析子Block
if err := block.ParseSubElements(); err != nil {
return err
}
// 提取参数
for _, parameter := range block.GetSubElements(){
switch parameter.GetType() {
case encoding.TlvIdentifier:
if err:= l.lsaNeighborRouterIdentifier.WireDecode(parameter); err != nil {
return err
}
case extensions.TlvMlsrAdjLsaLogicFaceUri:
if err:= l.AdjLsaLogicFaceUri.WireDecode(parameter); err != nil {
return err
}
case extensions.TlvMlsrAdjLsaLinkCost:
if err:= l.AdjLsaLinkCost.WireDecode(parameter); err != nil {
return err
}
}
}
return nil
}
+328 -1
View File
@@ -8,11 +8,338 @@
package lsa
import "minlib/encoding"
import (
"errors"
"minlib/component"
"minlib/encoding"
)
// 为邻接信息的数组起一个别称
type LsaAdjacencyContainer []*AdjLsaAdjacencyInfo
//
// AdjLsaAdjacenctList
// @Description: 保存邻接信息的列表
//
type AdjLsaAdjacenctList struct {
encoding.SelfEncodingBase
lsaAdjacencys LsaAdjacencyContainer
}
//
// insert
// @Description: 插入一条邻接信息
// @receiver l
// @param info
//
func (l *AdjLsaAdjacenctList) Insert(info *AdjLsaAdjacencyInfo) {
l.lsaAdjacencys = append(l.lsaAdjacencys, info)
}
//
// getAdjList
// @Description: 获取包含所有邻接信息的邻接表
// @receiver l
// @return []*AdjLsaAdjacencyInfo
//
func (l *AdjLsaAdjacenctList) GetAdjList() []*AdjLsaAdjacencyInfo {
return l.lsaAdjacencys
}
//
// isNeighbor
// @Description: 判断某个MIR标识符是否是邻居MIR
// @receiver l
// @param routerIdentifier
// @return bool
//
func (l *AdjLsaAdjacenctList) IsNeighbor(routerIdentifier *component.Identifier) bool {
for i:=0; i<len(l.lsaAdjacencys); i++ {
if l.lsaAdjacencys[i].lsaNeighborRouterIdentifier.ToString()==routerIdentifier.ToString(){
return true
}
}
return false
}
//
// findIndexByRouterIdentifier
// @Description: 根据MIR标识符找到其在邻接表中的位置。
// todo: 需要注意的是,这里只会按序查找,并返回第一个找到的邻接信息。所有调用的函数都应知晓这一点。
// @receiver l
// @param routerIdentifier
// @return int
// @return error
//
func (l *AdjLsaAdjacenctList) findIndexByRouterIdentifier(routerIdentifier *component.Identifier) (int,error) {
for i:=0; i<len(l.lsaAdjacencys); i++ {
if l.lsaAdjacencys[i].lsaNeighborRouterIdentifier.ToString()==routerIdentifier.ToString(){
return i,nil
}
}
return -1,errors.New("no found the identifier")
}
//
// IncrementTimedOutInterestCount
// @Description: 将指定MIR标识符对应的邻接信息中的失败的hello兴趣包次数加一。
// 注意,这里只对按序找到的第一个邻接信息加一
// @receiver l
// @param routerIdentifier
// @return error
//
func (l *AdjLsaAdjacenctList) IncrementTimedOutInterestCount(routerIdentifier *component.Identifier) error {
if index,err:=l.findIndexByRouterIdentifier(routerIdentifier);err!=nil{
return err
}else{
l.lsaAdjacencys[index].InterestTimedOutNo++
return nil
}
}
//
// SetTimedOutInterestCount
// @Description: 设置指定MIR标识符对应的邻接信息中的失败的hello兴趣包次数。
// 注意,这里只对按序找到的第一个邻接信息进行设置
// @receiver l
// @param routerIdentifier
// @param count
// @return error
//
func (l *AdjLsaAdjacenctList) SetTimedOutInterestCount(routerIdentifier *component.Identifier, count uint32) error {
if index,err:=l.findIndexByRouterIdentifier(routerIdentifier);err!=nil{
return err
}else{
l.lsaAdjacencys[index].InterestTimedOutNo=count
return nil
}
}
//
// GetTimedOutInterestCount
// @Description: 获取指定MIR标识符对应的邻接信息中的失败的hello兴趣包次数
// @receiver l
// @param routerIdentifier
// @return uint32
// @return error
//
func (l *AdjLsaAdjacenctList) GetTimedOutInterestCount(routerIdentifier *component.Identifier) (uint32,error) {
if index,err:=l.findIndexByRouterIdentifier(routerIdentifier);err!=nil{
return -1,err
}else{
return l.lsaAdjacencys[index].InterestTimedOutNo,nil
}
}
//
// GetStatusOfNeighbor
// @Description: 获取指定MIR标识符对应MIR的状态
// @receiver l
// @param routerIdentifier
// @return int
// @return error
//
func (l *AdjLsaAdjacenctList) GetStatusOfNeighbor(routerIdentifier *component.Identifier) (int,error) {
if index,err:=l.findIndexByRouterIdentifier(routerIdentifier);err!=nil{
return -2,err
}else{
return l.lsaAdjacencys[index].Status,nil
}
}
//
// SetStatusOfNeighbor
// @Description: 设置指定MIR标识符对应MIR的状态
// @receiver l
// @param routerIdentifier
// @param status
// @return error
//
func (l *AdjLsaAdjacenctList) SetStatusOfNeighbor(routerIdentifier *component.Identifier,status int) error {
if index,err:=l.findIndexByRouterIdentifier(routerIdentifier);err!=nil{
return err
}else{
l.lsaAdjacencys[index].Status=status
return nil
}
}
//
// isAdjLsaBuildable
// @Description: 当所有邻居的状态可知时,此表可用于构建邻接LSA。
// 当邻居状态是ACTIVE或INACTIVE时,就表示此邻居状态可知;
// 当一个邻居还在被联络,即在发送hello兴趣包给它时,则此邻居状态为UNKNOWN。
// 在Nlsr中,本函数的代码实现与代码注释不一致,后面可以进一步研究其原因,甚至可以提交issue给nlsr项目组。
// @receiver l
// @param interestRetryNo 联系邻居的hello兴趣包的最大重传次数
// @return bool 此邻接表的实体对象是否能够用于构建一个邻接LSA
//
func (l *AdjLsaAdjacenctList) IsAdjLsaBuildable(interestRetryNo uint32) bool {
for i:=0; i<len(l.lsaAdjacencys); i++ {
switch l.lsaAdjacencys[i].Status{
case STATUS_UNKNOWN:
if l.lsaAdjacencys[i].InterestTimedOutNo<interestRetryNo{
return false
}
}
}
return true
}
//
// GetAdjacency
// @Description: 根据路由器标识符返回其对应的那条邻接信息
// @receiver l
// @param routerIdentifier
// @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
}
}
//
// GetNumOfActiveNeighbor
// @Description: 获取状态为ACTIVE的邻居MIR的数目
// @receiver l
// @return int
//
func (l *AdjLsaAdjacenctList) GetNumOfActiveNeighbor() int {
numbers:=0
for i:=0; i<len(l.lsaAdjacencys); i++ {
switch l.lsaAdjacencys[i].Status{
case STATUS_ACTIVE:
numbers++
}
}
return numbers
}
//
// GetNumOfINActiveNeighbor
// @Description: 获取状态为INACTIVE的邻居MIR的数目
// @receiver l
// @return int
//
func (l *AdjLsaAdjacenctList) GetNumOfINActiveNeighbor() int {
numbers:=0
for i:=0; i<len(l.lsaAdjacencys); i++ {
switch l.lsaAdjacencys[i].Status{
case STATUS_INACTIVE:
numbers++
}
}
return numbers
}
//
// GetNumOfUnknownNeighbor
// @Description: 获取状态为Unknown的邻居MIR的数目
// @receiver l
// @return int
//
func (l *AdjLsaAdjacenctList) GetNumOfUnknownNeighbor() int {
numbers:=0
for i:=0; i<len(l.lsaAdjacencys); i++ {
switch l.lsaAdjacencys[i].Status{
case STATUS_UNKNOWN:
numbers++
}
}
return numbers
}
//
// Size
// @Description: 邻接信息的条数
// @receiver l
// @return int
//
func (l *AdjLsaAdjacenctList) Size() int {
return len(l.lsaAdjacencys)
}
//
// Reset
// @Description: 清空所有邻接信息
// @receiver l
//
func (l *AdjLsaAdjacenctList) Reset() {
l.lsaAdjacencys=[]*AdjLsaAdjacencyInfo{}
}
//
// FindAdjacencyByRouterIdentifier
// @Description: 获取指定路由器标识的所有邻接信息条目。
// 这就表明一个邻接表中可能有包含同一个MIR标识的多个邻接信息条目,
// 前面的一些设置或获取操作均只对按序查到的第一个邻接信息条目进行操作。
// todo:后面应根据需求,增加实现对所有符合条件的邻接信息进行操作的函数。
// @receiver l
// @param routerIdentifier
// @return []*AdjLsaAdjacencyInfo
//
func (l *AdjLsaAdjacenctList) FindAdjacencyByRouterIdentifier(routerIdentifier *component.Identifier) []*AdjLsaAdjacencyInfo {
var infos []*AdjLsaAdjacencyInfo
for i:=0; i<len(l.lsaAdjacencys); i++ {
if l.lsaAdjacencys[i].lsaNeighborRouterIdentifier.ToString()==routerIdentifier.ToString(){
infos=append(infos,l.lsaAdjacencys[i])
}
}
return infos
}
//
// FindAdjacencyByLogicFaceId
// @Description: 获取指定LogicFaceId的所有邻接信息条目
// @receiver l
// @param id
// @return []*AdjLsaAdjacencyInfo
//
func (l *AdjLsaAdjacenctList) FindAdjacencyByLogicFaceId(id *AdjLsaLogicFaceId) []*AdjLsaAdjacencyInfo {
var infos []*AdjLsaAdjacencyInfo
for i:=0; i<len(l.lsaAdjacencys); i++ {
if l.lsaAdjacencys[i].LogicFaceId()==id.Value(){
infos=append(infos,l.lsaAdjacencys[i])
}
}
return infos
}
//
// FindAdjacencyByLogicFaceUri
// @Description: 获取指定logicfaceuri的所有邻接信息条目。
// @receiver l
// @param uri
// @return []*AdjLsaAdjacencyInfo
//
func (l *AdjLsaAdjacenctList) FindAdjacencyByLogicFaceUri(uri *AdjLsaLogicFaceUri) []*AdjLsaAdjacencyInfo {
var infos []*AdjLsaAdjacencyInfo
for i:=0; i<len(l.lsaAdjacencys); i++ {
if l.lsaAdjacencys[i].AdjLsaLogicFaceUri.Value()==uri.Value(){
infos=append(infos,l.lsaAdjacencys[i])
}
}
return infos
}
func (l *AdjLsaAdjacenctList) getLogicFaceId(uri *AdjLsaLogicFaceUri) (uint64,error) {
for i:=0; i<len(l.lsaAdjacencys); i++ {
if l.lsaAdjacencys[i].AdjLsaLogicFaceUri.Value()==uri.Value(){
return l.lsaAdjacencys[i].AdjLsaLogicFaceId.Value(),nil
}
}
return -1,nil
}
// todo:有待实现编码解码
func (l *AdjLsaAdjacenctList) WireEncode(encoder *encoding.Encoder) (int,error) {
panic("")
}
func (l *AdjLsaAdjacenctList) WireDecode(block *encoding.Block) error {
panic("")
}