From 085533e1dc74ba7d2b41cce52118a0aa4010d7b9 Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Thu, 7 May 2015 14:34:45 -0700 Subject: [PATCH] Replace handwritten math stub by the generated one --- .../Grpc.Examples.MathClient/MathClient.cs | 2 +- .../Grpc.Examples.MathServer/MathServer.cs | 2 +- .../MathClientServerTests.cs | 6 +- src/csharp/Grpc.Examples/MathExamples.cs | 14 +- src/csharp/Grpc.Examples/MathGrpc.cs | 256 ++++++++---------- src/csharp/Grpc.Examples/MathServiceImpl.cs | 2 +- 6 files changed, 121 insertions(+), 161 deletions(-) diff --git a/src/csharp/Grpc.Examples.MathClient/MathClient.cs b/src/csharp/Grpc.Examples.MathClient/MathClient.cs index ca7683d399f..85d9cdc7a61 100644 --- a/src/csharp/Grpc.Examples.MathClient/MathClient.cs +++ b/src/csharp/Grpc.Examples.MathClient/MathClient.cs @@ -43,7 +43,7 @@ namespace math using (Channel channel = new Channel("127.0.0.1:23456")) { - MathGrpc.IMathServiceClient stub = new MathGrpc.MathServiceClientStub(channel); + Math.IMathClient stub = new Math.MathClient(channel); MathExamples.DivExample(stub); MathExamples.DivAsyncExample(stub).Wait(); diff --git a/src/csharp/Grpc.Examples.MathServer/MathServer.cs b/src/csharp/Grpc.Examples.MathServer/MathServer.cs index cfde9b42c76..d05e3f28080 100644 --- a/src/csharp/Grpc.Examples.MathServer/MathServer.cs +++ b/src/csharp/Grpc.Examples.MathServer/MathServer.cs @@ -45,7 +45,7 @@ namespace math GrpcEnvironment.Initialize(); Server server = new Server(); - server.AddServiceDefinition(MathGrpc.BindService(new MathServiceImpl())); + server.AddServiceDefinition(Math.BindService(new MathServiceImpl())); int port = server.AddListeningPort(host, 23456); server.Start(); diff --git a/src/csharp/Grpc.Examples.Tests/MathClientServerTests.cs b/src/csharp/Grpc.Examples.Tests/MathClientServerTests.cs index 4ada95edd6d..2d20b0403a4 100644 --- a/src/csharp/Grpc.Examples.Tests/MathClientServerTests.cs +++ b/src/csharp/Grpc.Examples.Tests/MathClientServerTests.cs @@ -50,7 +50,7 @@ namespace math.Tests string host = "localhost"; Server server; Channel channel; - MathGrpc.IMathServiceClient client; + Math.IMathClient client; [TestFixtureSetUp] public void Init() @@ -58,7 +58,7 @@ namespace math.Tests GrpcEnvironment.Initialize(); server = new Server(); - server.AddServiceDefinition(MathGrpc.BindService(new MathServiceImpl())); + server.AddServiceDefinition(Math.BindService(new MathServiceImpl())); int port = server.AddListeningPort(host, Server.PickUnusedPort); server.Start(); channel = new Channel(host + ":" + port); @@ -69,7 +69,7 @@ namespace math.Tests { headerBuilder.Add(new Metadata.MetadataEntry("customHeader", "abcdef")); }); - client = MathGrpc.NewStub(channel, stubConfig); + client = Math.NewStub(channel, stubConfig); } [TestFixtureTearDown] diff --git a/src/csharp/Grpc.Examples/MathExamples.cs b/src/csharp/Grpc.Examples/MathExamples.cs index dba5a7736cd..d8ea8566cb9 100644 --- a/src/csharp/Grpc.Examples/MathExamples.cs +++ b/src/csharp/Grpc.Examples/MathExamples.cs @@ -39,34 +39,34 @@ namespace math { public static class MathExamples { - public static void DivExample(MathGrpc.IMathServiceClient stub) + public static void DivExample(Math.IMathClient stub) { DivReply result = stub.Div(new DivArgs.Builder { Dividend = 10, Divisor = 3 }.Build()); Console.WriteLine("Div Result: " + result); } - public static async Task DivAsyncExample(MathGrpc.IMathServiceClient stub) + public static async Task DivAsyncExample(Math.IMathClient stub) { Task resultTask = stub.DivAsync(new DivArgs.Builder { Dividend = 4, Divisor = 5 }.Build()); DivReply result = await resultTask; Console.WriteLine("DivAsync Result: " + result); } - public static async Task DivAsyncWithCancellationExample(MathGrpc.IMathServiceClient stub) + public static async Task DivAsyncWithCancellationExample(Math.IMathClient stub) { Task resultTask = stub.DivAsync(new DivArgs.Builder { Dividend = 4, Divisor = 5 }.Build()); DivReply result = await resultTask; Console.WriteLine(result); } - public static async Task FibExample(MathGrpc.IMathServiceClient stub) + public static async Task FibExample(Math.IMathClient stub) { var call = stub.Fib(new FibArgs.Builder { Limit = 5 }.Build()); List result = await call.ResponseStream.ToList(); Console.WriteLine("Fib Result: " + string.Join("|", result)); } - public static async Task SumExample(MathGrpc.IMathServiceClient stub) + public static async Task SumExample(Math.IMathClient stub) { var numbers = new List { @@ -80,7 +80,7 @@ namespace math Console.WriteLine("Sum Result: " + await call.Result); } - public static async Task DivManyExample(MathGrpc.IMathServiceClient stub) + public static async Task DivManyExample(Math.IMathClient stub) { var divArgsList = new List { @@ -93,7 +93,7 @@ namespace math Console.WriteLine("DivMany Result: " + string.Join("|", await call.ResponseStream.ToList())); } - public static async Task DependendRequestsExample(MathGrpc.IMathServiceClient stub) + public static async Task DependendRequestsExample(Math.IMathClient stub) { var numbers = new List { diff --git a/src/csharp/Grpc.Examples/MathGrpc.cs b/src/csharp/Grpc.Examples/MathGrpc.cs index 9ab60137f7d..db680c3da90 100644 --- a/src/csharp/Grpc.Examples/MathGrpc.cs +++ b/src/csharp/Grpc.Examples/MathGrpc.cs @@ -1,162 +1,122 @@ -#region Copyright notice and license - -// Copyright 2015, Google Inc. -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -#endregion +// Generated by the protocol buffer compiler. DO NOT EDIT! +// source: math.proto +#region Designer generated code using System; using System.Threading; using System.Threading.Tasks; using Grpc.Core; -namespace math -{ - /// - /// Math service definitions (this is handwritten version of code that will normally be generated). - /// - public static class MathGrpc +namespace math { + public static class Math + { + static readonly string __ServiceName = "math.Math"; + + static readonly Marshaller __Marshaller_DivArgs = Marshallers.Create((arg) => arg.ToByteArray(), DivArgs.ParseFrom); + static readonly Marshaller __Marshaller_DivReply = Marshallers.Create((arg) => arg.ToByteArray(), DivReply.ParseFrom); + static readonly Marshaller __Marshaller_FibArgs = Marshallers.Create((arg) => arg.ToByteArray(), FibArgs.ParseFrom); + static readonly Marshaller __Marshaller_Num = Marshallers.Create((arg) => arg.ToByteArray(), Num.ParseFrom); + + static readonly Method __Method_Div = new Method( + MethodType.Unary, + "Div", + __Marshaller_DivArgs, + __Marshaller_DivReply); + + static readonly Method __Method_DivMany = new Method( + MethodType.DuplexStreaming, + "DivMany", + __Marshaller_DivArgs, + __Marshaller_DivReply); + + static readonly Method __Method_Fib = new Method( + MethodType.ServerStreaming, + "Fib", + __Marshaller_FibArgs, + __Marshaller_Num); + + static readonly Method __Method_Sum = new Method( + MethodType.ClientStreaming, + "Sum", + __Marshaller_Num, + __Marshaller_Num); + + // client-side stub interface + public interface IMathClient { - static readonly string ServiceName = "/math.Math"; - - static readonly Marshaller DivArgsMarshaller = Marshallers.Create((arg) => arg.ToByteArray(), DivArgs.ParseFrom); - static readonly Marshaller DivReplyMarshaller = Marshallers.Create((arg) => arg.ToByteArray(), DivReply.ParseFrom); - static readonly Marshaller NumMarshaller = Marshallers.Create((arg) => arg.ToByteArray(), Num.ParseFrom); - static readonly Marshaller FibArgsMarshaller = Marshallers.Create((arg) => arg.ToByteArray(), FibArgs.ParseFrom); - - static readonly Method DivMethod = new Method( - MethodType.Unary, - "Div", - DivArgsMarshaller, - DivReplyMarshaller); - - static readonly Method FibMethod = new Method( - MethodType.ServerStreaming, - "Fib", - FibArgsMarshaller, - NumMarshaller); - - static readonly Method SumMethod = new Method( - MethodType.ClientStreaming, - "Sum", - NumMarshaller, - NumMarshaller); - - static readonly Method DivManyMethod = new Method( - MethodType.DuplexStreaming, - "DivMany", - DivArgsMarshaller, - DivReplyMarshaller); - - public interface IMathServiceClient - { - DivReply Div(DivArgs request, CancellationToken token = default(CancellationToken)); - - Task DivAsync(DivArgs request, CancellationToken token = default(CancellationToken)); - - AsyncServerStreamingCall Fib(FibArgs request, CancellationToken token = default(CancellationToken)); - - AsyncClientStreamingCall Sum(CancellationToken token = default(CancellationToken)); - - AsyncDuplexStreamingCall DivMany(CancellationToken token = default(CancellationToken)); - } - - public class MathServiceClientStub : AbstractStub, IMathServiceClient - { - public MathServiceClientStub(Channel channel) : this(channel, StubConfiguration.Default) - { - } - - public MathServiceClientStub(Channel channel, StubConfiguration config) : base(channel, config) - { - } - - public DivReply Div(DivArgs request, CancellationToken token = default(CancellationToken)) - { - var call = CreateCall(ServiceName, DivMethod); - return Calls.BlockingUnaryCall(call, request, token); - } - - public Task DivAsync(DivArgs request, CancellationToken token = default(CancellationToken)) - { - var call = CreateCall(ServiceName, DivMethod); - return Calls.AsyncUnaryCall(call, request, token); - } - - public AsyncServerStreamingCall Fib(FibArgs request, CancellationToken token = default(CancellationToken)) - { - var call = CreateCall(ServiceName, FibMethod); - return Calls.AsyncServerStreamingCall(call, request, token); - } - - public AsyncClientStreamingCall Sum(CancellationToken token = default(CancellationToken)) - { - var call = CreateCall(ServiceName, SumMethod); - return Calls.AsyncClientStreamingCall(call, token); - } - - public AsyncDuplexStreamingCall DivMany(CancellationToken token = default(CancellationToken)) - { - var call = CreateCall(ServiceName, DivManyMethod); - return Calls.AsyncDuplexStreamingCall(call, token); - } - } - - // server-side interface - public interface IMathService - { - Task Div(ServerCallContext context, DivArgs request); - - Task Fib(ServerCallContext context, FibArgs request, IServerStreamWriter responseStream); + DivReply Div(DivArgs request, CancellationToken token = default(CancellationToken)); + Task DivAsync(DivArgs request, CancellationToken token = default(CancellationToken)); + AsyncDuplexStreamingCall DivMany(CancellationToken token = default(CancellationToken)); + AsyncServerStreamingCall Fib(FibArgs request, CancellationToken token = default(CancellationToken)); + AsyncClientStreamingCall Sum(CancellationToken token = default(CancellationToken)); + } - Task Sum(ServerCallContext context, IAsyncStreamReader requestStream); + // server-side interface + public interface IMath + { + Task Div(ServerCallContext context, DivArgs request); + Task DivMany(ServerCallContext context, IAsyncStreamReader requestStream, IServerStreamWriter responseStream); + Task Fib(ServerCallContext context, FibArgs request, IServerStreamWriter responseStream); + Task Sum(ServerCallContext context, IAsyncStreamReader requestStream); + } - Task DivMany(ServerCallContext context, IAsyncStreamReader requestStream, IServerStreamWriter responseStream); - } + // client stub + public class MathClient : AbstractStub, IMathClient + { + public MathClient(Channel channel) : this(channel, StubConfiguration.Default) + { + } + public MathClient(Channel channel, StubConfiguration config) : base(channel, config) + { + } + public DivReply Div(DivArgs request, CancellationToken token = default(CancellationToken)) + { + var call = CreateCall(__ServiceName, __Method_Div); + return Calls.BlockingUnaryCall(call, request, token); + } + public Task DivAsync(DivArgs request, CancellationToken token = default(CancellationToken)) + { + var call = CreateCall(__ServiceName, __Method_Div); + return Calls.AsyncUnaryCall(call, request, token); + } + public AsyncDuplexStreamingCall DivMany(CancellationToken token = default(CancellationToken)) + { + var call = CreateCall(__ServiceName, __Method_DivMany); + return Calls.AsyncDuplexStreamingCall(call, token); + } + public AsyncServerStreamingCall Fib(FibArgs request, CancellationToken token = default(CancellationToken)) + { + var call = CreateCall(__ServiceName, __Method_Fib); + return Calls.AsyncServerStreamingCall(call, request, token); + } + public AsyncClientStreamingCall Sum(CancellationToken token = default(CancellationToken)) + { + var call = CreateCall(__ServiceName, __Method_Sum); + return Calls.AsyncClientStreamingCall(call, token); + } + } - public static ServerServiceDefinition BindService(IMathService serviceImpl) - { - return ServerServiceDefinition.CreateBuilder(ServiceName) - .AddMethod(DivMethod, serviceImpl.Div) - .AddMethod(FibMethod, serviceImpl.Fib) - .AddMethod(SumMethod, serviceImpl.Sum) - .AddMethod(DivManyMethod, serviceImpl.DivMany).Build(); - } + // creates service definition that can be registered with a server + public static ServerServiceDefinition BindService(IMath serviceImpl) + { + return ServerServiceDefinition.CreateBuilder(__ServiceName) + .AddMethod(__Method_Div, serviceImpl.Div) + .AddMethod(__Method_DivMany, serviceImpl.DivMany) + .AddMethod(__Method_Fib, serviceImpl.Fib) + .AddMethod(__Method_Sum, serviceImpl.Sum).Build(); + } - public static IMathServiceClient NewStub(Channel channel) - { - return new MathServiceClientStub(channel); - } + // creates a new client stub + public static IMathClient NewStub(Channel channel) + { + return new MathClient(channel); + } - public static IMathServiceClient NewStub(Channel channel, StubConfiguration config) - { - return new MathServiceClientStub(channel, config); - } + // creates a new client stub + public static IMathClient NewStub(Channel channel, StubConfiguration config) + { + return new MathClient(channel, config); } + } } +#endregion \ No newline at end of file diff --git a/src/csharp/Grpc.Examples/MathServiceImpl.cs b/src/csharp/Grpc.Examples/MathServiceImpl.cs index 800dee87354..16d7724178f 100644 --- a/src/csharp/Grpc.Examples/MathServiceImpl.cs +++ b/src/csharp/Grpc.Examples/MathServiceImpl.cs @@ -44,7 +44,7 @@ namespace math /// /// Implementation of MathService server /// - public class MathServiceImpl : MathGrpc.IMathService + public class MathServiceImpl : Math.IMath { public Task Div(ServerCallContext context, DivArgs request) {