-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.go
79 lines (59 loc) · 1.36 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
/**
* Sessions
*/
package main
import (
"log"
"net/http"
)
type Session struct {
clients []*Client
path string
}
func (s *Session) getClient(r *http.Request) *Client {
client := NewClient(r)
// First, try to return an existing client
for _, _client := range s.clients {
if _client.equal(client) {
return _client
}
}
s.log(client.id + " connected")
// Create a new client since the client is new to this session
s.clients = append(s.clients, client)
return client
}
func (s *Session) log(message string) {
log.Printf("#%s: %s", s.path, message)
}
func (s *Session) peers(client *Client) []*Client {
peers := []*Client{}
for _, peer := range s.clients {
if !client.equal(peer) {
peers = append(peers, peer)
}
}
return peers
}
func NewSession(path string) *Session {
return &Session{
clients: []*Client{},
path: path,
}
}
type SessionManager struct {
sessions map[string]*Session
}
func (sm SessionManager) getSession(path string) *Session {
session, found := sm.sessions[path]
if !found {
session = NewSession(path)
sm.sessions[path] = session
}
return session
}
func NewSessionManager() *SessionManager {
return &SessionManager{
sessions: make(map[string]*Session),
}
}