forked from zhaojh329/rttys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbroker.go
160 lines (134 loc) · 3.85 KB
/
broker.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"fmt"
"github.com/gorilla/websocket"
"github.com/json-iterator/go"
log "github.com/sirupsen/logrus"
"time"
)
const (
RTTY_MESSAGE_VERSION = 2
RTTY_MAX_SESSION_ID = 1000000
)
type Broker struct {
devices map[string]*Device
sessions map[string]*Session
connecting chan *Device /* Connecting requests from Device. */
disconnecting chan *Device /* Disconnecting requests from Device. */
logining chan *User /* Login requests from the User. */
logouting chan *User /* Logout requests from the User. */
inDevMessage chan *DevMessage /* Buffered channel of inbound messages from device. */
inUsrMessage chan *UsrMessage /* Buffered channel of inbound messages from user. */
}
type Session struct {
dev *Device
user *User
devsid uint8
}
func newBroker() *Broker {
return &Broker{
connecting: make(chan *Device, 100),
disconnecting: make(chan *Device, 100),
logining: make(chan *User, 100),
logouting: make(chan *User, 100),
devices: make(map[string]*Device),
sessions: make(map[string]*Session),
inDevMessage: make(chan *DevMessage, 1000),
inUsrMessage: make(chan *UsrMessage, 1000),
}
}
func (br *Broker) newSession(user *User) bool {
devid := user.devid
sid := genUniqueID("tty")
if dev, ok := br.devices[devid]; ok {
devsid := dev.getFreeSid()
if devsid < 1 {
log.Warn("Not found available devsid")
return false
}
br.sessions[sid] = &Session{dev, user, devsid}
dev.sessions[devsid] = sid
user.sid = sid
msg := fmt.Sprintf(`{"type":"login","sid":%d}`, devsid)
// Notify the device to create a pty and associate it with a session id
dev.wsWrite(websocket.TextMessage, []byte(msg))
log.Info("New session:", sid)
return true
} else {
// Notify the user that the device is offline
msg := `{"type":"login","err":1,"msg":"offline"}`
user.wsWrite(websocket.TextMessage, []byte(msg))
log.Info("Device", devid, "offline")
return false
}
}
func (br *Broker) run() {
for {
select {
case dev := <-br.connecting:
if _, ok := br.devices[dev.devid]; ok {
log.Warn("ID conflicting:", dev.devid)
dev.Close()
} else {
br.devices[dev.devid] = dev
log.Info("New device:", dev.devid)
}
case dev := <-br.disconnecting:
if dev, ok := br.devices[dev.devid]; ok {
delete(br.devices, dev.devid)
log.Info("Died device:", dev.devid)
for sid, session := range br.sessions {
if session.dev.devid == dev.devid {
session.user.Close()
delete(br.sessions, sid)
log.Info("Delete session: ", sid)
}
}
}
case user := <-br.logining:
if !br.newSession(user) {
time.AfterFunc(500*time.Millisecond, user.Close)
}
case user := <-br.logouting:
if session, ok := br.sessions[user.sid]; ok {
devsid := session.devsid
dev := session.dev
sid := user.sid
msg := fmt.Sprintf(`{"type":"logout","sid":%d}`, devsid)
dev.wsWrite(websocket.TextMessage, []byte(msg))
delete(br.sessions, sid)
delete(session.dev.sessions, devsid)
log.Info("Delete session: ", sid)
}
case msg := <-br.inDevMessage:
msgType := msg.msgType
data := msg.data
devsid := uint8(0)
if msgType == websocket.BinaryMessage {
devsid = data[0]
data = data[1:]
} else {
typ := jsoniter.Get(data, "type").ToString()
if typ == "cmd" {
handleCmdResp(data)
continue
}
val := jsoniter.Get(data, "sid").ToInt()
devsid = uint8(val)
}
sid := msg.dev.sessions[devsid]
if session, ok := br.sessions[sid]; ok {
session.user.wsWrite(msgType, data)
}
case msg := <-br.inUsrMessage:
msgType := msg.msgType
data := msg.data
if session, ok := br.sessions[msg.user.sid]; ok {
if msgType == websocket.BinaryMessage {
data = append([]byte{session.devsid}, data...)
}
session.dev.wsWrite(msgType, data)
}
}
}
}