Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 1 | #region Copyright notice and license |
| 2 | |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | // Copyright 2015-2016 gRPC authors. |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 4 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 8 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 10 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 16 | |
| 17 | #endregion |
| 18 | |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 19 | using System; |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 20 | using System.Collections.Generic; |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 21 | using System.Threading; |
| 22 | using System.Threading.Tasks; |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 23 | using Grpc.Core; |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 24 | using Grpc.Core.Utils; |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 25 | |
Jan Tattermusch | 5c42d8d | 2015-08-03 08:14:30 -0700 | [diff] [blame] | 26 | namespace Math |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 27 | { |
| 28 | /// <summary> |
| 29 | /// Implementation of MathService server |
| 30 | /// </summary> |
Jan Tattermusch | d39426d | 2016-03-14 16:07:52 -0700 | [diff] [blame] | 31 | public class MathServiceImpl : Math.MathBase |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 32 | { |
Jan Tattermusch | d39426d | 2016-03-14 16:07:52 -0700 | [diff] [blame] | 33 | public override Task<DivReply> Div(DivArgs request, ServerCallContext context) |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 34 | { |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 35 | return Task.FromResult(DivInternal(request)); |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 36 | } |
| 37 | |
Jan Tattermusch | d39426d | 2016-03-14 16:07:52 -0700 | [diff] [blame] | 38 | public override async Task Fib(FibArgs request, IServerStreamWriter<Num> responseStream, ServerCallContext context) |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 39 | { |
Jan Tattermusch | d6a8397 | 2016-05-10 16:38:23 -0700 | [diff] [blame] | 40 | var limit = request.Limit > 0 ? request.Limit : long.MaxValue; |
| 41 | var fibEnumerator = FibInternal(limit).GetEnumerator(); |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 42 | |
Jan Tattermusch | d6a8397 | 2016-05-10 16:38:23 -0700 | [diff] [blame] | 43 | // Keep streaming the sequence until the call is cancelled. |
| 44 | // Use CancellationToken from ServerCallContext to detect the cancellation. |
| 45 | while (!context.CancellationToken.IsCancellationRequested && fibEnumerator.MoveNext()) |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 46 | { |
Jan Tattermusch | d6a8397 | 2016-05-10 16:38:23 -0700 | [diff] [blame] | 47 | await responseStream.WriteAsync(fibEnumerator.Current); |
| 48 | await Task.Delay(100); |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
Jan Tattermusch | d39426d | 2016-03-14 16:07:52 -0700 | [diff] [blame] | 52 | public override async Task<Num> Sum(IAsyncStreamReader<Num> requestStream, ServerCallContext context) |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 53 | { |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 54 | long sum = 0; |
Jan Tattermusch | b8c77c5 | 2017-08-09 09:05:54 +0200 | [diff] [blame] | 55 | await requestStream.ForEachAsync(num => |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 56 | { |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 57 | sum += num.Num_; |
Jan Tattermusch | b8c77c5 | 2017-08-09 09:05:54 +0200 | [diff] [blame] | 58 | return TaskUtils.CompletedTask; |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 59 | }); |
Jan Tattermusch | 2852631 | 2015-08-03 09:21:38 -0700 | [diff] [blame] | 60 | return new Num { Num_ = sum }; |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Jan Tattermusch | d39426d | 2016-03-14 16:07:52 -0700 | [diff] [blame] | 63 | public override async Task DivMany(IAsyncStreamReader<DivArgs> requestStream, IServerStreamWriter<DivReply> responseStream, ServerCallContext context) |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 64 | { |
Jan Tattermusch | f22abfb | 2015-08-09 16:15:34 -0700 | [diff] [blame] | 65 | await requestStream.ForEachAsync(async divArgs => await responseStream.WriteAsync(DivInternal(divArgs))); |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | static DivReply DivInternal(DivArgs args) |
| 69 | { |
Jan Tattermusch | d6a8397 | 2016-05-10 16:38:23 -0700 | [diff] [blame] | 70 | if (args.Divisor == 0) |
| 71 | { |
| 72 | // One can finish the RPC with non-ok status by throwing RpcException instance. |
| 73 | // Alternatively, resulting status can be set using ServerCallContext.Status |
| 74 | throw new RpcException(new Status(StatusCode.InvalidArgument, "Division by zero")); |
| 75 | } |
| 76 | |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 77 | long quotient = args.Dividend / args.Divisor; |
| 78 | long remainder = args.Dividend % args.Divisor; |
Jan Tattermusch | 2852631 | 2015-08-03 09:21:38 -0700 | [diff] [blame] | 79 | return new DivReply { Quotient = quotient, Remainder = remainder }; |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | static IEnumerable<Num> FibInternal(long n) |
| 83 | { |
| 84 | long a = 1; |
Jan Tattermusch | 2852631 | 2015-08-03 09:21:38 -0700 | [diff] [blame] | 85 | yield return new Num { Num_ = a }; |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 86 | |
| 87 | long b = 1; |
| 88 | for (long i = 0; i < n - 1; i++) |
| 89 | { |
| 90 | long temp = a; |
| 91 | a = b; |
| 92 | b = temp + b; |
Jan Tattermusch | 2852631 | 2015-08-03 09:21:38 -0700 | [diff] [blame] | 93 | yield return new Num { Num_ = a }; |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 94 | } |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 95 | } |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 96 | } |
| 97 | } |