69 lines
1.5 KiB
Go
69 lines
1.5 KiB
Go
package message_server
|
|
|
|
import (
|
|
"sync"
|
|
"time"
|
|
|
|
"joylink.club/bj-rtsts-server/message_server/ms_api"
|
|
"joylink.club/bj-rtsts-server/message_server/ms_manage"
|
|
"joylink.club/bj-rtsts-server/ts/simulation/wayside/memory"
|
|
)
|
|
|
|
var smsMap sync.Map
|
|
|
|
// 仿真消息服务
|
|
// 管理仿真消息服务,整体可以作为一个消息服务,也可以每个消息子服务各自作为一个消息服务,暂时先按整体作为一个消息服务的方式使用
|
|
type SimulationMs struct {
|
|
vs *memory.VerifySimulation
|
|
mss []ms_api.IMsgServer
|
|
}
|
|
|
|
// 启动仿真所需的消息服务
|
|
func Start(vs *memory.VerifySimulation) {
|
|
_, ok := smsMap.Load(vs.SimulationId)
|
|
if !ok {
|
|
sms := &SimulationMs{
|
|
vs: vs,
|
|
mss: []ms_api.IMsgServer{
|
|
NewSfpMs(vs),
|
|
},
|
|
}
|
|
ms_manage.Register(sms)
|
|
smsMap.Store(vs.SimulationId, sms)
|
|
}
|
|
}
|
|
|
|
// 关闭仿真消息服务
|
|
func Close(vs *memory.VerifySimulation) {
|
|
sms, ok := smsMap.Load(vs.SimulationId)
|
|
if ok {
|
|
ms_manage.Unregister(sms.(*SimulationMs))
|
|
smsMap.Delete(vs.SimulationId)
|
|
}
|
|
}
|
|
|
|
// 获取通道
|
|
func (sms *SimulationMs) GetChannel() string {
|
|
return sms.vs.SimulationId
|
|
}
|
|
|
|
// 发送消息间隔时间,单位ms
|
|
func (sms *SimulationMs) GetInterval() time.Duration {
|
|
return 200 * time.Millisecond
|
|
}
|
|
|
|
// 构造定时发送的消息
|
|
func (sms *SimulationMs) OnTick() ([]*ms_api.TopicMsg, error) {
|
|
var tmList []*ms_api.TopicMsg
|
|
for _, ms := range sms.mss {
|
|
tm, err := ms.OnTick()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(tm) > 0 {
|
|
tmList = append(tmList, tm...)
|
|
}
|
|
}
|
|
return tmList, nil
|
|
}
|