From 665d9d2130f3f52335cd3ee0d2df17c54733be6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Wed, 25 Nov 2020 22:06:13 +0100 Subject: [PATCH] Create + Delete session. --- internal/session/manager.go | 27 +++++++++++------ internal/session/session.go | 42 ++++++++++++--------------- internal/types/session.go | 4 +-- internal/websocket/handler/handler.go | 3 +- 4 files changed, 40 insertions(+), 36 deletions(-) diff --git a/internal/session/manager.go b/internal/session/manager.go index f5cad684..8d90d7ea 100644 --- a/internal/session/manager.go +++ b/internal/session/manager.go @@ -1,6 +1,7 @@ package session import ( + "fmt" "sync" "github.com/kataras/go-events" @@ -36,21 +37,24 @@ type SessionManagerCtx struct { emmiter events.EventEmmiter } -func (manager *SessionManagerCtx) New(id string, admin bool) types.Session { +func (manager *SessionManagerCtx) Create(profile MemberProfile) (types.Session, error) { manager.membersMu.Lock() defer manager.membersMu.Unlock() + id, err := utils.NewUID(32) + if err != nil { + return nil, err + } + session := &SessionCtx{ id: id, manager: manager, logger: manager.logger.With().Str("id", id).Logger(), - profile: MemberProfile{ - is_admin: admin, - }, + profile: profile, } manager.members[id] = session - return session + return session, nil } func (manager *SessionManagerCtx) Get(id string) (types.Session, bool) { @@ -61,14 +65,19 @@ func (manager *SessionManagerCtx) Get(id string) (types.Session, bool) { return session, ok } -func (manager *SessionManagerCtx) Destroy(id string) error { +func (manager *SessionManagerCtx) Delete(id string) error { manager.membersMu.Lock() defer manager.membersMu.Unlock() session, ok := manager.members[id] - if ok { - delete(manager.members, id) - return session.destroy() + if !ok { + return fmt.Errorf("Member not found.") + } + + delete(manager.members, id) + + if session.Connected() { + return session.Disconnect("member deleted") } return nil diff --git a/internal/session/session.go b/internal/session/session.go index 3136f5de..0254a36f 100644 --- a/internal/session/session.go +++ b/internal/session/session.go @@ -70,9 +70,6 @@ func (session *SessionCtx) SetWebSocketConnected(connected bool) { session.manager.emmiter.Emit("disconnected", session) session.websocket_peer = nil - - // TODO: Refactor. - _ = session.manager.Destroy(session.id) } } @@ -96,14 +93,27 @@ func (session *SessionCtx) SetWebRTCConnected(connected bool) { } func (session *SessionCtx) Disconnect(reason string) error { - // TODO: End WebSocket connection. - // TODO: End WebRTC connection. - - return session.Send( + if err := session.Send( message.Disconnect{ Event: event.SYSTEM_DISCONNECT, Message: reason, - }) + }); err != nil { + return err + } + + if session.websocket_peer != nil { + if err := session.websocket_peer.Destroy(); err != nil { + return err + } + } + + if session.webrtc_peer != nil { + if err := session.webrtc_peer.Destroy(); err != nil { + return err + } + } + + return nil } func (session *SessionCtx) Send(v interface{}) error { @@ -121,19 +131,3 @@ func (session *SessionCtx) SignalAnswer(sdp string) error { return session.webrtc_peer.SignalAnswer(sdp) } - -func (session *SessionCtx) destroy() error { - if session.websocket_peer != nil { - if err := session.websocket_peer.Destroy(); err != nil { - return err - } - } - - if session.webrtc_peer != nil { - if err := session.webrtc_peer.Destroy(); err != nil { - return err - } - } - - return nil -} diff --git a/internal/types/session.go b/internal/types/session.go index 70b0b533..f141145a 100644 --- a/internal/types/session.go +++ b/internal/types/session.go @@ -19,9 +19,9 @@ type Session interface { } type SessionManager interface { - New(id string, admin bool) Session + Create(profile MemberProfile) (Session, error) Get(id string) (Session, bool) - Destroy(id string) error + Delete(id string) error HasHost() bool SetHost(host Session) diff --git a/internal/websocket/handler/handler.go b/internal/websocket/handler/handler.go index 3f3abb61..19068b33 100644 --- a/internal/websocket/handler/handler.go +++ b/internal/websocket/handler/handler.go @@ -49,13 +49,14 @@ func (h *MessageHandlerCtx) Connected(session types.Session, websocket_peer type return true, "" } +// TODO: Remove, unused. func (h *MessageHandlerCtx) Disconnected(id string) error { // TODO: Refactor. if h.locked && len(h.sessions.Admins()) == 0 { h.locked = false } - return h.sessions.Destroy(id) + return h.sessions.Delete(id) } func (h *MessageHandlerCtx) Message(session types.Session, raw []byte) error {