mirror of
https://gitee.com/willfree/mlsr.git
synced 2026-06-11 09:19:35 +08:00
147 lines
3.0 KiB
Go
147 lines
3.0 KiB
Go
// Package route
|
|
// @Author: Wang Feng
|
|
// @Description:
|
|
// @Version: 0.1.0
|
|
// @Date: 2022/5/5 20:41
|
|
// @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
|
//
|
|
|
|
package route
|
|
|
|
import (
|
|
"minlib/component"
|
|
"minlib/encoding"
|
|
"mlsr/extensions"
|
|
)
|
|
|
|
//
|
|
// RoutingTableEntry
|
|
// @Description: 路由表条目
|
|
//
|
|
type RoutingTableEntry struct {
|
|
m_destination *component.Identifier
|
|
NextHopList
|
|
}
|
|
|
|
//
|
|
// ToString
|
|
// @Description: 转string。用于测试
|
|
// @receiver e
|
|
// @return string
|
|
//
|
|
func (e *RoutingTableEntry) ToString() string {
|
|
res := ""
|
|
res += "目的路由器: " + e.m_destination.ToUri() + "\n"
|
|
res += e.NextHopList.ToString()
|
|
return res
|
|
}
|
|
|
|
//
|
|
// NewRoutingTableEntry
|
|
// @Description: 新建一个路由表条目
|
|
// @param dest
|
|
// @return *RoutingTableEntry
|
|
// @return error
|
|
//
|
|
func NewRoutingTableEntry(dest *component.Identifier) (*RoutingTableEntry, error) {
|
|
// todo: 判断dest是否格式有效
|
|
rte := new(RoutingTableEntry)
|
|
rte.m_destination = dest
|
|
return rte, nil
|
|
}
|
|
|
|
//
|
|
// GetDestination
|
|
// @Description: 获取目的路由器
|
|
// @receiver e
|
|
// @return *component.Identifier
|
|
//
|
|
func (e *RoutingTableEntry) GetDestination() *component.Identifier {
|
|
return e.m_destination
|
|
}
|
|
|
|
//
|
|
// SetDestination
|
|
// @Description: 设置目的路由器
|
|
// @receiver e
|
|
// @param dest
|
|
//
|
|
func (e *RoutingTableEntry) SetDestination(dest *component.Identifier) {
|
|
e.m_destination = dest
|
|
}
|
|
|
|
//
|
|
// WireEncode
|
|
// @Description: 线速编码
|
|
// @receiver l
|
|
// @param encoder
|
|
// @return int
|
|
// @return error
|
|
//
|
|
func (e *RoutingTableEntry) WireEncode(encoder *encoding.Encoder) (int, error) {
|
|
totalLength := 0
|
|
// TLV-Value
|
|
tmpLen, err := e.NextHopList.WireEncode(encoder)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
totalLength += tmpLen
|
|
tmpLen, err = e.m_destination.WireEncode(encoder)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
totalLength += tmpLen
|
|
|
|
// TLV-Length
|
|
tmpLen, err = encoder.PrependVarNumber(encoding.VlInt(totalLength))
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
totalLength += tmpLen
|
|
|
|
// TLV-Type
|
|
tmpLen, err = encoder.PrependVarNumber(extensions.TlvMlsrRoutingTableEntry)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
totalLength += tmpLen
|
|
|
|
return totalLength, nil
|
|
}
|
|
|
|
//
|
|
// WireDecode
|
|
// @Description: 线速解码
|
|
// @receiver l
|
|
// @param block
|
|
// @return error
|
|
//
|
|
func (e *RoutingTableEntry) WireDecode(block *encoding.Block) error {
|
|
// 检查 TLV-TYPE 是否正确
|
|
if err := encoding.ExpectType(block.GetType(), extensions.TlvMlsrRoutingTableEntry); err != nil {
|
|
return err
|
|
}
|
|
|
|
// 首先解析子Block
|
|
if err := block.ParseSubElements(); err != nil {
|
|
return err
|
|
}
|
|
|
|
// 提取参数
|
|
for _, parameter := range block.GetSubElements() {
|
|
switch parameter.GetType() {
|
|
case encoding.TlvIdentifier:
|
|
iden := component.Identifier{}
|
|
if err := iden.WireDecode(parameter); err != nil {
|
|
return err
|
|
}
|
|
e.m_destination = &iden
|
|
case extensions.TlvMlsrNextHopList:
|
|
if err := e.NextHopList.WireDecode(parameter); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|