-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsessions.go
82 lines (72 loc) · 1.78 KB
/
sessions.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
package spice
import (
"fmt"
"sync"
)
// sessionTable holds a mapping of SessionID and destination node computeAddress
// map[sessionid]computeAddress
type sessionTable struct {
lock sync.Mutex
entries map[uint32]*sessionEntry
}
type sessionEntry struct {
computeAddress string
usageCount int
authToken string
}
func newSessionTable() *sessionTable {
return &sessionTable{
entries: make(map[uint32]*sessionEntry),
}
}
func (s *sessionTable) Lookup(session uint32) bool {
s.lock.Lock()
defer s.lock.Unlock()
_, ok := s.entries[session]
return ok
}
func (s *sessionTable) OTP(session uint32) string {
s.lock.Lock()
defer s.lock.Unlock()
if _, ok := s.entries[session]; ok {
return s.entries[session].authToken
}
return ""
}
func (s *sessionTable) Compute(session uint32) string {
s.lock.Lock()
defer s.lock.Unlock()
if _, ok := s.entries[session]; ok {
return s.entries[session].computeAddress
}
return ""
}
func (s *sessionTable) Add(session uint32, destination string, otp string) {
s.lock.Lock()
defer s.lock.Unlock()
if _, ok := s.entries[session]; !ok {
s.entries[session] = &sessionEntry{computeAddress: destination, usageCount: 1, authToken: otp}
}
return
}
func (s *sessionTable) Connect(session uint32) (string, error) {
s.lock.Lock()
defer s.lock.Unlock()
if _, ok := s.entries[session]; !ok {
return "", fmt.Errorf("no such session in table")
}
s.entries[session].usageCount = s.entries[session].usageCount + 1
return s.entries[session].computeAddress, nil
}
func (s *sessionTable) Disconnect(session uint32) {
s.lock.Lock()
defer s.lock.Unlock()
if _, ok := s.entries[session]; !ok {
return
}
s.entries[session].usageCount = s.entries[session].usageCount - 1
if s.entries[session].usageCount < 1 {
delete(s.entries, session)
}
return
}