1
0
mirror of https://gitee.com/willfree/mlsr.git synced 2026-06-06 12:29:27 +08:00
Files
mlsr/lsdb/SequencingManager.go
T

271 lines
6.5 KiB
Go

// Package lsdb
// @Author: Wang Feng
// @Description:
// @Version: 0.1.0
// @Date: 2022/3/24 17:40
// @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
//
package lsdb
import (
"errors"
common2 "minlib/common"
"mlsr/lsa"
"mlsr/utils"
"path"
"strconv"
"strings"
"sync"
)
var once sync.Once // 用以保证SequencingManager的使用是单例模式
var instance *SequencingManager // 单例模式的序列号管理器
var seqFileName string = ".mlsrSeqNo.txt" // 保存序列号的文件名
//
// SequencingManager
// @Description: 序列号管理器
//
type SequencingManager struct {
// 三种序列号
adjLsaSeq uint64
nameLsaSeq uint64
corLsaSeq uint64
// 序列号保存到的文件路径
seqFileNameWithPath string
}
//
// GetInstance
// @Description: 获取序列号管理器的单例。如果是第一次,则创建它
// @param filePath 序列号同步到本地文件的文件位置
// @return *SequencingManager
//
func GetInstance() *SequencingManager {
once.Do(func() {
homePath, err := common2.Home()
if err != nil {
common2.LogError("Get current user home path failed!")
}
// 生成序列号管理器文件路径,并日志记录下来
filePath := path.Join(homePath, seqFileName)
common2.LogInfo("SequencingManager file path is " + filePath)
// 从文件中初始化该序列号管理器
instance = &SequencingManager{0, 0, 0,
filePath}
_ = instance.setSeqFileDirectory(filePath)
instance.InitiateSeqNoFromFile()
})
return instance
}
//
// SetSeqFileDirectory
// @Description: 设置序列号文件的保存路径
// linux系统下,默认保存路径为home路径
// @receiver s
// @param filePath
//
func (s *SequencingManager) setSeqFileDirectory(filePath string) error {
s.seqFileNameWithPath = filePath
return nil
}
//
// InitiateSeqNoFromFile
// @Description: 从文件中初始化各种类型lsa的seq
// @receiver s
//
func (s *SequencingManager) InitiateSeqNoFromFile() {
// 先判断文件是否存在,不存在,则初始化文件
_, b := utils.IsExists(s.seqFileNameWithPath)
if !b {
common2.LogInfo("sequencingManager file not exists, fileinfo is " + s.seqFileNameWithPath)
homePath, err := common2.Home()
if err != nil {
common2.LogError("Get current user home path failed!")
}
// 生成序列号管理器文件,并将序列号写入文件
filePath := path.Join(homePath, seqFileName)
_ = 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.")
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())
}
// 读取名称
s.nameLsaSeq, err = strconv.ParseUint(seqsArray[1], 10, 64)
if err != nil {
common2.LogFatal("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())
}
}
//
// WriteSeqNoToFile
// @Description: 将当前seq写入文件
// @receiver s
//
func (s *SequencingManager) writeSeqNoToFile() {
// 将序列号写入文件。依次表示:邻接LSA序列号、名称LSA序列号、双曲LSA序列号
utils.ClearAndWriteFile(s.seqFileNameWithPath, strconv.FormatUint(s.adjLsaSeq, 10)+" "+
strconv.FormatUint(s.nameLsaSeq, 10)+" "+
strconv.FormatUint(s.corLsaSeq, 10))
}
//
// SetLsaSeq
// @Description: 设置序列号
// @receiver s
// @param seqNo
// @param lsaType
//
func (s *SequencingManager) SetLsaSeq(seqNo uint64, lsaType lsa.LsaType) {
switch lsaType {
case lsa.LsaADJACENCYType:
s.adjLsaSeq = seqNo
case lsa.LsaNAMEType:
s.nameLsaSeq = seqNo
case lsa.LsaCOORDINATEType:
s.corLsaSeq = seqNo
}
// 更新到文件
s.writeSeqNoToFile()
}
//
// GetLsaSeq
// @Description: 查找指定类型LSA的当前序列号
// @receiver s
// @param lsaType
// @return uint64
// @return error
//
func (s *SequencingManager) GetLsaSeq(lsaType lsa.LsaType) (uint64, error) {
switch lsaType {
case lsa.LsaADJACENCYType:
return s.adjLsaSeq, nil
case lsa.LsaNAMEType:
return s.nameLsaSeq, nil
case lsa.LsaCOORDINATEType:
return s.corLsaSeq, nil
}
return 0, errors.New("no found the lsaType: " + strconv.Itoa(int(lsaType)))
}
//
// GetAdjLsaSeq
// @Description: 获取邻接Lsa序列号
// @receiver s
// @return uint64
//
func (s *SequencingManager) GetAdjLsaSeq() uint64 {
return s.adjLsaSeq
}
//
// SetAdjLsaSeq
// @Description: 设置邻接Lsa序列号
// @receiver s
// @param seq
//
func (s *SequencingManager) SetAdjLsaSeq(seq uint64) {
s.adjLsaSeq = seq
// 更新到文件
s.writeSeqNoToFile()
}
//
// GetNameLsaSeq
// @Description: 获取名称LSA序列号
// @receiver s
// @return uint64
//
func (s *SequencingManager) GetNameLsaSeq() uint64 {
return s.nameLsaSeq
}
//
// SetNameLsaSeq
// @Description: 设置名称LSA序列号
// @receiver s
// @param seq
//
func (s *SequencingManager) SetNameLsaSeq(seq uint64) {
s.nameLsaSeq = seq
// 更新到文件
s.writeSeqNoToFile()
}
//
// GetCorLsaSeq
// @Description: 获取坐标LSA序列号
// @receiver s
// @return uint64
//
func (s *SequencingManager) GetCorLsaSeq() uint64 {
return s.corLsaSeq
}
//
// SetCorLsaSeq
// @Description: 设置坐标LSA序列号
// @receiver s
// @param seq
//
func (s *SequencingManager) SetCorLsaSeq(seq uint64) {
s.corLsaSeq = seq
// 更新到文件
s.writeSeqNoToFile()
}
//
// IncreaseAdjLsaSeq
// @Description: 邻接LSA序列号加一
// @receiver s
//
func (s *SequencingManager) IncreaseAdjLsaSeq() {
s.adjLsaSeq++
// 更新到文件
s.writeSeqNoToFile()
}
//
// IncreaseNameLsaSeq
// @Description: 名称LSA序列号加一
// @receiver s
//
func (s *SequencingManager) IncreaseNameLsaSeq() {
s.nameLsaSeq++
// 更新到文件
s.writeSeqNoToFile()
}
//
// IncreaseCorLsaSeq
// @Description: 坐标LSA序列号加一
// @receiver s
//
func (s *SequencingManager) IncreaseCorLsaSeq() {
s.corLsaSeq++
// 更新到文件
s.writeSeqNoToFile()
}