-
Notifications
You must be signed in to change notification settings - Fork 2
/
hub.go
424 lines (375 loc) · 8.42 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
package chat
import (
"crypto/tls"
"log"
"net"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"
)
type messageType int
const (
defaultChannelName = "general"
join = messageType(iota)
listUsers
listChannels
create
leave
text
mute
unmute
dm
quit
)
// A Config sets the options the server needs when it starts.
type Config struct {
TCPPortAddr string
TCPSPortAddr string
HTTPPortAddr string
HTTPSPortAddr string
IPAddr string
LogFilename string
}
// A message contains the information needed for the server and clients to
// communicate.
type message struct {
Channel string
Username string
Text string
Time time.Time
MessageType messageType
}
func newMessage(channel, username, text string, messageType messageType) *message {
return &message{
Channel: channel,
Username: username,
Text: text,
Time: time.Now(),
MessageType: messageType,
}
}
// A channel is the equivalent of a "chat room", containing a name,
// and information about the users belonging to it.
type channel struct {
name string
users map[*User]bool
activeUsers map[string]*User
}
func newChannel(channelName string, activeUsers map[string]*User) *channel {
return &channel{
name: channelName,
users: make(map[*User]bool),
activeUsers: activeUsers,
}
}
func (c *channel) join(u *User) {
if _, ok := c.users[u]; ok {
u.conn.write(newMessage(c.name, u.name, "Changing to channel "+c.name+"\n", join))
return
}
c.users[u] = true
c.broadcast(newMessage(c.name, u.name, u.name+" has joined "+c.name+"\n", join))
}
func (c *channel) leave(u *User) {
delete(c.users, u)
}
func (c *channel) broadcast(m *message) {
for u := range c.users {
if _, ok := c.activeUsers[u.name]; !ok {
continue
}
err := u.conn.write(m)
if err != nil {
// for debugging only, this needs to use the actual logger
log.Println("Broadcast error: ", err.Error())
}
}
}
// A hub is the server. It contains all the information about connected
// clients, and sends and receives messages, essentially acting as a message
// broker.
type hub struct {
logger *log.Logger
channels map[string]*channel
users map[string]*User
userCh chan *User
messageCh chan *message
}
func newHub(l *log.Logger) *hub {
return &hub{
logger: l,
channels: make(map[string]*channel),
users: make(map[string]*User),
userCh: make(chan *User),
messageCh: make(chan *message),
}
}
func (h *hub) newUser(u *User) {
h.users[u.name] = u
h.channels[defaultChannelName].join(u)
go u.conn.read()
}
func (h *hub) listUsers(m *message) {
user, ok := h.users[m.Username]
if !ok {
return
}
var users []string
if m.Channel != "" {
ch, ok := h.channels[m.Channel]
if !ok {
user.conn.write(newMessage("you", "server", "Channel "+m.Channel+" doesn't exist.\n", text))
return
}
for u := range ch.users {
users = append(users, u.name)
}
} else {
for user := range h.users {
users = append(users, user)
}
}
m.Text = strings.Join(users, ",")
user.conn.write(m)
}
func (h *hub) listChannels(m *message) {
user, ok := h.users[m.Username]
if !ok {
return
}
var chans []string
for ch := range h.channels {
chans = append(chans, ch)
}
m.Text = strings.Join(chans, ",")
user.conn.write(m)
}
func (h *hub) joinChannel(m *message) {
user, ok := h.users[m.Username]
if !ok {
return
}
if ch, ok := h.channels[m.Channel]; ok {
ch.join(user)
return
}
m.Text = "Sorry, the channel " + m.Channel + " doesn't exist.\n"
m.MessageType = text
user.conn.write(m)
}
func (h *hub) leaveChannel(m *message) {
user, ok := h.users[m.Username]
if !ok {
return
}
if m.Channel == defaultChannelName {
m.Text = "You can't leave the default channel (which is " + defaultChannelName + ").\n"
m.MessageType = text
user.conn.write(m)
return
}
ch, ok := h.channels[m.Channel]
if !ok {
m.Text = "The channel " + m.Channel + " doesn't exist, so you can't leave it.\n"
m.MessageType = text
user.conn.write(m)
return
}
if _, ok = ch.users[user]; !ok {
m.Text = "You're not a member of the channel " + m.Channel + ".\n"
m.MessageType = text
user.conn.write(m)
return
}
ch.leave(user)
m.Text = "Left channel " + m.Channel + ". Returning you to the general channel.\n"
user.conn.write(m)
}
func (h *hub) createChannel(m *message) {
user, ok := h.users[m.Username]
if !ok {
return
}
ch, ok := h.channels[m.Channel]
if ok {
ch.join(user)
return
}
newCh := newChannel(m.Channel, h.users)
h.channels[m.Channel] = newCh
newCh.join(user)
}
func (h *hub) broadcast(m *message) {
h.logger.Printf("(%s to %s): %s", m.Username, m.Channel, m.Text)
ch, ok := h.channels[m.Channel]
if !ok {
return
}
ch.broadcast(m)
}
func (h *hub) mute(m *message) {
user, ok := h.users[m.Username]
if !ok {
return
}
if _, ok := h.users[m.Channel]; !ok {
m.MessageType = text
m.Text = "The user " + m.Channel + " doesn't exist.\n"
user.conn.write(m)
return
}
user.conn.write(m)
}
func (h *hub) unmute(m *message) {
user, ok := h.users[m.Username]
if !ok {
return
}
if _, ok := h.users[m.Channel]; !ok {
m.MessageType = text
m.Text = "The user " + m.Channel + " doesn't exist.\n"
user.conn.write(m)
return
}
user.conn.write(m)
}
func (h *hub) dm(m *message) {
h.logger.Printf("(%s to %s): %s", m.Username, m.Channel, m.Text)
sender, ok := h.users[m.Username]
if !ok {
return
}
recipient, ok := h.users[m.Channel]
if !ok {
m.MessageType = text
m.Text = "Sorry, the user " + m.Channel + " doesn't exist.\n"
sender.conn.write(m)
return
}
recipient.conn.write(m)
sender.conn.write(m)
}
func (h *hub) quit(m *message) {
h.logger.Printf("(%s to %s): %s", m.Username, m.Channel, m.Text)
user, ok := h.users[m.Username]
if !ok {
return
}
user.conn.close()
delete(h.users, m.Username)
h.channels[defaultChannelName].broadcast(m)
}
func (h *hub) run() {
h.channels[defaultChannelName] = newChannel(defaultChannelName, h.users)
for {
select {
case user := <-h.userCh:
// bug: if a user joins and then quits and re-joins with that same
// name, you get a write to closed error
h.newUser(user)
case message := <-h.messageCh:
switch message.MessageType {
case join:
h.joinChannel(message)
case listUsers:
h.listUsers(message)
case listChannels:
h.listChannels(message)
case create:
h.createChannel(message)
case leave:
h.leaveChannel(message)
case text:
h.broadcast(message)
case mute:
h.mute(message)
case unmute:
h.unmute(message)
case dm:
h.dm(message)
case quit:
h.quit(message)
}
}
}
}
func (h *hub) serve(port string) error {
server, err := net.Listen("tcp", port)
if err != nil {
return err
}
h.logger.Println("Server started on", port)
go func() {
for {
conn, err := server.Accept()
if err != nil {
h.logger.Println(err.Error())
}
go func() {
h.userCh <- createTCPUser(conn, h)
}()
}
}()
h.run()
return nil
}
func (h *hub) serveSecure(port string) error {
server, err := tls.Listen("tcp", port, DefaultTLSConfig())
if err != nil {
h.logger.Println("Unable to start secure server:", err.Error())
return err
}
h.logger.Println("Secure server started on", port)
go func() {
for {
conn, err := server.Accept()
if err != nil {
h.logger.Println(err.Error())
}
go func() {
h.userCh <- createTCPUser(conn, h)
}()
}
}()
h.run()
return nil
}
func (h *hub) serveHTTP(port string, mux http.Handler) error {
h.logger.Println("HTTP server started on", port)
return http.ListenAndServe(port, mux)
}
func (h *hub) serveHTTPS(port string, mux http.Handler) error {
server := &http.Server{
Addr: port,
Handler: mux,
TLSConfig: DefaultTLSConfig(),
}
h.logger.Println("HTTPS server started on", port)
return server.ListenAndServeTLS("", "")
}
// ListenAndServe starts the TCP and HTTP servers based on the given config.
func ListenAndServe(l *log.Logger, cfg *Config) error {
h := newHub(l)
errCh := make(chan error, 4)
mux := getServeMux(h)
go h.serveHTTP(":"+cfg.HTTPPortAddr, mux)
go h.serveHTTPS(":"+cfg.HTTPSPortAddr, mux)
go h.serve(":" + cfg.TCPPortAddr)
go h.serveSecure(":" + cfg.TCPSPortAddr)
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, syscall.SIGINT, syscall.SIGTERM)
for {
select {
case err := <-errCh:
if err != nil {
h.logger.Fatalf("%s\n", err.Error())
}
case s := <-signalCh:
log.Printf("Captured %v. Exiting...\n", s)
os.Exit(1)
}
}
}