-
Notifications
You must be signed in to change notification settings - Fork 0
/
talkers.go
201 lines (158 loc) · 2.93 KB
/
talkers.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
package main
import (
"websocket"
"time"
"rand"
"strconv"
)
type TalkUser struct {
ws *websocket.Conn
c chan string
t chan bool
salt string
exit bool
}
type TalkChannel []uint64
var talkUsers = map[uint64] *TalkUser {}
var talkChannels = map[uint64] TalkChannel {}
var globalStore = map[string] string {}
var currentUserIdx uint64 = 0
var currentChannelIdx uint64 = 0
func NewChannel() uint64 {
ch := make(TalkChannel, 0, 5000)
// INICIA CODIGO CRITICO
currentChannelIdx += 1
myIdx := currentChannelIdx
// TERMINA CODIGO CRITICO
talkChannels[myIdx] = ch
return myIdx
}
func KillChannel(id uint64) {
ch := talkChannels[id]
if ch == nil {
return
}
talkChannels[id] = nil, false
}
func JoinChannel(id uint64, uid uint64) {
ch := talkChannels[id]
if ch == nil {
return
}
usr := talkUsers[uid]
if usr == nil {
return
}
newCh := append(ch, uid)
talkChannels[id] = newCh
}
func LeaveChannel(id uint64, uid uint64) {
ch := talkChannels[id]
if ch == nil {
return
}
for i := 0; i < len(ch); i++ {
newCh := append(ch[0 : i], ch[i + 1 :]...)
talkChannels[id] = newCh
break
}
}
func SendToChannel(id uint64, message string) {
ch := talkChannels[id]
if ch == nil {
return
}
for i := 0; i < len(ch); i++ {
SendToUser(ch[i], message)
}
}
func SendToUser(id uint64, message string) {
usr := talkUsers[id]
if usr == nil {
return
}
usr.c <- message
}
func NewUser() string {
usr := new(TalkUser)
usr.c = make(chan string)
usr.t = make(chan bool)
usr.exit = false
usr.salt = strconv.Itoa(rand.Int())
go UserSocket(usr)
go UserTimeout(usr)
// INICIA CODIGO CRITICO
currentUserIdx += 1
myIdx := currentUserIdx
// TERMINA CODIGO CRITICO
talkUsers[myIdx] = usr
// Join to the Global Channel
JoinChannel(1, myIdx)
return strconv.Uitoa64(myIdx) + "!" + usr.salt
}
func KillUser(id uint64) {
usr := talkUsers[id]
if usr == nil {
return
}
usr.exit = true
talkUsers[id] = nil, false
}
func LinkUser(id uint64, salt string, ws *websocket.Conn) (*TalkUser) {
usr := talkUsers[id]
if usr == nil {
return nil
}
UnlinkUser(id, usr.ws)
if usr.ws == nil && usr.salt == salt {
usr.ws = ws
return usr
}
return nil
}
func UnlinkUser(id uint64, ws *websocket.Conn) {
usr := talkUsers[id]
if usr == nil {
return
}
if usr.ws == ws {
if usr.ws != nil {
usr.ws.Close()
}
usr.ws = nil
}
}
func UserSocket(usr *TalkUser) {
for usr.exit == false {
if usr.ws == nil {
time.Sleep(1e8)
continue
}
select {
case <-usr.t:
continue
case m := <-usr.c:
if usr.ws != nil {
_, err := usr.ws.Write([]byte(m))
if err != nil {
usr.ws = nil
}
}
}
}
}
func UserTimeout(usr *TalkUser) {
for usr.exit == false {
time.Sleep(1e8)
usr.t <- false
}
}
func Store(key, value string) {
globalStore[key] = value
}
func Clear(key string) {
globalStore[key] = "", false
}
func Retrieve(key string) string {
return globalStore[key]
}