-
Notifications
You must be signed in to change notification settings - Fork 1
/
mapsafemapconn.go
176 lines (155 loc) · 4.1 KB
/
mapsafemapconn.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
package shoset
import (
"fmt"
"os"
"sync"
"github.com/spf13/viper"
)
// MapSafeMapConn : simple key map safe for goroutines...
type MapSafeMapConn struct {
m map[string]*MapSafeConn
sync.Mutex
ConfigName string
viperConfig *viper.Viper
}
// NewMapSafeMapConn : constructor
func NewMapSafeMapConn() *MapSafeMapConn {
m := new(MapSafeMapConn)
m.m = make(map[string]*MapSafeConn)
return m
}
// Get : Get a value from a MapSafeMapConn
func (m *MapSafeMapConn) Get(key string) *MapSafeConn {
m.Lock()
defer m.Unlock()
return m.m[key]
}
func (m *MapSafeMapConn) GetConfig() ([]string, []string) {
m.Lock()
defer m.Unlock()
return m._getConfig()
}
func (m *MapSafeMapConn) _getConfig() ([]string, []string) {
return m.viperConfig.GetStringSlice("join"), m.viperConfig.GetStringSlice("link")
}
// Set : assign a value to a MapSafeMapConn
func (m *MapSafeMapConn) Set(lname, key, protocolType, shosetType string, value *ShosetConn) *MapSafeMapConn {
m.Lock()
defer m.Unlock()
value.ch.LnamesByProtocol.Set(protocolType, lname)
value.ch.LnamesByType.Set(shosetType, lname)
if lname != "" && key != "" {
if m.m[lname] == nil {
m.m[lname] = NewMapSafeConn()
}
m.m[lname].Set(key, value)
}
keys := m.m[lname].Keys("out")
if len(keys) != 0 {
m.updateFile(lname, protocolType, keys)
}
return m
}
// Delete : delete a value in a MapSafeMapConn
func (m *MapSafeMapConn) Delete(lname, key string) {
m.Lock()
// var address string
_, ok := m.m[lname]
var lNamesByProtocol *MapSafeStrings
if ok {
shosetConn := m.m[lname].Get(key)
if shosetConn != nil {
// address = shosetConn.ch.GetBindAddress()
// fmt.Println(address, " enter delete")
lNamesByProtocol = shosetConn.ch.LnamesByProtocol
}
// if address == "127.0.0.1:8004" || address == "127.0.0.1:8002" {
// fmt.Println(address, " delete : ", key)
// }
m.m[lname].Delete(key)
}
var protocolTypes []string
if lNamesByProtocol != nil {
lNamesByProtocol.Iterate(
func(protocol string, lNames map[string]bool) {
// if lname in lNames
if lNames[lname] && protocol == "bye" {
// if address == "127.0.0.1:8004" || address == "127.0.0.1:8002" {
// fmt.Println(address, " update file")
// }
remotesToJoin, remotesToLink := m._getConfig()
// fmt.Println(remotesToJoin, remotesToLink)
if contains(remotesToJoin, key) {
protocolTypes = append(protocolTypes, "join")
}
if contains(remotesToLink, key) {
protocolTypes = append(protocolTypes, "link")
}
keys := m.m[lname].Keys("out")
for _, protocolType := range protocolTypes {
m.updateFile(lname, protocolType, keys)
}
}
},
)
}
m.Unlock()
}
func (m *MapSafeMapConn) updateFile(lname, protocolType string, keys []string) {
// if address == "127.0.0.1:8004" || address == "127.0.0.1:8002" {
// fmt.Println(address, "keys : ", keys)
// }
if m.ConfigName != "" {
m.viperConfig.Set(protocolType, keys)
dirname, err := os.UserHomeDir()
if err != nil {
fmt.Println(err)
}
m.viperConfig.WriteConfigAs(dirname + "/.shoset_config/" + m.ConfigName + ".yaml")
}
}
func (m *MapSafeMapConn) SetConfigName(name string) {
if name != "" {
m.ConfigName = name
}
}
// Iterate : iterate through MapSafeMapConn Values using a function
func (m *MapSafeMapConn) Iterate(lname string, iter func(string, *ShosetConn)) {
m.Lock()
mapConn := m.m[lname]
if mapConn != nil {
mapConn.Iterate(iter)
}
m.Unlock()
}
func (m *MapSafeMapConn) IterateAll(iter func(string, *ShosetConn)) {
m.Lock()
for _, lname := range m._keys() {
mapConn := m.m[lname]
if mapConn != nil {
mapConn.Iterate(iter)
}
}
m.Unlock()
}
// Len : return length of the map
func (m *MapSafeMapConn) Len() int {
return len(m.m)
}
func (m *MapSafeMapConn) SetViper(viperConfig *viper.Viper) {
m.viperConfig = viperConfig
}
func (m *MapSafeMapConn) _keys() []string { // list of logical names inside ConnsByName
lName := make([]string, m.Len())
i := 0
for key := range m.m {
lName[i] = key
i++
}
return lName[:i]
}
func (m *MapSafeMapConn) Keys() []string { // list of logical names inside ConnsByName
m.Lock()
defer m.Unlock()
return m._keys()
}