-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
86 lines (74 loc) · 1.51 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
package main
import (
"fmt"
"os"
)
func main() {
// HACK - only let one seed job run, the one we care about for waypoint (?)
// only our main seed job will have an "http" port.
if os.Getenv("NOMAD_HOST_PORT_http") == "" && os.Getenv("NOMAD_ALLOC_INDEX") == "0" {
select {} // block forever instead of killing so nomad doesn't try to replace.
}
arg := "seed"
if len(os.Args) > 1 {
arg = os.Args[1]
}
SetGlobals()
seed := NewCell("0-0")
switch arg {
case "test":
SendUDP("hello there", seed)
case "run":
Run(seed)
case "pattern":
SetPattern(os.Args[2])
}
}
func Run(seed *Cell) {
self := NewCell(GetName())
isSeed := seed.Name() == self.Name()
logger.Info("self: " + self.Name())
if isSeed {
CacheAllCells()
go self.Listen()
go Ticker()
ApiListen()
} else {
self.Listen()
}
}
func GetName() (name string) {
// see vars.go for these globals
idx := AllocIdx
width := MaxWidth
var x, y int
if idx == 0 {
return "0-0"
}
x = idx % width
if x == 0 {
x = width
}
y = (idx-1)/width + 1
name = fmt.Sprintf("%d-%d", x, y)
return name
}
func CacheAllCells() {
for x := 1; x <= MaxWidth; x++ {
for y := 1; y <= MaxHeight; y++ {
c := NewCell(fmt.Sprintf("%d-%d", x, y))
AllCells = append(AllCells, c)
}
}
}
func SetPattern(p string) {
cdns := NewConsulDNS()
addr, err := cdns.GetServiceAddr("0-0-http")
if err != nil {
logger.Error("getting address", "err", err)
return
}
h := NewHTTP("http://" + addr)
_, body := h.Get(fmt.Sprintf("/p/%s", p))
logger.Info(string(body))
}