Archived
2
0
This repository has been archived on 2024-06-24. You can view files and clone it, but cannot push or open issues or pull requests.
neko-custom/server/internal/session/manager.go

219 lines
4.7 KiB
Go
Raw Normal View History

2020-01-19 12:30:09 +13:00
package session
import (
"fmt"
2021-02-16 03:41:08 +13:00
"sync"
2020-01-19 12:30:09 +13:00
"github.com/kataras/go-events"
2020-01-25 04:47:37 +13:00
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
2020-01-19 12:30:09 +13:00
2021-10-06 09:38:24 +13:00
"m1k1o/neko/internal/types"
"m1k1o/neko/internal/utils"
2020-01-19 12:30:09 +13:00
)
func New(remote types.RemoteManager) *SessionManager {
2020-01-19 12:30:09 +13:00
return &SessionManager{
2020-01-25 04:47:37 +13:00
logger: log.With().Str("module", "session").Logger(),
2020-01-19 12:30:09 +13:00
host: "",
remote: remote,
2020-01-19 12:30:09 +13:00
members: make(map[string]*Session),
emmiter: events.New(),
}
}
type SessionManager struct {
2021-02-16 03:41:08 +13:00
mu sync.Mutex
2020-01-25 04:47:37 +13:00
logger zerolog.Logger
2020-01-19 12:30:09 +13:00
host string
remote types.RemoteManager
2020-01-19 12:30:09 +13:00
members map[string]*Session
emmiter events.EventEmmiter
}
func (manager *SessionManager) New(id string, admin bool, socket types.WebSocket) types.Session {
2020-01-19 12:30:09 +13:00
session := &Session{
2020-01-25 04:47:37 +13:00
id: id,
admin: admin,
manager: manager,
2020-01-21 03:38:07 +13:00
socket: socket,
2020-01-25 04:47:37 +13:00
logger: manager.logger.With().Str("id", id).Logger(),
2020-01-21 03:38:07 +13:00
connected: false,
2020-01-19 12:30:09 +13:00
}
2021-02-16 03:41:08 +13:00
manager.mu.Lock()
2020-01-25 04:47:37 +13:00
manager.members[id] = session
2021-07-23 06:58:15 +12:00
if !manager.remote.Streaming() && len(manager.members) > 0 {
manager.remote.StartStream()
}
2021-02-16 03:41:08 +13:00
manager.mu.Unlock()
2021-02-16 03:41:08 +13:00
manager.emmiter.Emit("created", id, session)
2020-01-19 12:30:09 +13:00
return session
}
2020-01-25 04:47:37 +13:00
func (manager *SessionManager) HasHost() bool {
return manager.host != ""
2020-01-19 12:30:09 +13:00
}
2020-01-25 04:47:37 +13:00
func (manager *SessionManager) IsHost(id string) bool {
return manager.host == id
2020-01-19 12:30:09 +13:00
}
2020-01-25 04:47:37 +13:00
func (manager *SessionManager) SetHost(id string) error {
2021-02-16 03:41:08 +13:00
manager.mu.Lock()
2020-01-25 04:47:37 +13:00
_, ok := manager.members[id]
2021-02-16 03:41:08 +13:00
manager.mu.Unlock()
2020-01-19 12:30:09 +13:00
if ok {
2020-01-25 04:47:37 +13:00
manager.host = id
manager.emmiter.Emit("host", id)
2020-01-19 12:30:09 +13:00
return nil
}
2021-02-16 03:41:08 +13:00
2020-01-19 12:30:09 +13:00
return fmt.Errorf("invalid session id %s", id)
}
2020-01-25 04:47:37 +13:00
func (manager *SessionManager) GetHost() (types.Session, bool) {
2021-02-16 03:41:08 +13:00
manager.mu.Lock()
defer manager.mu.Unlock()
2020-01-25 04:47:37 +13:00
host, ok := manager.members[manager.host]
2020-01-19 12:30:09 +13:00
return host, ok
}
2020-01-25 04:47:37 +13:00
func (manager *SessionManager) ClearHost() {
id := manager.host
manager.host = ""
manager.emmiter.Emit("host_cleared", id)
2020-01-19 12:30:09 +13:00
}
2020-01-25 04:47:37 +13:00
func (manager *SessionManager) Has(id string) bool {
2021-02-16 03:41:08 +13:00
manager.mu.Lock()
defer manager.mu.Unlock()
2020-01-25 04:47:37 +13:00
_, ok := manager.members[id]
2020-01-19 12:30:09 +13:00
return ok
}
2020-01-25 04:47:37 +13:00
func (manager *SessionManager) Get(id string) (types.Session, bool) {
2021-02-16 03:41:08 +13:00
manager.mu.Lock()
defer manager.mu.Unlock()
2020-01-25 04:47:37 +13:00
session, ok := manager.members[id]
2020-01-19 12:30:09 +13:00
return session, ok
}
2020-04-07 08:14:08 +12:00
func (manager *SessionManager) Admins() []*types.Member {
2021-02-16 03:41:08 +13:00
manager.mu.Lock()
defer manager.mu.Unlock()
2020-04-07 08:14:08 +12:00
members := []*types.Member{}
for _, session := range manager.members {
if !session.connected || !session.admin {
continue
}
member := session.Member()
if member != nil {
members = append(members, member)
}
}
2021-02-16 03:41:08 +13:00
2020-04-07 08:14:08 +12:00
return members
}
2020-01-25 04:47:37 +13:00
func (manager *SessionManager) Members() []*types.Member {
2021-02-16 03:41:08 +13:00
manager.mu.Lock()
defer manager.mu.Unlock()
2020-01-25 04:47:37 +13:00
members := []*types.Member{}
for _, session := range manager.members {
if !session.connected {
continue
2020-01-21 03:38:07 +13:00
}
2020-01-25 04:47:37 +13:00
member := session.Member()
if member != nil {
members = append(members, member)
}
}
return members
2020-01-19 12:30:09 +13:00
}
2021-10-06 10:10:10 +13:00
func (manager *SessionManager) Destroy(id string) {
2021-02-16 03:41:08 +13:00
manager.mu.Lock()
2020-01-25 04:47:37 +13:00
session, ok := manager.members[id]
2020-01-19 12:30:09 +13:00
if ok {
err := session.destroy()
2020-01-25 04:47:37 +13:00
delete(manager.members, id)
2021-07-23 06:58:15 +12:00
if manager.remote.Streaming() && len(manager.members) <= 0 {
manager.remote.StopStream()
}
2021-02-16 03:41:08 +13:00
manager.mu.Unlock()
2020-04-07 08:14:08 +12:00
manager.emmiter.Emit("destroyed", id, session)
2021-10-06 10:10:10 +13:00
manager.logger.Err(err).Str("session_id", id).Msg("destorying session")
return
2020-01-19 12:30:09 +13:00
}
2021-02-16 03:41:08 +13:00
manager.mu.Unlock()
2020-01-19 12:30:09 +13:00
}
2020-01-25 04:47:37 +13:00
func (manager *SessionManager) Clear() error {
return nil
2020-01-19 12:30:09 +13:00
}
func (manager *SessionManager) Broadcast(v interface{}, exclude interface{}) error {
2021-02-16 03:41:08 +13:00
manager.mu.Lock()
defer manager.mu.Unlock()
2020-01-25 04:47:37 +13:00
for id, session := range manager.members {
if !session.connected {
continue
}
2020-01-19 12:30:09 +13:00
2020-01-25 04:47:37 +13:00
if exclude != nil {
if in, _ := utils.ArrayIn(id, exclude); in {
continue
}
}
2020-01-19 12:30:09 +13:00
2020-01-25 04:47:37 +13:00
if err := session.Send(v); err != nil {
return err
}
2020-01-21 05:22:24 +13:00
}
return nil
}
2020-01-25 04:47:37 +13:00
func (manager *SessionManager) OnHost(listener func(id string)) {
manager.emmiter.On("host", func(payload ...interface{}) {
2020-01-19 12:30:09 +13:00
listener(payload[0].(string))
})
}
2020-01-25 04:47:37 +13:00
func (manager *SessionManager) OnHostCleared(listener func(id string)) {
manager.emmiter.On("host_cleared", func(payload ...interface{}) {
2020-01-19 12:30:09 +13:00
listener(payload[0].(string))
})
}
2020-04-07 08:14:08 +12:00
func (manager *SessionManager) OnDestroy(listener func(id string, session types.Session)) {
2020-01-25 04:47:37 +13:00
manager.emmiter.On("destroyed", func(payload ...interface{}) {
2020-04-07 08:14:08 +12:00
listener(payload[0].(string), payload[1].(*Session))
2020-01-19 12:30:09 +13:00
})
}
2020-01-25 04:47:37 +13:00
func (manager *SessionManager) OnCreated(listener func(id string, session types.Session)) {
manager.emmiter.On("created", func(payload ...interface{}) {
2020-01-21 03:38:07 +13:00
listener(payload[0].(string), payload[1].(*Session))
})
}
2020-01-25 04:47:37 +13:00
func (manager *SessionManager) OnConnected(listener func(id string, session types.Session)) {
manager.emmiter.On("connected", func(payload ...interface{}) {
listener(payload[0].(string), payload[1].(*Session))
2020-01-19 12:30:09 +13:00
})
}