forked from zhaojh329/rttys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbroker.go
179 lines (151 loc) · 4.68 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* Copyright (C) 2017 Jianhui Zhao <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*/
package main
import (
"fmt"
"time"
"github.com/buger/jsonparser"
"github.com/gorilla/websocket"
)
const RTTY_MESSAGE_VERSION = 2
const 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.Println("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.Println("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.Println("Device", devid, "offline")
return false
}
}
func (br *Broker) run() {
for {
select {
case dev := <-br.connecting:
if _, ok := br.devices[dev.devid]; ok {
log.Println("ID conflicting:", dev.devid)
dev.Close()
} else {
br.devices[dev.devid] = dev
log.Println("New device:", dev.devid)
}
case dev := <-br.disconnecting:
if dev, ok := br.devices[dev.devid]; ok {
delete(br.devices, dev.devid)
log.Println("Died device:", dev.devid)
for sid, session := range br.sessions {
if session.dev.devid == dev.devid {
session.user.Close()
delete(br.sessions, sid)
log.Println("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.Println("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, _ := jsonparser.GetString(data, "type")
if typ == "cmd" {
handleCmdResp(data)
continue
}
val, _ := jsonparser.GetInt(data, "sid")
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 {
devsid := make([]byte, 1)
devsid[0] = session.devsid
data = append(devsid, data...)
}
session.dev.wsWrite(msgType, data)
}
}
}
}