2020-02-11 14:46:58 +01:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"image"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"net/rpc"
|
2020-02-13 23:57:56 +01:00
|
|
|
"sync"
|
2020-02-11 14:46:58 +01:00
|
|
|
"time"
|
2020-02-14 16:37:19 +01:00
|
|
|
|
|
|
|
"github.com/SpeckiJ/Hochwasser/pixelflut"
|
2020-02-11 14:46:58 +01:00
|
|
|
)
|
|
|
|
|
2020-02-14 22:29:58 +01:00
|
|
|
// Rán represents the RPC hub, used to coordinate `Hevring` clients.
|
|
|
|
// Implements `Fluter`
|
2020-02-12 11:20:15 +01:00
|
|
|
type Rán struct {
|
|
|
|
clients []*rpc.Client
|
|
|
|
task FlutTask
|
2020-02-14 16:37:19 +01:00
|
|
|
metrics pixelflut.Performance
|
2020-02-12 11:20:15 +01:00
|
|
|
}
|
|
|
|
|
2020-02-13 23:57:56 +01:00
|
|
|
// SummonRán sets up the RPC master, accepting connections at addres (":1234")
|
|
|
|
// Connects calls methods on each client's rpc provider, killing all clients
|
|
|
|
// when stopChan is closed.
|
|
|
|
func SummonRán(address string, stopChan chan bool, wg *sync.WaitGroup) *Rán {
|
2020-02-12 11:20:15 +01:00
|
|
|
r := new(Rán)
|
2020-02-11 14:46:58 +01:00
|
|
|
|
|
|
|
l, err := net.Listen("tcp", address)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
fmt.Printf("[rán] rpc server listening on %s\n", l.Addr())
|
|
|
|
|
2020-02-12 11:20:15 +01:00
|
|
|
// serve tcp port, handshake
|
2020-02-11 14:46:58 +01:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
conn, err := l.Accept()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
client := rpc.NewClient(conn)
|
2020-02-12 11:20:15 +01:00
|
|
|
r.clients = append(r.clients, client)
|
|
|
|
fmt.Printf("[rán] client connected (%v). current clients: %v\n",
|
|
|
|
conn.RemoteAddr(), len(r.clients))
|
2020-02-11 14:46:58 +01:00
|
|
|
|
2020-02-12 11:20:15 +01:00
|
|
|
if (r.task != FlutTask{}) {
|
|
|
|
ack := FlutAck{}
|
|
|
|
err = client.Call("Hevring.Flut", r.task, &ack)
|
|
|
|
if err != nil || !ack.Ok {
|
|
|
|
log.Printf("[rán] client didn't accept task")
|
|
|
|
}
|
2020-02-11 14:46:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// poll clients
|
|
|
|
go func() {
|
|
|
|
for {
|
2020-02-12 11:20:15 +01:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-02-11 14:46:58 +01:00
|
|
|
|
|
|
|
var clients []*rpc.Client
|
2020-02-14 16:37:19 +01:00
|
|
|
|
|
|
|
r.metrics.Conns = 0
|
|
|
|
r.metrics.BytesPerSec = 0
|
|
|
|
r.metrics.BytesTotal = 0
|
|
|
|
|
2020-02-12 11:20:15 +01:00
|
|
|
for _, c := range r.clients {
|
2020-02-14 16:37:19 +01:00
|
|
|
status := FlutStatus{}
|
|
|
|
err := c.Call("Hevring.Status", r.metrics.Enabled, &status)
|
2020-02-13 23:57:56 +01:00
|
|
|
if err == nil && status.Ok {
|
2020-02-11 14:46:58 +01:00
|
|
|
clients = append(clients, c)
|
2020-02-14 16:37:19 +01:00
|
|
|
r.metrics.Conns += status.Conns
|
|
|
|
r.metrics.BytesPerSec += status.BytesPerSec
|
|
|
|
r.metrics.BytesTotal += status.BytesTotal
|
2020-02-11 14:46:58 +01:00
|
|
|
}
|
|
|
|
}
|
2020-02-12 11:20:15 +01:00
|
|
|
if len(r.clients) != len(clients) {
|
|
|
|
fmt.Printf("[rán] client disconnected. current clients: %v\n", len(clients))
|
|
|
|
}
|
|
|
|
r.clients = clients
|
2020-02-11 14:46:58 +01:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-02-14 16:37:19 +01:00
|
|
|
// print performance
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
if r.metrics.Enabled {
|
|
|
|
fmt.Println(r.metrics)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-02-14 22:29:58 +01:00
|
|
|
go RunREPL(r)
|
|
|
|
go r.killClients(stopChan, wg)
|
2020-02-12 13:55:04 +01:00
|
|
|
|
2020-02-14 22:29:58 +01:00
|
|
|
return r
|
|
|
|
}
|
2020-02-14 16:37:19 +01:00
|
|
|
|
2020-02-14 22:29:58 +01:00
|
|
|
func (r *Rán) getTask() FlutTask { return r.task }
|
2020-02-14 16:37:19 +01:00
|
|
|
|
2020-02-14 22:29:58 +01:00
|
|
|
func (r *Rán) toggleMetrics() {
|
|
|
|
r.metrics.Enabled = !r.metrics.Enabled
|
|
|
|
}
|
2020-02-12 13:55:04 +01:00
|
|
|
|
2020-02-14 22:29:58 +01:00
|
|
|
func (r *Rán) applyTask(t FlutTask) {
|
|
|
|
if (t == FlutTask{}) { // @robustness: FlutTask should provide .IsValid()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
r.task = t
|
2020-12-29 22:03:21 +01:00
|
|
|
if t.Paused {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for i, c := range r.clients {
|
2020-02-14 22:29:58 +01:00
|
|
|
ack := FlutAck{}
|
|
|
|
err := c.Call("Hevring.Flut", r.task, &ack)
|
|
|
|
if err != nil || !ack.Ok {
|
2020-12-29 22:03:21 +01:00
|
|
|
log.Printf("[rán] client %d didn't accept task", i)
|
2020-02-12 13:55:04 +01:00
|
|
|
}
|
2020-02-14 22:29:58 +01:00
|
|
|
}
|
|
|
|
}
|
2020-02-11 14:46:58 +01:00
|
|
|
|
2020-02-14 22:29:58 +01:00
|
|
|
func (r *Rán) stopTask() {
|
|
|
|
// @robustness: errorchecking
|
|
|
|
for _, c := range r.clients {
|
|
|
|
ack := FlutAck{}
|
|
|
|
c.Call("Hevring.Stop", 0, &ack) // @speed: async
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Rán) killClients(stopChan <-chan bool, wg *sync.WaitGroup) {
|
|
|
|
<-stopChan
|
|
|
|
for _, c := range r.clients {
|
|
|
|
ack := FlutAck{}
|
|
|
|
c.Call("Hevring.Die", 0, &ack) // @speed: async
|
|
|
|
}
|
|
|
|
wg.Done()
|
2020-02-11 14:46:58 +01:00
|
|
|
}
|
|
|
|
|
2020-02-13 23:57:56 +01:00
|
|
|
// SetTask assigns a FlutTask to Rán, distributing it to all clients
|
2020-02-14 12:39:16 +01:00
|
|
|
func (r *Rán) SetTask(img *image.NRGBA, offset image.Point, address string, maxConns int) {
|
2020-02-12 11:20:15 +01:00
|
|
|
// @incomplete: smart task creation:
|
|
|
|
// fetch server state & sample foreign activity in image regions. assign
|
|
|
|
// subregions to clients (per connection), considering their bandwidth.
|
|
|
|
|
2020-12-29 22:03:21 +01:00
|
|
|
r.applyTask(FlutTask{
|
|
|
|
Address: address,
|
|
|
|
MaxConns: maxConns,
|
|
|
|
Img: img,
|
|
|
|
Offset: offset,
|
|
|
|
Shuffle: true,
|
|
|
|
})
|
2020-02-11 14:46:58 +01:00
|
|
|
}
|