2020-10-23 03:54:50 +13:00
|
|
|
package session
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/kataras/go-events"
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
2020-10-29 07:15:48 +13:00
|
|
|
"demodesk/neko/internal/types"
|
|
|
|
"demodesk/neko/internal/utils"
|
2020-10-23 03:54:50 +13:00
|
|
|
)
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func New(capture types.CaptureManager) *SessionManagerCtx {
|
|
|
|
return &SessionManagerCtx{
|
2020-10-23 03:54:50 +13:00
|
|
|
logger: log.With().Str("module", "session").Logger(),
|
2020-11-01 11:53:19 +13:00
|
|
|
host: nil,
|
2020-11-02 04:09:48 +13:00
|
|
|
capture: capture,
|
|
|
|
members: make(map[string]*SessionCtx),
|
2020-10-23 03:54:50 +13:00
|
|
|
emmiter: events.New(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
type SessionManagerCtx struct {
|
2020-10-23 03:54:50 +13:00
|
|
|
logger zerolog.Logger
|
2020-11-01 11:53:19 +13:00
|
|
|
host types.Session
|
2020-11-02 04:09:48 +13:00
|
|
|
capture types.CaptureManager
|
|
|
|
members map[string]*SessionCtx
|
2020-10-23 03:54:50 +13:00
|
|
|
emmiter events.EventEmmiter
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (manager *SessionManagerCtx) New(id string, admin bool, socket types.WebSocket) types.Session {
|
|
|
|
session := &SessionCtx{
|
2020-10-23 03:54:50 +13:00
|
|
|
id: id,
|
|
|
|
admin: admin,
|
|
|
|
manager: manager,
|
|
|
|
socket: socket,
|
|
|
|
logger: manager.logger.With().Str("id", id).Logger(),
|
|
|
|
connected: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
manager.members[id] = session
|
2020-11-01 11:03:37 +13:00
|
|
|
manager.emmiter.Emit("created", session)
|
2020-10-23 03:54:50 +13:00
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
if !manager.capture.Streaming() && len(manager.members) > 0 {
|
|
|
|
manager.capture.StartStream()
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
return session
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (manager *SessionManagerCtx) Get(id string) (types.Session, bool) {
|
|
|
|
session, ok := manager.members[id]
|
|
|
|
return session, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *SessionManagerCtx) Has(id string) bool {
|
|
|
|
_, ok := manager.members[id]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *SessionManagerCtx) Destroy(id string) error {
|
|
|
|
session, ok := manager.members[id]
|
|
|
|
if ok {
|
|
|
|
delete(manager.members, id)
|
|
|
|
err := session.destroy()
|
|
|
|
|
|
|
|
if !manager.capture.Streaming() && len(manager.members) <= 0 {
|
|
|
|
manager.capture.StopStream()
|
|
|
|
}
|
|
|
|
|
|
|
|
manager.emmiter.Emit("destroy", id)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---
|
|
|
|
// host
|
|
|
|
// ---
|
|
|
|
func (manager *SessionManagerCtx) HasHost() bool {
|
2020-11-01 11:53:19 +13:00
|
|
|
return manager.host != nil
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (manager *SessionManagerCtx) SetHost(host types.Session) {
|
2020-11-01 11:53:19 +13:00
|
|
|
manager.host = host
|
|
|
|
manager.emmiter.Emit("host", host)
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (manager *SessionManagerCtx) GetHost() types.Session {
|
2020-11-01 11:53:19 +13:00
|
|
|
return manager.host
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (manager *SessionManagerCtx) ClearHost() {
|
2020-11-01 11:53:19 +13:00
|
|
|
host := manager.host
|
|
|
|
manager.host = nil
|
|
|
|
manager.emmiter.Emit("host_cleared", host)
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
// ---
|
|
|
|
// members list
|
|
|
|
// ---
|
|
|
|
func (manager *SessionManagerCtx) Admins() []types.Session {
|
|
|
|
var sessions []types.Session
|
2020-10-23 03:54:50 +13:00
|
|
|
for _, session := range manager.members {
|
|
|
|
if !session.connected || !session.admin {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
sessions = append(sessions, session)
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
2020-11-02 04:09:48 +13:00
|
|
|
|
|
|
|
return sessions
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (manager *SessionManagerCtx) Members() []types.Session {
|
|
|
|
var sessions []types.Session
|
2020-10-23 03:54:50 +13:00
|
|
|
for _, session := range manager.members {
|
|
|
|
if !session.connected {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
sessions = append(sessions, session)
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
return sessions
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (manager *SessionManagerCtx) Broadcast(v interface{}, exclude interface{}) error {
|
2020-10-23 03:54:50 +13:00
|
|
|
for id, session := range manager.members {
|
|
|
|
if !session.connected {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if exclude != nil {
|
|
|
|
if in, _ := utils.ArrayIn(id, exclude); in {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := session.Send(v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
// ---
|
|
|
|
// events
|
|
|
|
// ---
|
|
|
|
func (manager *SessionManagerCtx) OnHost(listener func(session types.Session)) {
|
2020-10-23 03:54:50 +13:00
|
|
|
manager.emmiter.On("host", func(payload ...interface{}) {
|
2020-11-02 04:09:48 +13:00
|
|
|
listener(payload[0].(*SessionCtx))
|
2020-10-23 03:54:50 +13:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (manager *SessionManagerCtx) OnHostCleared(listener func(session types.Session)) {
|
2020-10-23 03:54:50 +13:00
|
|
|
manager.emmiter.On("host_cleared", func(payload ...interface{}) {
|
2020-11-02 04:09:48 +13:00
|
|
|
listener(payload[0].(*SessionCtx))
|
2020-10-23 03:54:50 +13:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (manager *SessionManagerCtx) OnDestroy(listener func(id string)) {
|
2020-11-01 11:53:19 +13:00
|
|
|
manager.emmiter.On("destroy", func(payload ...interface{}) {
|
|
|
|
listener(payload[0].(string))
|
2020-10-23 03:54:50 +13:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (manager *SessionManagerCtx) OnCreated(listener func(session types.Session)) {
|
2020-10-23 03:54:50 +13:00
|
|
|
manager.emmiter.On("created", func(payload ...interface{}) {
|
2020-11-02 04:09:48 +13:00
|
|
|
listener(payload[0].(*SessionCtx))
|
2020-10-23 03:54:50 +13:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (manager *SessionManagerCtx) OnConnected(listener func(session types.Session)) {
|
2020-10-23 03:54:50 +13:00
|
|
|
manager.emmiter.On("connected", func(payload ...interface{}) {
|
2020-11-02 04:09:48 +13:00
|
|
|
listener(payload[0].(*SessionCtx))
|
2020-10-23 03:54:50 +13:00
|
|
|
})
|
|
|
|
}
|