-
Notifications
You must be signed in to change notification settings - Fork 1
/
cell.go
273 lines (234 loc) · 5.65 KB
/
cell.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
package main
import (
"fmt"
"math/rand"
"net"
"strconv"
"strings"
"sync"
"time"
)
type Cell struct {
x int
y int
alive bool
addr string
n map[string]*Cell
cdns *ConsulDNS
mut sync.RWMutex
}
func NewCell(name string) *Cell {
bits := strings.Split(name, "-")
x, _ := strconv.Atoi(bits[0])
y, _ := strconv.Atoi(bits[1])
return &Cell{
x: x,
y: y,
alive: true,
cdns: NewConsulDNS(),
}
}
func (c *Cell) Name() string {
return fmt.Sprintf("%d-%d", c.x, c.y)
}
func (c *Cell) Service() string {
if c.IsSeed() {
return c.Name()
}
return fmt.Sprintf("cell-%d", c.Index())
}
func (c *Cell) Index() int {
width := MaxWidth
if c.x == 0 && c.y == 0 {
return 0
}
idx := c.x + width*c.y - width
return idx
}
func (c *Cell) IsSeed() bool {
return c.Name() == "0-0"
}
func (c *Cell) Address() (string, error) {
if c.addr != "" {
return c.addr, nil
}
// turns out dns is wayyyy faster than http
addr, err := c.cdns.GetServiceAddr(c.Service())
if err != nil {
logger.Error("getting address", "err", err)
return "", err
}
c.addr = addr
return addr, err
}
func (c *Cell) Neighbors() map[string]*Cell {
if c.n != nil {
return c.n
}
all := [8]*Cell{
// comments assuming cell "2-2"
// top row
NewCell(fmt.Sprintf("%d-%d", c.x-1, c.y-1)), // 1-1
NewCell(fmt.Sprintf("%d-%d", c.x, c.y-1)), // 2-1
NewCell(fmt.Sprintf("%d-%d", c.x+1, c.y-1)), // 3-1
// middle row
NewCell(fmt.Sprintf("%d-%d", c.x-1, c.y)), // 1-2
// 2-2 is self.
NewCell(fmt.Sprintf("%d-%d", c.x+1, c.y)), // 3-2
// bottom row
NewCell(fmt.Sprintf("%d-%d", c.x-1, c.y+1)), // 1-3
NewCell(fmt.Sprintf("%d-%d", c.x, c.y+1)), // 2-3
NewCell(fmt.Sprintf("%d-%d", c.x+1, c.y+1)), // 3-3
}
var valid = make(map[string]*Cell)
for _, n := range all {
if n.x < 1 || n.y < 1 || n.x > MaxWidth || n.y > MaxHeight {
continue
}
valid[n.Name()] = n
}
if c.Name() != "0-0" { // HACK
c.n = valid
}
return valid
}
func (c *Cell) Listen() (err error) {
addr := "0.0.0.0:" + UdpPort
conn, err := net.ListenPacket("udp", addr)
if err != nil {
logger.Error("starting udp server", "err", err)
return
}
defer conn.Close()
seed := NewCell("0-0")
cellReports := 0
tickStart := time.Now()
errChan := make(chan error, 1)
buf := make([]byte, 512)
go func() {
for {
n, dst, err := conn.ReadFrom(buf)
// "Callers should always process the n > 0 bytes returned before considering the error err." hm.
if err != nil {
errChan <- err
}
if buf[:n] == nil {
continue
}
msg := string(buf[:n])
start := time.Now()
Mut.Lock()
parts := strings.Split(msg, " ")
switch parts[0] {
case "ping":
break
case "tick":
c.Tick(seed, "")
case "pattern":
c.Tick(seed, parts[1])
default: // updates from neighbors
nName := parts[0]
nAlive := parts[1] == "true"
Statuses[nName] = nAlive
if c.IsSeed() {
cellReports += 1
if cellReports == MaxWidth*MaxHeight {
tickEnd := time.Now()
logger.Info("all cells reported to seed", "duration", tickEnd.Sub(tickStart))
tickStart = tickEnd
cellReports = 0
}
}
}
Mut.Unlock()
// respond to client
out := append(buf[:n], '\n') // add newline so Readline() in client SendUDP() knows end of response
i, err := conn.WriteTo(out, dst)
if err != nil {
logger.Error("responding to client",
"i", i,
"err", err)
}
end := time.Now()
logger.Debug("Listen",
"msg", string(buf[:n]),
"duration", end.Sub(start))
}
}()
logger.Info("udp listener started", "addr", addr)
select { // TODO: learn more about select
case err = <-errChan:
}
return
}
func (c *Cell) Tick(seed *Cell, p string) {
tickStart := time.Now()
// avoid race: wait for all cells to get the tick.
// it takes up to ~20ms for seed to finish 49 cells on laptop
// NOTE: SendUDP's Deadline must be longer than this*8.
sleep := time.Duration(MaxWidth * MaxHeight / 30) // TODO: hmmm.. magic.
if sleep < 25 {
sleep = 25
}
time.Sleep(sleep * time.Millisecond)
if p == "random" {
rand.Seed(time.Now().UnixNano())
c.alive = rand.Intn(3) > 1 // ~1/3 of the time
} else {
if !ApplyPattern(c, p) {
c.alive = c.GetNextLiveness()
}
}
c.UpdateNeighbors()
go c.Update(seed)
tickEnd := time.Now()
tickDelta := tickEnd.Sub(tickStart)
logger.Debug("tickDelta", "duration", tickDelta)
}
func (c *Cell) GetNextLiveness() bool {
totalAlive := 0
for _, n := range c.Neighbors() {
alive, ok := Statuses[n.Name()]
if !ok { // if any neighbor doesn't exist yet, default to alive
return true
}
if alive {
totalAlive++
}
}
logger.Info("GetNextLiveness", "totalAlive", totalAlive)
// Any live cell with two or three live neighbors lives on to the next generation.
// Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
// Any live cell with fewer than two live neighbors dies, as if by underpopulation.
// Any live cell with more than three live neighbors dies, as if by overpopulation.
beAlive := false
if c.alive == true {
beAlive = totalAlive == 2 || totalAlive == 3 // 2 or 3
} else {
beAlive = totalAlive == 3 // exactly 3
}
return beAlive
}
func (c *Cell) UpdateNeighbors() {
for _, n := range c.Neighbors() {
go c.Update(n)
}
}
func (c *Cell) Update(n *Cell) (err error) {
// send self status to a neighbor
maxSleep := MaxWidth * MaxHeight / 6
if maxSleep < 50 {
maxSleep = 50
}
jitter := rand.Intn(maxSleep)
sleep := time.Duration(jitter)
time.Sleep(sleep * time.Millisecond)
d := fmt.Sprintf("%s %t", c.Name(), c.alive)
err = SendUDP(d, n)
if err != nil {
logger.Error("updating neighbor",
"name", n.Name(),
"err", err)
}
return
}