Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 1 | #region Copyright notice and license |
| 2 | |
Jan Tattermusch | 07002d7 | 2015-02-13 11:24:27 -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 | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 34 | using System; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -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.Tasks; |
| 38 | using Grpc.Core.Utils; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 39 | |
| 40 | namespace math |
| 41 | { |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 42 | public static class MathExamples |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 43 | { |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 44 | public static void DivExample(MathGrpc.IMathServiceClient stub) |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 45 | { |
| 46 | DivReply result = stub.Div(new DivArgs.Builder { Dividend = 10, Divisor = 3 }.Build()); |
| 47 | Console.WriteLine("Div Result: " + result); |
| 48 | } |
| 49 | |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 50 | public static void DivAsyncExample(MathGrpc.IMathServiceClient stub) |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 51 | { |
| 52 | Task<DivReply> call = stub.DivAsync(new DivArgs.Builder { Dividend = 4, Divisor = 5 }.Build()); |
| 53 | DivReply result = call.Result; |
| 54 | Console.WriteLine(result); |
| 55 | } |
| 56 | |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 57 | public static void DivAsyncWithCancellationExample(MathGrpc.IMathServiceClient stub) |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 58 | { |
| 59 | Task<DivReply> call = stub.DivAsync(new DivArgs.Builder { Dividend = 4, Divisor = 5 }.Build()); |
| 60 | DivReply result = call.Result; |
| 61 | Console.WriteLine(result); |
| 62 | } |
| 63 | |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 64 | public static void FibExample(MathGrpc.IMathServiceClient stub) |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 65 | { |
| 66 | var recorder = new RecordingObserver<Num>(); |
| 67 | stub.Fib(new FibArgs.Builder { Limit = 5 }.Build(), recorder); |
| 68 | |
| 69 | List<Num> numbers = recorder.ToList().Result; |
| 70 | Console.WriteLine("Fib Result: " + string.Join("|", recorder.ToList().Result)); |
| 71 | } |
| 72 | |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 73 | public static void SumExample(MathGrpc.IMathServiceClient stub) |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 74 | { |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 75 | List<Num> numbers = new List<Num>{new Num.Builder { Num_ = 1 }.Build(), |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 76 | new Num.Builder { Num_ = 2 }.Build(), |
| 77 | new Num.Builder { Num_ = 3 }.Build()}; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 78 | |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 79 | var res = stub.Sum(); |
| 80 | foreach (var num in numbers) { |
| 81 | res.Inputs.OnNext(num); |
| 82 | } |
| 83 | res.Inputs.OnCompleted(); |
| 84 | |
| 85 | Console.WriteLine("Sum Result: " + res.Task.Result); |
| 86 | } |
| 87 | |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 88 | public static void DivManyExample(MathGrpc.IMathServiceClient stub) |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 89 | { |
| 90 | List<DivArgs> divArgsList = new List<DivArgs>{ |
| 91 | new DivArgs.Builder { Dividend = 10, Divisor = 3 }.Build(), |
| 92 | new DivArgs.Builder { Dividend = 100, Divisor = 21 }.Build(), |
| 93 | new DivArgs.Builder { Dividend = 7, Divisor = 2 }.Build() |
| 94 | }; |
| 95 | |
| 96 | var recorder = new RecordingObserver<DivReply>(); |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 97 | |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 98 | var inputs = stub.DivMany(recorder); |
| 99 | foreach (var input in divArgsList) |
| 100 | { |
| 101 | inputs.OnNext(input); |
| 102 | } |
| 103 | inputs.OnCompleted(); |
| 104 | |
| 105 | Console.WriteLine("DivMany Result: " + string.Join("|", recorder.ToList().Result)); |
| 106 | } |
| 107 | |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 108 | public static void DependendRequestsExample(MathGrpc.IMathServiceClient stub) |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 109 | { |
| 110 | var numberList = new List<Num> |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 111 | { new Num.Builder{ Num_ = 1 }.Build(), |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 112 | new Num.Builder{ Num_ = 2 }.Build(), new Num.Builder{ Num_ = 3 }.Build() |
| 113 | }; |
| 114 | |
| 115 | numberList.ToObservable(); |
| 116 | |
| 117 | //IObserver<Num> numbers; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 118 | //Task<Num> call = stub.Sum(out numbers); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 119 | //foreach (var num in numberList) |
| 120 | //{ |
| 121 | // numbers.OnNext(num); |
| 122 | //} |
| 123 | //numbers.OnCompleted(); |
| 124 | |
| 125 | //Num sum = call.Result; |
| 126 | |
| 127 | //DivReply result = stub.Div(new DivArgs.Builder { Dividend = sum.Num_, Divisor = numberList.Count }.Build()); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |