aboutsummaryrefslogtreecommitdiffhomepage
path: root/examples/csharp/route_guide/RouteGuideServer
diff options
context:
space:
mode:
authorGravatar Craig Tiller <ctiller@google.com>2015-08-31 15:59:13 -0700
committerGravatar Craig Tiller <ctiller@google.com>2015-08-31 15:59:13 -0700
commit245bd8875b4640104df433f6a432b5d63799bfb8 (patch)
tree5ddd592e172d9e7fec49300f54db6b0bdff7c275 /examples/csharp/route_guide/RouteGuideServer
parentae69ad1bcf0322200ebeed5476ee9978101beeb7 (diff)
parentcbdfaba7ae41634c66d1a32cbc2ea674a34be9c6 (diff)
Merge github.com:grpc/grpc into second-coming
Diffstat (limited to 'examples/csharp/route_guide/RouteGuideServer')
-rw-r--r--examples/csharp/route_guide/RouteGuideServer/App.config6
-rw-r--r--examples/csharp/route_guide/RouteGuideServer/Program.cs30
-rw-r--r--examples/csharp/route_guide/RouteGuideServer/Properties/AssemblyInfo.cs36
-rw-r--r--examples/csharp/route_guide/RouteGuideServer/RouteGuideImpl.cs184
-rw-r--r--examples/csharp/route_guide/RouteGuideServer/RouteGuideServer.csproj101
-rw-r--r--examples/csharp/route_guide/RouteGuideServer/packages.config11
6 files changed, 368 insertions, 0 deletions
diff --git a/examples/csharp/route_guide/RouteGuideServer/App.config b/examples/csharp/route_guide/RouteGuideServer/App.config
new file mode 100644
index 0000000000..8e15646352
--- /dev/null
+++ b/examples/csharp/route_guide/RouteGuideServer/App.config
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<configuration>
+ <startup>
+ <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
+ </startup>
+</configuration> \ No newline at end of file
diff --git a/examples/csharp/route_guide/RouteGuideServer/Program.cs b/examples/csharp/route_guide/RouteGuideServer/Program.cs
new file mode 100644
index 0000000000..e00b4d6723
--- /dev/null
+++ b/examples/csharp/route_guide/RouteGuideServer/Program.cs
@@ -0,0 +1,30 @@
+using Grpc.Core;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace examples
+{
+ class Program
+ {
+ static void Main(string[] args)
+ {
+ var features = RouteGuideUtil.ParseFeatures(RouteGuideUtil.DefaultFeaturesFile);
+ GrpcEnvironment.Initialize();
+
+ Server server = new Server();
+ server.AddServiceDefinition(RouteGuide.BindService(new RouteGuideImpl(features)));
+ int port = server.AddListeningPort("localhost", 50052);
+ server.Start();
+
+ Console.WriteLine("RouteGuide server listening on port " + port);
+ Console.WriteLine("Press any key to stop the server...");
+ Console.ReadKey();
+
+ server.ShutdownAsync().Wait();
+ GrpcEnvironment.Shutdown();
+ }
+ }
+}
diff --git a/examples/csharp/route_guide/RouteGuideServer/Properties/AssemblyInfo.cs b/examples/csharp/route_guide/RouteGuideServer/Properties/AssemblyInfo.cs
new file mode 100644
index 0000000000..a161b1d602
--- /dev/null
+++ b/examples/csharp/route_guide/RouteGuideServer/Properties/AssemblyInfo.cs
@@ -0,0 +1,36 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// General Information about an assembly is controlled through the following
+// set of attributes. Change these attribute values to modify the information
+// associated with an assembly.
+[assembly: AssemblyTitle("RouteGuideServer")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("RouteGuideServer")]
+[assembly: AssemblyCopyright("Copyright © 2015")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// Setting ComVisible to false makes the types in this assembly not visible
+// to COM components. If you need to access a type in this assembly from
+// COM, set the ComVisible attribute to true on that type.
+[assembly: ComVisible(false)]
+
+// The following GUID is for the ID of the typelib if this project is exposed to COM
+[assembly: Guid("908bdeef-05cc-42bf-9498-c4c573df8925")]
+
+// Version information for an assembly consists of the following four values:
+//
+// Major Version
+// Minor Version
+// Build Number
+// Revision
+//
+// You can specify all the values or you can default the Build and Revision Numbers
+// by using the '*' as shown below:
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]
diff --git a/examples/csharp/route_guide/RouteGuideServer/RouteGuideImpl.cs b/examples/csharp/route_guide/RouteGuideServer/RouteGuideImpl.cs
new file mode 100644
index 0000000000..0bdf386fc3
--- /dev/null
+++ b/examples/csharp/route_guide/RouteGuideServer/RouteGuideImpl.cs
@@ -0,0 +1,184 @@
+using System;
+using System.Collections.Concurrent;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace examples
+{
+ /// <summary>
+ /// Example implementation of RouteGuide server.
+ /// </summary>
+ public class RouteGuideImpl : RouteGuide.IRouteGuide
+ {
+ readonly List<Feature> features;
+ private readonly ConcurrentDictionary<Point, List<RouteNote>> routeNotes =
+ new ConcurrentDictionary<Point, List<RouteNote>>();
+
+ public RouteGuideImpl(List<Feature> features)
+ {
+ this.features = features;
+ }
+
+ /// <summary>
+ /// Gets the feature at the requested point. If no feature at that location
+ /// exists, an unnammed feature is returned at the provided location.
+ /// </summary>
+ public Task<Feature> GetFeature(Grpc.Core.ServerCallContext context, Point request)
+ {
+ return Task.FromResult(CheckFeature(request));
+ }
+
+ /// <summary>
+ /// Gets all features contained within the given bounding rectangle.
+ /// </summary>
+ public async Task ListFeatures(Grpc.Core.ServerCallContext context, Rectangle request, Grpc.Core.IServerStreamWriter<Feature> responseStream)
+ {
+ int left = Math.Min(request.Lo.Longitude, request.Hi.Longitude);
+ int right = Math.Max(request.Lo.Longitude, request.Hi.Longitude);
+ int top = Math.Max(request.Lo.Latitude, request.Hi.Latitude);
+ int bottom = Math.Min(request.Lo.Latitude, request.Hi.Latitude);
+
+ foreach (var feature in features)
+ {
+ if (!RouteGuideUtil.Exists(feature))
+ {
+ continue;
+ }
+
+ int lat = feature.Location.Latitude;
+ int lon = feature.Location.Longitude;
+ if (lon >= left && lon <= right && lat >= bottom && lat <= top)
+ {
+ await responseStream.WriteAsync(feature);
+ }
+ }
+ }
+
+ /// <summary>
+ /// Gets a stream of points, and responds with statistics about the "trip": number of points,
+ /// number of known features visited, total distance traveled, and total time spent.
+ /// </summary>
+ public async Task<RouteSummary> RecordRoute(Grpc.Core.ServerCallContext context, Grpc.Core.IAsyncStreamReader<Point> requestStream)
+ {
+ int pointCount = 0;
+ int featureCount = 0;
+ int distance = 0;
+ Point previous = null;
+ var stopwatch = new Stopwatch();
+ stopwatch.Start();
+
+ while (await requestStream.MoveNext())
+ {
+ var point = requestStream.Current;
+ pointCount++;
+ if (RouteGuideUtil.Exists(CheckFeature(point)))
+ {
+ featureCount++;
+ }
+ if (previous != null)
+ {
+ distance += (int) CalcDistance(previous, point);
+ }
+ previous = point;
+ }
+
+ stopwatch.Stop();
+ return RouteSummary.CreateBuilder().SetPointCount(pointCount)
+ .SetFeatureCount(featureCount).SetDistance(distance)
+ .SetElapsedTime((int) (stopwatch.ElapsedMilliseconds / 1000)).Build();
+ }
+
+ /// <summary>
+ /// Receives a stream of message/location pairs, and responds with a stream of all previous
+ /// messages at each of those locations.
+ /// </summary>
+ public async Task RouteChat(Grpc.Core.ServerCallContext context, Grpc.Core.IAsyncStreamReader<RouteNote> requestStream, Grpc.Core.IServerStreamWriter<RouteNote> responseStream)
+ {
+ while (await requestStream.MoveNext())
+ {
+ var note = requestStream.Current;
+ List<RouteNote> notes = GetOrCreateNotes(note.Location);
+
+ List<RouteNote> prevNotes;
+ lock (notes)
+ {
+ prevNotes = new List<RouteNote>(notes);
+ }
+
+ foreach (var prevNote in prevNotes)
+ {
+ await responseStream.WriteAsync(prevNote);
+ }
+
+ lock (notes)
+ {
+ notes.Add(note);
+ }
+ }
+ }
+
+
+ /// <summary>
+ /// Get the notes list for the given location. If missing, create it.
+ /// </summary>
+ private List<RouteNote> GetOrCreateNotes(Point location)
+ {
+ List<RouteNote> notes = new List<RouteNote>();
+ routeNotes.TryAdd(location, notes);
+ return routeNotes[location];
+ }
+
+ /// <summary>
+ /// Gets the feature at the given point.
+ /// </summary>
+ /// <param name="location">the location to check</param>
+ /// <returns>The feature object at the point Note that an empty name indicates no feature.</returns>
+ private Feature CheckFeature(Point location)
+ {
+ foreach (var feature in features)
+ {
+ if (feature.Location.Latitude == location.Latitude
+ && feature.Location.Longitude == location.Longitude)
+ {
+ return feature;
+ }
+ }
+
+ // No feature was found, return an unnamed feature.
+ return Feature.CreateBuilder().SetName("").SetLocation(location).Build();
+ }
+
+ /// <summary>
+ /// Calculate the distance between two points using the "haversine" formula.
+ /// This code was taken from http://www.movable-type.co.uk/scripts/latlong.html.
+ /// </summary>
+ /// <param name="start">the starting point</param>
+ /// <param name="end">the end point</param>
+ /// <returns>the distance between the points in meters</returns>
+ private static double CalcDistance(Point start, Point end)
+ {
+ double lat1 = RouteGuideUtil.GetLatitude(start);
+ double lat2 = RouteGuideUtil.GetLatitude(end);
+ double lon1 = RouteGuideUtil.GetLongitude(start);
+ double lon2 = RouteGuideUtil.GetLongitude(end);
+ int r = 6371000; // metres
+ double φ1 = ToRadians(lat1);
+ double φ2 = ToRadians(lat2);
+ double Δφ = ToRadians(lat2 - lat1);
+ double Δλ = ToRadians(lon2 - lon1);
+
+ double a = Math.Sin(Δφ / 2) * Math.Sin(Δφ / 2) + Math.Cos(φ1) * Math.Cos(φ2) * Math.Sin(Δλ / 2) * Math.Sin(Δλ / 2);
+ double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
+
+ return r * c;
+ }
+
+ private static double ToRadians(double val)
+ {
+ return (Math.PI / 180) * val;
+ }
+ }
+}
diff --git a/examples/csharp/route_guide/RouteGuideServer/RouteGuideServer.csproj b/examples/csharp/route_guide/RouteGuideServer/RouteGuideServer.csproj
new file mode 100644
index 0000000000..9ac1cfbf4d
--- /dev/null
+++ b/examples/csharp/route_guide/RouteGuideServer/RouteGuideServer.csproj
@@ -0,0 +1,101 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <Import Project="..\packages\grpc.native.csharp_ext.0.10.0\build\portable-net45\grpc.native.csharp_ext.props" Condition="Exists('..\packages\grpc.native.csharp_ext.0.10.0\build\portable-net45\grpc.native.csharp_ext.props')" />
+ <Import Project="..\packages\grpc.dependencies.openssl.redist.1.0.2.2\build\portable-net45\grpc.dependencies.openssl.redist.props" Condition="Exists('..\packages\grpc.dependencies.openssl.redist.1.0.2.2\build\portable-net45\grpc.dependencies.openssl.redist.props')" />
+ <Import Project="..\packages\grpc.dependencies.zlib.redist.1.2.8.9\build\portable-net45\grpc.dependencies.zlib.redist.props" Condition="Exists('..\packages\grpc.dependencies.zlib.redist.1.2.8.9\build\portable-net45\grpc.dependencies.zlib.redist.props')" />
+ <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProjectGuid>{4B7C7794-BE24-4477-ACE7-18259EB73D27}</ProjectGuid>
+ <OutputType>Exe</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>RouteGuideServer</RootNamespace>
+ <AssemblyName>RouteGuideServer</AssemblyName>
+ <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
+ <FileAlignment>512</FileAlignment>
+ <NuGetPackageImportStamp>74781d8b</NuGetPackageImportStamp>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <PlatformTarget>AnyCPU</PlatformTarget>
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Debug\</OutputPath>
+ <DefineConstants>DEBUG;TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+ <PlatformTarget>AnyCPU</PlatformTarget>
+ <DebugType>pdbonly</DebugType>
+ <Optimize>true</Optimize>
+ <OutputPath>bin\Release\</OutputPath>
+ <DefineConstants>TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="Google.ProtocolBuffers">
+ <HintPath>..\packages\Google.ProtocolBuffers.2.4.1.555\lib\net40\Google.ProtocolBuffers.dll</HintPath>
+ </Reference>
+ <Reference Include="Google.ProtocolBuffers.Serialization">
+ <HintPath>..\packages\Google.ProtocolBuffers.2.4.1.555\lib\net40\Google.ProtocolBuffers.Serialization.dll</HintPath>
+ </Reference>
+ <Reference Include="Grpc.Core, Version=0.6.5668.37363, Culture=neutral, processorArchitecture=MSIL">
+ <SpecificVersion>False</SpecificVersion>
+ <HintPath>..\packages\Grpc.Core.0.6.0\lib\net45\Grpc.Core.dll</HintPath>
+ </Reference>
+ <Reference Include="System" />
+ <Reference Include="System.Collections.Immutable, Version=1.1.36.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
+ <SpecificVersion>False</SpecificVersion>
+ <HintPath>..\packages\System.Collections.Immutable.1.1.36\lib\portable-net45+win8+wp8+wpa81\System.Collections.Immutable.dll</HintPath>
+ </Reference>
+ <Reference Include="System.Core" />
+ <Reference Include="System.Interactive.Async">
+ <HintPath>..\packages\Ix-Async.1.2.3\lib\net45\System.Interactive.Async.dll</HintPath>
+ </Reference>
+ <Reference Include="System.Xml.Linq" />
+ <Reference Include="System.Data.DataSetExtensions" />
+ <Reference Include="Microsoft.CSharp" />
+ <Reference Include="System.Data" />
+ <Reference Include="System.Xml" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="Program.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="RouteGuideImpl.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <None Include="App.config" />
+ <None Include="packages.config" />
+ </ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\RouteGuide\RouteGuide.csproj">
+ <Project>{49954d9c-5f17-4662-96b2-73be833dd81a}</Project>
+ <Name>RouteGuide</Name>
+ </ProjectReference>
+ </ItemGroup>
+ <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+ <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
+ <PropertyGroup>
+ <ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
+ </PropertyGroup>
+ <Error Condition="!Exists('..\packages\grpc.dependencies.zlib.redist.1.2.8.9\build\portable-net45\grpc.dependencies.zlib.redist.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\grpc.dependencies.zlib.redist.1.2.8.9\build\portable-net45\grpc.dependencies.zlib.redist.props'))" />
+ <Error Condition="!Exists('..\packages\grpc.dependencies.zlib.redist.1.2.8.9\build\portable-net45\grpc.dependencies.zlib.redist.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\grpc.dependencies.zlib.redist.1.2.8.9\build\portable-net45\grpc.dependencies.zlib.redist.targets'))" />
+ <Error Condition="!Exists('..\packages\grpc.dependencies.openssl.redist.1.0.2.2\build\portable-net45\grpc.dependencies.openssl.redist.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\grpc.dependencies.openssl.redist.1.0.2.2\build\portable-net45\grpc.dependencies.openssl.redist.props'))" />
+ <Error Condition="!Exists('..\packages\grpc.dependencies.openssl.redist.1.0.2.2\build\portable-net45\grpc.dependencies.openssl.redist.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\grpc.dependencies.openssl.redist.1.0.2.2\build\portable-net45\grpc.dependencies.openssl.redist.targets'))" />
+ <Error Condition="!Exists('..\packages\grpc.native.csharp_ext.0.10.0\build\portable-net45\grpc.native.csharp_ext.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\grpc.native.csharp_ext.0.10.0\build\portable-net45\grpc.native.csharp_ext.props'))" />
+ <Error Condition="!Exists('..\packages\grpc.native.csharp_ext.0.10.0\build\portable-net45\grpc.native.csharp_ext.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\grpc.native.csharp_ext.0.10.0\build\portable-net45\grpc.native.csharp_ext.targets'))" />
+ </Target>
+ <Import Project="..\packages\grpc.dependencies.zlib.redist.1.2.8.9\build\portable-net45\grpc.dependencies.zlib.redist.targets" Condition="Exists('..\packages\grpc.dependencies.zlib.redist.1.2.8.9\build\portable-net45\grpc.dependencies.zlib.redist.targets')" />
+ <Import Project="..\packages\grpc.dependencies.openssl.redist.1.0.2.2\build\portable-net45\grpc.dependencies.openssl.redist.targets" Condition="Exists('..\packages\grpc.dependencies.openssl.redist.1.0.2.2\build\portable-net45\grpc.dependencies.openssl.redist.targets')" />
+ <Import Project="..\packages\grpc.native.csharp_ext.0.10.0\build\portable-net45\grpc.native.csharp_ext.targets" Condition="Exists('..\packages\grpc.native.csharp_ext.0.10.0\build\portable-net45\grpc.native.csharp_ext.targets')" />
+ <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
+ Other similar extension points exist, see Microsoft.Common.targets.
+ <Target Name="BeforeBuild">
+ </Target>
+ <Target Name="AfterBuild">
+ </Target>
+ -->
+</Project> \ No newline at end of file
diff --git a/examples/csharp/route_guide/RouteGuideServer/packages.config b/examples/csharp/route_guide/RouteGuideServer/packages.config
new file mode 100644
index 0000000000..5922553bc3
--- /dev/null
+++ b/examples/csharp/route_guide/RouteGuideServer/packages.config
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="utf-8"?>
+<packages>
+ <package id="Google.ProtocolBuffers" version="2.4.1.555" targetFramework="net45" />
+ <package id="Grpc" version="0.6.0" targetFramework="net45" />
+ <package id="Grpc.Core" version="0.6.0" targetFramework="net45" />
+ <package id="grpc.dependencies.openssl.redist" version="1.0.2.2" targetFramework="net45" />
+ <package id="grpc.dependencies.zlib.redist" version="1.2.8.9" targetFramework="net45" />
+ <package id="grpc.native.csharp_ext" version="0.10.0" targetFramework="net45" />
+ <package id="Ix-Async" version="1.2.3" targetFramework="net45" />
+ <package id="System.Collections.Immutable" version="1.1.36" targetFramework="net45" />
+</packages> \ No newline at end of file