Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 1 | #region Copyright notice and license |
| 2 | |
Jan Tattermusch | af77b3d | 2015-02-13 11:22:21 -0800 | [diff] [blame] | 3 | // Copyright 2015, Google Inc. |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 4 | // All rights reserved. |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 5 | // |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 6 | // Redistribution and use in source and binary forms, with or without |
| 7 | // modification, are permitted provided that the following conditions are |
| 8 | // met: |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 9 | // |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 10 | // * 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 Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 19 | // |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 20 | // 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 Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 34 | using System; |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 35 | using System.Collections.Generic; |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 36 | using System.Threading; |
| 37 | using System.Threading.Tasks; |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 38 | using Grpc.Core; |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 39 | using Grpc.Core.Utils; |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 40 | |
Jan Tattermusch | 5c42d8d | 2015-08-03 08:14:30 -0700 | [diff] [blame^] | 41 | namespace Math |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 42 | { |
| 43 | /// <summary> |
| 44 | /// Implementation of MathService server |
| 45 | /// </summary> |
Jan Tattermusch | 085533e | 2015-05-07 14:34:45 -0700 | [diff] [blame] | 46 | public class MathServiceImpl : Math.IMath |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 47 | { |
Jan Tattermusch | 25bb2ef | 2015-07-21 12:15:53 -0700 | [diff] [blame] | 48 | public Task<DivReply> Div(DivArgs request, ServerCallContext context) |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 49 | { |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 50 | return Task.FromResult(DivInternal(request)); |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Jan Tattermusch | 25bb2ef | 2015-07-21 12:15:53 -0700 | [diff] [blame] | 53 | public async Task Fib(FibArgs request, IServerStreamWriter<Num> responseStream, ServerCallContext context) |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 54 | { |
| 55 | if (request.Limit <= 0) |
| 56 | { |
Jan Tattermusch | f7cfc8a | 2015-07-23 17:26:35 -0700 | [diff] [blame] | 57 | // keep streaming the sequence until cancelled. |
| 58 | IEnumerator<Num> fibEnumerator = FibInternal(long.MaxValue).GetEnumerator(); |
| 59 | while (!context.CancellationToken.IsCancellationRequested && fibEnumerator.MoveNext()) |
| 60 | { |
| 61 | await responseStream.WriteAsync(fibEnumerator.Current); |
| 62 | await Task.Delay(100); |
| 63 | } |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 64 | } |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 65 | |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 66 | if (request.Limit > 0) |
| 67 | { |
| 68 | foreach (var num in FibInternal(request.Limit)) |
| 69 | { |
Jan Tattermusch | 43dc50b | 2015-05-18 15:27:19 -0700 | [diff] [blame] | 70 | await responseStream.WriteAsync(num); |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 71 | } |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
Jan Tattermusch | 25bb2ef | 2015-07-21 12:15:53 -0700 | [diff] [blame] | 75 | public async Task<Num> Sum(IAsyncStreamReader<Num> requestStream, ServerCallContext context) |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 76 | { |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 77 | long sum = 0; |
| 78 | await requestStream.ForEach(async num => |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 79 | { |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 80 | sum += num.Num_; |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 81 | }); |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 82 | return Num.CreateBuilder().SetNum_(sum).Build(); |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Jan Tattermusch | 25bb2ef | 2015-07-21 12:15:53 -0700 | [diff] [blame] | 85 | public async Task DivMany(IAsyncStreamReader<DivArgs> requestStream, IServerStreamWriter<DivReply> responseStream, ServerCallContext context) |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 86 | { |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 87 | await requestStream.ForEach(async divArgs => |
| 88 | { |
Jan Tattermusch | 43dc50b | 2015-05-18 15:27:19 -0700 | [diff] [blame] | 89 | await responseStream.WriteAsync(DivInternal(divArgs)); |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 90 | }); |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | static DivReply DivInternal(DivArgs args) |
| 94 | { |
| 95 | long quotient = args.Dividend / args.Divisor; |
| 96 | long remainder = args.Dividend % args.Divisor; |
| 97 | return new DivReply.Builder { Quotient = quotient, Remainder = remainder }.Build(); |
| 98 | } |
| 99 | |
| 100 | static IEnumerable<Num> FibInternal(long n) |
| 101 | { |
| 102 | long a = 1; |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 103 | yield return new Num.Builder { Num_ = a }.Build(); |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 104 | |
| 105 | long b = 1; |
| 106 | for (long i = 0; i < n - 1; i++) |
| 107 | { |
| 108 | long temp = a; |
| 109 | a = b; |
| 110 | b = temp + b; |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 111 | yield return new Num.Builder { Num_ = a }.Build(); |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 112 | } |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 113 | } |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 114 | } |
| 115 | } |