Hochwasser/main.go

79 lines
2.3 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"
"github.com/SpeckiJ/Hochwasser/rpc"
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")
var rán = flag.String("rán", "", "enable rpc server to distribute jobs, listening on the given address/port")
var hevring = flag.String("hevring", "", "connect to rán rpc server at given address")
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
if *rán != "" { // @fixme: should validate proper address?
2020-02-11 14:46:58 +01:00
rpc.SummonRán(*rán)
}
if *hevring != "" { // @fixme: should validate proper address?
rpc.ConnectHevring(*hevring)
}
2020-02-05 12:02:21 +01:00
offset := image.Pt(*image_offsetx, *image_offsety)
img := readImage(*image_path)
if *fetchImgPath != "" {
2020-02-11 14:46:58 +01:00
fetchedImg := pixelflut.FetchImage(img.Bounds().Add(offset), *address, 1)
2020-02-06 01:01:05 +01:00
*connections -= 1
2020-02-11 14:46:58 +01:00
defer writeImage(*fetchImgPath, fetchedImg)
}
// 🌊🌊🌊🌊🌊🌊🌊🌊🌊🌊🌊🌊🌊🌊🌊
pixelflut.Flut(img, offset, *shuffle, *address, *connections)
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)
2019-01-06 22:00:43 +01:00
}