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

175 lines
3.8 KiB
Go
Raw Normal View History

2020-01-19 12:30:09 +13:00
package session
import (
"fmt"
"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
2020-01-25 04:47:37 +13:00
"n.eko.moe/neko/internal/types"
2020-01-19 12:30:09 +13:00
"n.eko.moe/neko/internal/utils"
)
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 {
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
}
2020-01-25 04:47:37 +13:00
manager.members[id] = session
manager.emmiter.Emit("created", id, session)
2020-01-19 12:30:09 +13:00
if manager.remote.Streaming() != true && len(manager.members) > 0 {
manager.remote.StartStream()
}
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 {
_, ok := manager.members[id]
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
}
return fmt.Errorf("invalid session id %s", id)
}
2020-01-25 04:47:37 +13:00
func (manager *SessionManager) GetHost() (types.Session, bool) {
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 {
_, 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) {
session, ok := manager.members[id]
2020-01-19 12:30:09 +13:00
return session, ok
}
2020-01-25 04:47:37 +13:00
func (manager *SessionManager) Members() []*types.Member {
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
}
2020-01-25 04:47:37 +13:00
func (manager *SessionManager) Destroy(id string) error {
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)
if manager.remote.Streaming() != false && len(manager.members) <= 0 {
manager.remote.StopStream()
}
2020-01-25 04:47:37 +13:00
manager.emmiter.Emit("destroyed", id)
2020-01-19 12:30:09 +13:00
return err
}
2020-01-19 12:30:09 +13:00
return nil
}
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 {
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-01-25 04:47:37 +13:00
func (manager *SessionManager) OnDestroy(listener func(id string)) {
manager.emmiter.On("destroyed", func(payload ...interface{}) {
listener(payload[0].(string))
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
})
}