add: Face 列表初始化

This commit is contained in:
zhengqi committed 2022-09-11 21:25:11 +08:00
1 parent 95de71b135
commit 42306a383f
2 files changed
+200 -28

No files matched your search

+11 -11
View File
@@ -32,7 +32,7 @@ type AdjLsaAdjacencyInfo struct {
}
//
// NewAdjLsaAdjacencyInfo
// NewAdjLsaAdjacencyInfo
// @Description: 创建一个结构体对象
// @param adjLsaLinkCost
// @param adjLsaLogicFaceUri
@@ -50,7 +50,7 @@ func NewAdjLsaAdjacencyInfo(adjLsaLinkCost AdjLsaLinkCost, adjLsaLogicFaceUri Ad
}
//
// NewAdjLsaAdjacencyInfoByInstance
// NewAdjLsaAdjacencyInfoByInstance
// @Description: 根据已有的结构体实例,创建邻接信息结构体
// @param adjLsaLinkCost
// @param adjLsaLogicFaceUri
@@ -74,7 +74,7 @@ func NewAdjLsaAdjacencyInfoByInstance(adjLsaLinkCost *AdjLsaLinkCost, adjLsaLogi
}
//
// GetNeighborRouterIdentidier
// GetNeighborRouterIdentifier
// @Description: 获取相邻路由器的标识
// @receiver l
// @return *component.Identifier
@@ -84,7 +84,7 @@ func (l *AdjLsaAdjacencyInfo) GetNeighborRouterIdentifier() *component.Identifie
}
//
// SetNeighborRouterIdentifier
// SetNeighborRouterIdentifier
// @Description:设置相邻路由器的标识
// @receiver l
// @param identifier
@@ -95,7 +95,7 @@ func (l *AdjLsaAdjacencyInfo) SetNeighborRouterIdentifier(routerIdentifier *comp
}
//
// Compare
// Compare
// @Description: 判断本邻接信息是不是与某个邻居MIR的邻接信息
// @receiver l
// @param identifier
@@ -110,7 +110,7 @@ func (l *AdjLsaAdjacencyInfo) Compare(routerIdentifier *component.Identifier) bo
}
//
// CompareLogicFaceUri
// CompareLogicFaceUri
// @Description: 根据FaceUri判断本邻接信息关联的Logicface
// @receiver l
// @param faceUri
@@ -124,7 +124,7 @@ func (l *AdjLsaAdjacencyInfo) CompareLogicFaceUri(faceUri *AdjLsaLogicFaceUri) b
}
//
// CompareLogicFaceUriString
// CompareLogicFaceUriString
// @Description: 根据LogicFaceUri判断本邻接信息关联的Logicface
// @receiver l
// @param faceUriString
@@ -138,7 +138,7 @@ func (l *AdjLsaAdjacencyInfo) CompareLogicFaceUriString(faceUriString string) bo
}
//
// CompareLogicFaceId
// CompareLogicFaceId
// @Description: 根据LogicFaceId判断关联的Logicface
// @receiver l
// @param faceId
@@ -152,7 +152,7 @@ func (l *AdjLsaAdjacencyInfo) CompareLogicFaceId(faceId uint64) bool {
}
//
// ToString
// ToString
// @Description: 转string
// @receiver l
//
@@ -175,7 +175,7 @@ func (l *AdjLsaAdjacencyInfo) ToString() string {
}
//
// WireEncode
// WireEncode
// @Description: 线速编码
// @receiver l
// @param encoder
@@ -220,7 +220,7 @@ func (l *AdjLsaAdjacencyInfo) WireEncode(encoder *encoding.Encoder) (int, error)
}
//
// WireDecode
// WireDecode
// @Description: 线速解码
// @receiver l
// @param block
+189 -17
View File
@@ -9,6 +9,7 @@
package mlsrcmd
import (
"encoding/json"
common2 "minlib/common"
"minlib/component"
"minlib/logicface"
@@ -19,20 +20,21 @@ import (
"mlsr/hello"
"mlsr/lsa"
"mlsr/lsdb"
"mlsr/mir"
"mlsr/route"
"mlsr/utils"
"time"
)
const unixPath = "/tmp/mir.sock"
type Mlsr struct {
LOCALHOST_PREFIX *component.Identifier
m_face *logicface.DummyClientLogicFace
m_scheduler *lsdb.MlsrScheduler
m_confParam *common.MlsrConfig
m_adjacencyList *lsa.AdjLsaAdjacenctList
m_namePrefixList *lsa.NameLsaNamePrefixList
//std::vector<ndn::Name> m_strategySetOnRouters; todo 不同前缀使用不同转发策略
LOCALHOST_PREFIX *component.Identifier
m_face *logicface.DummyClientLogicFace
m_scheduler *lsdb.MlsrScheduler
m_confParam *common.MlsrConfig
m_adjacencyList *lsa.AdjLsaAdjacenctList
m_namePrefixList *lsa.NameLsaNamePrefixList
m_fib *route.Fib
m_lsdb *lsdb.Lsdb
m_routingTable *route.RoutingTable
@@ -43,10 +45,15 @@ type Mlsr struct {
m_onNewLsaConnection *utils2.Connection
m_onPrefixRegistrationSuccess *utils.SignalConnection
m_onInitialHelloDataValidated *utils.SignalConnection
// 用于与MIR进行交互的控制器
m_controller *mgmt.MIRController
m_faceDatasetController *mgmt.MIRController
// todo face通知流
}
//
// Init
// Init
// @Description: Mlsr初始化
// @receiver m
// @param face face
@@ -181,10 +188,33 @@ func (m *Mlsr) Init(face *logicface.DummyClientLogicFace, scheduler *lsdb.MlsrSc
common2.LogFatal("设置组播策略失败:", err)
}
// 管理用 Controller
m.m_controller = mgmt.CreateMIRController(func() (mgmt.IMgmtLogicFace, error) {
face := new(logicface.LogicFace)
// 建立unix连接
if err := face.InitWithUnixSocket(unixPath); err != nil {
return nil, err
}
return face, nil
}, true, chain)
m.m_faceDatasetController = mgmt.CreateMIRController(func() (mgmt.IMgmtLogicFace, error) {
face := new(logicface.LogicFace)
// 建立unix连接
if err := face.InitWithUnixSocket(unixPath); err != nil {
return nil, err
}
return face, nil
}, true, chain)
// 拉取Face列表并初始化
m.InitializeFaces(m.processFaceDataset, m.onFaceDatasetFetchTimeout)
// todo: face通知流
}
//
// RegisterStrategyForCerts
// RegisterStrategyForCerts
// @Description: 为某个前缀设置转发策略
// @receiver m
// @param originRouter
@@ -194,21 +224,163 @@ func (m *Mlsr) RegisterStrategyForCerts(originRouter *component.Identifier) {
// 然而MIR中默认策略即为BEST_ROUTE策略,所以该函数不用实现
}
//
// AddDispatcherTopPrefix
// @Description: 注册TopPrefix todo 去学习一下MIR原理
// AddDispatcherTopPrefix
// @Description: 注册TopPrefix
// @receiver m
//
func (m *Mlsr) AddDispatcherTopPrefix() {
// Dispatcher 用于处理rib的请求
// 暂时不需要实现
}
//
// RegisterPrefix
// RegisterPrefix
// @Description: 使用MLSR的LogicFace注册一个前缀
// @receiver m
// @param prefix
//
func (m *Mlsr) RegisterPrefix(prefix *component.Identifier) {
// Dispatcher 用于处理rib的请求
// 暂时不需要实现
}
// InitializeFaces
// @Description: 通过MIR的face列表初始化邻居节点
// @receiver m
// @param notification
//
func (m *Mlsr) onFaceEventNotification(notification interface{}) {
panic("Implement me")
}
// InitializeFaces
//
// @Description: 通过MIR的face列表初始化邻居节点
//
// @receiver m
//
// @param onFetchSuccess 拉取成功的回调函数
//
// @param onFetchFailure 拉取失败的回调函数
//
func (m *Mlsr) InitializeFaces(onFetchSuccess func([]mgmt.FaceInfo), onFetchFailure func(int, string)) {
// 参数校验
if onFetchFailure == nil || onFetchSuccess == nil {
common2.LogFatal("InitializeFaces: Invalid Arguments")
}
// 开始拉取Face列表
commandExecutor, err := m.m_faceDatasetController.PrepareCommandExecutor(mgmt.CreateLogicFaceListCommand(mir.TopPrefix))
if err != nil {
common2.LogFatal(err.Error())
}
commandExecutor.SetAutoShutdown(true)
// 执行命令拉取结果
response, err := commandExecutor.Start()
if err != nil {
// 拉取失败,调用onFetchFailure回调
onFetchFailure(response.Code, err.Error())
}
// 反序列化,得到Face列表
var faceInfoList []mgmt.FaceInfo
err = json.Unmarshal(response.GetBytes(), &faceInfoList)
if err != nil {
common2.LogFatal(err.Error())
}
// 拉取成功,调用onFetchSuccess回调
onFetchSuccess(faceInfoList)
}
// processFaceDataset(onFetchSuccess)
//
// @Description: 处理向MIR拉取到的Face列表
//
// @receiver m
//
// @param faces 拉取到的Face信息列表
//
func (m *Mlsr) processFaceDataset(faces []mgmt.FaceInfo) {
// 遍历nlsr.conf中指定的所有邻居节点
for _, adjacent := range m.m_adjacencyList.GetAdjList() {
faceUriString := adjacent.LogicFaceUri()
// 遍历 face 列表,查看有无匹配项
for _, face := range faces {
if adjacent.LogicFaceId() == 0 && faceUriString == face.RemoteUri {
// 更新邻居节点的LogicFaceId
adjacent.SetLogicFaceId(face.LogicFaceId)
m.registerAdjacencyPrefixes(adjacent)
}
}
if adjacent.LogicFaceId() == 0 {
common2.LogWarn("The adjacency ", adjacent.GetNeighborRouterIdentifier().ToUri(), " has no Face information in this dataset.")
}
}
// 继续调度FaceDataset的拉取任务
m.scheduleDatasetFetch()
}
// registerAdjacencyPrefixes
//
// @Description: 向MIR注册邻接节点的前缀
//
// @receiver m
//
// @param adj 邻接节点信息
//
func (m *Mlsr) registerAdjacencyPrefixes(adj *lsa.AdjLsaAdjacencyInfo) {
faceUri := adj.LogicFaceUri()
linkCost := adj.LinkCost()
adjName := adj.GetNeighborRouterIdentifier()
// 注册前缀AdjName
if err := m.m_fib.RegisterPrefix(adjName, faceUri, linkCost); err != nil {
common2.LogError(err.Error())
}
// 注册前缀LsaPrefix
if err := m.m_fib.RegisterPrefix(m.m_confParam.GetLsaPrefix(), faceUri, linkCost); err != nil {
common2.LogError(err.Error())
}
}
// scheduleDatasetFetch
//
// @Description: 调度向MIR拉取Face列表的任务
//
// @receiver m
//
func (m *Mlsr) scheduleDatasetFetch() {
// 生成调度任务
job, err := m.m_scheduler.ScheduleTaskAtTime(time.Now().
Add(time.Duration(m.m_confParam.LogicFaceDatasetFetchInterval)*time.Second), 1).
Do(func() {
m.InitializeFaces(m.processFaceDataset, m.onFaceDatasetFetchTimeout)
})
if err != nil {
// 生成任务异常,暂停调度
common2.LogError(err.Error())
return
}
job.LimitRunsTo(1)
// 开始调度任务
m.m_scheduler.StartAsync()
}
// onFaceDatasetFetchTimeout
//
// @Description: 向MIR拉取Face列表失败时的回调函数
//
// @receiver m
//
// @param code 错误代码
//
// @param msg 错误信息
//
func (m *Mlsr) onFaceDatasetFetchTimeout(code int, msg string) {
common2.LogError("Failed to fetch dataset: ", msg, "not trying again this time.")
m.scheduleDatasetFetch()
}