2023-09-09 09:24:25 -06:00
|
|
|
package event_bus
|
2023-09-07 22:13:40 -06:00
|
|
|
|
|
|
|
import (
|
2023-09-09 09:24:25 -06:00
|
|
|
"encoding/json"
|
|
|
|
"github.com/fastenhealth/fasten-onprem/backend/pkg/models"
|
|
|
|
"github.com/sirupsen/logrus"
|
2023-09-07 22:13:40 -06:00
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
|
|
|
// It keeps a list of clients those are currently attached
|
|
|
|
// and broadcasting events to those clients.
|
2023-09-20 14:57:12 -06:00
|
|
|
type eventBus struct {
|
|
|
|
logger logrus.FieldLogger
|
2023-09-09 09:24:25 -06:00
|
|
|
|
2023-09-07 22:13:40 -06:00
|
|
|
// Events are pushed to this channel by the main events-gathering routine
|
2023-09-20 14:57:12 -06:00
|
|
|
message chan EventBusMessage
|
2023-09-07 22:13:40 -06:00
|
|
|
|
|
|
|
// New client connections
|
2023-09-20 14:57:12 -06:00
|
|
|
newListener chan *EventBusListener
|
2023-09-07 22:13:40 -06:00
|
|
|
|
|
|
|
// Closed client connections
|
2023-09-20 14:57:12 -06:00
|
|
|
closedListener chan *EventBusListener
|
2023-09-07 22:13:40 -06:00
|
|
|
|
|
|
|
// Total client connections
|
2023-09-20 14:57:12 -06:00
|
|
|
totalRoomListeners map[string][]*EventBusListener
|
2023-09-08 12:27:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type EventBusListener struct {
|
|
|
|
ResponseChan chan string
|
|
|
|
UserID string
|
|
|
|
}
|
|
|
|
|
|
|
|
type EventBusMessage struct {
|
|
|
|
UserID string
|
|
|
|
Message string
|
2023-09-07 22:13:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// It Listens all incoming requests from clients.
|
|
|
|
// Handles addition and removal of clients and broadcast messages to clients.
|
2023-09-08 10:27:37 -06:00
|
|
|
// TODO: determine how to route messages based on authenticated client
|
2023-09-20 14:57:12 -06:00
|
|
|
func (bus *eventBus) listen() {
|
2023-09-07 22:13:40 -06:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
// Add new available client
|
2023-09-20 14:57:12 -06:00
|
|
|
case listener := <-bus.newListener:
|
2023-09-08 12:27:38 -06:00
|
|
|
//check if this userId room already exists, or create it
|
2023-09-20 14:57:12 -06:00
|
|
|
if _, exists := bus.totalRoomListeners[listener.UserID]; !exists {
|
|
|
|
bus.totalRoomListeners[listener.UserID] = []*EventBusListener{}
|
2023-09-08 12:27:38 -06:00
|
|
|
}
|
2023-09-20 14:57:12 -06:00
|
|
|
bus.totalRoomListeners[listener.UserID] = append(bus.totalRoomListeners[listener.UserID], listener)
|
|
|
|
log.Printf("Listener added to room: `%s`. %d registered listeners", listener.UserID, len(bus.totalRoomListeners[listener.UserID]))
|
2023-09-07 22:13:40 -06:00
|
|
|
|
|
|
|
// Remove closed client
|
2023-09-20 14:57:12 -06:00
|
|
|
case listener := <-bus.closedListener:
|
|
|
|
if _, exists := bus.totalRoomListeners[listener.UserID]; !exists {
|
2023-09-08 12:27:38 -06:00
|
|
|
log.Printf("Room `%s` not found", listener.UserID)
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
//loop through all the listeners in the room and remove the one that matches
|
2023-09-20 14:57:12 -06:00
|
|
|
for i, v := range bus.totalRoomListeners[listener.UserID] {
|
2023-09-08 12:27:38 -06:00
|
|
|
if v.ResponseChan == listener.ResponseChan {
|
2023-09-20 14:57:12 -06:00
|
|
|
bus.totalRoomListeners[listener.UserID] = append(bus.totalRoomListeners[listener.UserID][:i], bus.totalRoomListeners[listener.UserID][i+1:]...)
|
2023-09-08 12:27:38 -06:00
|
|
|
close(listener.ResponseChan)
|
2023-09-20 14:57:12 -06:00
|
|
|
log.Printf("Removed listener from room: `%s`. %d registered clients", listener.UserID, len(bus.totalRoomListeners[listener.UserID]))
|
2023-09-08 12:27:38 -06:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-07 22:13:40 -06:00
|
|
|
|
|
|
|
// Broadcast message to client
|
2023-09-20 14:57:12 -06:00
|
|
|
case eventMsg := <-bus.message:
|
|
|
|
if _, exists := bus.totalRoomListeners[eventMsg.UserID]; !exists {
|
2023-09-08 12:27:38 -06:00
|
|
|
log.Printf("Room `%s` not found, could not send message: `%s`", eventMsg.UserID, eventMsg.Message)
|
|
|
|
continue
|
|
|
|
} else {
|
2023-09-20 14:57:12 -06:00
|
|
|
for _, roomListener := range bus.totalRoomListeners[eventMsg.UserID] {
|
2023-09-08 12:27:38 -06:00
|
|
|
roomListener.ResponseChan <- eventMsg.Message
|
|
|
|
}
|
2023-09-07 22:13:40 -06:00
|
|
|
}
|
2023-09-08 12:27:38 -06:00
|
|
|
|
2023-09-07 22:13:40 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-09 09:24:25 -06:00
|
|
|
|
2023-09-20 14:57:12 -06:00
|
|
|
func (bus *eventBus) PublishMessage(eventMsg models.EventInterface) error {
|
|
|
|
bus.logger.Infof("Publishing message to room: `%s`", eventMsg.GetUserID())
|
2023-09-09 09:24:25 -06:00
|
|
|
payload, err := json.Marshal(eventMsg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-20 14:57:12 -06:00
|
|
|
bus.message <- EventBusMessage{
|
2023-09-09 09:24:25 -06:00
|
|
|
UserID: eventMsg.GetUserID(),
|
|
|
|
Message: string(payload),
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-09-20 14:57:12 -06:00
|
|
|
|
|
|
|
func (bus *eventBus) AddListener(listener *EventBusListener) {
|
|
|
|
bus.newListener <- listener
|
|
|
|
}
|
|
|
|
func (bus *eventBus) RemoveListener(listener *EventBusListener) {
|
|
|
|
bus.closedListener <- listener
|
|
|
|
}
|
|
|
|
func (bus *eventBus) TotalRooms() int {
|
|
|
|
return len(bus.totalRoomListeners)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bus *eventBus) TotalListenersByRoom(room string) int {
|
|
|
|
listeners, ok := bus.totalRoomListeners[room]
|
|
|
|
if !ok {
|
|
|
|
return 0
|
|
|
|
} else {
|
|
|
|
return len(listeners)
|
|
|
|
}
|
|
|
|
}
|