Files

135 lines
3.3 KiB
Go
Raw Permalink Normal View History

2023-02-26 20:49:29 +11:00
package application
import (
"context"
2023-02-26 20:49:29 +11:00
"encoding/json"
"fmt"
"net/http"
)
2023-08-27 16:52:07 +10:00
const (
CallBinding = 0
)
2023-09-28 11:38:59 -05:00
func (m *MessageProcessor) callErrorCallback(window Window, message string, callID *string, err error) {
2023-02-26 20:49:29 +11:00
errorMsg := fmt.Sprintf(message, err)
m.Error(errorMsg)
2023-09-28 11:38:59 -05:00
window.CallError(*callID, errorMsg)
2023-02-26 20:49:29 +11:00
}
2023-09-28 11:38:59 -05:00
func (m *MessageProcessor) callCallback(window Window, callID *string, result string, isJSON bool) {
window.CallResponse(*callID, result)
2023-02-26 20:49:29 +11:00
}
func (m *MessageProcessor) processCallCancelMethod(method int, rw http.ResponseWriter, r *http.Request, window Window, params QueryParams) {
args, err := params.Args()
if err != nil {
m.httpError(rw, "Unable to parse arguments: %s", err.Error())
return
}
callID := args.String("call-id")
if callID == nil || *callID == "" {
m.Error("call-id is required")
return
}
m.l.Lock()
cancel := m.runningCalls[*callID]
m.l.Unlock()
if cancel != nil {
cancel()
}
m.ok(rw)
}
2023-09-28 11:38:59 -05:00
func (m *MessageProcessor) processCallMethod(method int, rw http.ResponseWriter, r *http.Request, window Window, params QueryParams) {
2023-02-26 20:49:29 +11:00
args, err := params.Args()
if err != nil {
2023-09-08 12:03:55 +10:00
m.httpError(rw, "Unable to parse arguments: %s", err.Error())
2023-02-26 20:49:29 +11:00
return
}
callID := args.String("call-id")
if callID == nil || *callID == "" {
2023-02-26 20:49:29 +11:00
m.Error("call-id is required")
return
}
2023-02-26 20:49:29 +11:00
switch method {
2023-08-27 16:52:07 +10:00
case CallBinding:
2023-02-26 20:49:29 +11:00
var options CallOptions
err := params.ToStruct(&options)
if err != nil {
m.callErrorCallback(window, "Error parsing call options: %s", callID, err)
2023-02-26 20:49:29 +11:00
return
}
var boundMethod *BoundMethod
2023-08-27 16:52:07 +10:00
if options.PackageName != "" {
boundMethod = globalApplication.bindings.Get(&options)
2023-08-24 20:24:08 +10:00
if boundMethod == nil {
m.callErrorCallback(window, "Error getting binding for method: %s", callID, fmt.Errorf("method '%s' not found", options.Name()))
return
}
} else {
2023-08-27 16:52:07 +10:00
boundMethod = globalApplication.bindings.GetByID(options.MethodID)
}
if boundMethod == nil {
2023-08-24 20:24:08 +10:00
m.callErrorCallback(window, "Error getting binding for method: %s", callID, fmt.Errorf("method ID '%s' not found", options.Name()))
2023-02-26 20:49:29 +11:00
return
}
ctx, cancel := context.WithCancel(context.WithoutCancel(r.Context()))
2024-05-02 13:46:07 +02:00
ambiguousID := false
m.l.Lock()
if m.runningCalls[*callID] != nil {
2024-05-02 13:46:07 +02:00
ambiguousID = true
} else {
m.runningCalls[*callID] = cancel
}
m.l.Unlock()
2024-05-02 13:46:07 +02:00
if ambiguousID {
cancel()
m.callErrorCallback(window, "Error calling method: %s, a method call with the same id is already running", callID, err)
return
}
2023-02-26 20:49:29 +11:00
go func() {
defer func() {
cancel()
m.l.Lock()
delete(m.runningCalls, *callID)
m.l.Unlock()
}()
result, err := boundMethod.Call(ctx, options.Args)
2023-02-26 20:49:29 +11:00
if err != nil {
m.callErrorCallback(window, "Error calling method: %s", callID, err)
2023-02-26 20:49:29 +11:00
return
}
var jsonResult = []byte("{}")
if result != nil {
// convert result to json
jsonResult, err = json.Marshal(result)
if err != nil {
m.callErrorCallback(window, "Error converting result to json: %s", callID, err)
return
}
2023-02-26 20:49:29 +11:00
}
m.callCallback(window, callID, string(jsonResult), true)
2024-05-02 13:46:07 +02:00
var jsonArgs struct {
Args json.RawMessage `json:"args"`
}
params.ToStruct(&jsonArgs)
m.Info("Call Binding:", "method", boundMethod, "args", string(jsonArgs.Args), "result", result)
2023-02-26 20:49:29 +11:00
}()
m.ok(rw)
default:
2023-09-08 12:03:55 +10:00
m.httpError(rw, "Unknown call method: %d", method)
2023-02-26 20:49:29 +11:00
}
}