mirror of
https://gitee.com/willfree/mlsr.git
synced 2026-06-15 20:04:48 +08:00
106 lines
2.6 KiB
Go
106 lines
2.6 KiB
Go
// Package route
|
|
// @Author: Wang Feng
|
|
// @Description:
|
|
// @Version: 0.1.0
|
|
// @Date: 2022/6/17 11:29
|
|
// @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
|
//
|
|
|
|
package route
|
|
|
|
import (
|
|
"fmt"
|
|
"minlib/component"
|
|
"minlib/encoding"
|
|
"testing"
|
|
)
|
|
|
|
//
|
|
// TestNewRoutingTableEntry
|
|
// @Description: 测试构造路由表项
|
|
// @param t
|
|
//
|
|
func TestNewRoutingTableEntry(t *testing.T) {
|
|
// 构造
|
|
iden, _ := component.CreateIdentifierByString("/pku/abc")
|
|
rte, err := NewRoutingTableEntry(iden)
|
|
if err != nil {
|
|
fmt.Println("构造路由表项失败")
|
|
}
|
|
// 打印
|
|
fmt.Println(rte.GetDestination().ToUri())
|
|
}
|
|
|
|
//
|
|
// TestRoutingTableEntry_WireEncode
|
|
// @Description: 测试编码
|
|
// @param t
|
|
//
|
|
func TestRoutingTableEntry_WireEncode(t *testing.T) {
|
|
// 构造三个下一跳
|
|
nthop1, _ := NewNextHop("/hop1", 10)
|
|
nthop2, _ := NewNextHop("/hop2", 20)
|
|
nthop3, _ := NewNextHop("/hop3", 30)
|
|
// 构造路由表项
|
|
iden, _ := component.CreateIdentifierByString("/pku/run")
|
|
hopList, _ := NewRoutingTableEntry(iden)
|
|
hopList.AddNextHop(nthop1)
|
|
hopList.AddNextHop(nthop2)
|
|
hopList.AddNextHop(nthop3)
|
|
|
|
// 编码
|
|
encoder := encoding.Encoder{}
|
|
err := encoder.EncoderReset(encoding.MaxPacketSize, 0)
|
|
if err != nil {
|
|
fmt.Println("RoutingTableEntry编码报错,", err.Error())
|
|
return
|
|
}
|
|
length, err := hopList.WireEncode(&encoder)
|
|
if err != nil {
|
|
t.Fatal("RoutingTableEntry编码失败", err.Error())
|
|
}
|
|
|
|
// 获取编码
|
|
buf, err := encoder.GetBuffer()
|
|
if err != nil {
|
|
t.Fatal("RoutingTableEntry获取buffer结果失败", err.Error())
|
|
}
|
|
|
|
// 打印编码结果。如果可以正确解码,则说明编码成功
|
|
fmt.Println(length)
|
|
for i := 0; i < len(buf); i++ {
|
|
fmt.Print(buf[i], ", ")
|
|
}
|
|
fmt.Println()
|
|
}
|
|
|
|
//
|
|
// TestRoutingTableEntry_WireDecode
|
|
// @Description: 测试解码
|
|
// @param t
|
|
//
|
|
func TestRoutingTableEntry_WireDecode(t *testing.T) {
|
|
// 由数组构建block
|
|
var buf = [80]byte{241, 1, 189, 76, 101, 16, 100, 2, 0, 47,
|
|
100, 4, 0, 112, 107, 117, 100, 4, 0, 114, 117, 110, 241,
|
|
1, 188, 54, 241, 1, 187, 14, 241, 1, 186, 1, 10, 241, 1,
|
|
185, 5, 47, 104, 111, 112, 49, 241, 1, 187, 14, 241, 1,
|
|
186, 1, 20, 241, 1, 185, 5, 47, 104, 111, 112, 50, 241,
|
|
1, 187, 14,
|
|
241, 1, 186, 1, 30, 241, 1, 185, 5, 47, 104, 111, 112, 51}
|
|
block, err := encoding.CreateBlockByBuffer(buf[:], true)
|
|
if err != nil {
|
|
fmt.Println("创建block失败 ", err.Error())
|
|
return
|
|
}
|
|
|
|
// 由Block构建结构体,并打印变量
|
|
base := new(RoutingTableEntry)
|
|
err = base.WireDecode(block)
|
|
if err != nil {
|
|
fmt.Println("线性解码失败 ", err.Error())
|
|
return
|
|
}
|
|
fmt.Println(base.ToString())
|
|
}
|