Files
mlsr/lsa/NameLsaNamePrefixInfo.go
T

160 lines
3.6 KiB
Go

// Package lsa
// @Author: Wang Feng
// @Description:
// @Version: 0.1.0
// @Date: 2022/3/17 16:29
// @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
//
package lsa
import (
"minlib/component"
"minlib/encoding"
"mlsr/extensions"
)
//// 疑问1,为啥是字符串?字符串是路由器的名称前缀吗?
//// 疑问2,用NameLsa去告知其他节点的这些名称前缀,是本地的,还是本地及其邻居的,还是全网的?
//type Sources []*NameLsaNamePrefixSource // 表示一个名称前缀的来源。由于其可能有多个来源,这里是列表形式。
// 表示一个名称前缀的来源。一个路由器发出的名称前缀是由其自身产生的,
// 这里的source应当指的是对该前缀由本机哪个程序产生的描述字符串。
// 此字符串列表在mlsr保留,以使程序有更好的延展性。
type Sources []string
type NameLsaNamePrefixInfo struct {
encoding.SelfEncodingBase
NamePrefix *component.Identifier
Sources
}
//
// InsertSource
// @Description: 插入一个source
// @receiver n
// @param source
//
func (n *NameLsaNamePrefixInfo) InsertSource(source string) {
n.Sources=append(n.Sources,source)
}
//
// RemoveSource
// @Description: 删除一个source
// @receiver n
// @param source
//
func (n *NameLsaNamePrefixInfo) RemoveSource(source string) {
var tmpSources []string
for i:=0; i<len(n.Sources); i++ {
if n.Sources[i] != source{
tmpSources = append(tmpSources,n.Sources[i])
}
}
n.Sources=tmpSources
}
//
// ToString
// @Description: 转字符串,用于展示PrefixInfo信息。
// @receiver n
//
func (n *NameLsaNamePrefixInfo) ToString() string {
var res string = ""
res+="{ "
res+=n.NamePrefix.ToUri()
res+=" ["
for i:=0;i<len(n.Sources);i++{
res += n.Sources[i]
if i<(len(n.Sources)-1){
res += ", "
}
}
res+="] }"
return res
}
//
// GetSource
// @Description: 获取指定source在列表中的索引值
// @receiver n
// @param source
// @return int 若返回-1,则表示没找到;否则则是要找source的索引值
//
func (n *NameLsaNamePrefixInfo) GetSource(source string) int {
for i:=0;i<len(n.Sources);i++{
if source==n.Sources[i]{
return i
}
}
return -1
}
//
// WireEncode
// @Description: 线速编码
// @receiver n
// @param encoder
// @return int
// @return error
//
func (n *NameLsaNamePrefixInfo) WireEncode(encoder *encoding.Encoder) (int,error) {
totalLength := 0
// TLV-Value
if tmpLen,err := n.NamePrefix.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.TlvMlsrNameLsaNamePrefixInfo); err!=nil{
return 0, err
}else{
totalLength += tmpLen
}
return totalLength,nil
}
//
// WireDecode
// @Description: 线速解码
// @receiver n
// @param block
// @return error
//
func (n *NameLsaNamePrefixInfo) WireDecode(block *encoding.Block) error {
// 检查 TLV-TYPE 是否正确
if err := encoding.ExpectType(block.GetType(), extensions.TlvMlsrNameLsaNamePrefixInfo); err != nil {
return err
}
// 首先解析子Block
if err := block.ParseSubElements(); err != nil {
return err
}
// 提取参数
for _, parameter := range block.GetSubElements(){
switch parameter.GetType() {
case encoding.TlvIdentifier:
ident := new(component.Identifier)
if err:= ident.WireDecode(parameter); err != nil {
return err
}
n.NamePrefix=ident
}
}
return nil
}