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"
|
2020-02-13 22:26:50 +01:00
|
|
|
"os/signal"
|
2019-01-06 22:00:43 +01:00
|
|
|
"runtime/pprof"
|
2020-02-13 22:26:50 +01:00
|
|
|
"sync"
|
2020-02-05 19:26:43 +01:00
|
|
|
|
|
|
|
"github.com/SpeckiJ/Hochwasser/pixelflut"
|
2020-02-14 22:29:58 +01:00
|
|
|
"github.com/SpeckiJ/Hochwasser/render"
|
2020-02-07 12:20:53 +01:00
|
|
|
"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")
|
2019-01-11 19:37:41 +01:00
|
|
|
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")
|
2020-02-05 12:16:44 +01:00
|
|
|
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-13 23:57:56 +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-07 12:20:53 +01:00
|
|
|
|
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-05 12:16:44 +01:00
|
|
|
|
2020-02-12 11:20:15 +01:00
|
|
|
if *ránAddr != "" {
|
|
|
|
// run RPC server, tasking clients to flut
|
2020-02-13 23:57:56 +01:00
|
|
|
wg.Add(1)
|
|
|
|
r := rpc.SummonRán(*ránAddr, stopChan, &wg)
|
2020-12-29 18:28:19 +01:00
|
|
|
// TODO: startup without a task, but init params
|
|
|
|
r.SetTask(img, offset, *address, *connections)
|
2020-02-12 11:20:15 +01:00
|
|
|
|
|
|
|
} else {
|
|
|
|
// fetch server state and save to file
|
|
|
|
// @incomplete: make this available also when not fluting?
|
|
|
|
if *fetchImgPath != "" {
|
2020-02-13 22:26:50 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-02-13 23:57:56 +01:00
|
|
|
// local 🌊🌊🌊🌊🌊🌊🌊🌊🌊🌊🌊🌊🌊🌊🌊
|
|
|
|
wg.Add(1)
|
2020-12-29 18:28:19 +01:00
|
|
|
go pixelflut.Flut(img, offset, *shuffle, false, false, false, *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
|
|
|
}
|
2020-02-13 23:57:56 +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
|
|
|
}
|