blob: e247ac9d73542691dc345e85be4452508b6dd731 [file] [log] [blame]
Jan Tattermuscha7fff862015-02-13 11:08:08 -08001#region Copyright notice and license
2
Jan Tattermuschaf77b3d2015-02-13 11:22:21 -08003// Copyright 2015, Google Inc.
Jan Tattermuscha7fff862015-02-13 11:08:08 -08004// All rights reserved.
Craig Tiller190d3602015-02-18 09:23:38 -08005//
Jan Tattermuscha7fff862015-02-13 11:08:08 -08006// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are
8// met:
Craig Tiller190d3602015-02-18 09:23:38 -08009//
Jan Tattermuscha7fff862015-02-13 11:08:08 -080010// * Redistributions of source code must retain the above copyright
11// notice, this list of conditions and the following disclaimer.
12// * Redistributions in binary form must reproduce the above
13// copyright notice, this list of conditions and the following disclaimer
14// in the documentation and/or other materials provided with the
15// distribution.
16// * Neither the name of Google Inc. nor the names of its
17// contributors may be used to endorse or promote products derived from
18// this software without specific prior written permission.
Craig Tiller190d3602015-02-18 09:23:38 -080019//
Jan Tattermuscha7fff862015-02-13 11:08:08 -080020// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32#endregion
33
Jan Tattermusch15111f52015-02-05 18:15:14 -080034using System;
Jan Tattermusch15111f52015-02-05 18:15:14 -080035using System.Collections.Generic;
Jan Tattermusch30868622015-02-19 09:22:33 -080036using System.Threading;
37using System.Threading.Tasks;
Jan Tattermuscha5272b62015-04-30 11:56:46 -070038using Grpc.Core;
Jan Tattermusch30868622015-02-19 09:22:33 -080039using Grpc.Core.Utils;
Jan Tattermusch15111f52015-02-05 18:15:14 -080040
41namespace math
42{
43 /// <summary>
44 /// Implementation of MathService server
45 /// </summary>
Jan Tattermusch085533e2015-05-07 14:34:45 -070046 public class MathServiceImpl : Math.IMath
Jan Tattermusch15111f52015-02-05 18:15:14 -080047 {
Jan Tattermusch8ab1f7e2015-05-06 15:56:30 -070048 public Task<DivReply> Div(ServerCallContext context, DivArgs request)
Jan Tattermusch15111f52015-02-05 18:15:14 -080049 {
Jan Tattermuscha5272b62015-04-30 11:56:46 -070050 return Task.FromResult(DivInternal(request));
Jan Tattermusch15111f52015-02-05 18:15:14 -080051 }
52
Jan Tattermusch8ab1f7e2015-05-06 15:56:30 -070053 public async Task Fib(ServerCallContext context, FibArgs request, IServerStreamWriter<Num> responseStream)
Jan Tattermusch15111f52015-02-05 18:15:14 -080054 {
55 if (request.Limit <= 0)
56 {
Jan Tattermuscha5272b62015-04-30 11:56:46 -070057 // TODO(jtattermusch): support cancellation
Jan Tattermusch15111f52015-02-05 18:15:14 -080058 throw new NotImplementedException("Not implemented yet");
59 }
Craig Tiller190d3602015-02-18 09:23:38 -080060
Jan Tattermusch15111f52015-02-05 18:15:14 -080061 if (request.Limit > 0)
62 {
63 foreach (var num in FibInternal(request.Limit))
64 {
Jan Tattermusch43dc50b2015-05-18 15:27:19 -070065 await responseStream.WriteAsync(num);
Jan Tattermusch15111f52015-02-05 18:15:14 -080066 }
Jan Tattermusch15111f52015-02-05 18:15:14 -080067 }
68 }
69
Jan Tattermusch8ab1f7e2015-05-06 15:56:30 -070070 public async Task<Num> Sum(ServerCallContext context, IAsyncStreamReader<Num> requestStream)
Jan Tattermusch15111f52015-02-05 18:15:14 -080071 {
Jan Tattermuscha5272b62015-04-30 11:56:46 -070072 long sum = 0;
73 await requestStream.ForEach(async num =>
Jan Tattermusch075dde42015-03-11 18:21:00 -070074 {
Jan Tattermuscha5272b62015-04-30 11:56:46 -070075 sum += num.Num_;
Jan Tattermusch15111f52015-02-05 18:15:14 -080076 });
Jan Tattermuscha5272b62015-04-30 11:56:46 -070077 return Num.CreateBuilder().SetNum_(sum).Build();
Jan Tattermusch15111f52015-02-05 18:15:14 -080078 }
79
Jan Tattermusch8ab1f7e2015-05-06 15:56:30 -070080 public async Task DivMany(ServerCallContext context, IAsyncStreamReader<DivArgs> requestStream, IServerStreamWriter<DivReply> responseStream)
Jan Tattermusch15111f52015-02-05 18:15:14 -080081 {
Jan Tattermuscha5272b62015-04-30 11:56:46 -070082 await requestStream.ForEach(async divArgs =>
83 {
Jan Tattermusch43dc50b2015-05-18 15:27:19 -070084 await responseStream.WriteAsync(DivInternal(divArgs));
Jan Tattermuscha5272b62015-04-30 11:56:46 -070085 });
Jan Tattermusch15111f52015-02-05 18:15:14 -080086 }
87
88 static DivReply DivInternal(DivArgs args)
89 {
90 long quotient = args.Dividend / args.Divisor;
91 long remainder = args.Dividend % args.Divisor;
92 return new DivReply.Builder { Quotient = quotient, Remainder = remainder }.Build();
93 }
94
95 static IEnumerable<Num> FibInternal(long n)
96 {
97 long a = 1;
Jan Tattermusch075dde42015-03-11 18:21:00 -070098 yield return new Num.Builder { Num_ = a }.Build();
Jan Tattermusch15111f52015-02-05 18:15:14 -080099
100 long b = 1;
101 for (long i = 0; i < n - 1; i++)
102 {
103 long temp = a;
104 a = b;
105 b = temp + b;
Jan Tattermusch075dde42015-03-11 18:21:00 -0700106 yield return new Num.Builder { Num_ = a }.Build();
Jan Tattermusch15111f52015-02-05 18:15:14 -0800107 }
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700108 }
Jan Tattermusch15111f52015-02-05 18:15:14 -0800109 }
110}