-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.go
101 lines (82 loc) · 1.89 KB
/
session.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package sipnexus
import (
"sync"
"time"
"github.com/google/uuid"
"github.com/itzmanish/sipnexus/pkg/logger"
"github.com/itzmanish/sipnexus/pkg/media"
)
type SessionStatus uint8
const (
SessionStatus_New SessionStatus = iota
SessionStatus_Ringing
SessionStatus_Connected
SessionStatus_Disconnected
SessionStatus_Failed
)
type Session struct {
ID string
CallID string
Status SessionStatus
rtc media.MediaEngine
CreatedAt time.Time
}
type SessionManager struct {
sessions map[string]*Session
mu sync.RWMutex
}
func NewSessionManager() *SessionManager {
return &SessionManager{
sessions: make(map[string]*Session),
}
}
func (sm *SessionManager) CreateSession(callID string) *Session {
sm.mu.Lock()
defer sm.mu.Unlock()
session := &Session{
ID: uuid.New().String(),
CallID: callID,
CreatedAt: time.Now(),
rtc: media.NewUDPMediaEngine(logger.NewLogger()),
}
sm.sessions[session.ID] = session
return session
}
func (sm *SessionManager) GetSession(sessionID string) (*Session, bool) {
sm.mu.RLock()
defer sm.mu.RUnlock()
session, exists := sm.sessions[sessionID]
return session, exists
}
func (sm *SessionManager) DeleteSession(sessionID string) {
sm.mu.Lock()
defer sm.mu.Unlock()
delete(sm.sessions, sessionID)
}
func (sm *SessionManager) GetOrCreateSession(callID string) *Session {
sm.mu.Lock()
defer sm.mu.Unlock()
for _, session := range sm.sessions {
if session.CallID == callID {
return session
}
}
// If no existing session found, create a new one
session := &Session{
ID: uuid.New().String(),
CallID: callID,
CreatedAt: time.Now(),
}
sm.sessions[session.ID] = session
return session
}
func (sm *SessionManager) CleanupSessions() {
sm.mu.Lock()
defer sm.mu.Unlock()
now := time.Now()
for id, session := range sm.sessions {
if now.Sub(session.CreatedAt) > 24*time.Hour {
delete(sm.sessions, id)
}
}
}