2020-02-07 12:20:53 +01:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-01-02 08:34:51 +01:00
|
|
|
"image"
|
2020-02-07 12:20:53 +01:00
|
|
|
"log"
|
2020-02-11 14:46:58 +01:00
|
|
|
"net"
|
2020-02-07 12:20:53 +01:00
|
|
|
"net/rpc"
|
2020-12-31 07:15:16 +01:00
|
|
|
"sync"
|
2020-02-12 11:20:15 +01:00
|
|
|
"time"
|
2020-02-07 12:20:53 +01:00
|
|
|
|
2020-02-12 11:20:15 +01:00
|
|
|
"github.com/SpeckiJ/Hochwasser/pixelflut"
|
2022-01-02 08:34:51 +01:00
|
|
|
"github.com/SpeckiJ/Hochwasser/render"
|
2020-02-12 11:20:15 +01:00
|
|
|
)
|
2020-02-11 14:46:58 +01:00
|
|
|
|
2022-01-02 08:34:51 +01:00
|
|
|
func ConnectHevring(ránAddress string, stop chan bool, wg *sync.WaitGroup) *Hevring {
|
2020-12-31 07:15:16 +01:00
|
|
|
h := new(Hevring)
|
|
|
|
rpc.Register(h)
|
2020-02-07 12:20:53 +01:00
|
|
|
|
2020-02-11 14:46:58 +01:00
|
|
|
fmt.Printf("[hevring] greeting Rán at %s\n", ránAddress)
|
|
|
|
conn, err := net.Dial("tcp", ránAddress)
|
2020-02-07 12:20:53 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-02-11 14:46:58 +01:00
|
|
|
go rpc.ServeConn(conn)
|
|
|
|
fmt.Printf("[hevring] awaiting task from Rán\n")
|
2020-12-31 07:15:16 +01:00
|
|
|
|
|
|
|
h.quit = stop
|
|
|
|
h.wg = wg
|
|
|
|
h.wg.Add(1)
|
|
|
|
go func() {
|
2021-01-02 15:49:30 +01:00
|
|
|
<-h.quit
|
|
|
|
h.quit = nil
|
2020-12-31 07:15:16 +01:00
|
|
|
if h.taskQuit != nil {
|
|
|
|
close(h.taskQuit)
|
|
|
|
h.taskQuit = nil
|
|
|
|
}
|
|
|
|
h.wg.Done()
|
|
|
|
}()
|
2022-01-02 08:34:51 +01:00
|
|
|
|
|
|
|
return h
|
2020-02-11 14:46:58 +01:00
|
|
|
}
|
2020-02-07 12:20:53 +01:00
|
|
|
|
2020-02-12 13:55:04 +01:00
|
|
|
type Hevring struct {
|
2022-01-02 08:34:51 +01:00
|
|
|
PreviewPath string
|
|
|
|
task pixelflut.FlutTask
|
|
|
|
taskQuit chan bool
|
|
|
|
quit chan bool
|
|
|
|
wg *sync.WaitGroup
|
2020-02-12 13:55:04 +01:00
|
|
|
}
|
2020-02-12 11:20:15 +01:00
|
|
|
|
2020-02-11 14:46:58 +01:00
|
|
|
type FlutAck struct{ Ok bool }
|
|
|
|
|
2020-02-14 16:37:19 +01:00
|
|
|
type FlutStatus struct {
|
|
|
|
*pixelflut.Performance
|
|
|
|
Ok bool
|
|
|
|
Fluting bool
|
|
|
|
}
|
|
|
|
|
2020-12-31 18:44:56 +01:00
|
|
|
func (h *Hevring) Flut(task pixelflut.FlutTask, reply *FlutAck) error {
|
2020-02-14 22:29:58 +01:00
|
|
|
// stop old task if new task is received
|
|
|
|
if h.taskQuit != nil {
|
|
|
|
close(h.taskQuit)
|
2020-02-12 13:55:04 +01:00
|
|
|
}
|
|
|
|
|
2020-12-31 07:15:16 +01:00
|
|
|
fmt.Printf("[hevring] Rán gave us work!\n%v\n", task)
|
2020-02-12 13:55:04 +01:00
|
|
|
h.task = task
|
2020-02-13 22:26:50 +01:00
|
|
|
h.taskQuit = make(chan bool)
|
|
|
|
|
2020-12-31 18:44:56 +01:00
|
|
|
go pixelflut.Flut(task, h.taskQuit, nil)
|
2022-01-02 08:34:51 +01:00
|
|
|
go h.savePreview(task.Img)
|
|
|
|
|
2020-02-11 14:46:58 +01:00
|
|
|
reply.Ok = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-14 16:37:19 +01:00
|
|
|
func (h *Hevring) Status(metrics bool, reply *FlutStatus) error {
|
|
|
|
pixelflut.PerformanceReporter.Enabled = metrics
|
|
|
|
reply.Performance = pixelflut.PerformanceReporter
|
2020-02-11 14:46:58 +01:00
|
|
|
reply.Ok = true
|
2020-02-14 16:37:19 +01:00
|
|
|
reply.Fluting = h.taskQuit != nil
|
2020-02-11 14:46:58 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Hevring) Stop(x int, reply *FlutAck) error {
|
2020-02-14 22:29:58 +01:00
|
|
|
if h.taskQuit != nil {
|
2020-02-12 13:55:04 +01:00
|
|
|
fmt.Println("[hevring] stopping task")
|
2020-12-31 18:44:56 +01:00
|
|
|
h.task = pixelflut.FlutTask{}
|
2020-02-13 22:26:50 +01:00
|
|
|
close(h.taskQuit)
|
|
|
|
h.taskQuit = nil
|
2020-02-12 13:55:04 +01:00
|
|
|
reply.Ok = true
|
|
|
|
}
|
2020-02-11 14:46:58 +01:00
|
|
|
return nil
|
2020-02-07 12:20:53 +01:00
|
|
|
}
|
2020-02-12 11:20:15 +01:00
|
|
|
|
|
|
|
func (h *Hevring) Die(x int, reply *FlutAck) error {
|
2020-02-14 22:29:58 +01:00
|
|
|
// @robustness: waiting for reply to be sent via timeout
|
|
|
|
// @incomplete: should try to reconnect for a bit first
|
|
|
|
go func() {
|
2020-02-13 23:57:56 +01:00
|
|
|
fmt.Println("[hevring] Rán disconnected, stopping")
|
2020-12-31 07:15:16 +01:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2021-01-02 15:49:30 +01:00
|
|
|
if h.quit != nil {
|
|
|
|
close(h.quit)
|
|
|
|
}
|
2020-02-12 11:20:15 +01:00
|
|
|
}()
|
|
|
|
reply.Ok = true
|
|
|
|
return nil
|
|
|
|
}
|
2022-01-02 08:34:51 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|