-
Notifications
You must be signed in to change notification settings - Fork 0
/
hub.go
197 lines (161 loc) · 4.52 KB
/
hub.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package ws
import (
"log"
"net/http"
"sync"
"github.com/google/uuid"
"github.com/gorilla/websocket"
"github.com/nucktwillieren/gorlazy/cache"
)
type Hub struct {
Name string
ExpectedMethod string
CMutex sync.Mutex
ConnectionPool map[string]map[string]*Channel //Group, ID, Channel
Unregister chan *Channel
Cache cache.Cache
/*BroadcastChan chan []byte
GroupSendChan chan map[string][]byte
SendChan chan map[string]map[string][]byte*/
}
type Context struct {
Conn *websocket.Conn
Cha *Channel
SecWebSocketProtocol string
Group string
ID string
Message []byte
TransportationPayloads []*TransportationPayload
}
func (ctx *Context) AddTranspotationPayload(class string, group string, target string, message []byte) {
ctx.TransportationPayloads = append(ctx.TransportationPayloads, &TransportationPayload{
Class: class,
TargetGroup: group,
TargetID: target,
Message: message,
})
}
func (ctx *Context) AddSingleTargetPayload(group, target string, message []byte) {
ctx.AddTranspotationPayload("channel", group, target, message)
}
func (ctx *Context) AddGroupTargetPayload(group string, message []byte) {
ctx.AddTranspotationPayload("group", group, "", message)
}
func (ctx *Context) AddBroadcastPayload(message []byte) {
ctx.AddTranspotationPayload("broadcast", "", "", message)
}
type TransportationPayload struct {
Class string
TargetGroup string
TargetID string
Message []byte
}
func Upgrading(w http.ResponseWriter, r *http.Request, ctx *Context) *Context {
//log.Println("Upgrading Protocol")
conn, err := wsupgrader.Upgrade(w, r, nil)
if err != nil {
log.Println("Failed to set websocket upgrade: ", err)
return ctx
}
ctx.Conn = conn
return ctx
}
func CreateConnection(w http.ResponseWriter, r *http.Request, hub *Hub, transLayer TransportationLayer) *Context {
secWebSocketProtocol := r.Header.Get("Sec-WebSocket-Protocol")
ctx := &Context{
SecWebSocketProtocol: secWebSocketProtocol,
}
ctx = Upgrading(w, r, ctx)
hub.Join(ctx.Conn, ctx, transLayer)
return ctx
}
func (h *Hub) DoesIDExist(t string, id string) bool {
for key := range h.ConnectionPool[t] {
if id == key {
return true
}
}
return false
}
func (h *Hub) GenUID(group string) string {
id := uuid.New().String()
for h.DoesIDExist(group, id) {
id = uuid.New().String()
}
return id
}
func (h *Hub) AddToPool(t string, id string, channel *Channel) {
if h.ConnectionPool[t] == nil {
h.ConnectionPool[t] = make(map[string]*Channel)
}
h.ConnectionPool[t][id] = channel
}
func (h *Hub) Join(conn *websocket.Conn, ctx *Context, transLayer TransportationLayer) {
h.CMutex.Lock()
cha := NewChannel(h.GenUID("test"), h, "test", conn, transLayer)
h.AddToPool(cha.GroupName, cha.ID, cha)
ctx.Cha = cha
go cha.Reader()
go cha.Writer()
h.CMutex.Unlock()
}
func (h *Hub) BroadcastToAll(msg []byte) {
for k := range h.ConnectionPool {
for v := range h.ConnectionPool[k] {
if client := h.ConnectionPool[k][v]; client != nil {
h.Send(client, msg)
}
}
}
}
func (h *Hub) SendToGroup(groupName string, msg []byte) {
for k := range h.ConnectionPool[groupName] {
if client := h.ConnectionPool[groupName][k]; client != nil {
h.Send(client, msg)
}
}
}
func (h *Hub) SendToChannel(groupName string, ID string, msg []byte) {
if client := h.ConnectionPool[groupName][ID]; client != nil {
h.Send(client, msg)
}
}
func (h *Hub) Send(client *Channel, msg []byte) (closed bool) {
defer func() {
if recover() != nil {
closed = true
}
}()
client.SendChan <- msg
return false
}
func (h *Hub) Start() {
for channel := range h.Unregister {
//if _, ok := h.ConnectionPool[channel.GroupName][channel.ID]; ok {
SafeClose(channel.SendChan)
delete(h.ConnectionPool[channel.GroupName], channel.ID)
//}
}
}
func SafeClose(ch chan []byte) (justClosed bool) {
defer func() {
if recover() != nil {
// The return result can be altered
// in a defer function call.
justClosed = false
}
}()
// assume ch != nil here.
close(ch) // panic if ch is closed
return true // <=> justClosed = true; return
}
func NewHub(name string) *Hub {
log.Println("Create New Hub, name= ", name)
hub := &Hub{
Name: name,
Unregister: make(chan *Channel),
ConnectionPool: make(map[string]map[string]*Channel),
}
go hub.Start()
return hub
}