Hochwasser/main.go

96 lines
2.4 KiB
Go
Raw Normal View History

2019-01-06 22:00:43 +01:00
package main
import (
"flag"
2019-01-11 17:29:00 +01:00
"image"
"log"
2019-01-06 22:00:43 +01:00
"net"
2019-01-11 17:29:00 +01:00
"os"
2019-01-06 22:00:43 +01:00
"runtime/pprof"
"time"
"github.com/SpeckiJ/Hochwasser/pixelflut"
2019-01-06 22:00:43 +01:00
)
var err error
var cpuprofile = flag.String("cpuprofile", "", "Destination file for CPU Profile")
2019-01-11 17:31:14 +01:00
var image_path = flag.String("image", "", "Absolute Path to image")
2019-01-06 22:00:43 +01:00
var image_offsetx = flag.Int("xoffset", 0, "Offset of posted image from left border")
var image_offsety = flag.Int("yoffset", 0, "Offset of posted image from top border")
var connections = flag.Int("connections", 4, "Number of simultaneous connections. Each connection posts a subimage")
2019-01-06 22:00:43 +01:00
var address = flag.String("host", "127.0.0.1:1337", "Server address")
2020-02-04 14:59:15 +01:00
var runtime = flag.String("runtime", "60s", "exit after timeout")
2019-01-11 18:27:29 +01:00
var shuffle = flag.Bool("shuffle", false, "pixel send ordering")
var fetchImgPath = flag.String("fetch-image", "", "path to save the fetched pixel state to")
2019-01-06 22:00:43 +01:00
func main() {
flag.Parse()
2019-01-11 17:31:14 +01:00
if *image_path == "" {
log.Fatal("No image provided")
2019-01-06 22:00:43 +01:00
}
2019-01-11 17:22:51 +01:00
// check connectivity by opening one test connection
conn, err := net.Dial("tcp", *address)
if err != nil {
log.Fatal(err)
}
conn.Close()
2019-01-06 22:00:43 +01:00
// Start cpu profiling if wanted
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
2020-02-04 14:59:15 +01:00
defer f.Close()
2019-01-06 22:00:43 +01:00
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
2019-01-11 17:31:14 +01:00
2020-02-05 12:02:21 +01:00
offset := image.Pt(*image_offsetx, *image_offsety)
img := readImage(*image_path)
fetchedImg := image.NewNRGBA(img.Bounds().Add(offset))
if *fetchImgPath != "" {
2020-02-06 01:01:05 +01:00
fetchCmds := pixelflut.CmdsFetchImage(fetchedImg.Bounds())
fetchMessages := fetchCmds.Chunk(1)
{
// @cleanup: encapsulate this in separate function exported from pixelflut
conn, err := net.Dial("tcp", *address)
if err != nil {
log.Fatal(err)
}
// defer conn.Close()
go pixelflut.FetchPixels(fetchedImg, conn)
go pixelflut.Bomb2(fetchMessages[0], conn)
}
*connections -= 1
}
2019-01-06 22:00:43 +01:00
// Generate and split messages into equal chunks
commands := pixelflut.CommandsFromImage(img, offset)
2019-01-11 18:27:29 +01:00
if *shuffle {
commands.Shuffle()
2019-01-11 18:27:29 +01:00
}
if *connections > 0 {
commandGroups := commands.Chunk(*connections)
for _, messages := range commandGroups {
go pixelflut.Bomb(messages, *address)
}
2019-01-06 22:00:43 +01:00
}
2020-02-04 14:59:15 +01:00
// Terminate after timeout to save resources
timer, err := time.ParseDuration(*runtime)
2019-01-06 22:00:43 +01:00
if err != nil {
log.Fatal("Invalid runtime specified: " + err.Error())
}
2020-02-04 14:59:15 +01:00
time.Sleep(timer)
if *fetchImgPath != "" {
writeImage(*fetchImgPath, fetchedImg)
}
2019-01-06 22:00:43 +01:00
}