@@ -39,11 +39,9 @@ func (cr *ChatRoom) Init(db *bolt.DB) {
39
39
return
40
40
}
41
41
42
- chat .clientsMtx .Lock ()
43
- client , ok := cr .namedClients [to ]
44
- chat .clientsMtx .Unlock ()
42
+ client := getNamedClient (to )
45
43
46
- if ok == false {
44
+ if client == nil {
47
45
fmt .Println ("Can't find " + to )
48
46
} else {
49
47
which := message .Which
@@ -63,6 +61,45 @@ func (cr *ChatRoom) Init(db *bolt.DB) {
63
61
}()
64
62
}
65
63
64
+ func getClient (sessionId string ) (* Client ) {
65
+ var result * Client
66
+
67
+ //fmt.Println("getClient will lock")
68
+ chat .clientsMtx .Lock ()
69
+ result = chat .clients [sessionId ]
70
+ chat .clientsMtx .Unlock ()
71
+ //fmt.Println("getClient did lock")
72
+
73
+ return result
74
+ }
75
+
76
+ func setClient (sessionId string , client * Client ) {
77
+ //fmt.Println("setClient lock")
78
+ chat .clientsMtx .Lock ()
79
+ chat .clients [sessionId ] = client
80
+ chat .clientsMtx .Unlock ()
81
+ //fmt.Println("setClient unlock")
82
+ }
83
+
84
+ func getNamedClient (name string ) (* Client ) {
85
+ var result * Client
86
+
87
+ //fmt.Println("getNamedClient lock")
88
+ chat .clientsMtx .Lock ()
89
+ result = chat .namedClients [name ]
90
+ chat .clientsMtx .Unlock ()
91
+ //fmt.Println("getNamedClient unlock")
92
+
93
+ return result
94
+ }
95
+
96
+ func setNamedClient (name string , client * Client ) {
97
+ //fmt.Println("setNamedClient lock")
98
+ chat .clientsMtx .Lock ()
99
+ chat .namedClients [name ] = client
100
+ chat .clientsMtx .Unlock ()
101
+ //fmt.Println("setNamedClient unlock")
102
+ }
66
103
// Client
67
104
68
105
type Client struct {
@@ -156,6 +193,7 @@ func wsHandler(w http.ResponseWriter, r *http.Request) {
156
193
for {
157
194
_ , data , err := conn .ReadMessage ()
158
195
if err != nil {
196
+ fmt .Println ("Error " , err )
159
197
fmt .Println ("\n Connection closed for session " + sessionId )
160
198
updatePresence (sessionId , false )
161
199
return
@@ -202,7 +240,7 @@ func wsHandler(w http.ResponseWriter, r *http.Request) {
202
240
case Haber_CALL_STOP :
203
241
fallthrough
204
242
case Haber_FILE :
205
- if _ , ok := chat . clients [ sessionId ]; ok {
243
+ if getClient ( sessionId ) != nil {
206
244
forward (sessionId , haber )
207
245
}
208
246
}
@@ -211,14 +249,16 @@ func wsHandler(w http.ResponseWriter, r *http.Request) {
211
249
}
212
250
213
251
func updatePresence (sessionId string , online bool ) {
214
- if client , ok := chat .clients [sessionId ]; ok {
252
+ client := getClient (sessionId )
253
+
254
+ if client != nil {
215
255
if online == client .online {
216
256
fmt .Printf ("updatePresence: %s is already %t\n " , client .name , client .online )
217
257
return
218
258
}
219
259
fmt .Println ("updatePresence sessionId=" + sessionId )
220
260
client .online = online
221
- chat . clients [ sessionId ] = client
261
+ setClient ( sessionId , client )
222
262
// inform subscribers
223
263
from := client .name
224
264
fmt .Println ("\t from=" + from )
@@ -274,26 +314,23 @@ func remove(s []string, r string) []string {
274
314
275
315
func receivedUsername (conn * websocket.Conn , username string ) string {
276
316
fmt .Println ("\n receivedUsername: " + username )
277
- defer chat .clientsMtx .Unlock ()
278
- chat .clientsMtx .Lock ()
279
317
280
318
sessionId := createSessionId ()
281
319
282
- var client * Client
283
- if c , ok := chat .namedClients [username ]; ok {
284
- client = c
285
- } else {
320
+ client := getNamedClient (username )
321
+ if client == nil {
286
322
client = & Client {
287
323
name : username ,
288
324
sessions : make (map [string ]* websocket.Conn ),
289
325
online : false ,
290
326
}
327
+ setNamedClient (username , client )
328
+ fmt .Println ("new client for " , username )
291
329
}
292
330
client .sessions [sessionId ] = conn
293
- chat .namedClients [username ] = client
294
331
fmt .Println ("new client name=" + client .name + " session=" + sessionId )
295
332
client .Load (chat .db )
296
- chat . clients [ sessionId ] = client
333
+ setClient ( sessionId , client )
297
334
sendContacts (client , sessionId )
298
335
updatePresence (sessionId , true )
299
336
@@ -302,8 +339,7 @@ func receivedUsername(conn *websocket.Conn, username string) string {
302
339
303
340
func sendContacts (client * Client , sessionId string ) {
304
341
for _ ,contact := range client .contacts {
305
- _ ,ok := chat .namedClients [contact .Name ]
306
- contact .Online = ok
342
+ contact .Online = getNamedClient (contact .Name ) != nil
307
343
}
308
344
309
345
buds := & Haber {
@@ -316,14 +352,14 @@ func sendContacts(client *Client, sessionId string) {
316
352
}
317
353
318
354
func forward (sessionId string , haber * Haber ) {
319
- sourceClient := chat . clients [ sessionId ]
355
+ sourceClient := getClient ( sessionId )
320
356
haber .From = sourceClient .name
321
357
chat .queue <- * haber // forward to all devices with source's and destination's names
322
358
}
323
359
324
360
func receivedContacts (sessionId string , haber * Haber ) {
325
361
fmt .Println ("receivedContacts for session " + sessionId )
326
- client := chat . clients [ sessionId ]
362
+ client := getClient ( sessionId )
327
363
client .contacts = haber .GetContacts ()
328
364
client .subscribeToContacts ()
329
365
client .Save (chat .db , haber )
0 commit comments