2024-05-14 22:34:41 +02:00
|
|
|
// Copyright 2024, Command Line Inc.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package eventbus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
2024-05-28 00:44:57 +02:00
|
|
|
"github.com/wavetermdev/thenextwave/pkg/waveobj"
|
2024-05-14 22:34:41 +02:00
|
|
|
)
|
|
|
|
|
2024-06-12 02:42:10 +02:00
|
|
|
type WSEventType struct {
|
|
|
|
EventType string `json:"eventtype"`
|
|
|
|
ORef string `json:"oref,omitempty"`
|
|
|
|
Data any `json:"data"`
|
2024-05-14 22:34:41 +02:00
|
|
|
}
|
|
|
|
|
2024-06-14 08:54:04 +02:00
|
|
|
const (
|
2024-06-24 23:34:31 +02:00
|
|
|
FileOp_Append = "append"
|
|
|
|
FileOp_Truncate = "truncate"
|
2024-06-14 08:54:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type WSFileEventData struct {
|
|
|
|
ZoneId string `json:"zoneid"`
|
|
|
|
FileName string `json:"filename"`
|
|
|
|
FileOp string `json:"fileop"`
|
|
|
|
Data64 string `json:"data64"`
|
|
|
|
}
|
|
|
|
|
2024-05-28 00:44:57 +02:00
|
|
|
type WindowWatchData struct {
|
2024-06-12 02:42:10 +02:00
|
|
|
WindowWSCh chan any
|
|
|
|
WaveWindowId string
|
|
|
|
WatchedORefs map[waveobj.ORef]bool
|
2024-05-28 00:44:57 +02:00
|
|
|
}
|
|
|
|
|
2024-05-14 22:34:41 +02:00
|
|
|
var globalLock = &sync.Mutex{}
|
2024-06-12 02:42:10 +02:00
|
|
|
var wsMap = make(map[string]*WindowWatchData) // websocketid => WindowWatchData
|
2024-05-14 22:34:41 +02:00
|
|
|
|
2024-06-12 02:42:10 +02:00
|
|
|
func RegisterWSChannel(connId string, windowId string, ch chan any) {
|
2024-05-14 22:34:41 +02:00
|
|
|
globalLock.Lock()
|
|
|
|
defer globalLock.Unlock()
|
2024-06-12 02:42:10 +02:00
|
|
|
wsMap[connId] = &WindowWatchData{
|
|
|
|
WindowWSCh: ch,
|
|
|
|
WaveWindowId: windowId,
|
|
|
|
WatchedORefs: make(map[waveobj.ORef]bool),
|
2024-05-28 00:44:57 +02:00
|
|
|
}
|
2024-05-14 22:34:41 +02:00
|
|
|
}
|
|
|
|
|
2024-06-12 02:42:10 +02:00
|
|
|
func UnregisterWSChannel(connId string) {
|
2024-05-14 22:34:41 +02:00
|
|
|
globalLock.Lock()
|
|
|
|
defer globalLock.Unlock()
|
2024-06-12 02:42:10 +02:00
|
|
|
delete(wsMap, connId)
|
2024-05-14 22:34:41 +02:00
|
|
|
}
|
|
|
|
|
2024-06-12 02:42:10 +02:00
|
|
|
func getWindowWatchesForWindowId(windowId string) []*WindowWatchData {
|
2024-05-28 00:44:57 +02:00
|
|
|
globalLock.Lock()
|
|
|
|
defer globalLock.Unlock()
|
2024-06-12 02:42:10 +02:00
|
|
|
var watches []*WindowWatchData
|
|
|
|
for _, wdata := range wsMap {
|
|
|
|
if wdata.WaveWindowId == windowId {
|
|
|
|
watches = append(watches, wdata)
|
2024-05-28 00:44:57 +02:00
|
|
|
}
|
|
|
|
}
|
2024-06-12 02:42:10 +02:00
|
|
|
return watches
|
2024-05-28 00:44:57 +02:00
|
|
|
}
|
|
|
|
|
2024-06-12 02:42:10 +02:00
|
|
|
func getAllWatches() []*WindowWatchData {
|
|
|
|
globalLock.Lock()
|
|
|
|
defer globalLock.Unlock()
|
|
|
|
watches := make([]*WindowWatchData, 0, len(wsMap))
|
|
|
|
for _, wdata := range wsMap {
|
|
|
|
watches = append(watches, wdata)
|
2024-05-14 22:34:41 +02:00
|
|
|
}
|
2024-06-12 02:42:10 +02:00
|
|
|
return watches
|
2024-05-14 22:34:41 +02:00
|
|
|
}
|
|
|
|
|
2024-06-12 02:42:10 +02:00
|
|
|
func SendEventToWindow(windowId string, event WSEventType) {
|
|
|
|
wwdArr := getWindowWatchesForWindowId(windowId)
|
|
|
|
for _, wdata := range wwdArr {
|
|
|
|
wdata.WindowWSCh <- event
|
2024-05-14 22:34:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-12 02:42:10 +02:00
|
|
|
func SendEvent(event WSEventType) {
|
|
|
|
wwdArr := getAllWatches()
|
|
|
|
for _, wdata := range wwdArr {
|
|
|
|
wdata.WindowWSCh <- event
|
2024-05-14 22:34:41 +02:00
|
|
|
}
|
|
|
|
}
|