From d7b5bd68426392986179cf18f9c9c145ccf7de33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Thu, 3 Dec 2020 16:10:52 +0100 Subject: [PATCH] extract member state to custom struct. --- internal/session/manager.go | 14 +++----- internal/session/session.go | 12 +++---- internal/types/event/events.go | 13 ++++--- internal/types/message/messages.go | 10 ++++-- internal/types/session.go | 5 ++- internal/webrtc/manager.go | 4 ++- internal/websocket/handler/session.go | 49 ++++++++++++--------------- internal/websocket/handler/system.go | 10 +++--- internal/websocket/manager.go | 24 +++++-------- 9 files changed, 64 insertions(+), 77 deletions(-) diff --git a/internal/session/manager.go b/internal/session/manager.go index 094f1699..06f320ca 100644 --- a/internal/session/manager.go +++ b/internal/session/manager.go @@ -240,20 +240,14 @@ func (manager *SessionManagerCtx) OnDisconnected(listener func(session types.Ses }) } -func (manager *SessionManagerCtx) OnReceivingStarted(listener func(session types.Session)) { - manager.emmiter.On("receiving_started", func(payload ...interface{}) { +func (manager *SessionManagerCtx) OnProfileChanged(listener func(session types.Session)) { + manager.emmiter.On("profile_changed", func(payload ...interface{}) { listener(payload[0].(*SessionCtx)) }) } -func (manager *SessionManagerCtx) OnReceivingStopped(listener func(session types.Session)) { - manager.emmiter.On("receiving_stopped", func(payload ...interface{}) { - listener(payload[0].(*SessionCtx)) - }) -} - -func (manager *SessionManagerCtx) OnProfileUpdated(listener func(session types.Session)) { - manager.emmiter.On("profile_updated", func(payload ...interface{}) { +func (manager *SessionManagerCtx) OnStateChanged(listener func(session types.Session)) { + manager.emmiter.On("state_changed", func(payload ...interface{}) { listener(payload[0].(*SessionCtx)) }) } diff --git a/internal/session/session.go b/internal/session/session.go index eb97f994..615f5a3a 100644 --- a/internal/session/session.go +++ b/internal/session/session.go @@ -61,7 +61,7 @@ func (session *SessionCtx) CanAccessClipboard() bool { func (session *SessionCtx) SetProfile(profile types.MemberProfile) { session.profile = profile - session.manager.emmiter.Emit("profile_updated", session) + session.manager.emmiter.Emit("profile_changed", session) } // --- @@ -140,12 +140,10 @@ func (session *SessionCtx) SetWebRTCPeer(webrtc_peer types.WebRTCPeer) { } func (session *SessionCtx) SetWebRTCConnected(connected bool) { - if connected { - session.webrtc_connected = true - session.manager.emmiter.Emit("receiving_started", session) - } else { - session.webrtc_connected = false - session.manager.emmiter.Emit("receiving_stopped", session) + session.webrtc_connected = connected + session.manager.emmiter.Emit("state_changed", session) + + if !connected { session.webrtc_peer = nil } } diff --git a/internal/types/event/events.go b/internal/types/event/events.go index f9df9eca..58e27168 100644 --- a/internal/types/event/events.go +++ b/internal/types/event/events.go @@ -13,13 +13,12 @@ const ( ) const ( - MEMBER_CREATED = "member/created" - MEMBER_DELETED = "member/deleted" - MEMBER_CONNECTED = "member/connected" - MEMBER_DISCONNECTED = "member/disconnected" - MEMBER_RECEIVING_STARTED = "member/receiving/started" - MEMBER_RECEIVING_STOPPED = "member/receiving/stopped" - MEMBER_PROFILE_UPDATED = "member/profile/updated" + MEMBER_CREATED = "member/created" + MEMBER_DELETED = "member/deleted" + MEMBER_CONNECTED = "member/connected" + MEMBER_DISCONNECTED = "member/disconnected" + MEMBER_PROFILE = "member/profile" + MEMBER_STATE = "member/state" ) const ( diff --git a/internal/types/message/messages.go b/internal/types/message/messages.go index 7a0633de..353ad52d 100644 --- a/internal/types/message/messages.go +++ b/internal/types/message/messages.go @@ -66,12 +66,18 @@ type MemberProfile struct { CanAccessClipboard bool `json:"can_access_clipboard"` } +type MemberState struct { + Event string `json:"event,omitempty"` + ID string `json:"id,omitempty"` + IsConnected bool `json:"is_connected"` + IsReceiving bool `json:"is_receiving"` +} + type MemberData struct { Event string `json:"event,omitempty"` ID string `json:"id"` Profile MemberProfile `json:"profile"` - IsConnected bool `json:"is_connected"` - IsReceiving bool `json:"is_receiving"` + State MemberState `json:"state"` } ///////////////////////////// diff --git a/internal/types/session.go b/internal/types/session.go index 70106950..635e755d 100644 --- a/internal/types/session.go +++ b/internal/types/session.go @@ -62,9 +62,8 @@ type SessionManager interface { OnDeleted(listener func(session Session)) OnConnected(listener func(session Session)) OnDisconnected(listener func(session Session)) - OnReceivingStarted(listener func(session Session)) - OnReceivingStopped(listener func(session Session)) - OnProfileUpdated(listener func(session Session)) + OnProfileChanged(listener func(session Session)) + OnStateChanged(listener func(session Session)) ImplicitHosting() bool diff --git a/internal/webrtc/manager.go b/internal/webrtc/manager.go index 1af845aa..6641904b 100644 --- a/internal/webrtc/manager.go +++ b/internal/webrtc/manager.go @@ -154,8 +154,10 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session) (string, bool connection.OnConnectionStateChange(func(state webrtc.PeerConnectionState) { switch state { case webrtc.PeerConnectionStateDisconnected: + manager.logger.Info().Str("id", session.ID()).Msg("peer disconnected successfully") + session.SetWebRTCConnected(false) case webrtc.PeerConnectionStateFailed: - manager.logger.Info().Str("id", session.ID()).Msg("peer disconnected") + manager.logger.Warn().Str("id", session.ID()).Msg("peer disconnected with error") session.SetWebRTCConnected(false) case webrtc.PeerConnectionStateConnected: manager.logger.Info().Str("id", session.ID()).Msg("peer connected") diff --git a/internal/websocket/handler/session.go b/internal/websocket/handler/session.go index b4e35e36..39070b80 100644 --- a/internal/websocket/handler/session.go +++ b/internal/websocket/handler/session.go @@ -10,9 +10,9 @@ func (h *MessageHandlerCtx) SessionCreated(session types.Session) error { // TODO: Join structs? h.sessions.Broadcast( message.MemberData{ - Event: event.MEMBER_CREATED, - ID: session.ID(), - Profile: message.MemberProfile{ + Event: event.MEMBER_CREATED, + ID: session.ID(), + Profile: message.MemberProfile{ Name: session.Name(), IsAdmin: session.IsAdmin(), CanLogin: session.CanLogin(), @@ -21,8 +21,10 @@ func (h *MessageHandlerCtx) SessionCreated(session types.Session) error { CanHost: session.CanHost(), CanAccessClipboard: session.CanAccessClipboard(), }, - IsConnected: session.IsConnected(), - IsReceiving: session.IsReceiving(), + State: message.MemberState{ + IsConnected: session.IsConnected(), + IsReceiving: session.IsReceiving(), + }, }, nil) return nil @@ -90,31 +92,11 @@ func (h *MessageHandlerCtx) SessionDisconnected(session types.Session) error { return nil } -func (h *MessageHandlerCtx) SessionReceivingStarted(session types.Session) error { - h.sessions.Broadcast( - message.MemberID{ - Event: event.MEMBER_RECEIVING_STARTED, - ID: session.ID(), - }, nil); - - return nil -} - -func (h *MessageHandlerCtx) SessionReceivingStopped(session types.Session) error { - h.sessions.Broadcast( - message.MemberID{ - Event: event.MEMBER_RECEIVING_STOPPED, - ID: session.ID(), - }, nil); - - return nil -} - -func (h *MessageHandlerCtx) SessionProfileUpdated(session types.Session) error { +func (h *MessageHandlerCtx) SessionProfileChanged(session types.Session) error { // TODO: Join structs? h.sessions.Broadcast( message.MemberProfile{ - Event: event.MEMBER_PROFILE_UPDATED, + Event: event.MEMBER_PROFILE, ID: session.ID(), Name: session.Name(), IsAdmin: session.IsAdmin(), @@ -127,3 +109,16 @@ func (h *MessageHandlerCtx) SessionProfileUpdated(session types.Session) error { return nil } + +func (h *MessageHandlerCtx) SessionStateChanged(session types.Session) error { + // TODO: Join structs? + h.sessions.Broadcast( + message.MemberState{ + Event: event.MEMBER_STATE, + ID: session.ID(), + IsConnected: session.IsConnected(), + IsReceiving: session.IsReceiving(), + }, nil) + + return nil +} diff --git a/internal/websocket/handler/system.go b/internal/websocket/handler/system.go index 4df5ebde..5572b96a 100644 --- a/internal/websocket/handler/system.go +++ b/internal/websocket/handler/system.go @@ -27,8 +27,8 @@ func (h *MessageHandlerCtx) systemInit(session types.Session) error { for _, session := range h.sessions.Members() { // TODO: Join structs? members[session.ID()] = message.MemberData{ - ID: session.ID(), - Profile: message.MemberProfile{ + ID: session.ID(), + Profile: message.MemberProfile{ Name: session.Name(), IsAdmin: session.IsAdmin(), CanLogin: session.CanLogin(), @@ -37,8 +37,10 @@ func (h *MessageHandlerCtx) systemInit(session types.Session) error { CanHost: session.CanHost(), CanAccessClipboard: session.CanAccessClipboard(), }, - IsConnected: session.IsConnected(), - IsReceiving: session.IsReceiving(), + State: message.MemberState{ + IsConnected: session.IsConnected(), + IsReceiving: session.IsReceiving(), + }, } } diff --git a/internal/websocket/manager.go b/internal/websocket/manager.go index 8ea29f85..e7c522b7 100644 --- a/internal/websocket/manager.go +++ b/internal/websocket/manager.go @@ -81,27 +81,19 @@ func (ws *WebSocketManagerCtx) Start() { } }) - ws.sessions.OnReceivingStarted(func(session types.Session) { - if err := ws.handler.SessionReceivingStarted(session); err != nil { - ws.logger.Warn().Str("id", session.ID()).Err(err).Msg("session receiving started with an error") + ws.sessions.OnProfileChanged(func(session types.Session) { + if err := ws.handler.SessionProfileChanged(session); err != nil { + ws.logger.Warn().Str("id", session.ID()).Err(err).Msg("session profile changed with an error") } else { - ws.logger.Debug().Str("id", session.ID()).Msg("session receiving started") + ws.logger.Debug().Str("id", session.ID()).Msg("session profile changed") } }) - ws.sessions.OnReceivingStopped(func(session types.Session) { - if err := ws.handler.SessionReceivingStopped(session); err != nil { - ws.logger.Warn().Str("id", session.ID()).Err(err).Msg("session receiving stopped with an error") + ws.sessions.OnStateChanged(func(session types.Session) { + if err := ws.handler.SessionStateChanged(session); err != nil { + ws.logger.Warn().Str("id", session.ID()).Err(err).Msg("session state changed with an error") } else { - ws.logger.Debug().Str("id", session.ID()).Msg("session receiving stopped") - } - }) - - ws.sessions.OnProfileUpdated(func(session types.Session) { - if err := ws.handler.SessionProfileUpdated(session); err != nil { - ws.logger.Warn().Str("id", session.ID()).Err(err).Msg("session profile updated with an error") - } else { - ws.logger.Debug().Str("id", session.ID()).Msg("session profile updated") + ws.logger.Debug().Str("id", session.ID()).Msg("session state changed") } })