mirror of
https://gitee.com/willfree/mlsr.git
synced 2026-06-03 15:56:13 +08:00
测试HelloProtocol文件
This commit is contained in:
@@ -0,0 +1,196 @@
|
||||
//package mir
|
||||
//@Author: Pei Xinyuan
|
||||
//@Description:
|
||||
//@Version: 0.1.0
|
||||
//@Date: 2022/6/24 12:12:00
|
||||
//@Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
|
||||
package mir
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/olekukonko/tablewriter"
|
||||
"minlib/common"
|
||||
"minlib/component"
|
||||
"minlib/logicface"
|
||||
"minlib/mgmt"
|
||||
"minlib/security"
|
||||
"mlsr/cmd"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
const (
|
||||
TopPrefix = "/min-mir/mgmt/localhost"
|
||||
IdentityDBPath = "/usr/local/.mir/identity/"
|
||||
DefaultId = "/mlsr/min/daemonGo"
|
||||
defaultPasswd = "pkusz"
|
||||
UnixPath = "/tmp/mir.sock"
|
||||
)
|
||||
|
||||
var controller *mgmt.MIRController
|
||||
|
||||
type NextHopInfo struct {
|
||||
LogicFaceId uint64
|
||||
Cost uint64
|
||||
}
|
||||
|
||||
type FibInfo struct {
|
||||
Identifier string
|
||||
NextHopsInfo []NextHopInfo
|
||||
}
|
||||
|
||||
// init
|
||||
// @Description: 初始化password、keychain和controller
|
||||
//
|
||||
func init() {
|
||||
//password first
|
||||
passwd, err := cmd.AskPassword()
|
||||
if err != nil {
|
||||
common.LogFatal(err)
|
||||
}
|
||||
//keychain initialization
|
||||
Keychain := new(security.KeyChain)
|
||||
if err := Keychain.InitialKeyChainByPath(IdentityDBPath); err != nil {
|
||||
common.LogFatal(err)
|
||||
}
|
||||
if identity := Keychain.GetIdentityByName(DefaultId); identity == nil {
|
||||
common.LogFatal("Identity => " + "not exists")
|
||||
} else {
|
||||
if err := Keychain.SetCurrentIdentity(identity, passwd); err != nil {
|
||||
common.LogFatal(err)
|
||||
}
|
||||
}
|
||||
//controller initialization
|
||||
controller = mgmt.CreateMIRController(func() (mgmt.IMgmtLogicFace, error) {
|
||||
face := new(logicface.LogicFace)
|
||||
//unix connection
|
||||
if err := face.InitWithUnixSocket(UnixPath); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return face, nil
|
||||
}, true, Keychain)
|
||||
}
|
||||
|
||||
// AddFib
|
||||
// @Description: 添加下一跳路由
|
||||
// @param prefix
|
||||
// @param logicFaceId
|
||||
// @param cost
|
||||
// @return error
|
||||
//
|
||||
|
||||
//add a para:pull/push
|
||||
func AddFib(prefix string, logicFaceId uint64, cost uint64) error {
|
||||
//Create Control Parameters
|
||||
parameters := &component.ControlParameters{}
|
||||
identifier, err := component.CreateIdentifierByString(prefix)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
parameters.SetPrefix(identifier)
|
||||
parameters.SetLogicFaceId(logicFaceId)
|
||||
parameters.SetCost(cost)
|
||||
|
||||
//Create Command Executor
|
||||
|
||||
commandExecutor, err := controller.PrepareCommandExecutor(mgmt.CreateFibAddCommand(TopPrefix, parameters))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
commandExecutor.SetAutoShutdown(true)
|
||||
|
||||
//Execute Command
|
||||
response, err := commandExecutor.Start()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
//Success
|
||||
if response.Code != mgmt.ControlResponseCodeSuccess {
|
||||
//failure
|
||||
return errors.New(fmt.Sprintf("Add next hop for %s => %s failed! errMsg:%s", prefix, logicFaceId, response.Msg))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// DelFib
|
||||
// @Description: 删除一个指定前缀和下一跳的路由
|
||||
// @param prefix
|
||||
// @param logicFaceId
|
||||
// @return error
|
||||
//
|
||||
func DelFib(prefix string, logicFaceId uint64) error {
|
||||
parametres := &component.ControlParameters{}
|
||||
identifier, err := component.CreateIdentifierByString(prefix)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
parametres.SetPrefix(identifier)
|
||||
parametres.SetLogicFaceId(logicFaceId)
|
||||
|
||||
//Create Command Executor
|
||||
commandExecutor, err := controller.PrepareCommandExecutor(mgmt.CreateFibDeleteCommand(TopPrefix, parametres))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
commandExecutor.SetAutoShutdown(true)
|
||||
|
||||
//Execute Command
|
||||
response, err := commandExecutor.Start()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
//success
|
||||
if response.Code != mgmt.ControlResponseCodeSuccess {
|
||||
//Failure
|
||||
return errors.New(fmt.Sprintf("Delete next hop for %s => %s failed! errMsg:%s", prefix, logicFaceId, response.Msg))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ListFib
|
||||
// @Description:显示所有前缀对应的所有下一跳信息
|
||||
// @return error
|
||||
//
|
||||
func ListFib() error {
|
||||
//Create Command Executor
|
||||
commnadExecutor, err := controller.PrepareCommandExecutor(mgmt.CreateFibListCommand(TopPrefix))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
commnadExecutor.SetAutoShutdown(true)
|
||||
//Execute Command
|
||||
response, err := commnadExecutor.Start()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
//反序列化,输出结果
|
||||
var fibInfoList []FibInfo
|
||||
err = json.Unmarshal(response.GetBytes(), &fibInfoList)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
//使用表格美化输出
|
||||
table := tablewriter.NewWriter(os.Stdout)
|
||||
for _, fibInfo := range fibInfoList {
|
||||
for _, nextHopInfo := range fibInfo.NextHopsInfo {
|
||||
table.Append([]string{fibInfo.Identifier, strconv.FormatUint(nextHopInfo.LogicFaceId, 10), strconv.FormatUint(nextHopInfo.Cost, 10)})
|
||||
}
|
||||
}
|
||||
table.SetHeader([]string{"Prefix", "LogicFaceId", "Cost"})
|
||||
table.SetHeaderColor(
|
||||
tablewriter.Colors{tablewriter.FgHiRedColor, tablewriter.Bold},
|
||||
tablewriter.Colors{tablewriter.FgHiRedColor, tablewriter.Bold},
|
||||
tablewriter.Colors{tablewriter.FgHiRedColor, tablewriter.Bold})
|
||||
table.SetCaption(true, "Fib Table Info")
|
||||
table.SetAlignment(tablewriter.ALIGN_CENTER)
|
||||
table.SetAutoMergeCellsByColumnIndex([]int{0})
|
||||
table.Render()
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user