Hochwasser/main.go

108 lines
3.1 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"
2019-01-06 22:00:43 +01:00
"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")
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")
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")
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
2020-02-12 11:20:15 +01:00
if *imgPath != "" {
offset := image.Pt(*x, *y)
img := readImage(*imgPath)
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
r := rpc.SummonRán(*ránAddr)
r.SetTask(img, offset, *address, *connections) // @incomplete
select {} // block forever
} else {
2020-02-12 11:20:15 +01:00
// local 🌊🌊🌊🌊🌊🌊🌊🌊🌊🌊🌊🌊🌊🌊🌊
var wg sync.WaitGroup
defer wg.Wait()
stopChan := make(chan bool)
defer close(stopChan)
wg.Add(1) // :cleanExit: is this WG needed? we only have one task running at a time?
go pixelflut.Flut(img, offset, *shuffle, *address, *connections, stopChan, &wg)
2019-01-06 22:00:43 +01:00
2020-02-12 11:20:15 +01:00
// 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
defer writeImage(*fetchImgPath, fetchedImg)
}
// :cleanExit logic:
// notify all async tasks to stop on interrupt or after timeout,
// then wait for clean shutdown of all tasks before exiting
// TODO: make this available to all invocation types
2020-02-12 11:20:15 +01:00
timer, err := time.ParseDuration(*runtime)
if err != nil {
log.Fatal("Invalid runtime specified: " + err.Error())
}
interruptChan := make(chan os.Signal)
signal.Notify(interruptChan, os.Interrupt)
select {
case <-time.After(timer):
case <-interruptChan:
}
2020-02-12 11:20:15 +01:00
}
} else if *hevringAddr != "" {
// connect to RPC server and execute their tasks
rpc.ConnectHevring(*hevringAddr)
select {} // block forever
} else {
log.Fatal("must specify -image or -hevring")
2019-01-06 22:00:43 +01:00
}
}