Files
mlsr/hello/HelloProtocol.go
T

151 lines
4.8 KiB
Go

// Package hello
// @Author: Pei Xinyuan
// @Description:
// @Version: 0.1.0
// @Date: 2022/7/14 14:49
// @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
//
package hello
import (
common2 "minlib/common"
"minlib/component"
"minlib/logicface"
"minlib/packet"
"minlib/security"
"mlsr/common"
"mlsr/lsa"
"mlsr/lsdb"
"mlsr/route"
)
// 用于hello兴趣包的名称的组件
const INFO_COMPONENT = "INFO"
const MLSR_COMPONENT = "mlsr"
// 正常收到数据包的触发函数
type DataCallback = func(interest *packet.Interest, data *packet.Data)
// 收到NACK的触发函数
type NackCallback = func(interest *packet.Interest, nack *packet.Nack)
// 超时未响应的触发函数
type TimeoutCallback = func(interest *packet.Interest)
//type OptionalNack = *packet.Nack
type HelloProtocol struct {
m_face *logicface.LogicFace
m_scheduler *lsdb.MlsrScheduler
m_keychain *security.KeyChain
mlsrConfig *common.MlsrConfig
m_routingTable *route.RoutingTable
m_lsdb *lsdb.Lsdb
m_adjacencyList *lsa.AdjLsaAdjacenctList
// 收包池
receivePacketQue chan *packet.Interest // 从其它路由器收包后,放入该队列
state bool // 程序生成状态, true 为正常运行, false 为关闭状态
}
//
// Init
// @Description: Hello协议-初始化
// @receiver hp
// @param face
// @param scheduler
// @param keychain
// @param mlsrConfig
// @param routingTable
// @param mlsdb
//
func (hp *HelloProtocol) Init(face *logicface.LogicFace,
scheduler *lsdb.MlsrScheduler,
keychain *security.KeyChain, mlsrConfig *common.MlsrConfig,
routingTable *route.RoutingTable, mlsdb *lsdb.Lsdb) {
hp.m_face = face
hp.m_scheduler = scheduler
hp.m_keychain = keychain
hp.mlsrConfig = mlsrConfig
hp.m_routingTable = routingTable
hp.m_lsdb = mlsdb
hp.m_adjacencyList = mlsrConfig.GetAdjacencys()
// todo mlsr大联调时应取消以下代码的注释
// 构造接收hello兴趣包的前缀
name, err := component.CreateIdentifierByString(
hp.mlsrConfig.GetRouterPrefix().ToUri() + "/" + MLSR_COMPONENT + "/" + INFO_COMPONENT)
if err != nil {
common2.LogFatal("HelloProtocol Setting interest filter for Hello interest error, interest generate wrong.")
return
}
// 设置监听前缀
err = hp.m_face.RegisterIdentifier(name, 3000)
if err != nil {
common2.LogFatal("HelloProtocol Setting interest filter for Hello interest error, identifier register wrong.")
return
}
// 开一个线程去接收前缀为hello协议所监听前缀的兴趣包,并进行处理
hp.receivePacketQue = make(chan *packet.Interest, 1000)
hp.state = true
// 启动处理接收到的包的协程
go hp.dealWithRecvPktRoutine()
// 启动从客户端接收包的协程
go hp.recvPacketFromClientRoutine()
}
//
// expressInterest
// @Description: 根据传入的兴趣包名称和兴趣包过期时间,构建兴趣包,并发出去
// @receiver hp
// @param interestName
// @param _millseconds
//
func (hp *HelloProtocol) expressInterest(interestName *component.Identifier, _millseconds uint32) {
common2.LogDebug("HelloProtocol Expressing Interest: " + interestName.ToUri())
interest := packet.NewInterest()
interest.SetName(interestName)
interest.SetInterestLifeTime(uint64(_millseconds))
// todo 这里需要实现的是,将兴趣包通过face发出,并对超时、nack、正常应答三种情况进行处理
}
//
// SendHelloInterest
// @Description: 向指定邻居路由器发送hello探测包
// @receiver hp
// @param neighbor
//
func (hp *HelloProtocol) SendHelloInterest(neighbor *component.Identifier) {
// 查找关于某个邻居路由器的邻接信息
adjInfo := hp.m_adjacencyList.FindAdjacencyByRouterIdentifier(neighbor)
// 如果没有找到,则不做处理
if adjInfo == nil {
return
}
// 如果找到了,且有对应的faceID,则发送一个hello探测包
if adjInfo.LogicFaceId() != 0 {
// interest name: /<neighbor>/NLSR/INFO/<router>
name, err := component.CreateIdentifierByString(adjInfo.GetNeighborRouterIdentifier().ToString() +
"/" + MLSR_COMPONENT + "/" + INFO_COMPONENT +
"/" + GetSinglePrefixForHelloProtocol(hp.mlsrConfig.GetRouterPrefix()))
if err != nil {
common2.LogDebug("HelloProtocol SendHelloInterest error, hello name create wrong.")
return
}
// 将data发出去
hp.expressInterest(name, hp.mlsrConfig.GetInterestResendTime())
common2.LogDebug("HelloProtocol Sending HELLO interest: " + name.ToUri())
}
// 安排按照配置文件中的时间,若干秒后【默认一分钟】发送下一个hello兴趣包
_, err := hp.m_scheduler.ScheduleTaskAfterDuration(hp.mlsrConfig.GetInfoInterestInterval(), 1).
Do(hp.SendHelloInterest, neighbor)
if err != nil {
common2.LogDebug("HelloProtocol ScheduleTask to send hello interest error. ")
return
}
}