2021-12-24 12:41:16 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"aim-oscar/oscar"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SessionManager struct {
|
|
|
|
sessions map[string]*oscar.Session
|
|
|
|
mutex *sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSessionManager() *SessionManager {
|
|
|
|
sm := &SessionManager{
|
|
|
|
sessions: make(map[string]*oscar.Session),
|
|
|
|
mutex: &sync.RWMutex{},
|
|
|
|
}
|
|
|
|
return sm
|
|
|
|
}
|
|
|
|
|
2022-01-16 14:43:48 -05:00
|
|
|
func (sm *SessionManager) SetSession(screen_name string, session *oscar.Session) {
|
2021-12-24 12:41:16 -05:00
|
|
|
sm.mutex.Lock()
|
2022-01-16 14:43:48 -05:00
|
|
|
sm.sessions[screen_name] = session
|
2021-12-24 12:41:16 -05:00
|
|
|
sm.mutex.Unlock()
|
|
|
|
}
|
|
|
|
|
2022-01-16 14:43:48 -05:00
|
|
|
func (sm *SessionManager) GetSession(screen_name string) *oscar.Session {
|
2021-12-24 12:41:16 -05:00
|
|
|
sm.mutex.RLock()
|
2022-01-16 14:43:48 -05:00
|
|
|
s, ok := sm.sessions[screen_name]
|
2021-12-24 12:41:16 -05:00
|
|
|
sm.mutex.RUnlock()
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-16 14:43:48 -05:00
|
|
|
func (sm *SessionManager) RemoveSession(screen_name string) {
|
2021-12-24 12:41:16 -05:00
|
|
|
sm.mutex.Lock()
|
2022-01-16 14:43:48 -05:00
|
|
|
sm.sessions[screen_name] = nil
|
2021-12-24 12:41:16 -05:00
|
|
|
sm.mutex.Unlock()
|
|
|
|
}
|