diff --git a/hello/HelloProtocol.go b/hello/HelloProtocol.go index 89d3838..8c209d6 100644 --- a/hello/HelloProtocol.go +++ b/hello/HelloProtocol.go @@ -19,6 +19,7 @@ import ( "mlsr/lsdb" "mlsr/route" "mlsr/utils" + "time" ) // 用于hello兴趣包的名称的组件 @@ -37,7 +38,7 @@ type TimeoutCallback = func(interest *packet.Interest) //type OptionalNack = *packet.Nack type HelloProtocol struct { - m_face *logicface.LogicFace + m_face *logicface.LogicFaceICN m_scheduler *lsdb.MlsrScheduler m_keychain *security.KeyChain mlsrConfig *common.MlsrConfig @@ -64,7 +65,7 @@ type HelloProtocol struct { // @param routingTable // @param mlsdb // -func (hp *HelloProtocol) Init(face *logicface.LogicFace, +func (hp *HelloProtocol) Init(face *logicface.LogicFaceICN, scheduler *lsdb.MlsrScheduler, keychain *security.KeyChain, mlsrConfig *common.MlsrConfig, routingTable *route.RoutingTable, mlsdb *lsdb.Lsdb) { @@ -115,7 +116,12 @@ func (hp *HelloProtocol) expressInterest(interestName *component.Identifier, _mi interest := packet.NewInterest() interest.SetName(interestName) interest.SetInterestLifeTime(uint64(_millseconds)) - // todo 这里需要实现的是,将兴趣包通过face发出,并对超时、nack、正常应答三种情况进行处理 + // 将兴趣包通过face发出,并对超时、nack、正常应答三种情况进行处理 + err := hp.m_face.ExpressInterest(interest, hp.onContent, hp.processInterestTimedOut, hp.processInterestNack) + if err != nil { + common2.LogDebug("HelloProtocol express interest error, maybe the icn logic face doesn't work.", err) + return + } } // @@ -158,13 +164,33 @@ func (hp *HelloProtocol) SendHelloInterest(neighbor *component.Identifier) { } } +// +// processInterestNack +// @Description: 发送兴趣包给邻居路由器,当收到nack时,执行本函数 +// @receiver hp +// @param interest +// @param nack +// +func (hp *HelloProtocol) processInterestNack(interest *packet.Interest, nack *packet.Nack) { + common2.LogDebug("HelloProtocol receive nack for interest: "+interest.GetName().ToUri()+ + ", the nack reason is: ", nack.GetNackReason(), "will treat as time out in 2s later.") + job, err := hp.m_scheduler.ScheduleTaskAfterDuration(2*time.Second, 1). + Do(hp.processInterestTimedOut, interest) + job.LimitRunsTo(1) + hp.m_scheduler.StartAsync() + if err != nil { + common2.LogError("HelloProtocol ScheduleTask to processInterestTimedOut error. ", err.Error()) + } +} + // // processInterestTimedOut -// @Description: 发送兴趣包给邻居路由器,当超时或nack时,执行本函数 +// @Description: 发送兴趣包给邻居路由器,当超时时,执行本函数 // @receiver hp // @param interest // func (hp *HelloProtocol) processInterestTimedOut(interest *packet.Interest) { + common2.LogDebug("HelloProtocol time out for interest: " + interest.GetName().ToUri()) // interest name: //NLSR/INFO/ interestName := interest.GetName() // 取出兴趣包倒数第二个组件,对其进行校验