mirror of
https://gitee.com/willfree/mlsr.git
synced 2026-06-03 15:56:13 +08:00
对序列号管理器做了充分测试
This commit is contained in:
+24
-25
@@ -13,7 +13,6 @@ import (
|
||||
common2 "minlib/common"
|
||||
"mlsr/lsa"
|
||||
"mlsr/utils"
|
||||
"os"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -23,12 +22,19 @@ import (
|
||||
var once sync.Once // 用以保证SequencingManager的使用是单例模式
|
||||
var instance *SequencingManager // 单例模式的序列号管理器
|
||||
|
||||
var seqFileName string = "mlsrSeqNo.txt" // 保存序列号的文件名
|
||||
var seqFileName string = ".mlsrSeqNo.txt" // 保存序列号的文件名
|
||||
|
||||
//
|
||||
// SequencingManager
|
||||
// @Description: 序列号管理器
|
||||
//
|
||||
type SequencingManager struct {
|
||||
adjLsaSeq uint64
|
||||
nameLsaSeq uint64
|
||||
corLsaSeq uint64
|
||||
// 三种序列号
|
||||
adjLsaSeq uint64
|
||||
nameLsaSeq uint64
|
||||
corLsaSeq uint64
|
||||
|
||||
// 序列号保存到的文件路径
|
||||
seqFileNameWithPath string
|
||||
}
|
||||
|
||||
@@ -42,7 +48,7 @@ func GetInstance() *SequencingManager {
|
||||
once.Do(func() {
|
||||
homePath, err := common2.Home()
|
||||
if err != nil {
|
||||
common2.LogFatal("Get current user home path failed!")
|
||||
common2.LogError("Get current user home path failed!")
|
||||
}
|
||||
// 生成序列号管理器文件路径,并日志记录下来
|
||||
filePath := path.Join(homePath, seqFileName)
|
||||
@@ -75,47 +81,40 @@ func (s *SequencingManager) setSeqFileDirectory(filePath string) error {
|
||||
//
|
||||
func (s *SequencingManager) InitiateSeqNoFromFile() {
|
||||
// 先判断文件是否存在,不存在,则初始化文件
|
||||
fileInfo, b := utils.IsExists(s.seqFileNameWithPath)
|
||||
common2.LogInfo("seq file info is " + fileInfo.Name())
|
||||
_, b := utils.IsExists(s.seqFileNameWithPath)
|
||||
if !b {
|
||||
common2.LogFatal("sequencingManager File not exists, fileinfo is ", fileInfo)
|
||||
common2.LogInfo("sequencingManager file not exists, fileinfo is " + s.seqFileNameWithPath)
|
||||
homePath, err := common2.Home()
|
||||
if err != nil {
|
||||
common2.LogFatal("Get current user home path failed!")
|
||||
common2.LogError("Get current user home path failed!")
|
||||
}
|
||||
// 生成序列号管理器文件,并日志记录下来
|
||||
// 生成序列号管理器文件,并将序列号写入文件
|
||||
filePath := path.Join(homePath, seqFileName)
|
||||
_ = instance.setSeqFileDirectory(filePath)
|
||||
_, err = os.Create(filePath)
|
||||
if err != nil {
|
||||
common2.LogFatal("create file failed![%v]\n", err)
|
||||
} else {
|
||||
common2.LogInfo("create sequencingManager file path successed, the path is" + filePath)
|
||||
}
|
||||
// 将序列号写入文件
|
||||
utils.WriteFile(filePath, "0 0 0") // 依次表示:邻接LSA序列号、名称LSA序列号、双曲LSA序列号
|
||||
_ = s.setSeqFileDirectory(filePath)
|
||||
utils.ClearAndWriteFile(filePath, "0 0 0") // 依次表示:邻接LSA序列号、名称LSA序列号、双曲LSA序列号
|
||||
}
|
||||
// 并读取数据
|
||||
seqs, err := utils.ReadFromFile(s.seqFileNameWithPath)
|
||||
common2.LogInfo("读取数据成功,序列号为: " + seqs)
|
||||
if err != nil {
|
||||
common2.LogFatal("read seqs from " + s.seqFileNameWithPath + " failed.")
|
||||
common2.LogError("read seqs from " + s.seqFileNameWithPath + " failed.")
|
||||
return
|
||||
}
|
||||
// 读取邻接
|
||||
seqsArray := strings.Fields(seqs)
|
||||
s.adjLsaSeq, err = strconv.ParseUint(seqsArray[0], 10, 64)
|
||||
if err != nil {
|
||||
common2.LogFatal("read seqs from " + s.seqFileNameWithPath + " failed, reason is " + err.Error())
|
||||
common2.LogError("read seqs from " + s.seqFileNameWithPath + " failed, reason is " + err.Error())
|
||||
}
|
||||
// 读取名称
|
||||
s.nameLsaSeq, err = strconv.ParseUint(seqsArray[1], 10, 64)
|
||||
if err != nil {
|
||||
common2.LogFatal("read seqs from " + s.seqFileNameWithPath + " failed, reason is " + err.Error())
|
||||
common2.LogError("read seqs from " + s.seqFileNameWithPath + " failed, reason is " + err.Error())
|
||||
}
|
||||
// 读取双曲
|
||||
s.corLsaSeq, err = strconv.ParseUint(seqsArray[2], 10, 64)
|
||||
if err != nil {
|
||||
common2.LogFatal("read seqs from " + s.seqFileNameWithPath + " failed, reason is " + err.Error())
|
||||
common2.LogError("read seqs from " + s.seqFileNameWithPath + " failed, reason is " + err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -126,7 +125,7 @@ func (s *SequencingManager) InitiateSeqNoFromFile() {
|
||||
//
|
||||
func (s *SequencingManager) writeSeqNoToFile() {
|
||||
// 将序列号写入文件。依次表示:邻接LSA序列号、名称LSA序列号、双曲LSA序列号
|
||||
utils.WriteFile(s.seqFileNameWithPath, strconv.FormatUint(s.adjLsaSeq, 10)+" "+
|
||||
utils.ClearAndWriteFile(s.seqFileNameWithPath, strconv.FormatUint(s.adjLsaSeq, 10)+" "+
|
||||
strconv.FormatUint(s.nameLsaSeq, 10)+" "+
|
||||
strconv.FormatUint(s.corLsaSeq, 10))
|
||||
}
|
||||
|
||||
@@ -8,9 +8,25 @@
|
||||
|
||||
package lsdb
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"fmt"
|
||||
"mlsr/lsa"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// 测试从文件读写序列号管理器
|
||||
func TestSequencingManager_InitiateSeqNoFromFile(t *testing.T) {
|
||||
// 构建序列号管理器
|
||||
seqManager := GetInstance()
|
||||
fmt.Println("path: ", seqManager.seqFileNameWithPath)
|
||||
|
||||
// 增加邻接序列号
|
||||
seqManager.IncreaseAdjLsaSeq()
|
||||
|
||||
// 设置,获取指定lsa序列号
|
||||
seqManager.SetLsaSeq(9999, lsa.LsaNAMEType)
|
||||
seq1 := seqManager.GetNameLsaSeq()
|
||||
seq2 := seqManager.GetAdjLsaSeq()
|
||||
seq3, _ := seqManager.GetLsaSeq(lsa.LsaCOORDINATEType)
|
||||
fmt.Println(seq1, seq2, seq3)
|
||||
}
|
||||
|
||||
@@ -70,7 +70,7 @@ func (t *RoutingTable) Calculate() {
|
||||
// 安排指定时间之后执行一个路由计算
|
||||
err := t.ScheduleRoutingTableCalculation()
|
||||
if err != nil {
|
||||
common2.LogFatal("Calculate error,Schedule RoutingTableCalculation error, ", err)
|
||||
common2.LogError("Calculate error,Schedule RoutingTableCalculation error, ", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -140,7 +140,7 @@ func (t *RoutingTable) ScheduleRoutingTableCalculation() error {
|
||||
//
|
||||
func (t *RoutingTable) calculateLsRoutingTable() {
|
||||
if t.lsdb.GetIsBuildAdjLsaScheduled() {
|
||||
common2.LogFatal("Adjacency build is scheduled, routing table can not be calculated :(")
|
||||
common2.LogError("Adjacency build is scheduled, routing table can not be calculated :(")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -154,13 +154,13 @@ func (t *RoutingTable) calculateLsRoutingTable() {
|
||||
// 构造路由映射表
|
||||
lsas, err := t.lsdb.GetLSAsByType(lsa.LsaADJACENCYType)
|
||||
if err != nil {
|
||||
common2.LogFatal("Get Lsa in calculateLsRoutingTable error, ", err)
|
||||
common2.LogError("Get Lsa in calculateLsRoutingTable error, ", err)
|
||||
}
|
||||
rMap := new(Calculator.RouterMap)
|
||||
rMap.Init()
|
||||
err = rMap.CreateFromAdjLsdb(lsas)
|
||||
if err != nil {
|
||||
common2.LogFatal("create RouterMap from lsdb error in calculateLsRoutingTable error, ", err)
|
||||
common2.LogError("create RouterMap from lsdb error in calculateLsRoutingTable error, ", err)
|
||||
}
|
||||
|
||||
nRouters := rMap.GetMapSize()
|
||||
@@ -171,7 +171,7 @@ func (t *RoutingTable) calculateLsRoutingTable() {
|
||||
// 使用计算器去计算路由
|
||||
err = calculator.CalculatePath(rMap, t, t.mlsrConfig, t.lsdb)
|
||||
if err != nil {
|
||||
common2.LogFatal("CalculatePath in calculateLsRoutingTable error, ", err)
|
||||
common2.LogError("CalculatePath in calculateLsRoutingTable error, ", err)
|
||||
}
|
||||
|
||||
// todo: 开始根据新计算的路由去更新NPT表
|
||||
|
||||
@@ -40,6 +40,37 @@ func ReadFromFile(path string) (string, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// WriteFile 写入文件,文件不存在则创建,如在则重写内容
|
||||
func ClearAndWriteFile(path string, str string) {
|
||||
_, b := IsFile(path)
|
||||
var f *os.File
|
||||
var err error
|
||||
if b {
|
||||
//打开文件,
|
||||
f, _ = os.OpenFile(path, os.O_RDWR|os.O_TRUNC, 0666)
|
||||
} else {
|
||||
//新建文件
|
||||
f, err = os.Create(path)
|
||||
}
|
||||
|
||||
//使用完毕,需要关闭文件
|
||||
defer func() {
|
||||
err = f.Close()
|
||||
if err != nil {
|
||||
fmt.Println("err = ", err)
|
||||
}
|
||||
}()
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("err = ", err)
|
||||
return
|
||||
}
|
||||
_, err = f.WriteString(str)
|
||||
if err != nil {
|
||||
fmt.Println("err = ", err)
|
||||
}
|
||||
}
|
||||
|
||||
// WriteFile 写入文件,文件不存在则创建,如在则追加内容
|
||||
func WriteFile(path string, str string) {
|
||||
_, b := IsFile(path)
|
||||
|
||||
Reference in New Issue
Block a user