From 5c92b75cf7081e1843244c505a723cefaeda8a6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Sat, 31 Oct 2020 23:53:19 +0100 Subject: [PATCH] Refactor: session remove duplicate ID in endpoints. --- internal/session/manager.go | 42 +++++++++++++-------------------- internal/session/session.go | 2 +- internal/types/session.go | 6 ++--- internal/websocket/admin.go | 27 ++++++++------------- internal/websocket/control.go | 31 ++++++++---------------- internal/websocket/session.go | 13 +++++----- internal/websocket/websocket.go | 12 +++++----- 7 files changed, 53 insertions(+), 80 deletions(-) diff --git a/internal/session/manager.go b/internal/session/manager.go index 67095db9..987783ce 100644 --- a/internal/session/manager.go +++ b/internal/session/manager.go @@ -1,8 +1,6 @@ package session import ( - "fmt" - "github.com/kataras/go-events" "github.com/rs/zerolog" "github.com/rs/zerolog/log" @@ -14,7 +12,7 @@ import ( func New(remote types.RemoteManager) *SessionManager { return &SessionManager{ logger: log.With().Str("module", "session").Logger(), - host: "", + host: nil, remote: remote, members: make(map[string]*Session), emmiter: events.New(), @@ -23,7 +21,7 @@ func New(remote types.RemoteManager) *SessionManager { type SessionManager struct { logger zerolog.Logger - host string + host types.Session remote types.RemoteManager members map[string]*Session emmiter events.EventEmmiter @@ -50,30 +48,22 @@ func (manager *SessionManager) New(id string, admin bool, socket types.WebSocket } func (manager *SessionManager) HasHost() bool { - return manager.host != "" + return manager.host != nil } -func (manager *SessionManager) SetHost(id string) error { - host, ok := manager.GetHost() - if ok { - manager.host = id - manager.emmiter.Emit("host", host) - return nil - } - return fmt.Errorf("invalid session id %s", id) +func (manager *SessionManager) SetHost(host types.Session) { + manager.host = host + manager.emmiter.Emit("host", host) } -func (manager *SessionManager) GetHost() (types.Session, bool) { - host, ok := manager.members[manager.host] - return host, ok +func (manager *SessionManager) GetHost() types.Session { + return manager.host } func (manager *SessionManager) ClearHost() { - host, ok := manager.GetHost() - manager.host = "" - if ok { - manager.emmiter.Emit("host_cleared", host) - } + host := manager.host + manager.host = nil + manager.emmiter.Emit("host_cleared", host) } func (manager *SessionManager) Has(id string) bool { @@ -119,14 +109,14 @@ func (manager *SessionManager) Members() []*types.Member { func (manager *SessionManager) Destroy(id string) error { session, ok := manager.members[id] if ok { + delete(manager.members, id) err := session.destroy() if !manager.remote.Streaming() && len(manager.members) <= 0 { manager.remote.StopStream() } - manager.emmiter.Emit("before_destroy", session) - delete(manager.members, id) + manager.emmiter.Emit("destroy", id) return err } @@ -164,9 +154,9 @@ func (manager *SessionManager) OnHostCleared(listener func(session types.Session }) } -func (manager *SessionManager) OnBeforeDestroy(listener func(session types.Session)) { - manager.emmiter.On("before_destroy", func(payload ...interface{}) { - listener(payload[0].(*Session)) +func (manager *SessionManager) OnDestroy(listener func(id string)) { + manager.emmiter.On("destroy", func(payload ...interface{}) { + listener(payload[0].(string)) }) } diff --git a/internal/session/session.go b/internal/session/session.go index 4074f4f8..41865f02 100644 --- a/internal/session/session.go +++ b/internal/session/session.go @@ -37,7 +37,7 @@ func (session *Session) Muted() bool { } func (session *Session) IsHost() bool { - return session.manager.host == session.id + return session.manager.host != nil && session.manager.host.ID() == session.ID() } func (session *Session) Connected() bool { diff --git a/internal/types/session.go b/internal/types/session.go index f7a75e42..ce973950 100644 --- a/internal/types/session.go +++ b/internal/types/session.go @@ -29,8 +29,8 @@ type Session interface { type SessionManager interface { New(id string, admin bool, socket WebSocket) Session HasHost() bool - SetHost(id string) error - GetHost() (Session, bool) + SetHost(Session) + GetHost() Session ClearHost() Has(id string) bool Get(id string) (Session, bool) @@ -40,7 +40,7 @@ type SessionManager interface { Broadcast(v interface{}, exclude interface{}) error OnHost(listener func(session Session)) OnHostCleared(listener func(session Session)) - OnBeforeDestroy(listener func(session Session)) + OnDestroy(listener func(id string)) OnCreated(listener func(session Session)) OnConnected(listener func(session Session)) } diff --git a/internal/websocket/admin.go b/internal/websocket/admin.go index 6061a330..eeea2db8 100644 --- a/internal/websocket/admin.go +++ b/internal/websocket/admin.go @@ -64,14 +64,10 @@ func (h *MessageHandler) adminControl(session types.Session) error { return nil } - host, ok := h.sessions.GetHost() + host := h.sessions.GetHost() + h.sessions.SetHost(session) - if err := h.sessions.SetHost(session.ID()); err != nil { - h.logger.Warn().Err(err).Msgf("SetHost failed") - return err - } - - if ok { + if host != nil { if err := h.sessions.Broadcast( message.AdminTarget{ Event: event.ADMIN_CONTROL, @@ -101,11 +97,10 @@ func (h *MessageHandler) adminRelease(session types.Session) error { return nil } - host, ok := h.sessions.GetHost() - + host := h.sessions.GetHost() h.sessions.ClearHost() - if ok { + if host != nil { if err := h.sessions.Broadcast( message.AdminTarget{ Event: event.ADMIN_RELEASE, @@ -135,23 +130,21 @@ func (h *MessageHandler) adminGive(session types.Session, payload *message.Admin return nil } - if !h.sessions.Has(payload.ID) { - h.logger.Debug().Str("id", payload.ID).Msg("user does not exist") + target, ok := h.sessions.Get(payload.ID) + if !ok { + h.logger.Debug().Str("id", target.ID()).Msg("user does not exist") return nil } // set host - if err := h.sessions.SetHost(payload.ID); err != nil { - h.logger.Warn().Err(err).Msgf("SetHost failed") - return err - } + h.sessions.SetHost(target) // let everyone know if err := h.sessions.Broadcast( message.AdminTarget{ Event: event.CONTROL_GIVE, ID: session.ID(), - Target: payload.ID, + Target: target.ID(), }, nil); err != nil { h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.CONTROL_LOCKED) return err diff --git a/internal/websocket/control.go b/internal/websocket/control.go index 828e3de1..f05afc2b 100644 --- a/internal/websocket/control.go +++ b/internal/websocket/control.go @@ -31,13 +31,11 @@ func (h *MessageHandler) controlRelease(session types.Session) error { } func (h *MessageHandler) controlRequest(session types.Session) error { - // check for host - if !h.sessions.HasHost() { + host := h.sessions.GetHost() + + if host == nil { // set host - if err := h.sessions.SetHost(session.ID()); err != nil { - h.logger.Warn().Err(err).Msgf("SetHost failed") - return err - } + h.sessions.SetHost(session) // let everyone know if err := h.sessions.Broadcast( @@ -48,14 +46,7 @@ func (h *MessageHandler) controlRequest(session types.Session) error { h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.CONTROL_LOCKED) return err } - - return nil - } - - // get host - host, ok := h.sessions.GetHost() - if ok { - + } else { // tell session there is a host if err := session.Send(message.Control{ Event: event.CONTROL_REQUEST, @@ -85,23 +76,21 @@ func (h *MessageHandler) controlGive(session types.Session, payload *message.Con return nil } - if !h.sessions.Has(payload.ID) { - h.logger.Debug().Str("id", payload.ID).Msg("user does not exist") + target, ok := h.sessions.Get(payload.ID) + if !ok { + h.logger.Debug().Str("id", target.ID()).Msg("user does not exist") return nil } // set host - if err := h.sessions.SetHost(payload.ID); err != nil { - h.logger.Warn().Err(err).Msgf("SetHost failed") - return err - } + h.sessions.SetHost(target) // let everyone know if err := h.sessions.Broadcast( message.ControlTarget{ Event: event.CONTROL_GIVE, ID: session.ID(), - Target: payload.ID, + Target: target.ID(), }, nil); err != nil { h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.CONTROL_LOCKED) return err diff --git a/internal/websocket/session.go b/internal/websocket/session.go index 5f2e000e..0a7ecbad 100644 --- a/internal/websocket/session.go +++ b/internal/websocket/session.go @@ -43,8 +43,8 @@ func (h *MessageHandler) SessionConnected(session types.Session) error { } // tell session there is a host - host, ok := h.sessions.GetHost() - if ok { + host := h.sessions.GetHost() + if host != nil { if err := session.Send(message.Control{ Event: event.CONTROL_LOCKED, ID: host.ID(), @@ -67,13 +67,14 @@ func (h *MessageHandler) SessionConnected(session types.Session) error { return nil } -func (h *MessageHandler) SessionDestroyed(session types.Session) error { +func (h *MessageHandler) SessionDestroyed(id string) error { // clear host if exists - if session.IsHost() { + host := h.sessions.GetHost() + if host != nil && host.ID() == id { h.sessions.ClearHost() if err := h.sessions.Broadcast(message.Control{ Event: event.CONTROL_RELEASE, - ID: session.ID(), + ID: id, }, nil); err != nil { h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.CONTROL_RELEASE) } @@ -83,7 +84,7 @@ func (h *MessageHandler) SessionDestroyed(session types.Session) error { if err := h.sessions.Broadcast( message.MemberDisconnected{ Event: event.MEMBER_DISCONNECTED, - ID: session.ID(), + ID: id, }, nil); err != nil { h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.MEMBER_DISCONNECTED) return err diff --git a/internal/websocket/websocket.go b/internal/websocket/websocket.go index fef8a888..248f5f78 100644 --- a/internal/websocket/websocket.go +++ b/internal/websocket/websocket.go @@ -71,11 +71,11 @@ func (ws *WebSocketHandler) Start() { } }) - ws.sessions.OnBeforeDestroy(func(session types.Session) { - if err := ws.handler.SessionDestroyed(session); err != nil { - ws.logger.Warn().Str("id", session.ID()).Err(err).Msg("session destroyed with and error") + ws.sessions.OnDestroy(func(id string) { + if err := ws.handler.SessionDestroyed(id); err != nil { + ws.logger.Warn().Str("id", id).Err(err).Msg("session destroyed with and error") } else { - ws.logger.Debug().Str("id", session.ID()).Msg("session destroyed") + ws.logger.Debug().Str("id", id).Msg("session destroyed") } }) @@ -94,8 +94,8 @@ func (ws *WebSocketHandler) Start() { if ws.sessions.HasHost() { text := ws.remote.ReadClipboard() if text != current { - session, ok := ws.sessions.GetHost() - if ok { + session := ws.sessions.GetHost() + if session != nil { if err := session.Send(message.Clipboard{ Event: event.CONTROL_CLIPBOARD, Text: text,