@@ -39,11 +39,9 @@ func (cr *ChatRoom) Init(db *bolt.DB) {
3939 return
4040 }
4141
42- chat .clientsMtx .Lock ()
43- client , ok := cr .namedClients [to ]
44- chat .clientsMtx .Unlock ()
42+ client := getNamedClient (to )
4543
46- if ok == false {
44+ if client == nil {
4745 fmt .Println ("Can't find " + to )
4846 } else {
4947 which := message .Which
@@ -63,6 +61,45 @@ func (cr *ChatRoom) Init(db *bolt.DB) {
6361 }()
6462}
6563
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+ }
66103// Client
67104
68105type Client struct {
@@ -156,6 +193,7 @@ func wsHandler(w http.ResponseWriter, r *http.Request) {
156193 for {
157194 _ , data , err := conn .ReadMessage ()
158195 if err != nil {
196+ fmt .Println ("Error " , err )
159197 fmt .Println ("\n Connection closed for session " + sessionId )
160198 updatePresence (sessionId , false )
161199 return
@@ -202,7 +240,7 @@ func wsHandler(w http.ResponseWriter, r *http.Request) {
202240 case Haber_CALL_STOP :
203241 fallthrough
204242 case Haber_FILE :
205- if _ , ok := chat . clients [ sessionId ]; ok {
243+ if getClient ( sessionId ) != nil {
206244 forward (sessionId , haber )
207245 }
208246 }
@@ -211,14 +249,16 @@ func wsHandler(w http.ResponseWriter, r *http.Request) {
211249}
212250
213251func updatePresence (sessionId string , online bool ) {
214- if client , ok := chat .clients [sessionId ]; ok {
252+ client := getClient (sessionId )
253+
254+ if client != nil {
215255 if online == client .online {
216256 fmt .Printf ("updatePresence: %s is already %t\n " , client .name , client .online )
217257 return
218258 }
219259 fmt .Println ("updatePresence sessionId=" + sessionId )
220260 client .online = online
221- chat . clients [ sessionId ] = client
261+ setClient ( sessionId , client )
222262 // inform subscribers
223263 from := client .name
224264 fmt .Println ("\t from=" + from )
@@ -274,26 +314,23 @@ func remove(s []string, r string) []string {
274314
275315func receivedUsername (conn * websocket.Conn , username string ) string {
276316 fmt .Println ("\n receivedUsername: " + username )
277- defer chat .clientsMtx .Unlock ()
278- chat .clientsMtx .Lock ()
279317
280318 sessionId := createSessionId ()
281319
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 {
286322 client = & Client {
287323 name : username ,
288324 sessions : make (map [string ]* websocket.Conn ),
289325 online : false ,
290326 }
327+ setNamedClient (username , client )
328+ fmt .Println ("new client for " , username )
291329 }
292330 client .sessions [sessionId ] = conn
293- chat .namedClients [username ] = client
294331 fmt .Println ("new client name=" + client .name + " session=" + sessionId )
295332 client .Load (chat .db )
296- chat . clients [ sessionId ] = client
333+ setClient ( sessionId , client )
297334 sendContacts (client , sessionId )
298335 updatePresence (sessionId , true )
299336
@@ -302,8 +339,7 @@ func receivedUsername(conn *websocket.Conn, username string) string {
302339
303340func sendContacts (client * Client , sessionId string ) {
304341 for _ ,contact := range client .contacts {
305- _ ,ok := chat .namedClients [contact .Name ]
306- contact .Online = ok
342+ contact .Online = getNamedClient (contact .Name ) != nil
307343 }
308344
309345 buds := & Haber {
@@ -316,14 +352,14 @@ func sendContacts(client *Client, sessionId string) {
316352}
317353
318354func forward (sessionId string , haber * Haber ) {
319- sourceClient := chat . clients [ sessionId ]
355+ sourceClient := getClient ( sessionId )
320356 haber .From = sourceClient .name
321357 chat .queue <- * haber // forward to all devices with source's and destination's names
322358}
323359
324360func receivedContacts (sessionId string , haber * Haber ) {
325361 fmt .Println ("receivedContacts for session " + sessionId )
326- client := chat . clients [ sessionId ]
362+ client := getClient ( sessionId )
327363 client .contacts = haber .GetContacts ()
328364 client .subscribeToContacts ()
329365 client .Save (chat .db , haber )
0 commit comments