Program.cs 9.0 KB

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