Skip to content

Commit

Permalink
strongly typed session events channel.
Browse files Browse the repository at this point in the history
  • Loading branch information
m1k1o committed Jan 29, 2023
1 parent f308071 commit 2649594
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 59 deletions.
56 changes: 23 additions & 33 deletions server/internal/session/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,21 @@ import (

func New(capture types.CaptureManager) *SessionManager {
return &SessionManager{
logger: log.With().Str("module", "session").Logger(),
host: "",
capture: capture,
sessionChannel: make(chan types.SessionInformation, 10),
hostChannel: make(chan types.HostInformation, 10),
members: make(map[string]*Session),
logger: log.With().Str("module", "session").Logger(),
host: "",
capture: capture,
eventsChannel: make(chan types.SessionEvent, 10),
members: make(map[string]*Session),
}
}

type SessionManager struct {
mu sync.Mutex
logger zerolog.Logger
host string
capture types.CaptureManager
members map[string]*Session
sessionChannel chan types.SessionInformation
hostChannel chan types.HostInformation
mu sync.Mutex
logger zerolog.Logger
host string
capture types.CaptureManager
members map[string]*Session
eventsChannel chan types.SessionEvent
// TODO: Handle locks in sessions as flags.
controlLocked bool
}
Expand All @@ -50,18 +48,12 @@ func (manager *SessionManager) New(id string, admin bool, socket types.WebSocket
manager.capture.Video().AddListener()
manager.mu.Unlock()

manager.sessionChannel <- types.SessionInformation{
Type: "created",
manager.eventsChannel <- types.SessionEvent{
Type: types.SESSION_CREATED,
Id: id,
Session: session,
}

go func() {
for {
// TODO: Unused.
<-manager.hostChannel
}
}()
return session
}

Expand All @@ -81,10 +73,11 @@ func (manager *SessionManager) SetHost(id string) error {
if ok {
manager.host = id

manager.hostChannel <- types.HostInformation{
Type: "host",
manager.eventsChannel <- types.SessionEvent{
Type: types.SESSION_HOST_SET,
Id: id,
}

return nil
}

Expand All @@ -102,8 +95,9 @@ func (manager *SessionManager) GetHost() (types.Session, bool) {
func (manager *SessionManager) ClearHost() {
id := manager.host
manager.host = ""
manager.hostChannel <- types.HostInformation{
Type: "host_cleared",

manager.eventsChannel <- types.SessionEvent{
Type: types.SESSION_HOST_CLEARED,
Id: id,
}
}
Expand Down Expand Up @@ -182,8 +176,8 @@ func (manager *SessionManager) Destroy(id string) {
manager.capture.Video().RemoveListener()
manager.mu.Unlock()

manager.sessionChannel <- types.SessionInformation{
Type: "destroyed",
manager.eventsChannel <- types.SessionEvent{
Type: types.SESSION_DESTROYED,
Id: id,
Session: session,
}
Expand Down Expand Up @@ -244,10 +238,6 @@ func (manager *SessionManager) AdminBroadcast(v interface{}, exclude interface{}
return nil
}

func (manager *SessionManager) GetSessionChannel() chan types.SessionInformation {
return manager.sessionChannel
}

func (manager *SessionManager) GetHostChannel() chan types.HostInformation {
return manager.hostChannel
func (manager *SessionManager) GetEventsChannel() chan types.SessionEvent {
return manager.eventsChannel
}
4 changes: 2 additions & 2 deletions server/internal/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ func (session *Session) SetPeer(peer types.Peer) error {
func (session *Session) SetConnected(connected bool) error {
session.connected = connected
if connected {
session.manager.sessionChannel <- types.SessionInformation{
Type: "connected",
session.manager.eventsChannel <- types.SessionEvent{
Type: types.SESSION_CONNECTED,
Id: session.id,
Session: session,
}
Expand Down
17 changes: 13 additions & 4 deletions server/internal/types/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,18 @@ type Member struct {
Muted bool `json:"muted"`
}

type SessionInformation struct {
Type string
type SessionEventType int

const (
SESSION_CREATED SessionEventType = iota
SESSION_CONNECTED
SESSION_DESTROYED
SESSION_HOST_SET
SESSION_HOST_CLEARED
)

type SessionEvent struct {
Type SessionEventType
Id string
Session Session
}
Expand Down Expand Up @@ -57,6 +67,5 @@ type SessionManager interface {
Clear() error
Broadcast(v interface{}, exclude interface{}) error
AdminBroadcast(v interface{}, exclude interface{}) error
GetSessionChannel() chan SessionInformation
GetHostChannel() chan HostInformation
GetEventsChannel() chan SessionEvent
}
44 changes: 24 additions & 20 deletions server/internal/websocket/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,24 +103,24 @@ type WebSocketHandler struct {
func (ws *WebSocketHandler) Start() {
go func() {
for {
channelMessage, ok := <-ws.sessions.GetSessionChannel()
e, ok := <-ws.sessions.GetEventsChannel()
if !ok {
ws.logger.Info().Msg("session channel was closed")
return
}

switch channelMessage.Type {
case "created":
if err := ws.handler.SessionCreated(channelMessage.Id, channelMessage.Session); err != nil {
ws.logger.Warn().Str("id", channelMessage.Id).Err(err).Msg("session created with and error")
switch e.Type {
case types.SESSION_CREATED:
if err := ws.handler.SessionCreated(e.Id, e.Session); err != nil {
ws.logger.Warn().Str("id", e.Id).Err(err).Msg("session created with and error")
} else {
ws.logger.Debug().Str("id", channelMessage.Id).Msg("session created")
ws.logger.Debug().Str("id", e.Id).Msg("session created")
}
case "connected":
if err := ws.handler.SessionConnected(channelMessage.Id, channelMessage.Session); err != nil {
ws.logger.Warn().Str("id", channelMessage.Id).Err(err).Msg("session connected with and error")
case types.SESSION_CONNECTED:
if err := ws.handler.SessionConnected(e.Id, e.Session); err != nil {
ws.logger.Warn().Str("id", e.Id).Err(err).Msg("session connected with and error")
} else {
ws.logger.Debug().Str("id", channelMessage.Id).Msg("session connected")
ws.logger.Debug().Str("id", e.Id).Msg("session connected")
}

// if control protection is enabled and at least one admin
Expand All @@ -134,24 +134,24 @@ func (ws *WebSocketHandler) Start() {
if err := ws.sessions.Broadcast(
message.AdminLock{
Event: event.ADMIN_UNLOCK,
ID: channelMessage.Id,
ID: e.Id,
Resource: "control",
}, nil); err != nil {
ws.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.ADMIN_UNLOCK)
}
}

// remove outdated stats
if channelMessage.Session.Admin() {
if e.Session.Admin() {
ws.lastAdminLeftAt = nil
} else {
ws.lastUserLeftAt = nil
}
case "destroyed":
if err := ws.handler.SessionDestroyed(channelMessage.Id); err != nil {
ws.logger.Warn().Str("id", channelMessage.Id).Err(err).Msg("session destroyed with and error")
case types.SESSION_DESTROYED:
if err := ws.handler.SessionDestroyed(e.Id); err != nil {
ws.logger.Warn().Str("id", e.Id).Err(err).Msg("session destroyed with and error")
} else {
ws.logger.Debug().Str("id", channelMessage.Id).Msg("session destroyed")
ws.logger.Debug().Str("id", e.Id).Msg("session destroyed")
}

membersCount := len(ws.sessions.Members())
Expand All @@ -164,29 +164,33 @@ func (ws *WebSocketHandler) Start() {
ws.state.Lock("control", CONTROL_PROTECTION_SESSION)
ws.sessions.SetControlLocked(true) // TODO: Handle locks in sessions as flags.
ws.logger.Info().Msgf("control locked and released on behalf of control protection")
ws.handler.AdminRelease(channelMessage.Id, channelMessage.Session)
ws.handler.AdminRelease(e.Id, e.Session)

if err := ws.sessions.Broadcast(
message.AdminLock{
Event: event.ADMIN_LOCK,
ID: channelMessage.Id,
ID: e.Id,
Resource: "control",
}, nil); err != nil {
ws.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.ADMIN_LOCK)
}
}

// if this was the last admin
if channelMessage.Session.Admin() && adminCount == 0 {
if e.Session.Admin() && adminCount == 0 {
now := time.Now()
ws.lastAdminLeftAt = &now
}

// if this was the last user
if !channelMessage.Session.Admin() && membersCount-adminCount == 0 {
if !e.Session.Admin() && membersCount-adminCount == 0 {
now := time.Now()
ws.lastUserLeftAt = &now
}
case types.SESSION_HOST_SET:
// TODO: Unused.
case types.SESSION_HOST_CLEARED:
// TODO: Unused.
}
}
}()
Expand Down

0 comments on commit 2649594

Please sign in to comment.