rpc: add repl, kill clients on exit

This commit is contained in:
Norwin Roosen 2020-02-12 13:55:04 +01:00
parent e716ae8d25
commit ada015e90f
2 changed files with 64 additions and 5 deletions

View File

@ -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
}

View File

@ -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
}