-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.go
142 lines (118 loc) · 2.42 KB
/
utils.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"net"
"sync"
"github.com/fatih/color"
)
const (
maxNode int = 301
maxData int = 2000
maxFail = 0.01
// config.Port int = 1111
)
var (
green = color.New(color.FgGreen)
red = color.New(color.FgRed)
blue = color.New(color.FgBlue)
)
var (
nodeGroup *[maxNode]dhtNode
nodeAddr *[maxNode]string
keyArray *[maxData]string
datalocal map[string]string
)
var (
wg *sync.WaitGroup
localIP string
)
var (
letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
totalFail int
totalCnt int
finalScore float64
// config map[string]interface{}
config configure
maxNodeSize int
maxDataSize int
)
type configure struct {
Frequency float64
Port int
}
func init() {
jsonBlob, err := ioutil.ReadFile("./config.json")
if err == nil {
err = json.Unmarshal(jsonBlob, &config)
}
if err != nil {
fmt.Println("error config json", err)
config.Frequency = 1.0
config.Port = 1111
}
totalFail = 0
totalCnt = 0
}
func getIP() string {
var localaddress string
ifaces, err := net.Interfaces()
if err != nil {
panic("init: failed to find network interfaces")
}
// find the first non-loopback interface with an IP address
for _, elt := range ifaces {
if elt.Flags&net.FlagLoopback == 0 && elt.Flags&net.FlagUp != 0 {
addrs, err := elt.Addrs()
if err != nil {
panic("init: failed to get addresses for network interface")
}
for _, addr := range addrs {
if ipnet, ok := addr.(*net.IPNet); ok {
if ip4 := ipnet.IP.To4(); len(ip4) == net.IPv4len {
localaddress = ip4.String()
break
}
}
}
}
}
if localaddress == "" {
panic("init: failed to find non-loopback interface with valid address on this node")
}
return localaddress
}
func randString(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
func toAddr(ip string, port int) string {
return fmt.Sprintf("%s:%d", ip, port)
}
type error struct {
e string
cnt int
all int
}
func (e *error) initInfo(msg string, failCnt int, testCnt int) {
e.e = msg
e.cnt = failCnt
e.all = testCnt
}
func (e *error) printlnError() {
if e.cnt > 0 {
red.Printf("%s error : %.4f\n", e.e, float64(e.cnt)/float64(e.all))
} else {
green.Printf("%s Passed\n", e.e)
}
}
func (e *error) finish() {
totalCnt += e.all
totalFail += e.cnt
e.printlnError()
}