2020-02-07 12:20:53 +01:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-02-12 11:20:15 +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-02-13 23:57:56 +01:00
|
|
|
"os"
|
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"
|
|
|
|
)
|
2020-02-11 14:46:58 +01:00
|
|
|
|
2020-02-07 12:20:53 +01:00
|
|
|
func ConnectHevring(ránAddress string) {
|
2020-02-11 14:46:58 +01:00
|
|
|
rpc.Register(new(Hevring))
|
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-02-07 12:20:53 +01:00
|
|
|
|
2020-02-12 13:55:04 +01:00
|
|
|
type Hevring struct {
|
2020-02-13 22:26:50 +01:00
|
|
|
task FlutTask
|
|
|
|
taskQuit chan bool
|
2020-02-12 13:55:04 +01:00
|
|
|
}
|
2020-02-12 11:20:15 +01:00
|
|
|
|
|
|
|
type FlutTask struct {
|
|
|
|
Address string
|
|
|
|
MaxConns int
|
|
|
|
Img *image.NRGBA // bug :imageType: should be image.Image, but can't be serialized. do conversion in task creation?
|
|
|
|
Offset image.Point
|
|
|
|
Shuffle bool
|
|
|
|
}
|
2020-02-07 12:20:53 +01:00
|
|
|
|
2020-02-11 14:46:58 +01:00
|
|
|
type FlutAck struct{ Ok bool }
|
|
|
|
|
2020-02-12 11:20:15 +01:00
|
|
|
func (h *Hevring) Flut(task FlutTask, reply *FlutAck) error {
|
2020-02-12 13:55:04 +01:00
|
|
|
if (h.task != FlutTask{}) {
|
|
|
|
// @incomplete: stop old task if new task is received
|
|
|
|
fmt.Println("[hevring] already have a task")
|
|
|
|
reply.Ok = false
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-12 11:20:15 +01:00
|
|
|
fmt.Printf("[hevring] Rán gave us /w o r k/! %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)
|
|
|
|
|
|
|
|
go pixelflut.Flut(task.Img, task.Offset, task.Shuffle, task.Address, task.MaxConns, h.taskQuit, nil)
|
2020-02-11 14:46:58 +01:00
|
|
|
reply.Ok = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Hevring) Status(x int, reply *FlutAck) error {
|
2020-02-12 11:20:15 +01:00
|
|
|
// @incomplete: provide performance metrics
|
2020-02-11 14:46:58 +01:00
|
|
|
reply.Ok = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Hevring) Stop(x int, reply *FlutAck) error {
|
2020-02-12 13:55:04 +01:00
|
|
|
if (h.task != FlutTask{}) {
|
|
|
|
fmt.Println("[hevring] stopping task")
|
|
|
|
h.task = 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 {
|
|
|
|
go func() { // @cleanup: hacky
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-02-13 23:57:56 +01:00
|
|
|
fmt.Println("[hevring] Rán disconnected, stopping")
|
|
|
|
os.Exit(0)
|
2020-02-12 11:20:15 +01:00
|
|
|
}()
|
|
|
|
reply.Ok = true
|
|
|
|
return nil
|
|
|
|
}
|