blob: 462fab4454fdf6f25b75ab8ad976fea82687e91b [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;
36using System.Reactive.Linq;
Jan Tattermusch30868622015-02-19 09:22:33 -080037using System.Threading;
38using System.Threading.Tasks;
39using Grpc.Core.Utils;
Jan Tattermusch15111f52015-02-05 18:15:14 -080040
41namespace 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 Tiller190d3602015-02-18 09:23:38 -080062
Jan Tattermusch15111f52015-02-05 18:15:14 -080063 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 Tiller190d3602015-02-18 09:23:38 -0800127
Jan Tattermusch15111f52015-02-05 18:15:14 -0800128 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 Tiller190d3602015-02-18 09:23:38 -0800146 Task.Factory.StartNew(() =>
Jan Tattermusch15111f52015-02-05 18:15:14 -0800147 responseObserver.OnNext(DivInternal(value)));
148 }
149 }
150 }
151}
152