3
0
Fork 0

CHG: refactor

CHG: use concurrent map instead of regular map with lock
DEL: global session control, moved to per server only
DEL: unused command system
This commit is contained in:
Thomas van Weert 2023-02-16 20:04:17 +01:00
parent 570f501a76
commit 9edcb83fc4
12 changed files with 216 additions and 335 deletions

View file

@ -6,32 +6,44 @@ import (
)
type Server struct {
Socket socketio.Socket
ID string
Sessions map[string]socketio.Socket
Attraction map[string]string
mux sync.RWMutex
Socket socketio.Socket
ID string
Sessions map[string]socketio.Socket
Attraction map[string]string
Mux *sync.RWMutex
}
func (serv *Server) CanAttraction(attraction string) bool {
serv.mux.RLock()
defer serv.mux.RUnlock()
serv.Mux.RLock()
defer serv.Mux.RUnlock()
_,b := serv.Attraction[attraction]
_, b := serv.Attraction[attraction]
return !b
}
func (serv *Server) AddSession(session, attraction string, io socketio.Socket) {
serv.mux.Lock()
func (serv *Server) AddSession(session, attraction string, io socketio.Socket) bool {
serv.Mux.Lock()
defer serv.Mux.Unlock()
// Check if session in use
if _, b := serv.Sessions[session]; b {
return false
}
// Check if attraction is already controlled
if _, b := serv.Attraction[attraction]; b {
return false
}
serv.Sessions[session] = io
serv.Attraction[attraction] = session
serv.mux.Unlock()
return true
}
func (serv *Server) HasSession(session string) (bool, socketio.Socket) {
serv.mux.RLock()
defer serv.mux.RUnlock()
serv.Mux.RLock()
defer serv.Mux.RUnlock()
io, b := serv.Sessions[session]
@ -39,8 +51,8 @@ func (serv *Server) HasSession(session string) (bool, socketio.Socket) {
}
func (serv *Server) RemoveSession(session, attraction string) {
serv.mux.Lock()
serv.Mux.Lock()
delete(serv.Sessions, session)
delete(serv.Attraction, attraction)
serv.mux.Unlock()
}
serv.Mux.Unlock()
}

View file

@ -1,8 +0,0 @@
package objects
type Session struct {
Token string `json:"token"`
UUID string `json:"uuid"`
ID string `json:"id"`
Attraction string `json:"attraction"`
}