From ada015e90f5197183fd85dcc35bb25fffa4f5135 Mon Sep 17 00:00:00 2001 From: Norwin Roosen Date: Wed, 12 Feb 2020 13:55:04 +0100 Subject: [PATCH] rpc: add repl, kill clients on exit --- rpc/dottir.go | 22 ++++++++++++++++++---- rpc/ran.go | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 64 insertions(+), 5 deletions(-) diff --git a/rpc/dottir.go b/rpc/dottir.go index 31d2fc2..14e9b83 100644 --- a/rpc/dottir.go +++ b/rpc/dottir.go @@ -23,7 +23,9 @@ func ConnectHevring(ránAddress string) { fmt.Printf("[hevring] awaiting task from Rán\n") } -type Hevring struct{} +type Hevring struct { + task FlutTask +} type FlutTask struct { Address string @@ -36,9 +38,16 @@ type FlutTask struct { type FlutAck struct{ Ok bool } func (h *Hevring) Flut(task FlutTask, reply *FlutAck) error { - // @incomplete: async errorhandling - // @incomplete: stop old task if new task is received + 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 + } + fmt.Printf("[hevring] Rán gave us /w o r k/! %v\n", task) + h.task = task + // @incomplete: async errorhandling pixelflut.Flut(task.Img, task.Offset, task.Shuffle, task.Address, task.MaxConns) reply.Ok = true return nil @@ -51,7 +60,12 @@ func (h *Hevring) Status(x int, reply *FlutAck) error { } func (h *Hevring) Stop(x int, reply *FlutAck) error { - reply.Ok = true + // @incomplete + if (h.task != FlutTask{}) { + fmt.Println("[hevring] stopping task") + h.task = FlutTask{} + reply.Ok = true + } return nil } diff --git a/rpc/ran.go b/rpc/ran.go index 5ef63e1..5b590fc 100644 --- a/rpc/ran.go +++ b/rpc/ran.go @@ -1,11 +1,16 @@ package rpc import ( + // "io" + "bufio" "fmt" "image" "log" "net" "net/rpc" + "os" + "os/signal" + "strings" "time" ) @@ -65,7 +70,47 @@ func SummonRán(address string) *Rán { } }() - // @incomplete: REPL to change tasks without loosing clients + // REPL to change tasks without loosing clients + go func() { + scanner := bufio.NewScanner(os.Stdin) + for scanner.Scan() { + input := strings.Split(scanner.Text(), " ") + cmd := strings.ToLower(input[0]) + args := input[1:] + if cmd == "stop" { + for _, c := range r.clients { + ack := FlutAck{} + c.Call("Hevring.Stop", 0, &ack) // @speed: async + } + + } else if cmd == "img" && len(args) > 0 { + // // @incomplete + // path := args[0] + // img := readImage(path) + // offset := image.Pt(0, 0) + // if len(args) == 3 { + // x := strconv.Atoi(args[1]) + // y := strconv.Atoi(args[2]) + // offset = image.Pt(x, y) + // } + // task := FlutTask{} + } + } + }() + + // kill clients on exit + go func() { + sigChan := make(chan os.Signal, 1) + signal.Notify(sigChan, os.Interrupt) + for { + <-sigChan + for _, c := range r.clients { + ack := FlutAck{} + c.Call("Hevring.Die", 0, &ack) // @speed: async + } + os.Exit(0) // @bug :cleanExit + } + }() return r }