2020-10-23 03:54:50 +13:00
|
|
|
package session
|
|
|
|
|
|
|
|
import (
|
2020-11-26 10:06:13 +13:00
|
|
|
"fmt"
|
2020-11-15 10:54:35 +13:00
|
|
|
"sync"
|
|
|
|
|
2020-10-23 03:54:50 +13:00
|
|
|
"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"
|
2020-11-02 06:39:12 +13:00
|
|
|
"demodesk/neko/internal/config"
|
2020-10-29 07:15:48 +13:00
|
|
|
"demodesk/neko/internal/utils"
|
2020-10-23 03:54:50 +13:00
|
|
|
)
|
|
|
|
|
2020-12-03 05:36:06 +13:00
|
|
|
func New(config *config.Session) *SessionManagerCtx {
|
2020-11-28 07:59:54 +13:00
|
|
|
manager := &SessionManagerCtx{
|
2020-11-15 11:05:09 +13:00
|
|
|
logger: log.With().Str("module", "session").Logger(),
|
|
|
|
host: nil,
|
|
|
|
hostMu: sync.Mutex{},
|
|
|
|
config: config,
|
|
|
|
members: make(map[string]*SessionCtx),
|
|
|
|
membersMu: sync.Mutex{},
|
|
|
|
emmiter: events.New(),
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
2020-11-28 07:59:54 +13:00
|
|
|
|
|
|
|
// create default admin account at startup
|
|
|
|
_ = manager.Create("admin", types.MemberProfile{
|
|
|
|
Secret: config.AdminPassword,
|
|
|
|
Name: "Administrator",
|
|
|
|
IsAdmin: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
// create default user account at startup
|
|
|
|
_ = manager.Create("user", types.MemberProfile{
|
|
|
|
Secret: config.Password,
|
|
|
|
Name: "User",
|
|
|
|
IsAdmin: false,
|
|
|
|
})
|
|
|
|
|
|
|
|
return manager
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
type SessionManagerCtx struct {
|
2020-11-15 11:05:09 +13:00
|
|
|
logger zerolog.Logger
|
|
|
|
host types.Session
|
|
|
|
hostMu sync.Mutex
|
|
|
|
config *config.Session
|
|
|
|
members map[string]*SessionCtx
|
|
|
|
membersMu sync.Mutex
|
|
|
|
emmiter events.EventEmmiter
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2020-11-28 07:59:54 +13:00
|
|
|
func (manager *SessionManagerCtx) Create(id string, profile types.MemberProfile) types.Session {
|
2020-11-15 11:05:09 +13:00
|
|
|
manager.membersMu.Lock()
|
|
|
|
defer manager.membersMu.Unlock()
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
session := &SessionCtx{
|
2020-10-23 03:54:50 +13:00
|
|
|
id: id,
|
|
|
|
manager: manager,
|
|
|
|
logger: manager.logger.With().Str("id", id).Logger(),
|
2020-11-26 10:06:13 +13:00
|
|
|
profile: profile,
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
manager.members[id] = session
|
2020-11-28 07:59:54 +13:00
|
|
|
return session
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (manager *SessionManagerCtx) Get(id string) (types.Session, bool) {
|
2020-11-15 11:05:09 +13:00
|
|
|
manager.membersMu.Lock()
|
|
|
|
defer manager.membersMu.Unlock()
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
session, ok := manager.members[id]
|
|
|
|
return session, ok
|
|
|
|
}
|
|
|
|
|
2020-11-26 10:06:13 +13:00
|
|
|
func (manager *SessionManagerCtx) Delete(id string) error {
|
2020-11-15 11:05:09 +13:00
|
|
|
manager.membersMu.Lock()
|
|
|
|
defer manager.membersMu.Unlock()
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
session, ok := manager.members[id]
|
2020-11-26 10:06:13 +13:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Member not found.")
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(manager.members, id)
|
|
|
|
|
2020-12-03 04:49:51 +13:00
|
|
|
if session.IsConnected() {
|
2020-11-26 10:06:13 +13:00
|
|
|
return session.Disconnect("member deleted")
|
2020-11-02 04:09:48 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---
|
|
|
|
// host
|
|
|
|
// ---
|
2020-12-03 05:23:35 +13:00
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (manager *SessionManagerCtx) HasHost() bool {
|
2020-11-15 10:54:35 +13:00
|
|
|
manager.hostMu.Lock()
|
|
|
|
defer manager.hostMu.Unlock()
|
|
|
|
|
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-15 10:54:35 +13:00
|
|
|
manager.hostMu.Lock()
|
|
|
|
defer manager.hostMu.Unlock()
|
|
|
|
|
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-15 10:54:35 +13:00
|
|
|
manager.hostMu.Lock()
|
|
|
|
defer manager.hostMu.Unlock()
|
|
|
|
|
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-15 10:54:35 +13:00
|
|
|
manager.hostMu.Lock()
|
|
|
|
defer manager.hostMu.Unlock()
|
|
|
|
|
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
|
|
|
|
// ---
|
2020-12-03 05:23:35 +13:00
|
|
|
|
2020-12-03 05:12:00 +13:00
|
|
|
func (manager *SessionManagerCtx) HasConnectedMembers() bool {
|
2020-11-15 11:05:09 +13:00
|
|
|
manager.membersMu.Lock()
|
|
|
|
defer manager.membersMu.Unlock()
|
|
|
|
|
2020-10-23 03:54:50 +13:00
|
|
|
for _, session := range manager.members {
|
2020-12-03 05:12:00 +13:00
|
|
|
if session.IsConnected() {
|
|
|
|
return true
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
}
|
2020-11-02 04:09:48 +13:00
|
|
|
|
2020-12-03 05:12:00 +13:00
|
|
|
return false
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (manager *SessionManagerCtx) Members() []types.Session {
|
2020-11-15 11:05:09 +13:00
|
|
|
manager.membersMu.Lock()
|
|
|
|
defer manager.membersMu.Unlock()
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
var sessions []types.Session
|
2020-10-23 03:54:50 +13:00
|
|
|
for _, session := range manager.members {
|
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-19 08:30:33 +13:00
|
|
|
func (manager *SessionManagerCtx) Broadcast(v interface{}, exclude interface{}) {
|
2020-11-15 11:05:09 +13:00
|
|
|
manager.membersMu.Lock()
|
|
|
|
defer manager.membersMu.Unlock()
|
|
|
|
|
2020-10-23 03:54:50 +13:00
|
|
|
for id, session := range manager.members {
|
2020-12-03 04:49:51 +13:00
|
|
|
if !session.IsConnected() {
|
2020-10-23 03:54:50 +13:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if exclude != nil {
|
|
|
|
if in, _ := utils.ArrayIn(id, exclude); in {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := session.Send(v); err != nil {
|
2020-11-19 08:30:33 +13:00
|
|
|
manager.logger.Warn().Err(err).Msgf("broadcasting event has failed")
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-19 09:34:39 +13:00
|
|
|
func (manager *SessionManagerCtx) AdminBroadcast(v interface{}, exclude interface{}) {
|
|
|
|
manager.membersMu.Lock()
|
|
|
|
defer manager.membersMu.Unlock()
|
|
|
|
|
|
|
|
for id, session := range manager.members {
|
2020-12-03 04:49:51 +13:00
|
|
|
if !session.IsConnected() || !session.IsAdmin() {
|
2020-11-19 09:34:39 +13:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if exclude != nil {
|
|
|
|
if in, _ := utils.ArrayIn(id, exclude); in {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := session.Send(v); err != nil {
|
|
|
|
manager.logger.Warn().Err(err).Msgf("broadcasting admin event has failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
// ---
|
|
|
|
// events
|
|
|
|
// ---
|
2020-12-03 05:23:35 +13:00
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
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-12-01 06:52:16 +13:00
|
|
|
func (manager *SessionManagerCtx) OnConnected(listener func(session types.Session)) {
|
|
|
|
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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-02 08:53:25 +13:00
|
|
|
func (manager *SessionManagerCtx) OnDisconnected(listener func(session types.Session)) {
|
|
|
|
manager.emmiter.On("disconnected", func(payload ...interface{}) {
|
|
|
|
listener(payload[0].(*SessionCtx))
|
|
|
|
})
|
|
|
|
}
|
2020-12-02 23:24:20 +13:00
|
|
|
|
2020-12-03 05:29:38 +13:00
|
|
|
func (manager *SessionManagerCtx) OnReceivingStarted(listener func(session types.Session)) {
|
|
|
|
manager.emmiter.On("receiving_started", func(payload ...interface{}) {
|
|
|
|
listener(payload[0].(*SessionCtx))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *SessionManagerCtx) OnReceivingStopped(listener func(session types.Session)) {
|
|
|
|
manager.emmiter.On("receiving_stopped", func(payload ...interface{}) {
|
|
|
|
listener(payload[0].(*SessionCtx))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-12-03 05:23:35 +13:00
|
|
|
// ---
|
|
|
|
// config
|
|
|
|
// ---
|
|
|
|
|
2020-12-02 23:24:20 +13:00
|
|
|
func (manager *SessionManagerCtx) ImplicitHosting() bool {
|
|
|
|
return manager.config.ImplicitHosting
|
|
|
|
}
|