From f2486d771ba6f5396883f51fa30107f78defe8e3 Mon Sep 17 00:00:00 2001 From: pxy <497407975@qq.com> Date: Thu, 5 May 2022 20:30:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=8B=E8=AF=95ExpressInterest=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 2 +- go.sum | 2 - hello/HelloProtocol.go | 7 +- hello/HelloProtocol_test.go | 146 ++++-------------------------------- hello/jj.txt | 144 +++++++++++++++++++++++++++++++++++ lsa/LsaBase_test.go | 14 ++-- 6 files changed, 171 insertions(+), 144 deletions(-) create mode 100644 hello/jj.txt diff --git a/go.mod b/go.mod index 810ee9e..79c0306 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module mlsr go 1.16 require ( - github.com/sirupsen/logrus v1.8.1 // indirect + github.com/sirupsen/logrus v1.8.1 github.com/smartystreets/goconvey v1.7.2 // indirect github.com/tidwall/btree v1.2.1 // indirect github.com/tidwall/buntdb v1.2.9 diff --git a/go.sum b/go.sum index 7d06bb2..6f7dbca 100644 --- a/go.sum +++ b/go.sum @@ -57,5 +57,3 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/ini.v1 v1.62.0 h1:duBzk771uxoUuOlyRLkHsygud9+5lrlGjdFBb4mSKDU= gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/hello/HelloProtocol.go b/hello/HelloProtocol.go index 850aec5..bae7bac 100644 --- a/hello/HelloProtocol.go +++ b/hello/HelloProtocol.go @@ -47,8 +47,8 @@ var adjacentList *lsa.AdjLsaAdjacenctList // @param interestNamePrefix // @param seconds // -func (h *HelloProtocol) ExpressInterest(interestName *component.Identifier, seconds uint32) { - logrus.Debug("Expressing Interest:%v", interestName) +func (h *HelloProtocol) ExpressInterest(interestName *component.Identifier, seconds uint32) error { + logrus.Debug("Expressing Interest: ", interestName) var interest *packet.Interest interest.SetName(interestName) //设置兴趣包的名字 interest.SetInterestLifeTime(uint64(seconds)) //设置兴趣包的生命周期 @@ -61,6 +61,7 @@ func (h *HelloProtocol) ExpressInterest(interestName *component.Identifier, seco } else { fmt.Println("Express unseccessfully!") } + return nil } // SendHelloInterest @@ -91,7 +92,7 @@ func (h *HelloProtocol) SendHelloInterest(neighbor *component.Identifier) { interestName.Append(info1) //在兴趣包名字后面添加“INFO” interestName.Append(thisRouterPrefix) //在兴趣包名字后面添加本路由器的前缀 h.ExpressInterest(interestName, h.mlsrConfig.HelloTimeout) //将兴趣包传出去 - logrus.Debug("Sending Hello Interest:%v", interestName) //日志记录 + logrus.Debug("Sending Hello Interest:", interestName) //日志记录 } //使用调度器循环调度上面事件,调度器里传入两个参数:一个是间隔时间,一个是event,意思是每隔HelloInterval时间就调用一次event事件 //h.scheduler.scheduler(h.mlsrConfig.HelloInterval,{h.SendHelloInterest(this.neighbor)}) diff --git a/hello/HelloProtocol_test.go b/hello/HelloProtocol_test.go index 2e42ced..0ff190e 100644 --- a/hello/HelloProtocol_test.go +++ b/hello/HelloProtocol_test.go @@ -8,148 +8,32 @@ package hello import ( - "math/rand" "minlib/component" - "minlib/packet" - "mlsr/lsa" "testing" - "time" ) +var h = new(HelloProtocol) + // // TestExpressInterest // @Description: 测试“发送一个Hello兴趣包” // @param t // -func (h *HelloProtocol) TestExpressInterest(t *testing.T) { +func TestExpressInterest(t *testing.T) { //构建*identifier var interestName *component.Identifier var str = "/min" - interestName.BuildIdentifierByString(str) - //声明并初始化一个生命周期 - var seconds uint32 = 10 - //测试这个方法 - h.ExpressInterest(interestName, seconds) -} - -// -// TestSendHelloInterest -// @Description: 测试发送hello兴趣包给一个邻居 -// @receiver h -// @param t -// -func (h *HelloProtocol) TestSendHelloInterest(t *testing.T) { - //构建*identifier - var neighbor *component.Identifier - var str = "/min/MLSR/INFO/router1" - neighbor.BuildIdentifierByString(str) - //构建*AdjLsaAdjacencyInfo - var info *lsa.AdjLsaAdjacencyInfo - info.SetLinkCost(1) - info.SetNeighborRouterIdentifier(neighbor) - info.SetLogicFaceId(182828) - //构建adjacentList - adjacentList.Insert(info) - //测试该方法,本方法中最后一步是ExpressInterest,若发送成功则输出“Express successfully”,否则输出“Express unsuccessfully” - h.SendHelloInterest(neighbor) -} - -// -// TestProcessInterest -// @Description: 测试处理来自邻居的Hello兴趣包 -// @receiver h -// @param t -// -func (h *HelloProtocol) TestProcessInterest(t *testing.T) { - //构建interest - var interest *packet.Interest - var interestName *component.Identifier - var str = "/min/MLSR/INFO/router1" - interestName.BuildIdentifierByString(str) - interest.SetName(interestName) - interest.SetInterestLifeTime(10) - interest.SetCanBePrefix(true) - interest.SetMustBeRefresh(true) - interest.SetNonce(1234) - interest.SetHopLimit(1234) - interest.SetCongestionLevel(1234) - interest.SetTtl(1234) - interest.Payload.SetValue(RandString(7000)) - //构建*AdjLsaAdjacencyInfo - var info *lsa.AdjLsaAdjacencyInfo - info.SetLinkCost(1) - info.SetNeighborRouterIdentifier(interestName) - info.SetLogicFaceId(182828) - //构建adjacentList - adjacentList.Insert(info) - //测试该方法 - h.ProcessInterest(interest) - -} - -// -// TestProcessInterestTimeOut -// @Description: 测试重连 -// @receiver h -// @param t -// -func (h *HelloProtocol) TestProcessInterestTimeOut(t *testing.T) { - //构建interest - var interest *packet.Interest - var interestName *component.Identifier - var str = "/min/MLSR/INFO/router1" - interestName.BuildIdentifierByString(str) - interest.SetName(interestName) - interest.SetInterestLifeTime(10) - interest.SetCanBePrefix(true) - interest.SetMustBeRefresh(true) - interest.SetNonce(1234) - interest.SetHopLimit(1234) - interest.SetCongestionLevel(1234) - interest.SetTtl(1234) - interest.Payload.SetValue(RandString(7000)) - //构建*AdjLsaAdjacencyInfo - var info *lsa.AdjLsaAdjacencyInfo - info.SetLinkCost(1) - info.SetNeighborRouterIdentifier(interestName) - info.SetLogicFaceId(182828) - //构建adjacentList - adjacentList.Insert(info) - //测试该方法 - h.ProcessInterestTimeOut(interest) - -} - -func (h *HelloProtocol) TestOnContent(t *testing.T) { - //构建data - data := new(packet.Data) - data.FreshnessPeriod.SetFreshnessPeriod(02346345465453) - data.Payload.SetValue([]byte{132, 221, 223, 25}) - data.CongestionMark.SetCongestionLevel(1190919) - name, _ := component.CreateIdentifierByString("/min/MLSR/INFO/router1/1") - data.SetName(name) - //测试该方法 - h.OnContent(data) - -} - -func (h *HelloProtocol) TestOnContentValidated(t *testing.T) { - //构建data - data := new(packet.Data) - data.FreshnessPeriod.SetFreshnessPeriod(02346345465453) - data.Payload.SetValue([]byte{132, 221, 223, 25}) - data.CongestionMark.SetCongestionLevel(1190919) - name, _ := component.CreateIdentifierByString("/min/MLSR/INFO/router1/1") - data.SetName(name) - //测试该方法 - h.OnContentValidated(data) -} -func RandString(len int) []byte { - r := rand.New(rand.NewSource(time.Now().UnixNano())) - bytes := make([]byte, len) - for i := 0; i < len; i++ { - b := r.Intn(26) + 65 - bytes[i] = byte(b) + if interestName != nil { + interestName.BuildIdentifierByString(str) + //声明并初始化一个生命周期 + var seconds uint32 = 10 + //测试这个方法 + //fmt.Println(h.ExpressInterest()) + //if h.ExpressInterest(interestName, seconds) != nil { + // fmt.Println("Success!") + //} + if h != nil { + h.ExpressInterest(interestName, seconds) + } } - return bytes } diff --git a/hello/jj.txt b/hello/jj.txt new file mode 100644 index 0000000..e1ae522 --- /dev/null +++ b/hello/jj.txt @@ -0,0 +1,144 @@ +//package hello +//@Author: Pei Xinyuan +//@Description: +//@Version: 0.1.0 +//@Date: 2022/5/5 17:04:00 +//@Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室 + +package hello + +import ( + "math/rand" + "minlib/component" + "minlib/packet" + "mlsr/lsa" + "testing" + "time" +) + +// +// TestSendHelloInterest +// @Description: 测试发送hello兴趣包给一个邻居 +// @receiver h +// @param t +// +func TestSendHelloInterest(t *testing.T) { + //构建*identifier + var neighbor *component.Identifier + var str = "/min/MLSR/INFO/router1" + neighbor.BuildIdentifierByString(str) + //构建*AdjLsaAdjacencyInfo + var info *lsa.AdjLsaAdjacencyInfo + info.SetLinkCost(1) + info.SetNeighborRouterIdentifier(neighbor) + info.SetLogicFaceId(182828) + //构建adjacentList + adjacentList.Insert(info) + //测试该方法,本方法中最后一步是ExpressInterest,若发送成功则输出“Express successfully”,否则输出“Express unsuccessfully” + //var h HelloProtocol + //h.SendHelloInterest(neighbor) +} + +// +// TestProcessInterest +// @Description: 测试处理来自邻居的Hello兴趣包 +// @receiver h +// @param t +// +func TestProcessInterest(t *testing.T) { + //构建interest + var interest *packet.Interest + var interestName *component.Identifier + var str = "/min/MLSR/INFO/router1" + interestName.BuildIdentifierByString(str) + interest.SetName(interestName) + interest.SetInterestLifeTime(10) + interest.SetCanBePrefix(true) + interest.SetMustBeRefresh(true) + interest.SetNonce(1234) + interest.SetHopLimit(1234) + interest.SetCongestionLevel(1234) + interest.SetTtl(1234) + interest.Payload.SetValue(RandString(7000)) + //构建*AdjLsaAdjacencyInfo + var info *lsa.AdjLsaAdjacencyInfo + info.SetLinkCost(1) + info.SetNeighborRouterIdentifier(interestName) + info.SetLogicFaceId(182828) + //构建adjacentList + adjacentList.Insert(info) + //测试该方法 + var h *HelloProtocol + h.ProcessInterest(interest) + +} + +// +// TestProcessInterestTimeOut +// @Description: 测试重连 +// @receiver h +// @param t +// +func TestProcessInterestTimeOut(t *testing.T) { + //构建interest + var interest *packet.Interest + var interestName *component.Identifier + var str = "/min/MLSR/INFO/router1" + interestName.BuildIdentifierByString(str) + interest.SetName(interestName) + interest.SetInterestLifeTime(10) + interest.SetCanBePrefix(true) + interest.SetMustBeRefresh(true) + interest.SetNonce(1234) + interest.SetHopLimit(1234) + interest.SetCongestionLevel(1234) + interest.SetTtl(1234) + interest.Payload.SetValue(RandString(7000)) + //构建*AdjLsaAdjacencyInfo + var info *lsa.AdjLsaAdjacencyInfo + info.SetLinkCost(1) + info.SetNeighborRouterIdentifier(interestName) + info.SetLogicFaceId(182828) + //构建adjacentList + adjacentList.Insert(info) + //测试该方法 + var h *HelloProtocol + h.ProcessInterestTimeOut(interest) + +} + +func TestOnContent(t *testing.T) { + //构建data + data := new(packet.Data) + data.FreshnessPeriod.SetFreshnessPeriod(02346345465453) + data.Payload.SetValue([]byte{132, 221, 223, 25}) + data.CongestionMark.SetCongestionLevel(1190919) + name, _ := component.CreateIdentifierByString("/min/MLSR/INFO/router1/1") + data.SetName(name) + //测试该方法 + var h *HelloProtocol + h.OnContent(data) + +} + +func TestOnContentValidated(t *testing.T) { + //构建data + data := new(packet.Data) + data.FreshnessPeriod.SetFreshnessPeriod(02346345465453) + data.Payload.SetValue([]byte{132, 221, 223, 25}) + data.CongestionMark.SetCongestionLevel(1190919) + name, _ := component.CreateIdentifierByString("/min/MLSR/INFO/router1/1") + data.SetName(name) + //测试该方法 + var h *HelloProtocol + h.OnContentValidated(data) +} +func RandString(len int) []byte { + r := rand.New(rand.NewSource(time.Now().UnixNano())) + bytes := make([]byte, len) + for i := 0; i < len; i++ { + b := r.Intn(26) + 65 + bytes[i] = byte(b) + } + return bytes +} diff --git a/lsa/LsaBase_test.go b/lsa/LsaBase_test.go index 7f665e0..508f546 100644 --- a/lsa/LsaBase_test.go +++ b/lsa/LsaBase_test.go @@ -24,8 +24,8 @@ import ( func TestLsaBase_GetType(t *testing.T) { base := new(LsaBase) fmt.Println(base.GetType()) - if base.GetType() != 0{ - log.Fatal("base type error: ",base.GetType()) + if base.GetType() != 0 { + log.Fatal("base type error: ", base.GetType()) } } @@ -39,7 +39,7 @@ func TestLsaBase_WireEncode(t *testing.T) { base := new(LsaBase) base.SetLsaExpirationTime(2000) base.SetLsaSequenceNumber(1234) - base.LsaOriginRouterIdentifier,_ = component.CreateIdentifierByString("/min/pku") + base.LsaOriginRouterIdentifier, _ = component.CreateIdentifierByString("/min/pku") // 构造编码器 encoder := encoding.Encoder{} @@ -61,8 +61,8 @@ func TestLsaBase_WireEncode(t *testing.T) { return } fmt.Println(cnt) - for i:=0;i < len(res);i++{ - fmt.Print(res[i],", ") + for i := 0; i < len(res); i++ { + fmt.Print(res[i], ", ") } fmt.Println() // 34 [241 1 154 30 101 16 100 2 0 47 100 4 0 109 105 110 100 4 0 112 107 117 241 1 165 2 4 210 241 1 164 2 7 208] @@ -90,6 +90,6 @@ func TestLsaBase_WireDecode(t *testing.T) { fmt.Println("线性解码失败 ", err.Error()) return } - fmt.Println(base.LsaExpirationTime.LsaExpirationTime()," ",base.LsaSequenceNumber.LsaSequenceNumber(), - " ",base.LsaOriginRouterIdentifier.ToUri()) + fmt.Println(base.LsaExpirationTime.LsaExpirationTime(), " ", base.LsaSequenceNumber.LsaSequenceNumber(), + " ", base.LsaOriginRouterIdentifier.ToUri()) }