github.com/nya3jp/tast@v0.0.0-20230601000426-85c8e4d83a9b/src/go.chromium.org/tast/core/internal/bundle/rpc.go (about) 1 // Copyright 2021 The ChromiumOS Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 package bundle 6 7 import ( 8 "io" 9 "strings" 10 11 "google.golang.org/grpc" 12 13 "go.chromium.org/tast/core/errors" 14 "go.chromium.org/tast/core/internal/protocol" 15 "go.chromium.org/tast/core/internal/rpc" 16 "go.chromium.org/tast/core/internal/testing" 17 ) 18 19 // RunRPCServer runs the bundle as an RPC server. 20 func RunRPCServer(r io.Reader, w io.Writer, scfg *StaticConfig) error { 21 reg := scfg.registry 22 return rpc.RunServer(r, w, reg.AllServices(), func(srv *grpc.Server, req *protocol.HandshakeRequest) error { 23 if err := checkRegistrationErrors(reg); err != nil { 24 return err 25 } 26 registerFixtureService(srv, reg) 27 protocol.RegisterTestServiceServer(srv, newTestServer(scfg, req.GetBundleInitParams())) 28 // TODO(b/187793617): Remove this check once we fully migrate to gRPC-based protocol. 29 // The check is currently needed because BundleInitParams is not available for some JSON-based protocol methods. 30 if req.GetBundleInitParams() != nil { 31 if err := reg.InitializeVars(req.GetBundleInitParams().GetVars()); err != nil { 32 return err 33 } 34 } 35 return nil 36 }) 37 } 38 39 // RunRPCServerTCP runs the bundle as an RPC server listening on TCP. 40 func RunRPCServerTCP(port int, handshakeReq *protocol.HandshakeRequest, stdin io.Reader, stdout, stderr io.Writer, scfg *StaticConfig) error { 41 reg := scfg.registry 42 return rpc.RunTCPServer(port, handshakeReq, stdin, stdout, stderr, reg.AllServices(), func(srv *grpc.Server, req *protocol.HandshakeRequest) error { 43 if err := checkRegistrationErrors(reg); err != nil { 44 return err 45 } 46 // TODO(b/187793617): Remove this check once we fully migrate to gRPC-based protocol. 47 // The check is currently needed because BundleInitParams is not available for some JSON-based protocol methods. 48 if req.GetBundleInitParams() != nil { 49 if err := reg.InitializeVars(req.GetBundleInitParams().GetVars()); err != nil { 50 return err 51 } 52 } 53 return nil 54 }) 55 } 56 57 func checkRegistrationErrors(reg *testing.Registry) error { 58 if errs := reg.Errors(); len(errs) > 0 { 59 msgs := make([]string, len(errs)) 60 for i, err := range errs { 61 msgs[i] = err.Error() 62 } 63 return errors.Errorf("bundle initialization failed: %s", strings.Join(msgs, "; ")) 64 } 65 return nil 66 }