|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright 2018 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 | + |
| 19 | +package main |
| 20 | + |
| 21 | +import ( |
| 22 | + "context" |
| 23 | + "fmt" |
| 24 | + "log" |
| 25 | + "time" |
| 26 | + |
| 27 | + "google.golang.org/grpc" |
| 28 | + ecpb "google.golang.org/grpc/examples/features/proto/echo" |
| 29 | + hwpb "google.golang.org/grpc/examples/helloworld/helloworld" |
| 30 | +) |
| 31 | + |
| 32 | +const ( |
| 33 | + address = "localhost:50051" |
| 34 | +) |
| 35 | + |
| 36 | +// callSayHello calls SayHello on c with the given name, and prints the |
| 37 | +// response. |
| 38 | +func callSayHello(c hwpb.GreeterClient, name string) { |
| 39 | + ctx, cancel := context.WithTimeout(context.Background(), time.Second) |
| 40 | + defer cancel() |
| 41 | + r, err := c.SayHello(ctx, &hwpb.HelloRequest{Name: name}) |
| 42 | + if err != nil { |
| 43 | + log.Fatalf("client.SayHello(_) = _, %v", err) |
| 44 | + } |
| 45 | + fmt.Println("Greeting: ", r.Message) |
| 46 | +} |
| 47 | + |
| 48 | +func callUnaryEcho(client ecpb.EchoClient, message string) { |
| 49 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 50 | + defer cancel() |
| 51 | + resp, err := client.UnaryEcho(ctx, &ecpb.EchoRequest{Message: message}) |
| 52 | + if err != nil { |
| 53 | + log.Fatalf("client.UnaryEcho(_) = _, %v: ", err) |
| 54 | + } |
| 55 | + fmt.Println("UnaryEcho: ", resp.Message) |
| 56 | +} |
| 57 | + |
| 58 | +func main() { |
| 59 | + // Set up a connection to the server. |
| 60 | + conn, err := grpc.Dial(address, grpc.WithInsecure()) |
| 61 | + if err != nil { |
| 62 | + log.Fatalf("did not connect: %v", err) |
| 63 | + } |
| 64 | + defer conn.Close() |
| 65 | + |
| 66 | + fmt.Println("--- calling helloworld.Greeter/SayHello ---") |
| 67 | + // Make a greeter client and send an RPC. |
| 68 | + hwc := hwpb.NewGreeterClient(conn) |
| 69 | + callSayHello(hwc, "multiplex") |
| 70 | + |
| 71 | + fmt.Println() |
| 72 | + fmt.Println("--- calling routeguide.RouteGuide/GetFeature ---") |
| 73 | + // Make a routeguild client with the same ClientConn. |
| 74 | + rgc := ecpb.NewEchoClient(conn) |
| 75 | + callUnaryEcho(rgc, "this is examples/multiplex") |
| 76 | +} |
0 commit comments