-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
162 lines (141 loc) · 3.01 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
// SPDX-License-Identifier: EUPL-1.2
package main
import (
"flag"
"fmt"
"image"
"image/color"
_ "image/gif" // unused imports to register image decoders
_ "image/jpeg"
_ "image/png"
"log"
"math/rand"
"net"
"os"
"os/signal"
"strings"
"sync"
"time"
)
type pfPixel struct {
p image.Point
c color.Color
}
func (px pfPixel) String() string {
r, g, b, a := px.c.RGBA()
x := px.p.X
y := px.p.Y
var as string
if byte(a) != 0xff {
as = fmt.Sprintf("%02x", byte(a))
}
return fmt.Sprintf("PX %d %d %02x%02x%02x%s\n",
x, y, byte(r), byte(g), byte(b), as)
}
var (
fDeterm = flag.Bool(
"deterministic", false, "initialise RNG deterministically")
fHost = flag.String(
"host", "localhost:1234", "host and port to connect to")
fImage = flag.String("image", "image.png", "image file name")
fN = flag.Int("n", 1, "number of concurrent routines")
fOnce = flag.Bool("once", false, "only run once")
fX = flag.Int("x", 0, "start of the image (x)")
fY = flag.Int("y", 0, "start of the image (y)")
)
func connWorker(wg *sync.WaitGroup, work chan []byte, counter chan int) {
defer wg.Done()
for {
conn, err := net.Dial("tcp", *fHost)
if err != nil {
log.Print(err)
continue
}
log.Println("connected")
defer conn.Close()
for w := range work {
len, err := conn.Write(w)
log.Printf("wrote %d bytes", len)
counter <- len
if err != nil {
log.Print(err)
break
}
}
_, more := <-work
if !more {
// this means work has been closed and we should quit
return
}
// … else there has been some error and we restart the connection
}
}
func main() {
flag.Parse()
if !*fDeterm {
t := time.Now().UnixNano()
log.Println("rand seed:", t)
rand.Seed(t)
}
reader, err := os.Open(*fImage)
if err != nil {
log.Panic(err)
}
img, _, err := image.Decode(reader)
if err != nil {
log.Panic(err)
}
reader.Close()
min := img.Bounds().Min
max := img.Bounds().Max
pxs := make([]pfPixel, 0, img.Bounds().Dx()*img.Bounds().Dy())
offset := image.Pt(*fX, *fY)
for x := min.X; x < max.X; x++ {
for y := min.Y; y < max.Y; y++ {
pxs = append(pxs, pfPixel{
image.Pt(x, y).Add(offset), img.At(x, y)})
}
}
log.Printf("image has %d pixels", len(pxs))
var b strings.Builder
for _, i := range rand.Perm(len(pxs)) {
b.WriteString(pxs[i].String())
}
log.Println("length of pixel data:", len(b.String()))
counter := make(chan int)
final := make(chan uint64)
go func() {
var bytes uint64
for c := range counter {
bytes += uint64(c)
}
final <- bytes
}()
var wg sync.WaitGroup
work := make(chan []byte, *fN)
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt)
go func() {
for {
select {
case s := <-sig:
log.Println("got signal", s)
close(work)
return
case work <- []byte(b.String()):
if *fOnce {
close(work)
return
}
}
}
}()
for i := 0; i < *fN; i++ {
wg.Add(1)
go connWorker(&wg, work, counter)
}
wg.Wait()
close(counter)
bytes := <-final / (1 << 20)
log.Printf("total written: %v MiB", bytes)
}