-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.go
119 lines (107 loc) · 2.63 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package teslo
import (
"encoding/json"
"github.com/gorilla/websocket"
"github.com/yanzay/log"
"github.com/yanzay/teslo/templates"
)
var upgrader = websocket.Upgrader{}
type Session struct {
ID string
conn *websocket.Conn
requests chan Event
responses chan Message
server *Server
close chan struct{}
}
type Event struct {
Type string `json:"event"`
ID string `json:"id"`
Parents []string `json:"parents"`
Data string `json:"data"`
}
func NewSession(server *Server, conn *websocket.Conn, sessionID string) *Session {
log.Debugf("Creating new session %v", conn)
return &Session{
ID: sessionID,
server: server,
conn: conn,
requests: make(chan Event, 0),
responses: make(chan Message, 0),
close: make(chan struct{}, 0),
}
}
func (s *Session) Start() {
log.Debugf("Starting session: %s", s.ID)
defer s.server.CloseSession(s.ID)
go s.writeLoop()
s.readLoop()
}
func (s *Session) Close() {
log.Debugf("Closing session: %s", s.ID)
close(s.close)
}
func (s *Session) Respond(id string, content string) {
s.responses <- Message{ID: id, Content: content}
}
func (s *Session) handleRequest(request *Event) {
log.Debugf("Handling request: %v", request)
if s.server.handlers[request.ID] != nil {
log.Debug("Running handler")
s.server.handlers[request.ID](s, request)
log.Debug("Handler exited")
} else {
for _, parent := range request.Parents {
if s.server.handlers[parent] != nil {
s.server.handlers[parent](s, request)
return
}
}
}
}
func (s *Session) writeLoop() {
log.Debug("Starting writeLoop")
for {
select {
case <-s.close:
log.Debugf("Exiting writeLoop for session: %s", s.ID)
return
case message := <-s.responses:
writer, err := s.conn.NextWriter(websocket.TextMessage)
if err != nil {
log.Errorf("Can't get next writer for websocket: %s", err)
s.Close()
return
}
log.Debugf("Sending response: %v", message)
templates.WriteMessage(writer, message.ID, message.Content)
err = writer.Close()
if err != nil {
log.Errorf("Can't write to websocket: %s", err)
s.Close()
return
}
}
}
}
func (s *Session) readLoop() {
defer log.Debug("exiting readLoop")
log.Debug("Starting readLoop")
for {
log.Debug("Getting next reader")
_, reader, err := s.conn.NextReader()
log.Debugf("reader ready, error: %s", err)
if err != nil {
log.Errorf("Can't get websocket reader: %s", err)
log.Debug("Closing session")
s.Close()
log.Debug("Session closed")
return
}
decoder := json.NewDecoder(reader)
e := &Event{}
decoder.Decode(e)
s.handleRequest(e)
log.Debug("request handled")
}
}