-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmapsafeconn.go
More file actions
102 lines (89 loc) · 1.91 KB
/
mapsafeconn.go
File metadata and controls
102 lines (89 loc) · 1.91 KB
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
package shoset
import (
"sync"
)
// MapSafeConn : simple key map safe for goroutines...
type MapSafeConn struct {
m map[string]*ShosetConn
sync.Mutex
}
// NewMapSafeConn : constructor
func NewMapSafeConn() *MapSafeConn {
m := new(MapSafeConn)
m.m = make(map[string]*ShosetConn)
return m
}
// Get : Get Map
func (m *MapSafeConn) GetM() map[string]*ShosetConn {
m.Lock()
defer m.Unlock()
return m.m
}
// Get : Get a value from a MapSafeConn
func (m *MapSafeConn) GetByType(shosetType string) []*ShosetConn {
var result []*ShosetConn
m.Lock()
for _, val := range m.m {
if val.GetRemoteShosetType() == shosetType {
result = append(result, val)
}
}
m.Unlock()
return result
}
// Get : Get a value from a MapSafeConn
func (m *MapSafeConn) Get(key string) *ShosetConn {
m.Lock()
defer m.Unlock()
return m.m[key]
}
// Set : assign a value to a MapSafeConn
func (m *MapSafeConn) Set(key string, value *ShosetConn) *MapSafeConn {
m.Lock()
// fmt.Println("Address set")
m.m[key] = value
m.Unlock()
return m
}
func (m *MapSafeConn) _keys(dir string) []string { // list of addresses
addresses := make([]string, m.Len()+1)
i := 0
for key := range m.m {
if dir != "all" {
if m.m[key].GetDir() == dir { // on ne veut pas le in du join
addresses[i] = key
i++
}
} else {
addresses[i] = key
i++
}
}
return addresses[:i]
}
func (m *MapSafeConn) Keys(dir string) []string { // list of addresses
m.Lock()
defer m.Unlock()
return m._keys(dir)
}
// Delete : delete a value in a MapSafeConn
func (m *MapSafeConn) Delete(key string) {
m.Lock()
_, ok := m.m[key]
if ok {
delete(m.m, key)
}
m.Unlock()
}
// Iterate : iterate through MapSafeConn Values using a function
func (m *MapSafeConn) Iterate(iter func(string, *ShosetConn)) {
m.Lock()
for key, val := range m.m {
iter(key, val)
}
m.Unlock()
}
// Len : return length of the map
func (m *MapSafeConn) Len() int {
return len(m.m)
}