From 9071a1800a23f12b54097ec14c62e3c434bd7cf5 Mon Sep 17 00:00:00 2001 From: Norwin Date: Sun, 2 Jan 2022 08:34:51 +0100 Subject: [PATCH] add -hevring-preview flag enables storing of the current task image to a local file --- main.go | 24 +++++++++++++----------- rpc/dottir.go | 27 ++++++++++++++++++++++----- 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/main.go b/main.go index 7bc0e56..7ecba43 100644 --- a/main.go +++ b/main.go @@ -18,16 +18,17 @@ import ( ) var ( - imgPath = flag.String("image", "", "Filepath of an image to flut") - ránAddr = flag.String("rán", "", "Start RPC server to distribute jobs, listening on the given address/port") - hevringAddr = flag.String("hevring", "", "Connect to PRC server at given address/port") - address = flag.String("host", ":1234", "Target server address") - connections = flag.Int("connections", 4, "Number of simultaneous connections. Each connection posts a subimage") - x = flag.Int("x", 0, "Offset of posted image from left border") - y = flag.Int("y", 0, "Offset of posted image from top border") - order = flag.String("order", "rtl", "Draw order (shuffle, ltr, rtl, ttb, btt)") - fetchImgPath = flag.String("fetch", "", "Enable fetching the screen area to the given local file, updating it each second") - cpuprofile = flag.String("cpuprofile", "", "Destination file for CPU Profile") + imgPath = flag.String("image", "", "Filepath of an image to flut") + ránAddr = flag.String("rán", "", "Start RPC server to distribute jobs, listening on the given address/port") + hevringAddr = flag.String("hevring", "", "Connect to RPC server at given address/port") + address = flag.String("host", ":1234", "Target server address") + connections = flag.Int("connections", 4, "Number of simultaneous connections. Each connection posts a subimage") + x = flag.Int("x", 0, "Offset of posted image from left border") + y = flag.Int("y", 0, "Offset of posted image from top border") + order = flag.String("order", "rtl", "Draw order (shuffle, ltr, rtl, ttb, btt)") + fetchImgPath = flag.String("fetch", "", "Enable fetching the screen area to the given local file, updating it each second") + hevringImgPath = flag.String("hevring-preview", "", "Write the current task image to the given PNG file") + cpuprofile = flag.String("cpuprofile", "", "Destination file for CPU Profile") ) func main() { @@ -83,7 +84,8 @@ func taskFromFlags(stop chan bool, wg *sync.WaitGroup) { } if startClient { - rpc.ConnectHevring(hev, stop, wg) + hevring := rpc.ConnectHevring(hev, stop, wg) + hevring.PreviewPath = *hevringImgPath } if fetchImg { diff --git a/rpc/dottir.go b/rpc/dottir.go index 21b13b5..5e6a670 100644 --- a/rpc/dottir.go +++ b/rpc/dottir.go @@ -2,6 +2,7 @@ package rpc import ( "fmt" + "image" "log" "net" "net/rpc" @@ -9,9 +10,10 @@ import ( "time" "github.com/SpeckiJ/Hochwasser/pixelflut" + "github.com/SpeckiJ/Hochwasser/render" ) -func ConnectHevring(ránAddress string, stop chan bool, wg *sync.WaitGroup) { +func ConnectHevring(ránAddress string, stop chan bool, wg *sync.WaitGroup) *Hevring { h := new(Hevring) rpc.Register(h) @@ -35,13 +37,16 @@ func ConnectHevring(ránAddress string, stop chan bool, wg *sync.WaitGroup) { } h.wg.Done() }() + + return h } type Hevring struct { - task pixelflut.FlutTask - taskQuit chan bool - quit chan bool - wg *sync.WaitGroup + PreviewPath string + task pixelflut.FlutTask + taskQuit chan bool + quit chan bool + wg *sync.WaitGroup } type FlutAck struct{ Ok bool } @@ -63,6 +68,8 @@ func (h *Hevring) Flut(task pixelflut.FlutTask, reply *FlutAck) error { h.taskQuit = make(chan bool) go pixelflut.Flut(task, h.taskQuit, nil) + go h.savePreview(task.Img) + reply.Ok = true return nil } @@ -99,3 +106,13 @@ func (h *Hevring) Die(x int, reply *FlutAck) error { reply.Ok = true return nil } + +func (h Hevring) savePreview(img image.Image) { + if h.PreviewPath != "" && img != nil { + err := render.WriteImage(h.PreviewPath, img) + if err != nil { + fmt.Printf("[hevring] unable to write preview: %s\n", err) + } + } + +}