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

RSDK-8832 - add docs around foreign service handler #4381

Merged
merged 3 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions robot/robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,8 @@ func TypeAndMethodDescFromMethod(r Robot, method string) (*resource.RPCAPI, *des
if len(methodParts) != 3 {
return nil, nil, grpc.UnimplementedError
}
protoSvc := methodParts[1]
protoMethod := methodParts[2]
protoSvc := methodParts[1] // e.g. viam.component.arm.v1.ArmService
protoMethod := methodParts[2] // e.g. DoCommand

var foundType *resource.RPCAPI
for _, resAPI := range r.ResourceRPCAPIs() {
Expand Down
10 changes: 10 additions & 0 deletions robot/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,12 @@ func (svc *webService) initMux(options weboptions.Options) (*goji.Mux, error) {
return mux, nil
}

// foreignServiceHandler is a bidi-streaming RPC service handler to support custom APIs.
// It is invoked instead of returning the "unimplemented" gRPC error whenever a request is received for
// an unregistered service or method. These method could be registered on a remote viam-server or a module server
// so this handler will attempt to route the request to the correct next node in the chain.
func (svc *webService) foreignServiceHandler(srv interface{}, stream googlegrpc.ServerStream) error {
// method will be in the form of PackageName.ServiceName/MethodName
method, ok := googlegrpc.MethodFromServerStream(stream)
if !ok {
return grpc.UnimplementedError
Expand All @@ -939,10 +944,15 @@ func (svc *webService) foreignServiceHandler(srv interface{}, stream googlegrpc.

firstMsg := dynamic.NewMessage(methodDesc.GetInputType())

// The stream blocks until it receives a message and attempts to deserialize
// the message into firstMsg - it will error out if the received message cannot
// be marshalled into the expected type.
if err := stream.RecvMsg(firstMsg); err != nil {
return err
}

// We expect each message to contain a "name" argument which will allow us to route
// the message towards the correct destination.
resource, fqName, err := robot.ResourceFromProtoMessage(svc.r, firstMsg, subType.API)
if err != nil {
svc.logger.Errorw("unable to route foreign message", "error", err)
Expand Down
Loading