Skip to content

Commit

Permalink
feat: multi-probe UX improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
monoxane committed May 22, 2023
1 parent 079d027 commit c1eed49
Show file tree
Hide file tree
Showing 12 changed files with 397 additions and 291 deletions.
43 changes: 43 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strconv"
"sync"
"syscall"
"time"

"github.com/gin-gonic/gin"
"github.com/google/uuid"
Expand All @@ -30,6 +31,8 @@ var (
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool { return true },
}

ProbeStats []*ProbeChannelStatus
)

type WebsocketConnection struct {
Expand All @@ -53,6 +56,12 @@ type DestinationUpdate struct {
Source SourceUpdate `json:"source"`
}

type ProbeChannelStatus struct {
Id int `json:"id"`
ActiveSource bool `json:"active_source"`
Clients int `json:"clients"`
}

type SourceUpdate struct {
Id int `json:"id"`
Label string `json:"label"`
Expand Down Expand Up @@ -92,8 +101,16 @@ func main() {

if Config.Probe.Enabled {
ProbeHandlers = make([]*ProbeSocketHandler, len(Config.Probe.RouterDestinations))
ProbeStats = make([]*ProbeChannelStatus, len(Config.Probe.RouterDestinations))
for i := range ProbeStats {
ProbeStats[i] = &ProbeChannelStatus{
Id: i,
}
}

for i := range Config.Probe.RouterDestinations {
ProbeHandlers[i] = &ProbeSocketHandler{
Id: i,
clients: make(map[*ProbeClient]bool),
register: make(chan *ProbeClient),
unregister: make(chan *ProbeClient),
Expand Down Expand Up @@ -129,6 +146,20 @@ func main() {
log.Println("Exiting")
}

func SendProbeStats() {
payload, _ := json.Marshal(ProbeStats)
update := MatrixWSMessage{
Type: "probe_stats",
Data: payload,
}

MatrixWSConnectionsMutex.Lock()
for conn := range MatrixWSConnections {
conn.Socket.WriteJSON(update)
}
MatrixWSConnectionsMutex.Unlock()
}

func serveHTTP() {
gin.SetMode(gin.ReleaseMode)

Expand Down Expand Up @@ -255,6 +286,12 @@ func HandleMatrixWS(c *gin.Context) {
MatrixWSConnectionsMutex.Unlock()

defer connection.Socket.Close()

go func() {
time.Sleep(500 * time.Millisecond)
SendProbeStats()
}()

for {
_, messageBytes, err := connection.Socket.ReadMessage()
if err != nil {
Expand Down Expand Up @@ -300,6 +337,9 @@ func HandleProbeStream(c *gin.Context) {

log.Printf("stream for probe %d connected from %s", index, c.RemoteIP())

ProbeStats[index].ActiveSource = true
SendProbeStats()

for {
data, err := ioutil.ReadAll(io.LimitReader(c.Request.Body, 1024))
if err != nil || len(data) == 0 {
Expand All @@ -309,5 +349,8 @@ func HandleProbeStream(c *gin.Context) {
ProbeHandlers[index].BroadcastData(&data)
}

ProbeStats[index].ActiveSource = false
SendProbeStats()

log.Printf("stream for probe %d disconnected from %s", index, c.RemoteIP())
}
10 changes: 7 additions & 3 deletions probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func (c *ProbeClient) Run() {
}

type ProbeSocketHandler struct {
Id int
clients map[*ProbeClient]bool // *client -> is connected (true/false)
register chan *ProbeClient
unregister chan *ProbeClient
Expand All @@ -102,14 +103,18 @@ func (h *ProbeSocketHandler) Run() {
select {
case client := <-h.register:
h.clients[client] = true
log.Printf("New client registered. Total: %d\n", len(h.clients))
log.Printf("[probe %d] client connected. active clients: %d\n", h.Id, len(h.clients))
ProbeStats[h.Id].Clients = len(h.clients)
SendProbeStats()

case client := <-h.unregister:
_, ok := h.clients[client]
if ok {
delete(h.clients, client)
}
log.Printf("Client unregistered. Total: %d\n", len(h.clients))
log.Printf("[probe %d] client disconnected. active clients: %d\n", h.Id, len(h.clients))
ProbeStats[h.Id].Clients = len(h.clients)
SendProbeStats()

case data := <-h.broadcast:
h.BroadcastData(data)
Expand All @@ -132,7 +137,6 @@ func (h *ProbeSocketHandler) ServeWS(c *gin.Context) {
return
}

log.Println("New client connected")
client := NewProbeClient(ws, h.unregister)

h.register <- client
Expand Down
2 changes: 1 addition & 1 deletion ui/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ module.exports = {
{ allowAsProps: true },
],
},
};
};
Loading

0 comments on commit c1eed49

Please sign in to comment.