Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add handler for unknown method #405

Merged
merged 10 commits into from
Jan 5, 2023
9 changes: 9 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ type Server struct {
// Calls are inserted into this queue, to be handled
// by a goroutine running handleCalls()
callQueue *mpsc.Queue[*Call]

// Handler for custom behavior of unknown methods
HandleUnknownMethod func(m capnp.Method) *Method
}

// New returns a client hook that makes calls to a set of methods.
Expand All @@ -126,6 +129,9 @@ func New(methods []Method, brand any, shutdown Shutdowner) *Server {
// Send starts a method call.
func (srv *Server) Send(ctx context.Context, s capnp.Send) (*capnp.Answer, capnp.ReleaseFunc) {
mm := srv.methods.find(s.Method)
if mm == nil && srv.HandleUnknownMethod != nil {
mm = srv.HandleUnknownMethod(s.Method)
}
if mm == nil {
return capnp.ErrorAnswer(s.Method, capnp.Unimplemented("unimplemented")), func() {}
}
Expand All @@ -150,6 +156,9 @@ func (srv *Server) Send(ctx context.Context, s capnp.Send) (*capnp.Answer, capnp
// Recv starts a method call.
func (srv *Server) Recv(ctx context.Context, r capnp.Recv) capnp.PipelineCaller {
mm := srv.methods.find(r.Method)
if mm == nil && srv.HandleUnknownMethod != nil {
mm = srv.HandleUnknownMethod(r.Method)
}
if mm == nil {
r.Reject(capnp.Unimplemented("unimplemented"))
return nil
Expand Down
44 changes: 44 additions & 0 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import (
"errors"
"strings"
"sync"
"sync/atomic"
"testing"

"github.com/stretchr/testify/require"

"capnproto.org/go/capnp/v3"
air "capnproto.org/go/capnp/v3/internal/aircraftlib"
"capnproto.org/go/capnp/v3/server"
Expand Down Expand Up @@ -91,6 +94,47 @@ func TestServerCall(t *testing.T) {
}
}
})
t.Run("Unimplemented hook", func(t *testing.T) {
hspaay marked this conversation as resolved.
Show resolved Hide resolved
t.Parallel()
var echoText = "are you there?"
var proxyReceived atomic.Value

// start a proxy server with hook
srv := server.New(nil, nil, nil)
srv.HandleUnknownMethod = func(method capnp.Method) *server.Method {
sm := server.Method{
Method: method,
Impl: nil,
}
sm.Impl = func(ctx context.Context, call *server.Call) error {
echoArgs := air.Echo_echo_Params(call.Args())
inText, err := echoArgs.In()
require.NoError(t, err)
proxyReceived.Store(inText)
// pretend we received an answer
echo := air.Echo_echo{Call: call}
resp, _ := echo.AllocResults()
err = resp.SetOut(inText)
return err
}
return &sm
}
blankBoot := capnp.NewClient(srv)
echoClient := air.Echo(blankBoot)
defer echoClient.Release()

ans, finish := echoClient.Echo(context.Background(), func(p air.Echo_echo_Params) error {
err := p.SetIn(echoText)
return err
})
defer finish()
resp, err := ans.Struct()
answerOut, _ := resp.Out()
rxValue := proxyReceived.Load()
require.Equal(t, echoText, rxValue)
assert.Equal(t, echoText, answerOut)
assert.NoError(t, err, "echo.Echo() error != <nil>; want success")
})
}

type callSeq uint32
Expand Down