dubbo.apache.org/dubbo-go/v3@v3.1.1/protocol/grpc/internal/routeguide/client.go (about) 1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. 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 package routeguide 19 20 import ( 21 "io" 22 "math/rand" 23 "time" 24 ) 25 26 import ( 27 log "github.com/dubbogo/gost/log/logger" 28 ) 29 30 import ( 31 "dubbo.apache.org/dubbo-go/v3/config" 32 ) 33 34 func init() { 35 config.SetConsumerServiceByInterfaceName("io.grpc.examples.helloworld.GreeterGrpc$RouteGuide", &RouteGuideClientImpl{}) 36 config.SetConsumerService(&RouteGuideClientImpl{}) 37 } 38 39 // printFeatures lists all the features within the given bounding Rectangle. 40 func PrintFeatures(stream RouteGuide_ListFeaturesClient) { 41 for { 42 feature, err := stream.Recv() 43 if err == io.EOF { 44 break 45 } 46 if err != nil { 47 log.Fatalf("Fait to receive a feature: %v", err) 48 } 49 log.Infof("Feature: name: %q, point:(%v, %v)", feature.GetName(), 50 feature.GetLocation().GetLatitude(), feature.GetLocation().GetLongitude()) 51 } 52 } 53 54 // runRecordRoute sends a sequence of points to server and expects to get a RouteSummary from server. 55 func RunRecordRoute(stream RouteGuide_RecordRouteClient) { 56 // Create a random number of random points 57 r := rand.New(rand.NewSource(time.Now().UnixNano())) 58 pointCount := int(r.Int31n(100)) + 2 // Traverse at least two points 59 var points []*Point 60 for i := 0; i < pointCount; i++ { 61 points = append(points, randomPoint(r)) 62 } 63 log.Infof("Traversing %d points.", len(points)) 64 for _, point := range points { 65 if err := stream.Send(point); err != nil { 66 log.Fatalf("%v.Send(%v) = %v", stream, point, err) 67 } 68 } 69 reply, err := stream.CloseAndRecv() 70 if err != nil { 71 log.Fatalf("%v.CloseAndRecv() got error %v, want %v", stream, err, nil) 72 } 73 log.Infof("Route summary: %v", reply) 74 } 75 76 // runRouteChat receives a sequence of route notes, while sending notes for various locations. 77 func RunRouteChat(stream RouteGuide_RouteChatClient) { 78 notes := []*RouteNote{ 79 {Location: &Point{Latitude: 0, Longitude: 1}, Message: "First message"}, 80 {Location: &Point{Latitude: 0, Longitude: 2}, Message: "Second message"}, 81 {Location: &Point{Latitude: 0, Longitude: 3}, Message: "Third message"}, 82 {Location: &Point{Latitude: 0, Longitude: 1}, Message: "Fourth message"}, 83 {Location: &Point{Latitude: 0, Longitude: 2}, Message: "Fifth message"}, 84 {Location: &Point{Latitude: 0, Longitude: 3}, Message: "Sixth message"}, 85 } 86 waitc := make(chan struct{}) 87 go func() { 88 for { 89 in, err := stream.Recv() 90 if err == io.EOF { 91 // read done. 92 close(waitc) 93 return 94 } 95 if err != nil { 96 log.Fatalf("Failed to receive a note : %v", err) 97 } 98 log.Infof("Got message %s at point(%d, %d)", in.Message, in.Location.Latitude, in.Location.Longitude) 99 } 100 }() 101 for _, note := range notes { 102 if err := stream.Send(note); err != nil { 103 log.Fatalf("Failed to send a note: %v", err) 104 } 105 } 106 stream.CloseSend() 107 <-waitc 108 } 109 110 func randomPoint(r *rand.Rand) *Point { 111 lat := (r.Int31n(180) - 90) * 1e7 112 long := (r.Int31n(360) - 180) * 1e7 113 return &Point{Latitude: lat, Longitude: long} 114 }