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

244 lines
4.8 KiB
Go
Raw Permalink 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
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
)
2022-09-13 08:12:47 +12:00
func New(capture types.CaptureManager) *SessionManager {
2020-01-19 12:30:09 +13:00
return &SessionManager{
2023-01-30 06:18:37 +13:00
logger: log.With().Str("module", "session").Logger(),
host: "",
capture: capture,
eventsChannel: make(chan types.SessionEvent, 10),
members: make(map[string]*Session),
2020-01-19 12:30:09 +13:00
}
}
type SessionManager struct {
2023-01-30 06:18:37 +13:00
mu sync.Mutex
logger zerolog.Logger
host string
capture types.CaptureManager
members map[string]*Session
eventsChannel chan types.SessionEvent
// TODO: Handle locks in sessions as flags.
2023-01-29 10:08:36 +13:00
controlLocked bool
2020-01-19 12:30:09 +13:00
}
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
2022-09-17 22:43:17 +12:00
manager.capture.Audio().AddListener()
manager.capture.Video().AddListener()
2021-02-16 03:41:08 +13:00
manager.mu.Unlock()
2023-01-30 06:18:37 +13:00
manager.eventsChannel <- types.SessionEvent{
Type: types.SESSION_CREATED,
2023-01-29 10:08:36 +13:00
Id: id,
Session: session,
}
2023-01-22 11:43:04 +13:00
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
2023-01-22 11:43:04 +13:00
2023-01-30 06:18:37 +13:00
manager.eventsChannel <- types.SessionEvent{
Type: types.SESSION_HOST_SET,
2023-01-29 10:08:36 +13:00
Id: id,
}
2023-01-30 06:18:37 +13:00
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 = ""
2023-01-30 06:18:37 +13:00
manager.eventsChannel <- types.SessionEvent{
Type: types.SESSION_HOST_CLEARED,
2023-01-29 10:08:36 +13:00
Id: 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
}
// TODO: Handle locks in sessions as flags.
func (manager *SessionManager) SetControlLocked(locked bool) {
manager.controlLocked = locked
}
func (manager *SessionManager) CanControl(id string) bool {
session, ok := manager.Get(id)
return ok && (!manager.controlLocked || session.Admin())
}
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)
2022-09-17 22:43:17 +12:00
manager.capture.Audio().RemoveListener()
manager.capture.Video().RemoveListener()
2021-02-16 03:41:08 +13:00
manager.mu.Unlock()
2023-01-30 06:18:37 +13:00
manager.eventsChannel <- types.SessionEvent{
Type: types.SESSION_DESTROYED,
2023-01-29 10:08:36 +13:00
Id: id,
Session: session,
}
2021-12-03 07:56:25 +13:00
manager.logger.Err(err).Str("session_id", id).Msg("destroying session")
2021-10-06 10:10:10 +13:00
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
}
2021-12-12 01:52:31 +13:00
return nil
}
func (manager *SessionManager) AdminBroadcast(v interface{}, exclude interface{}) error {
manager.mu.Lock()
defer manager.mu.Unlock()
for id, session := range manager.members {
if !session.connected || !session.admin {
continue
}
if exclude != nil {
if in, _ := utils.ArrayIn(id, exclude); in {
continue
}
}
if err := session.Send(v); err != nil {
return err
}
}
2020-01-21 05:22:24 +13:00
return nil
}
2023-01-30 06:18:37 +13:00
func (manager *SessionManager) GetEventsChannel() chan types.SessionEvent {
return manager.eventsChannel
2020-01-19 12:30:09 +13:00
}