Hochwasser/rpc/dottir.go

103 lines
2.2 KiB
Go
Raw Normal View History

package rpc
import (
"fmt"
2020-02-12 11:20:15 +01:00
"image"
"log"
2020-02-11 14:46:58 +01:00
"net"
"net/rpc"
"os"
2020-02-12 11:20:15 +01:00
"time"
2020-02-12 11:20:15 +01:00
"github.com/SpeckiJ/Hochwasser/pixelflut"
)
2020-02-11 14:46:58 +01:00
func ConnectHevring(ránAddress string) {
2020-02-11 14:46:58 +01:00
rpc.Register(new(Hevring))
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)
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-02-12 13:55:04 +01:00
type Hevring struct {
task FlutTask
taskQuit chan bool
2020-02-12 13:55:04 +01:00
}
2020-02-12 11:20:15 +01:00
type FlutTask struct {
2020-12-29 18:28:19 +01:00
Address string
MaxConns int
Img *image.NRGBA
Offset image.Point
Paused bool
2020-12-29 18:28:19 +01:00
Shuffle bool // TODO: refactor these as RenderOpts bitfield
RGBSplit bool
RandOffset bool
}
func (t FlutTask) String() string {
return fmt.Sprintf(
" %d conns @ %s\n img %v offset %v\n shuffle %v rgbsplit %v randoffset %v paused %v",
t.MaxConns, t.Address, t.Img.Bounds().Size(), t.Offset, t.Shuffle, t.RGBSplit, t.RandOffset, t.Paused,
)
2020-02-12 11:20:15 +01:00
}
2020-02-11 14:46:58 +01:00
type FlutAck struct{ Ok bool }
type FlutStatus struct {
*pixelflut.Performance
Ok bool
Fluting bool
}
2020-02-12 11:20:15 +01:00
func (h *Hevring) Flut(task 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
}
fmt.Printf("[hevring] Rán gave us /w o r k/!\n%v\n", task)
2020-02-12 13:55:04 +01:00
h.task = task
h.taskQuit = make(chan bool)
go pixelflut.Flut(task.Img, task.Offset, task.Shuffle, task.RGBSplit, task.RandOffset, task.Address, task.MaxConns, h.taskQuit, nil)
2020-02-11 14:46:58 +01:00
reply.Ok = true
return nil
}
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
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")
h.task = FlutTask{}
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-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-12 11:20:15 +01:00
time.Sleep(100 * time.Millisecond)
fmt.Println("[hevring] Rán disconnected, stopping")
os.Exit(0)
2020-02-12 11:20:15 +01:00
}()
reply.Ok = true
return nil
}