-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
307 lines (263 loc) · 6.16 KB
/
main.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package main
import (
"os"
"runtime/pprof"
"strconv"
"strings"
"sync"
"time"
"code.google.com/p/go.net/context"
config "github.com/jbenet/go-ipfs/repo/config"
u "github.com/jbenet/go-ipfs/util"
"flag"
"bufio"
"encoding/json"
"errors"
"fmt"
"runtime"
)
var _ = json.Decoder{}
var ErrArgCount = errors.New("not enough arguments")
// Test config represents a test configuration
// right now, its just the number of nodes...
// TODO: decide if its still worth keeping a struct around
type testConfig struct {
NumNodes int
}
type nodeBWInfo struct {
BwIn, BwOut uint64
MesSend, MesRecv uint64
}
type transferInfo struct {
Size int
Time int64
Speed float64
}
type Statistics struct {
BwStats []nodeBWInfo
Transfers []transferInfo
}
var gslock sync.Mutex
var globalStats Statistics
func ExecConfigLine(s string) bool {
if len(s) > 0 && s[0] == '#' {
return false
}
if s == "--" {
return true
}
if strings.Contains(s, "->") {
parts := strings.Split(s, "->")
lrange, err := ParseRange(parts[0])
if err != nil {
fmt.Printf("Error parsing range: %s\n", err)
return false
}
rrange, err := ParseRange(parts[1])
if err != nil {
fmt.Printf("Error parsing range: %s\n", err)
return false
}
for _, n := range lrange {
for _, r := range rrange {
BootstrapTo(configs[n], configs[r])
}
}
bootstrappingSet = true
} else if strings.HasPrefix(s, "off") {
parts := strings.Split(s, " ")
if len(parts) < 2 {
fmt.Printf("Syntax error, no range given!\n")
return false
}
rng, err := ParseRange(parts[1])
if err != nil {
fmt.Printf("Syntax error: %s\n", err)
return false
}
for _, v := range rng {
disabledAtStart[v] = true
}
} else {
fmt.Printf("Invalid Syntax for setup: '%s'\n", s)
return false
}
return false
}
func ParseCommandFile(finame string, cfg *testConfig) (*bufio.Scanner, error) {
fi, err := os.Open(finame)
if err != nil {
return nil, err
}
scan := bufio.NewScanner(fi)
if !scan.Scan() {
return nil, errors.New("Invalid file syntax! first line must be num nodes")
}
num, err := strconv.Atoi(scan.Text())
if err != nil {
return nil, err
}
cfg.NumNodes = num
SetupNConfigs(cfg)
for scan.Scan() {
if ExecConfigLine(scan.Text()) {
goto out
}
}
// If we read through the whole file as config, set to read commands from stdin
// XXX: hacky
scan = nil
out:
// If no bootstrapping options selected, everyone bootstraps with node 0
if !bootstrappingSet {
fmt.Println("Setting default bootstrapping config.")
for i := 1; i < len(configs); i++ {
BootstrapTo(configs[i], configs[0])
}
}
return scan, nil
}
func SetupNConfigs(c *testConfig) {
disabledAtStart = make([]bool, c.NumNodes)
for i := 0; i < c.NumNodes; i++ {
ncfg := BuildConfig(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", 10000+i))
if setuprpc {
ncfg.Addresses.API = fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", 9000+i)
}
configs = append(configs, ncfg)
}
}
func ConfigPrompt(scan *bufio.Scanner) error {
fmt.Println("Please enter number of nodes:")
if !scan.Scan() {
return errors.New("not enough input!")
}
nnum := scan.Text()
n, err := strconv.Atoi(nnum)
if err != nil {
return err
}
c := new(testConfig)
c.NumNodes = n
SetupNConfigs(c)
fmt.Println("Enter bootstrapping config: ('--' to stop)")
for scan.Scan() {
if ExecConfigLine(scan.Text()) {
break
}
}
return nil
}
func SetupNodes(master context.Context) {
controllers = make([]NodeController, len(configs))
for i, ncfg := range configs {
if !disabledAtStart[i] {
nd := nodeFromConfig(master, ncfg)
controllers[i] = &localNode{nd}
}
}
fmt.Println("Finished DHT creation.")
}
// global array of nodes, because im lazy and hate passing things to functions
var controllers []NodeController
var configs []*config.Config
var disabledAtStart []bool
var setuprpc bool
var bootstrappingSet bool
var logquiet bool
var masterCtx context.Context
func main() {
cmdfile := flag.String("f", "", "a file of commands to run")
serv := flag.String("s", "", "address to run d3 viz server on")
rpc := flag.Bool("r", false, "whether or not to turn on rpc")
def := flag.Bool("default", false, "whether or not to load default config")
ins := flag.Bool("inspect", false, "whether or not to inspect stack afterwards")
quiet := flag.Bool("q", false, "supress obnoxious log messages")
flag.Parse()
logquiet = *quiet
setuprpc = *rpc
u.Debug = true
runtime.GOMAXPROCS(10)
if *serv != "" {
go RunServer(*serv)
}
// Setup Configuration and inputs
var scan *bufio.Scanner
testconf := new(testConfig)
if *cmdfile != "" {
fiscan, err := ParseCommandFile(*cmdfile, testconf)
if err != nil {
fmt.Println(err)
return
}
scan = fiscan
} else {
scan = bufio.NewScanner(os.Stdin)
if *def { // Default configuration
testconf.NumNodes = 15
SetupNConfigs(testconf)
for _, cfg := range configs[1:] {
BootstrapTo(cfg, configs[0])
}
} else {
ConfigPrompt(scan)
if scan.Err() != nil {
fmt.Printf("Scan error: %s\n", scan.Err())
}
}
}
ctx, cancel := context.WithCancel(context.TODO())
masterCtx = ctx
// Build ipfs nodes as specified by the global array of configurations
SetupNodes(ctx)
defer func() {
fi, err := os.Create("mem.prof")
if err != nil {
panic(err)
}
pprof.WriteHeapProfile(fi)
fi.Close()
}()
fi, err := os.Create("cpu.prof")
if err != nil {
panic(err)
}
defer fi.Close()
pprof.StartCPUProfile(fi)
defer pprof.StopCPUProfile()
// Begin command execution
fmt.Println("Enter a command:")
for scan.Scan() {
if len(scan.Text()) == 0 {
continue
}
// ignore comments
if len(scan.Text()) > 0 && scan.Text()[0] == '#' {
continue
}
if scan.Text() == "==" {
// Switch over input to standard in
scan = bufio.NewScanner(os.Stdin)
continue
}
if !RunCommand(scan.Text()) {
return
}
}
cancel()
fmt.Println("Cleaning up and printing bandwidth(I/O)")
/*
for _, c := range controllers {
globalStats.BwStats = append(globalStats.BwStats, c.GetStatistics())
}
gsjson, err := json.MarshalIndent(globalStats, "", "\t")
if err != nil {
panic(err)
}
fmt.Println(string(gsjson))
*/
if *ins {
time.Sleep(time.Second * 2)
panic("lets take a look at things.")
}
}