blob: 0cc1b791372abc842b3c122c65a2226b14936459 [file] [log] [blame]
Jan Tattermuscha7fff862015-02-13 11:08:08 -08001#region Copyright notice and license
2
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003// Copyright 2015-2016 gRPC authors.
Craig Tiller190d3602015-02-18 09:23:38 -08004//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005// 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 Tiller190d3602015-02-18 09:23:38 -08008//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009// http://www.apache.org/licenses/LICENSE-2.0
Craig Tiller190d3602015-02-18 09:23:38 -080010//
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011// 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 Tattermuscha7fff862015-02-13 11:08:08 -080016
17#endregion
18
Jan Tattermusch15111f52015-02-05 18:15:14 -080019using System;
Jan Tattermusch15111f52015-02-05 18:15:14 -080020using System.Collections.Generic;
Jan Tattermusch30868622015-02-19 09:22:33 -080021using System.Threading;
22using System.Threading.Tasks;
Jan Tattermuscha5272b62015-04-30 11:56:46 -070023using Grpc.Core;
Jan Tattermusch30868622015-02-19 09:22:33 -080024using Grpc.Core.Utils;
Jan Tattermusch15111f52015-02-05 18:15:14 -080025
Jan Tattermusch5c42d8d2015-08-03 08:14:30 -070026namespace Math
Jan Tattermusch15111f52015-02-05 18:15:14 -080027{
28 /// <summary>
29 /// Implementation of MathService server
30 /// </summary>
Jan Tattermuschd39426d2016-03-14 16:07:52 -070031 public class MathServiceImpl : Math.MathBase
Jan Tattermusch15111f52015-02-05 18:15:14 -080032 {
Jan Tattermuschd39426d2016-03-14 16:07:52 -070033 public override Task<DivReply> Div(DivArgs request, ServerCallContext context)
Jan Tattermusch15111f52015-02-05 18:15:14 -080034 {
Jan Tattermuscha5272b62015-04-30 11:56:46 -070035 return Task.FromResult(DivInternal(request));
Jan Tattermusch15111f52015-02-05 18:15:14 -080036 }
37
Jan Tattermuschd39426d2016-03-14 16:07:52 -070038 public override async Task Fib(FibArgs request, IServerStreamWriter<Num> responseStream, ServerCallContext context)
Jan Tattermusch15111f52015-02-05 18:15:14 -080039 {
Jan Tattermuschd6a83972016-05-10 16:38:23 -070040 var limit = request.Limit > 0 ? request.Limit : long.MaxValue;
41 var fibEnumerator = FibInternal(limit).GetEnumerator();
Craig Tiller190d3602015-02-18 09:23:38 -080042
Jan Tattermuschd6a83972016-05-10 16:38:23 -070043 // 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 Tattermusch15111f52015-02-05 18:15:14 -080046 {
Jan Tattermuschd6a83972016-05-10 16:38:23 -070047 await responseStream.WriteAsync(fibEnumerator.Current);
48 await Task.Delay(100);
Jan Tattermusch15111f52015-02-05 18:15:14 -080049 }
50 }
51
Jan Tattermuschd39426d2016-03-14 16:07:52 -070052 public override async Task<Num> Sum(IAsyncStreamReader<Num> requestStream, ServerCallContext context)
Jan Tattermusch15111f52015-02-05 18:15:14 -080053 {
Jan Tattermuscha5272b62015-04-30 11:56:46 -070054 long sum = 0;
Jan Tattermuschb8c77c52017-08-09 09:05:54 +020055 await requestStream.ForEachAsync(num =>
Jan Tattermusch075dde42015-03-11 18:21:00 -070056 {
Jan Tattermuscha5272b62015-04-30 11:56:46 -070057 sum += num.Num_;
Jan Tattermuschb8c77c52017-08-09 09:05:54 +020058 return TaskUtils.CompletedTask;
Jan Tattermusch15111f52015-02-05 18:15:14 -080059 });
Jan Tattermusch28526312015-08-03 09:21:38 -070060 return new Num { Num_ = sum };
Jan Tattermusch15111f52015-02-05 18:15:14 -080061 }
62
Jan Tattermuschd39426d2016-03-14 16:07:52 -070063 public override async Task DivMany(IAsyncStreamReader<DivArgs> requestStream, IServerStreamWriter<DivReply> responseStream, ServerCallContext context)
Jan Tattermusch15111f52015-02-05 18:15:14 -080064 {
Jan Tattermuschf22abfb2015-08-09 16:15:34 -070065 await requestStream.ForEachAsync(async divArgs => await responseStream.WriteAsync(DivInternal(divArgs)));
Jan Tattermusch15111f52015-02-05 18:15:14 -080066 }
67
68 static DivReply DivInternal(DivArgs args)
69 {
Jan Tattermuschd6a83972016-05-10 16:38:23 -070070 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 Tattermusch15111f52015-02-05 18:15:14 -080077 long quotient = args.Dividend / args.Divisor;
78 long remainder = args.Dividend % args.Divisor;
Jan Tattermusch28526312015-08-03 09:21:38 -070079 return new DivReply { Quotient = quotient, Remainder = remainder };
Jan Tattermusch15111f52015-02-05 18:15:14 -080080 }
81
82 static IEnumerable<Num> FibInternal(long n)
83 {
84 long a = 1;
Jan Tattermusch28526312015-08-03 09:21:38 -070085 yield return new Num { Num_ = a };
Jan Tattermusch15111f52015-02-05 18:15:14 -080086
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 Tattermusch28526312015-08-03 09:21:38 -070093 yield return new Num { Num_ = a };
Jan Tattermusch15111f52015-02-05 18:15:14 -080094 }
Jan Tattermuscha5272b62015-04-30 11:56:46 -070095 }
Jan Tattermusch15111f52015-02-05 18:15:14 -080096 }
97}