aboutsummaryrefslogtreecommitdiffhomepage
path: root/examples/csharp/route_guide/RouteGuideClient
diff options
context:
space:
mode:
authorGravatar Jan Tattermusch <jtattermusch@google.com>2015-08-31 10:27:27 -0700
committerGravatar Jan Tattermusch <jtattermusch@google.com>2015-09-02 12:59:00 -0700
commit121c4d1221f6b6f32288cb5ee9bca3e221a9c4d3 (patch)
treecbed8aef9162ca4d09cfed6d5c6e98235d519fad /examples/csharp/route_guide/RouteGuideClient
parent97eaec1cdc28174f1f5bd2bd74aba19f0869d75e (diff)
polishing route_guide example
Diffstat (limited to 'examples/csharp/route_guide/RouteGuideClient')
-rw-r--r--examples/csharp/route_guide/RouteGuideClient/Program.cs86
1 files changed, 45 insertions, 41 deletions
diff --git a/examples/csharp/route_guide/RouteGuideClient/Program.cs b/examples/csharp/route_guide/RouteGuideClient/Program.cs
index 2f1d631cce..4ddb526585 100644
--- a/examples/csharp/route_guide/RouteGuideClient/Program.cs
+++ b/examples/csharp/route_guide/RouteGuideClient/Program.cs
@@ -30,27 +30,24 @@ namespace Examples
{
Log("*** GetFeature: lat={0} lon={1}", lat, lon);
- Point request = Point.CreateBuilder().SetLatitude(lat).SetLongitude(lon).Build();
+ Point request = new Point { Latitude = lat, Longitude = lon };
Feature feature = client.GetFeature(request);
- if (RouteGuideUtil.Exists(feature))
+ if (feature.Exists())
{
Log("Found feature called \"{0}\" at {1}, {2}",
- feature.Name,
- RouteGuideUtil.GetLatitude(feature.Location),
- RouteGuideUtil.GetLongitude(feature.Location));
+ feature.Name, feature.Location.GetLatitude(), feature.Location.GetLongitude());
}
else
{
Log("Found no feature at {0}, {1}",
- RouteGuideUtil.GetLatitude(feature.Location),
- RouteGuideUtil.GetLongitude(feature.Location));
+ feature.Location.GetLatitude(), feature.Location.GetLongitude());
}
}
catch (RpcException e)
{
Log("RPC failed " + e);
- throw e;
+ throw;
}
}
@@ -65,18 +62,20 @@ namespace Examples
Log("*** ListFeatures: lowLat={0} lowLon={1} hiLat={2} hiLon={3}", lowLat, lowLon, hiLat,
hiLon);
- Rectangle request =
- Rectangle.CreateBuilder()
- .SetLo(Point.CreateBuilder().SetLatitude(lowLat).SetLongitude(lowLon).Build())
- .SetHi(Point.CreateBuilder().SetLatitude(hiLat).SetLongitude(hiLon).Build()).Build();
+ Rectangle request = new Rectangle
+ {
+ Lo = new Point { Latitude = lowLat, Longitude = lowLon },
+ Hi = new Point { Latitude = hiLat, Longitude = hiLon }
+ };
using (var call = client.ListFeatures(request))
{
+ var responseStream = call.ResponseStream;
StringBuilder responseLog = new StringBuilder("Result: ");
- while (await call.ResponseStream.MoveNext())
+ while (await responseStream.MoveNext())
{
- Feature feature = call.ResponseStream.Current;
+ Feature feature = responseStream.Current;
responseLog.Append(feature.ToString());
}
Log(responseLog.ToString());
@@ -85,7 +84,7 @@ namespace Examples
catch (RpcException e)
{
Log("RPC failed " + e);
- throw e;
+ throw;
}
}
@@ -107,8 +106,7 @@ namespace Examples
{
int index = rand.Next(features.Count);
Point point = features[index].Location;
- Log("Visiting point {0}, {1}", RouteGuideUtil.GetLatitude(point),
- RouteGuideUtil.GetLongitude(point));
+ Log("Visiting point {0}, {1}", point.GetLatitude(), point.GetLongitude());
await call.RequestStream.WriteAsync(point);
@@ -117,7 +115,7 @@ namespace Examples
}
await call.RequestStream.CompleteAsync();
- RouteSummary summary = await call.Result;
+ RouteSummary summary = await call.ResponseAsync;
Log("Finished trip with {0} points. Passed {1} features. "
+ "Travelled {2} meters. It took {3} seconds.", summary.PointCount,
summary.FeatureCount, summary.Distance, summary.ElapsedTime);
@@ -128,7 +126,7 @@ namespace Examples
catch (RpcException e)
{
Log("RPC failed", e);
- throw e;
+ throw;
}
}
@@ -141,8 +139,13 @@ namespace Examples
try
{
Log("*** RouteChat");
- var requests =
- new List<RouteNote> { NewNote("First message", 0, 0), NewNote("Second message", 0, 1), NewNote("Third message", 1, 0), NewNote("Fourth message", 1, 1) };
+ var requests = new List<RouteNote>
+ {
+ NewNote("First message", 0, 0),
+ NewNote("Second message", 0, 1),
+ NewNote("Third message", 1, 0),
+ NewNote("Fourth message", 0, 0)
+ };
using (var call = client.RouteChat())
{
@@ -172,7 +175,7 @@ namespace Examples
catch (RpcException e)
{
Log("RPC failed", e);
- throw e;
+ throw;
}
}
@@ -188,36 +191,37 @@ namespace Examples
private RouteNote NewNote(string message, int lat, int lon)
{
- return RouteNote.CreateBuilder().SetMessage(message).SetLocation(
- Point.CreateBuilder().SetLatitude(lat).SetLongitude(lat).Build()).Build();
+ return new RouteNote
+ {
+ Message = message,
+ Location = new Point { Latitude = lat, Longitude = lon }
+ };
}
}
static void Main(string[] args)
{
- GrpcEnvironment.Initialize();
+ var channel = new Channel("127.0.0.1:50052", Credentials.Insecure);
+ var client = new RouteGuideClient(RouteGuide.NewClient(channel));
- using (Channel channel = new Channel("127.0.0.1:50052"))
- {
- var client = new RouteGuideClient(RouteGuide.NewStub(channel));
-
- // Looking for a valid feature
- client.GetFeature(409146138, -746188906);
+ // Looking for a valid feature
+ client.GetFeature(409146138, -746188906);
- // Feature missing.
- client.GetFeature(0, 0);
+ // Feature missing.
+ client.GetFeature(0, 0);
- // Looking for features between 40, -75 and 42, -73.
- client.ListFeatures(400000000, -750000000, 420000000, -730000000).Wait();
+ // Looking for features between 40, -75 and 42, -73.
+ client.ListFeatures(400000000, -750000000, 420000000, -730000000).Wait();
- // Record a few randomly selected points from the features file.
- client.RecordRoute(RouteGuideUtil.ParseFeatures(RouteGuideUtil.DefaultFeaturesFile), 10).Wait();
+ // Record a few randomly selected points from the features file.
+ client.RecordRoute(RouteGuideUtil.ParseFeatures(RouteGuideUtil.DefaultFeaturesFile), 10).Wait();
- // Send and receive some notes.
- client.RouteChat().Wait();
- }
+ // Send and receive some notes.
+ client.RouteChat().Wait();
- GrpcEnvironment.Shutdown();
+ channel.ShutdownAsync().Wait();
+ Console.WriteLine("Press any key to exit...");
+ Console.ReadKey();
}
}
}