forked from armon/go-chord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
200 lines (166 loc) · 4.66 KB
/
util.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package buddystore
import (
"bytes"
"fmt"
"math/big"
"math/rand"
"net"
"strconv"
"time"
"github.com/golang/glog"
"github.com/huin/goupnp/dcps/internetgateway1"
)
const LISTEN_TIMEOUT = 30 * time.Second
// Generates a random stabilization time
func randStabilize(conf *Config) time.Duration {
min := conf.StabilizeMin
max := conf.StabilizeMax
r := rand.Float64()
return time.Duration((r * float64(max-min)) + float64(min))
}
// Checks if a key is STRICTLY between two ID's exclusively
func between(id1, id2, key []byte) bool {
// Check for ring wrap around
if bytes.Compare(id1, id2) == 1 {
return bytes.Compare(id1, key) == -1 ||
bytes.Compare(id2, key) == 1
}
// Handle the normal case
return bytes.Compare(id1, key) == -1 &&
bytes.Compare(id2, key) == 1
}
// Checks if a key is between two ID's, right inclusive
func betweenRightIncl(id1, id2, key []byte) bool {
// Check for ring wrap around
if bytes.Compare(id1, id2) == 1 {
return bytes.Compare(id1, key) == -1 ||
bytes.Compare(id2, key) >= 0
}
return bytes.Compare(id1, key) == -1 &&
bytes.Compare(id2, key) >= 0
}
// Computes the offset by (n + 2^exp) % (2^mod)
func powerOffset(id []byte, exp int, mod int) []byte {
// Copy the existing slice
off := make([]byte, len(id))
copy(off, id)
// Convert the ID to a bigint
idInt := big.Int{}
idInt.SetBytes(id)
// Get the offset
two := big.NewInt(2)
offset := big.Int{}
offset.Exp(two, big.NewInt(int64(exp)), nil)
// Sum
sum := big.Int{}
sum.Add(&idInt, &offset)
// Get the ceiling
ceil := big.Int{}
ceil.Exp(two, big.NewInt(int64(mod)), nil)
// Apply the mod
idInt.Mod(&sum, &ceil)
// Add together
return idInt.Bytes()
}
// max returns the max of two ints
func max(a, b int) int {
if a >= b {
return a
} else {
return b
}
}
// min returns the min of two ints
func min(a, b int) int {
if a <= b {
return a
} else {
return b
}
}
// Returns the vnode nearest a key
func nearestVnodeToKey(vnodes []*Vnode, key []byte) *Vnode {
for i := len(vnodes) - 1; i >= 0; i-- {
if bytes.Compare(vnodes[i].Id, key) == -1 { // TODO : How does this promise "nearest Vnode to Key"?
return vnodes[i]
}
}
// Return the last vnode
return vnodes[len(vnodes)-1]
}
// Merges errors together
func mergeErrors(err1, err2 error) error {
if err1 == nil {
return err2
} else if err2 == nil {
return err1
} else {
return fmt.Errorf("%s\n%s", err1, err2)
}
}
func GetLocalExternalAddresses() (localAddr string, externalAddr string) {
upnpclient, _, err := internetgateway1.NewWANIPConnection1Clients()
if err == nil && len(upnpclient) > 0 {
externalAddr, err = upnpclient[0].GetExternalIPAddress()
glog.Infof("External IP: %s, err: %s", externalAddr, err)
} else {
glog.Infof("No external IP")
}
ifaces, err := net.Interfaces()
if err == nil {
for _, iface := range ifaces {
if iface.Flags&net.FlagLoopback == net.FlagLoopback {
continue
}
addrs, _ := iface.Addrs()
// TODO: Figure out how to handle IPv6 addresses.
if len(addrs) > 0 && addrs[0].(*net.IPNet).IP.To4() != nil {
glog.Infof("Local Address: %s", addrs[0].(*net.IPNet).IP.To4().String())
localAddr = addrs[0].(*net.IPNet).IP.String()
return
}
}
}
return
}
func CreateNewTCPTransport(localOnly bool) (int, Transport, *Config) {
return CreateNewTCPTransportWithConfig(localOnly, DefaultConfig)
}
func CreateNewTCPTransportWithConfig(localOnly bool, configGen func(string) *Config) (int, Transport, *Config) {
var localAddr, externalAddr string
var transport Transport
var err error = fmt.Errorf("Dummy error")
var port int
var listen string
for err != nil {
port = int(rand.Uint32()%(64512) + 1024)
glog.Infof("PORT: %d", port)
listen = net.JoinHostPort("0.0.0.0", strconv.Itoa(port))
glog.Infof("Listen Address: %s", listen)
transport, err = InitTCPTransport(listen, LISTEN_TIMEOUT)
}
if !localOnly {
localAddr, externalAddr = GetLocalExternalAddresses()
upnpclient, errs, err := internetgateway1.NewWANIPConnection1Clients()
glog.Infof("Client: %q, errors: %q, error: %q", upnpclient, errs, err)
if err == nil && len(upnpclient) > 0 {
err = upnpclient[0].AddPortMapping("", uint16(port), "TCP", uint16(port), localAddr, true, "BuddyStore", 0)
glog.Infof("Added port mapping: %s", err)
}
} else {
localAddr = "localhost"
externalAddr = "localhost"
}
conf := configGen(listen)
if len(externalAddr) > 0 {
conf.Hostname = net.JoinHostPort(externalAddr, strconv.Itoa(port))
} else {
conf.Hostname = net.JoinHostPort(localAddr, strconv.Itoa(port))
}
return port, transport, conf
}
func copyOfVnodesList(inList []*Vnode, n int) []*Vnode {
nodesList := make([]*Vnode, n)
copy(nodesList, inList[:n])
return nodesList
}