remote: Allow protocol input & cancellation
This commit reworks the protocol being used on top of websockets. It now permits sending input to the server, and interrupting the remote process.
This commit is contained in:
parent
746ebcd9e3
commit
c19215068e
12 changed files with 383 additions and 217 deletions
|
|
@ -2,10 +2,7 @@ package socket
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"time"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/exporter"
|
||||
|
|
@ -13,14 +10,16 @@ import (
|
|||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/instances/purger"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/provision"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/wisski"
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/tkw1536/goprogram/status"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/tkw1536/pkglib/httpx"
|
||||
"github.com/tkw1536/pkglib/lazy"
|
||||
)
|
||||
|
||||
type Sockets struct {
|
||||
component.Base
|
||||
|
||||
actions lazy.Lazy[ActionMap]
|
||||
|
||||
Dependencies struct {
|
||||
Provision *provision.Provision
|
||||
Instances *instances.Instances
|
||||
|
|
@ -31,115 +30,33 @@ type Sockets struct {
|
|||
|
||||
// Serve handles a connection to the websocket api
|
||||
func (socket *Sockets) Serve(conn httpx.WebSocketConnection) {
|
||||
// read the next message to act on
|
||||
message, ok := <-conn.Read()
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
name := string(message.Bytes)
|
||||
|
||||
// perform a generic action first
|
||||
if action, ok := actions[name]; ok {
|
||||
socket.Handle(conn, action)
|
||||
return
|
||||
}
|
||||
|
||||
// then do the socket actions
|
||||
if action, ok := igActions[name]; ok {
|
||||
socket.Handle(conn, action)
|
||||
}
|
||||
}
|
||||
|
||||
var instanceParamsTimeout = time.Second
|
||||
|
||||
type actionResult struct {
|
||||
Success bool `json:"success"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
func (*Sockets) reportErrorToClient(conn httpx.WebSocketConnection, err error) {
|
||||
// create an action result
|
||||
var result actionResult
|
||||
if err == nil {
|
||||
result.Success = true
|
||||
} else {
|
||||
result.Success = false
|
||||
result.Error = err.Error()
|
||||
}
|
||||
|
||||
// marshal the result, ignoring any error silently
|
||||
data, err := json.Marshal(result)
|
||||
// handle the websocket connection!
|
||||
name, err := socket.actions.Get(socket.Actions).Handle(conn)
|
||||
if err != nil {
|
||||
return
|
||||
zerolog.Ctx(conn.Context()).Err(err).Str("name", name).Msg("Error handling websocket")
|
||||
}
|
||||
|
||||
// and send it as a binary message to the client
|
||||
<-conn.Write(httpx.WebSocketMessage{Type: websocket.BinaryMessage, Bytes: data})
|
||||
}
|
||||
|
||||
var errInsufficientParams = errors.New("insufficient parameters")
|
||||
var errParameterTimeout = errors.New("timed out reading parameters")
|
||||
|
||||
func (socket *Sockets) Handle(conn httpx.WebSocketConnection, action SocketAction) (err error) {
|
||||
// report the error to the client
|
||||
defer func() {
|
||||
// NOTE: the closure is needed here!
|
||||
socket.reportErrorToClient(conn, err)
|
||||
}()
|
||||
|
||||
// read the parameters
|
||||
params := make([]string, action.NumParams)
|
||||
for i := range params {
|
||||
select {
|
||||
case message, ok := <-conn.Read():
|
||||
if !ok {
|
||||
return errInsufficientParams
|
||||
}
|
||||
params[i] = string(message.Bytes)
|
||||
case <-time.After(instanceParamsTimeout):
|
||||
return errParameterTimeout
|
||||
}
|
||||
}
|
||||
|
||||
// build a stream
|
||||
writer := &status.LineBuffer{
|
||||
Line: func(line string) {
|
||||
<-conn.WriteText(line)
|
||||
// Generic returns a new action that calls handler with the provided number of parameters
|
||||
func (sockets *Sockets) Generic(numParams int, handler func(ctx context.Context, socket *Sockets, in io.Reader, out io.Writer, params ...string) error) Action {
|
||||
return Action{
|
||||
NumParams: numParams,
|
||||
Handle: func(ctx context.Context, in io.Reader, out io.Writer, params ...string) error {
|
||||
return handler(ctx, sockets, in, out, params...)
|
||||
},
|
||||
FlushLineOnClose: true,
|
||||
}
|
||||
defer writer.Close()
|
||||
|
||||
// handle the interactive action
|
||||
return action.HandleInteractive(conn.Context(), socket, writer, params...)
|
||||
}
|
||||
|
||||
// IAction is like SocketAction, but takes the slug of an instance (runnning or not) as the first parameter
|
||||
type IAction struct {
|
||||
NumParams int
|
||||
|
||||
HandleInteractive func(ctx context.Context, sockets *Sockets, instance *wisski.WissKI, out io.Writer, params ...string) error
|
||||
}
|
||||
|
||||
// AsGenericAction turns this InstanceAction into a generic action
|
||||
func (ia IAction) AsGenericAction() SocketAction {
|
||||
return SocketAction{
|
||||
NumParams: ia.NumParams + 1,
|
||||
HandleInteractive: func(ctx context.Context, sockets *Sockets, out io.Writer, params ...string) error {
|
||||
// Insstance returns a new action that calls handler with a specific WissKI instance
|
||||
func (sockets *Sockets) Instance(numParams int, handler func(ctx context.Context, sockets *Sockets, instance *wisski.WissKI, in io.Reader, out io.Writer, params ...string) error) Action {
|
||||
return Action{
|
||||
NumParams: numParams + 1,
|
||||
Handle: func(ctx context.Context, in io.Reader, out io.Writer, params ...string) error {
|
||||
instance, err := sockets.Dependencies.Instances.WissKI(ctx, params[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return ia.HandleInteractive(ctx, sockets, instance, out, params[1:]...)
|
||||
return handler(ctx, sockets, instance, in, out, params[1:]...)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// SocketAction represents an action handled via socket
|
||||
type SocketAction struct {
|
||||
NumParams int
|
||||
|
||||
HandleInteractive func(ctx context.Context, sockets *Sockets, out io.Writer, params ...string) error
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue