You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

88 lines
2.5 KiB

  1. /*
  2. *
  3. * Copyright 2019 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. // Binary server is an example server.
  19. package main
  20. import (
  21. "context"
  22. "flag"
  23. "fmt"
  24. "log"
  25. "net"
  26. "google.golang.org/grpc"
  27. "google.golang.org/grpc/codes"
  28. ecpb "google.golang.org/grpc/examples/features/proto/echo"
  29. hwpb "google.golang.org/grpc/examples/helloworld/helloworld"
  30. "google.golang.org/grpc/reflection"
  31. "google.golang.org/grpc/status"
  32. )
  33. var port = flag.Int("port", 50051, "the port to serve on")
  34. // hwServer is used to implement helloworld.GreeterServer.
  35. type hwServer struct{}
  36. // SayHello implements helloworld.GreeterServer
  37. func (s *hwServer) SayHello(ctx context.Context, in *hwpb.HelloRequest) (*hwpb.HelloReply, error) {
  38. return &hwpb.HelloReply{Message: "Hello " + in.Name}, nil
  39. }
  40. type ecServer struct{}
  41. func (s *ecServer) UnaryEcho(ctx context.Context, req *ecpb.EchoRequest) (*ecpb.EchoResponse, error) {
  42. return &ecpb.EchoResponse{Message: req.Message}, nil
  43. }
  44. func (s *ecServer) ServerStreamingEcho(*ecpb.EchoRequest, ecpb.Echo_ServerStreamingEchoServer) error {
  45. return status.Errorf(codes.Unimplemented, "not implemented")
  46. }
  47. func (s *ecServer) ClientStreamingEcho(ecpb.Echo_ClientStreamingEchoServer) error {
  48. return status.Errorf(codes.Unimplemented, "not implemented")
  49. }
  50. func (s *ecServer) BidirectionalStreamingEcho(ecpb.Echo_BidirectionalStreamingEchoServer) error {
  51. return status.Errorf(codes.Unimplemented, "not implemented")
  52. }
  53. func main() {
  54. flag.Parse()
  55. lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port))
  56. if err != nil {
  57. log.Fatalf("failed to listen: %v", err)
  58. }
  59. fmt.Printf("server listening at %v\n", lis.Addr())
  60. s := grpc.NewServer()
  61. // Register Greeter on the server.
  62. hwpb.RegisterGreeterServer(s, &hwServer{})
  63. // Register RouteGuide on the same server.
  64. ecpb.RegisterEchoServer(s, &ecServer{})
  65. // Register reflection service on gRPC server.
  66. reflection.Register(s)
  67. if err := s.Serve(lis); err != nil {
  68. log.Fatalf("failed to serve: %v", err)
  69. }
  70. }