/
stream.go
170 lines (136 loc) · 2.9 KB
/
stream.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
package gochat
import (
"fmt"
"github.com/simonz05/godis"
"log"
)
type Stream interface {
// Get the receive channel of messages to send to the client
Receive() <-chan string
// Send a string chat message
Send(msg string) error
// Close the stream.
Close()
// Check whether the stream is closed.
IsClosed() bool
// Get a printable name of the area.
Name() string
}
type streamImpl struct {
pub *godis.Client
sub *godis.Sub
rcv chan string
area *Area
user *User
signal chan bool
}
func RegisterStream(area *Area, user *User) (Stream, error) {
pub := godis.New(Cfg.SubAddr, Cfg.SubDb, Cfg.SubPassword)
sub := godis.NewSub(Cfg.SubAddr, Cfg.SubDb, Cfg.SubPassword)
err := sub.Subscribe(area.token("Updates"))
if err != nil {
return nil, err
}
s := &streamImpl{
pub,
sub,
make(chan string, 0),
area,
user,
make(chan bool, 0),
}
go s.rcvGoroutine()
// Do on-join activities
s.onJoin()
return s, nil
}
func (s *streamImpl) IsClosed() bool {
select {
case <-s.signal:
// Closed
return true
default:
// Open!
return false
}
return false
}
func (s *streamImpl) Receive() <-chan string {
return s.rcv
}
func (s *streamImpl) Send(m string) error {
if s.IsClosed() {
return throw("Stream is closed!")
}
msg := newStreamChatMessage(&Message{s.user, m})
_, err := s.pub.Publish(s.area.token("Updates"), msg.String())
if err != nil {
return err
}
return nil
}
func (s *streamImpl) Close() {
if s.IsClosed() {
log.Println("Repeat close on closed stream.")
return
}
// Signal the goroutines
close(s.signal)
s.sub.Unsubscribe(s.area.token("Updates"))
// Notify others of leaving
s.onLeave()
// Finish up
s.sub.Close()
s.pub.Quit()
}
func (s *streamImpl) rcvGoroutine() {
// Send any new messages
loop:
for {
select {
case redisMsg, ok := <-s.sub.Messages:
if !ok {
// Connection lost!
break loop
}
s.rcv <- string(redisMsg.Elem.Bytes())
case <-s.signal:
// Stream Closed!
break loop
}
}
close(s.rcv)
}
func (s *streamImpl) Name() string {
return fmt.Sprintf("Area: %s, User: %s", s.area.String(), s.user.String())
}
func (s *streamImpl) onJoin() error {
// Notify user-join
join := newStreamUserMessage(s.user, true)
_, err := s.pub.Publish(s.area.token("Updates"), join.String())
if err != nil {
return err
}
// Send join chat message
msg := newStreamChatMessage(s.area.joinMsg(s.user))
_, err = s.pub.Publish(s.area.token("Updates"), msg.String())
if err != nil {
return err
}
return nil
}
func (s *streamImpl) onLeave() error {
// Notify user-join
join := newStreamUserMessage(s.user, false) // false means leave
_, err := s.pub.Publish(s.area.token("Updates"), join.String())
if err != nil {
return err
}
// Send join chat message
msg := newStreamChatMessage(s.area.leaveMsg(s.user))
_, err = s.pub.Publish(s.area.token("Updates"), msg.String())
if err != nil {
return err
}
return nil
}