forked from ajmadsen/jingleping-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
229 lines (197 loc) · 4.96 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
package main
import (
"flag"
"fmt"
"image"
"log"
"math/rand"
"net"
"os"
"os/signal"
"time"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"golang.org/x/net/icmp"
"golang.org/x/net/ipv6"
)
var (
dstNetFlag = flag.String("dst-net", "2001:610:1908:a000", "the destination network of the ipv6 tree")
imageFlag = flag.String("image", "", "the image to ping to the tree")
xOffFlag = flag.Int("x", 0, "the x offset to draw the image")
yOffFlag = flag.Int("y", 0, "the y offset to draw the image")
rateFlag = flag.Int("rate", 5, "how many times to draw the image per second")
workersFlag = flag.Int("workers", 1, "the number of workers to use")
onceFlag = flag.Bool("once", false, "abort after 1 loop")
)
const (
maxX = 1920
maxY = 1080
)
// filled on package initialization. Contains a simple ICMPv6 ECHO request.
var pingPacket []byte
// worker drains the incoming channel, sending ping packets to the incoming
// addresses.
func worker(ch <-chan *net.IPAddr) {
log.Printf("starting worker")
for {
c, err := icmp.ListenPacket("ip6:ipv6-icmp", "::")
if err != nil {
log.Fatalf("could not open ping socket: %s", err)
}
for a := range ch {
_, err = c.WriteTo(pingPacket, a)
if err != nil {
log.Printf("warning: could not send ping packet: %s", err)
c2, err := icmp.ListenPacket("ip6:ipv6-icmp", "::")
if err != nil {
log.Fatalf("could not open ping socket: %s", err)
} else {
c.Close()
c = c2
}
}
}
}
}
// fill fills the pixel channel with the frame(s) of desired image. Each frame
// has its own delay, which the filler uses to time consecutive frames. The
// filler loops forever.
func fill(ch chan<- *net.IPAddr, frames [][]*net.IPAddr, delay []time.Duration, rate int) {
for {
Frame:
for fidx, frame := range frames {
// frame clock
ticker := time.NewTimer(delay[fidx])
for {
repeat := time.NewTimer(time.Second / time.Duration(rate))
for _, a := range frame {
ch <- a
}
if *onceFlag {
for 0 != len(ch) {
time.Sleep(1 * time.Second)
}
exit()
return
}
// then wait on both
select {
case <-ticker.C:
continue Frame
case <-repeat.C:
}
}
}
}
}
func shuffle(a []*net.IPAddr) {
for i := range a {
j := rand.Intn(i + 1)
a[i], a[j] = a[j], a[i]
}
}
// makeAddrs takes an image or frame, along with the destination network of the
// display board and desired offset for the image, and yields a list of
// addresses to ping to draw the image to the board.
func makeAddrs(img image.Image, dstNet string, xOff, yOff int) []*net.IPAddr {
var addrs []*net.IPAddr
tip := net.ParseIP(fmt.Sprintf("%s::", dstNet))
bounds := img.Bounds()
for y := 0; y < bounds.Dy(); y++ {
for x := 0; x < bounds.Dx(); x++ {
r, g, b, a := img.At(bounds.Min.X+x, bounds.Min.Y+y).RGBA()
a = a >> 8
if a > 0 {
// Each channel is 16-bit, just shift down for 8-bit needed
ip := make(net.IP, len(tip))
copy(ip, tip)
// x
ip[8] = byte((x + xOff) >> 8)
ip[9] = byte(x + xOff)
// y
ip[10] = byte((y + yOff) >> 8)
ip[11] = byte(y + yOff)
// rgba
ip[12] = byte(b >> 8)
ip[13] = byte(g >> 8)
ip[14] = byte(r >> 8)
ip[15] = uint8(a)
addrs = append(addrs, &net.IPAddr{
IP: ip,
})
}
}
}
// os.Exit(0)
shuffle(addrs)
return addrs
}
func main() {
flag.Parse()
if *imageFlag == "" {
fmt.Fprintln(os.Stderr, "the image flag must be provided")
os.Exit(1)
}
var delays []time.Duration
var frames [][]*net.IPAddr
var qLen int
// Read the image frame(s), convert frames to addresses. Ensure everything
// image related is cleaned up ASAP so we don't hold on to pixels we don't
// need.
{
var imgs []image.Image
{
f, err := os.Open(*imageFlag)
if err != nil {
log.Fatalf("could not open image: %s", err)
}
defer f.Close()
imgs, delays, err = decodeImage(f)
if err != nil {
log.Fatalf("could not decode image: %s", err)
}
}
bounds := imgs[0].Bounds()
log.Printf("image bounds: %d %d", bounds.Dx(), bounds.Dy())
for _, img := range imgs {
addrs := makeAddrs(img, *dstNetFlag, *xOffFlag, *yOffFlag)
if len(addrs) > qLen {
qLen = len(addrs)
}
frames = append(frames, addrs)
}
}
// If delay isn't set at this point, we just have one image. Use the
// provided flag to determine how many times to draw the image per second.
if delays == nil {
delays = []time.Duration{time.Second / time.Duration(*rateFlag)}
}
log.Printf("queue length: %d", qLen)
pixCh := make(chan *net.IPAddr, qLen)
go fill(pixCh, frames, delays, *rateFlag)
for i := 0; i < *workersFlag; i++ {
go worker(pixCh)
}
// wait for interruption
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt)
<-ch
log.Println("exiting...")
}
// Setup ping packet
func init() {
var err error
p := &icmp.Message{
Type: ipv6.ICMPTypeEchoRequest,
Code: 0,
Body: &icmp.Echo{
ID: 0xFFFF,
Seq: 1,
},
}
pingPacket, err = p.Marshal(nil)
if err != nil {
panic(err)
}
}