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; |
| 36 | using System.Reactive.Linq; |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 37 | using System.Threading; |
| 38 | using System.Threading.Tasks; |
| 39 | using Grpc.Core.Utils; |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 40 | |
| 41 | namespace math |
| 42 | { |
| 43 | /// <summary> |
| 44 | /// Implementation of MathService server |
| 45 | /// </summary> |
| 46 | public class MathServiceImpl : MathGrpc.IMathService |
| 47 | { |
| 48 | public void Div(DivArgs request, IObserver<DivReply> responseObserver) |
| 49 | { |
| 50 | var response = DivInternal(request); |
| 51 | responseObserver.OnNext(response); |
| 52 | responseObserver.OnCompleted(); |
| 53 | } |
| 54 | |
| 55 | public void Fib(FibArgs request, IObserver<Num> responseObserver) |
| 56 | { |
| 57 | if (request.Limit <= 0) |
| 58 | { |
| 59 | // TODO: support cancellation.... |
| 60 | throw new NotImplementedException("Not implemented yet"); |
| 61 | } |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 62 | |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 63 | if (request.Limit > 0) |
| 64 | { |
| 65 | foreach (var num in FibInternal(request.Limit)) |
| 66 | { |
| 67 | responseObserver.OnNext(num); |
| 68 | } |
| 69 | responseObserver.OnCompleted(); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | public IObserver<Num> Sum(IObserver<Num> responseObserver) |
| 74 | { |
| 75 | var recorder = new RecordingObserver<Num>(); |
| 76 | Task.Factory.StartNew(() => { |
| 77 | |
| 78 | List<Num> inputs = recorder.ToList().Result; |
| 79 | |
| 80 | long sum = 0; |
| 81 | foreach (Num num in inputs) |
| 82 | { |
| 83 | sum += num.Num_; |
| 84 | } |
| 85 | |
| 86 | responseObserver.OnNext(Num.CreateBuilder().SetNum_(sum).Build()); |
| 87 | responseObserver.OnCompleted(); |
| 88 | }); |
| 89 | return recorder; |
| 90 | } |
| 91 | |
| 92 | public IObserver<DivArgs> DivMany(IObserver<DivReply> responseObserver) |
| 93 | { |
| 94 | return new DivObserver(responseObserver); |
| 95 | } |
| 96 | |
| 97 | static DivReply DivInternal(DivArgs args) |
| 98 | { |
| 99 | long quotient = args.Dividend / args.Divisor; |
| 100 | long remainder = args.Dividend % args.Divisor; |
| 101 | return new DivReply.Builder { Quotient = quotient, Remainder = remainder }.Build(); |
| 102 | } |
| 103 | |
| 104 | static IEnumerable<Num> FibInternal(long n) |
| 105 | { |
| 106 | long a = 1; |
| 107 | yield return new Num.Builder { Num_=a }.Build(); |
| 108 | |
| 109 | long b = 1; |
| 110 | for (long i = 0; i < n - 1; i++) |
| 111 | { |
| 112 | long temp = a; |
| 113 | a = b; |
| 114 | b = temp + b; |
| 115 | yield return new Num.Builder { Num_=a }.Build(); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | private class DivObserver : IObserver<DivArgs> { |
| 120 | |
| 121 | readonly IObserver<DivReply> responseObserver; |
| 122 | |
| 123 | public DivObserver(IObserver<DivReply> responseObserver) |
| 124 | { |
| 125 | this.responseObserver = responseObserver; |
| 126 | } |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 127 | |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 128 | public void OnCompleted() |
| 129 | { |
| 130 | Task.Factory.StartNew(() => |
| 131 | responseObserver.OnCompleted()); |
| 132 | } |
| 133 | |
| 134 | public void OnError(Exception error) |
| 135 | { |
| 136 | throw new NotImplementedException(); |
| 137 | } |
| 138 | |
| 139 | public void OnNext(DivArgs value) |
| 140 | { |
| 141 | // TODO: currently we need this indirection because |
| 142 | // responseObserver waits for write to finish, this |
| 143 | // callback is called from grpc threadpool which |
| 144 | // currently only has one thread. |
| 145 | // Same story for OnCompleted(). |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 146 | Task.Factory.StartNew(() => |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 147 | responseObserver.OnNext(DivInternal(value))); |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |