-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathhandle.go
51 lines (42 loc) · 1.33 KB
/
handle.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
package main
import (
"fmt"
"sync"
"github.com/gautamrege/gochat/api"
)
// Ensure that users are added / removed using a mutex!
type PeerHandleMapSync struct {
sync.RWMutex
PeerHandleMap map[string]api.Handle
}
// Insert user if not exists already then add it
func (hs *PeerHandleMapSync) Insert(newHandle api.Handle) (err error) {
hs.Lock()
// TODO-WORKSHOP-STEP-3: This code should insert the newHandle into the PeerHandleMap
hs.Unlock()
return nil
}
// get the user details from the map with given name
func (hs *PeerHandleMapSync) Get(name string) (handle api.Handle, ok bool) {
hs.Lock()
// TODO-WORKSHOP-STEP-4: This code should fetch the handle from the PeerHandleMap based on the key name
// TODO-THINK: Why is this in a Lock() method?
hs.Unlock()
return
}
// delete the user from map
func (hs *PeerHandleMapSync) Delete(name string) {
hs.Lock()
// TODO-WORKSHOP-STEP-5: This code should remove the handle from the PeerHandleMap based on the key name
hs.Unlock()
fmt.Println("UserHandle Removed for ", name)
}
func String(h api.Handle) string {
return fmt.Sprintf("%s@%s:%d", h.Name, h.Host, h.Port)
}
func (hs PeerHandleMapSync) String() string {
var users string
// TODO-WORKSHOP-STEP-6: This code should print the list of all names of the handles in the PeerHandleMap
// TODO-THINK: Do we need a Lock here?
return users
}