Hochwasser/main.go

106 lines
3.0 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"
"os/signal"
2019-01-06 22:00:43 +01:00
"runtime/pprof"
"sync"
"github.com/SpeckiJ/Hochwasser/pixelflut"
2020-02-14 22:29:58 +01:00
"github.com/SpeckiJ/Hochwasser/render"
"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")
2020-02-12 11:20:15 +01:00
var imgPath = flag.String("image", "", "Absolute Path to image")
var x = flag.Int("x", 0, "Offset of posted image from left border")
var y = flag.Int("y", 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")
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")
2020-02-12 11:20:15 +01:00
var ránAddr = flag.String("rán", "", "enable rpc server to distribute jobs, listening on the given address/port")
var hevringAddr = 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:22:51 +01:00
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
// :cleanExit setup
// stop chan is closed at end of main process, telling async tasks to stop.
// wg waits until async tasks gracefully stopped
wg := sync.WaitGroup{}
stopChan := make(chan bool)
interruptChan := make(chan os.Signal)
signal.Notify(interruptChan, os.Interrupt)
2020-02-12 11:20:15 +01:00
if *imgPath != "" {
offset := image.Pt(*x, *y)
2020-02-14 22:29:58 +01:00
imgTmp, err := render.ReadImage(*imgPath)
if err != nil {
log.Println(err)
return
}
img := render.ImgToNRGBA(imgTmp)
2020-02-12 11:20:15 +01:00
// check connectivity by opening one test connection
conn, err := net.Dial("tcp", *address)
if err != nil {
log.Fatal(err)
}
conn.Close()
2020-02-12 11:20:15 +01:00
if *ránAddr != "" {
// run RPC server, tasking clients to flut
wg.Add(1)
r := rpc.SummonRán(*ránAddr, stopChan, &wg)
2020-02-12 11:20:15 +01:00
r.SetTask(img, offset, *address, *connections) // @incomplete
} else {
// fetch server state and save to file
// @incomplete: make this available also when not fluting?
if *fetchImgPath != "" {
fetchedImg := pixelflut.FetchImage(img.Bounds().Add(offset), *address, 1, stopChan)
2020-02-12 11:20:15 +01:00
*connections -= 1
2020-02-14 22:29:58 +01:00
defer render.WriteImage(*fetchImgPath, fetchedImg)
2020-02-12 11:20:15 +01:00
}
// local 🌊🌊🌊🌊🌊🌊🌊🌊🌊🌊🌊🌊🌊🌊🌊
wg.Add(1)
go pixelflut.Flut(img, offset, *shuffle, *address, *connections, stopChan, &wg)
2020-02-12 11:20:15 +01:00
}
} else if *hevringAddr != "" {
// connect to RPC server and execute their tasks
rpc.ConnectHevring(*hevringAddr)
} else {
log.Fatal("must specify -image or -hevring")
2019-01-06 22:00:43 +01:00
}
// :cleanExit logic:
// notify all async tasks to stop on interrupt
// then wait for clean shutdown of all tasks before exiting
// TODO: make this available to all invocation types
select {
case <-interruptChan:
}
close(stopChan)
wg.Wait()
2019-01-06 22:00:43 +01:00
}