1 // Copyright 2015 gRPC authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 using Grpc.Core; 16 using System; 17 using System.Collections.Generic; 18 using System.Linq; 19 using System.Text; 20 using System.Threading.Tasks; 21 22 namespace Routeguide 23 { 24 class Program 25 { 26 /// <summary> 27 /// Sample client code that makes gRPC calls to the server. 28 /// </summary> 29 public class RouteGuideClient 30 { 31 readonly RouteGuide.RouteGuideClient client; 32 RouteGuideClient(RouteGuide.RouteGuideClient client)33 public RouteGuideClient(RouteGuide.RouteGuideClient client) 34 { 35 this.client = client; 36 } 37 38 /// <summary> 39 /// Blocking unary call example. Calls GetFeature and prints the response. 40 /// </summary> GetFeature(int lat, int lon)41 public void GetFeature(int lat, int lon) 42 { 43 try 44 { 45 Log("*** GetFeature: lat={0} lon={1}", lat, lon); 46 47 Point request = new Point { Latitude = lat, Longitude = lon }; 48 49 Feature feature = client.GetFeature(request); 50 if (feature.Exists()) 51 { 52 Log("Found feature called \"{0}\" at {1}, {2}", 53 feature.Name, feature.Location.GetLatitude(), feature.Location.GetLongitude()); 54 } 55 else 56 { 57 Log("Found no feature at {0}, {1}", 58 feature.Location.GetLatitude(), feature.Location.GetLongitude()); 59 } 60 } 61 catch (RpcException e) 62 { 63 Log("RPC failed " + e); 64 throw; 65 } 66 } 67 68 69 /// <summary> 70 /// Server-streaming example. Calls listFeatures with a rectangle of interest. Prints each response feature as it arrives. 71 /// </summary> ListFeatures(int lowLat, int lowLon, int hiLat, int hiLon)72 public async Task ListFeatures(int lowLat, int lowLon, int hiLat, int hiLon) 73 { 74 try 75 { 76 Log("*** ListFeatures: lowLat={0} lowLon={1} hiLat={2} hiLon={3}", lowLat, lowLon, hiLat, 77 hiLon); 78 79 Rectangle request = new Rectangle 80 { 81 Lo = new Point { Latitude = lowLat, Longitude = lowLon }, 82 Hi = new Point { Latitude = hiLat, Longitude = hiLon } 83 }; 84 85 using (var call = client.ListFeatures(request)) 86 { 87 var responseStream = call.ResponseStream; 88 StringBuilder responseLog = new StringBuilder("Result: "); 89 90 while (await responseStream.MoveNext()) 91 { 92 Feature feature = responseStream.Current; 93 responseLog.Append(feature.ToString()); 94 } 95 Log(responseLog.ToString()); 96 } 97 } 98 catch (RpcException e) 99 { 100 Log("RPC failed " + e); 101 throw; 102 } 103 } 104 105 /// <summary> 106 /// Client-streaming example. Sends numPoints randomly chosen points from features 107 /// with a variable delay in between. Prints the statistics when they are sent from the server. 108 /// </summary> RecordRoute(List<Feature> features, int numPoints)109 public async Task RecordRoute(List<Feature> features, int numPoints) 110 { 111 try 112 { 113 Log("*** RecordRoute"); 114 using (var call = client.RecordRoute()) 115 { 116 // Send numPoints points randomly selected from the features list. 117 StringBuilder numMsg = new StringBuilder(); 118 Random rand = new Random(); 119 for (int i = 0; i < numPoints; ++i) 120 { 121 int index = rand.Next(features.Count); 122 Point point = features[index].Location; 123 Log("Visiting point {0}, {1}", point.GetLatitude(), point.GetLongitude()); 124 125 await call.RequestStream.WriteAsync(point); 126 127 // A bit of delay before sending the next one. 128 await Task.Delay(rand.Next(1000) + 500); 129 } 130 await call.RequestStream.CompleteAsync(); 131 132 RouteSummary summary = await call.ResponseAsync; 133 Log("Finished trip with {0} points. Passed {1} features. " 134 + "Travelled {2} meters. It took {3} seconds.", summary.PointCount, 135 summary.FeatureCount, summary.Distance, summary.ElapsedTime); 136 137 Log("Finished RecordRoute"); 138 } 139 } 140 catch (RpcException e) 141 { 142 Log("RPC failed", e); 143 throw; 144 } 145 } 146 147 /// <summary> 148 /// Bi-directional streaming example. Send some chat messages, and print any 149 /// chat messages that are sent from the server. 150 /// </summary> RouteChat()151 public async Task RouteChat() 152 { 153 try 154 { 155 Log("*** RouteChat"); 156 var requests = new List<RouteNote> 157 { 158 NewNote("First message", 0, 0), 159 NewNote("Second message", 0, 1), 160 NewNote("Third message", 1, 0), 161 NewNote("Fourth message", 0, 0) 162 }; 163 164 using (var call = client.RouteChat()) 165 { 166 var responseReaderTask = Task.Run(async () => 167 { 168 while (await call.ResponseStream.MoveNext()) 169 { 170 var note = call.ResponseStream.Current; 171 Log("Got message \"{0}\" at {1}, {2}", note.Message, 172 note.Location.Latitude, note.Location.Longitude); 173 } 174 }); 175 176 foreach (RouteNote request in requests) 177 { 178 Log("Sending message \"{0}\" at {1}, {2}", request.Message, 179 request.Location.Latitude, request.Location.Longitude); 180 181 await call.RequestStream.WriteAsync(request); 182 } 183 await call.RequestStream.CompleteAsync(); 184 await responseReaderTask; 185 186 Log("Finished RouteChat"); 187 } 188 } 189 catch (RpcException e) 190 { 191 Log("RPC failed", e); 192 throw; 193 } 194 } 195 Log(string s, params object[] args)196 private void Log(string s, params object[] args) 197 { 198 Console.WriteLine(string.Format(s, args)); 199 } 200 Log(string s)201 private void Log(string s) 202 { 203 Console.WriteLine(s); 204 } 205 NewNote(string message, int lat, int lon)206 private RouteNote NewNote(string message, int lat, int lon) 207 { 208 return new RouteNote 209 { 210 Message = message, 211 Location = new Point { Latitude = lat, Longitude = lon } 212 }; 213 } 214 } 215 Main(string[] args)216 static void Main(string[] args) 217 { 218 var channel = new Channel("127.0.0.1:50052", ChannelCredentials.Insecure); 219 var client = new RouteGuideClient(new RouteGuide.RouteGuideClient(channel)); 220 221 // Looking for a valid feature 222 client.GetFeature(409146138, -746188906); 223 224 // Feature missing. 225 client.GetFeature(0, 0); 226 227 // Looking for features between 40, -75 and 42, -73. 228 client.ListFeatures(400000000, -750000000, 420000000, -730000000).Wait(); 229 230 // Record a few randomly selected points from the features file. 231 client.RecordRoute(RouteGuideUtil.LoadFeatures(), 10).Wait(); 232 233 // Send and receive some notes. 234 client.RouteChat().Wait(); 235 236 channel.ShutdownAsync().Wait(); 237 Console.WriteLine("Press any key to exit..."); 238 Console.ReadKey(); 239 } 240 } 241 } 242