Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 1 | #region Copyright notice and license |
Jan Tattermusch | acb842c | 2016-03-25 16:54:14 -0700 | [diff] [blame] | 2 | // Copyright 2015-2016, Google Inc. |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 3 | // All rights reserved. |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 4 | // |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are |
| 7 | // met: |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 8 | // |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 9 | // * Redistributions of source code must retain the above copyright |
| 10 | // notice, this list of conditions and the following disclaimer. |
| 11 | // * Redistributions in binary form must reproduce the above |
| 12 | // copyright notice, this list of conditions and the following disclaimer |
| 13 | // in the documentation and/or other materials provided with the |
| 14 | // distribution. |
| 15 | // * Neither the name of Google Inc. nor the names of its |
| 16 | // contributors may be used to endorse or promote products derived from |
| 17 | // this software without specific prior written permission. |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 18 | // |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 30 | #endregion |
| 31 | |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 32 | using System; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 33 | using System.Collections.Generic; |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 34 | using System.Threading.Tasks; |
| 35 | using Grpc.Core.Utils; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 36 | |
Jan Tattermusch | 2852631 | 2015-08-03 09:21:38 -0700 | [diff] [blame] | 37 | namespace Math |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 38 | { |
Jan Tattermusch | 13cd125 | 2015-03-06 10:11:50 -0800 | [diff] [blame] | 39 | public static class MathExamples |
| 40 | { |
Jan Tattermusch | 809148d | 2016-03-22 15:09:41 -0700 | [diff] [blame] | 41 | public static void DivExample(Math.MathClient client) |
Jan Tattermusch | 13cd125 | 2015-03-06 10:11:50 -0800 | [diff] [blame] | 42 | { |
Jan Tattermusch | 2852631 | 2015-08-03 09:21:38 -0700 | [diff] [blame] | 43 | DivReply result = client.Div(new DivArgs { Dividend = 10, Divisor = 3 }); |
Jan Tattermusch | 13cd125 | 2015-03-06 10:11:50 -0800 | [diff] [blame] | 44 | Console.WriteLine("Div Result: " + result); |
| 45 | } |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 46 | |
Jan Tattermusch | 809148d | 2016-03-22 15:09:41 -0700 | [diff] [blame] | 47 | public static async Task DivAsyncExample(Math.MathClient client) |
Jan Tattermusch | 13cd125 | 2015-03-06 10:11:50 -0800 | [diff] [blame] | 48 | { |
Jan Tattermusch | 2852631 | 2015-08-03 09:21:38 -0700 | [diff] [blame] | 49 | DivReply result = await client.DivAsync(new DivArgs { Dividend = 4, Divisor = 5 }); |
Jan Tattermusch | 8c61db9 | 2015-04-02 15:22:31 -0700 | [diff] [blame] | 50 | Console.WriteLine("DivAsync Result: " + result); |
| 51 | } |
| 52 | |
Jan Tattermusch | 809148d | 2016-03-22 15:09:41 -0700 | [diff] [blame] | 53 | public static async Task FibExample(Math.MathClient client) |
Jan Tattermusch | 13cd125 | 2015-03-06 10:11:50 -0800 | [diff] [blame] | 54 | { |
Jan Tattermusch | 2852631 | 2015-08-03 09:21:38 -0700 | [diff] [blame] | 55 | using (var call = client.Fib(new FibArgs { Limit = 5 })) |
Jan Tattermusch | 9f550a3 | 2015-05-18 16:00:36 -0700 | [diff] [blame] | 56 | { |
Jan Tattermusch | f22abfb | 2015-08-09 16:15:34 -0700 | [diff] [blame] | 57 | List<Num> result = await call.ResponseStream.ToListAsync(); |
Jan Tattermusch | 9f550a3 | 2015-05-18 16:00:36 -0700 | [diff] [blame] | 58 | Console.WriteLine("Fib Result: " + string.Join("|", result)); |
| 59 | } |
Jan Tattermusch | 13cd125 | 2015-03-06 10:11:50 -0800 | [diff] [blame] | 60 | } |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 61 | |
Jan Tattermusch | 809148d | 2016-03-22 15:09:41 -0700 | [diff] [blame] | 62 | public static async Task SumExample(Math.MathClient client) |
Jan Tattermusch | 13cd125 | 2015-03-06 10:11:50 -0800 | [diff] [blame] | 63 | { |
Jan Tattermusch | 8c61db9 | 2015-04-02 15:22:31 -0700 | [diff] [blame] | 64 | var numbers = new List<Num> |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 65 | { |
Jan Tattermusch | 2852631 | 2015-08-03 09:21:38 -0700 | [diff] [blame] | 66 | new Num { Num_ = 1 }, |
| 67 | new Num { Num_ = 2 }, |
| 68 | new Num { Num_ = 3 } |
Jan Tattermusch | 13cd125 | 2015-03-06 10:11:50 -0800 | [diff] [blame] | 69 | }; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 70 | |
Jan Tattermusch | e5c9b80 | 2015-07-14 17:46:58 -0700 | [diff] [blame] | 71 | using (var call = client.Sum()) |
Jan Tattermusch | 9f550a3 | 2015-05-18 16:00:36 -0700 | [diff] [blame] | 72 | { |
Jan Tattermusch | f22abfb | 2015-08-09 16:15:34 -0700 | [diff] [blame] | 73 | await call.RequestStream.WriteAllAsync(numbers); |
Jan Tattermusch | a236ff2 | 2015-07-21 12:33:31 -0700 | [diff] [blame] | 74 | Console.WriteLine("Sum Result: " + await call.ResponseAsync); |
Jan Tattermusch | 9f550a3 | 2015-05-18 16:00:36 -0700 | [diff] [blame] | 75 | } |
Jan Tattermusch | 13cd125 | 2015-03-06 10:11:50 -0800 | [diff] [blame] | 76 | } |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 77 | |
Jan Tattermusch | 809148d | 2016-03-22 15:09:41 -0700 | [diff] [blame] | 78 | public static async Task DivManyExample(Math.MathClient client) |
Jan Tattermusch | 13cd125 | 2015-03-06 10:11:50 -0800 | [diff] [blame] | 79 | { |
Jan Tattermusch | 8c61db9 | 2015-04-02 15:22:31 -0700 | [diff] [blame] | 80 | var divArgsList = new List<DivArgs> |
Jan Tattermusch | 13cd125 | 2015-03-06 10:11:50 -0800 | [diff] [blame] | 81 | { |
Jan Tattermusch | 2852631 | 2015-08-03 09:21:38 -0700 | [diff] [blame] | 82 | new DivArgs { Dividend = 10, Divisor = 3 }, |
| 83 | new DivArgs { Dividend = 100, Divisor = 21 }, |
| 84 | new DivArgs { Dividend = 7, Divisor = 2 } |
Jan Tattermusch | 13cd125 | 2015-03-06 10:11:50 -0800 | [diff] [blame] | 85 | }; |
Jan Tattermusch | e5c9b80 | 2015-07-14 17:46:58 -0700 | [diff] [blame] | 86 | using (var call = client.DivMany()) |
Jan Tattermusch | 9f550a3 | 2015-05-18 16:00:36 -0700 | [diff] [blame] | 87 | { |
Jan Tattermusch | f22abfb | 2015-08-09 16:15:34 -0700 | [diff] [blame] | 88 | await call.RequestStream.WriteAllAsync(divArgsList); |
| 89 | Console.WriteLine("DivMany Result: " + string.Join("|", await call.ResponseStream.ToListAsync())); |
Jan Tattermusch | 9f550a3 | 2015-05-18 16:00:36 -0700 | [diff] [blame] | 90 | } |
Jan Tattermusch | 13cd125 | 2015-03-06 10:11:50 -0800 | [diff] [blame] | 91 | } |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 92 | |
Jan Tattermusch | 809148d | 2016-03-22 15:09:41 -0700 | [diff] [blame] | 93 | public static async Task DependendRequestsExample(Math.MathClient client) |
Jan Tattermusch | 13cd125 | 2015-03-06 10:11:50 -0800 | [diff] [blame] | 94 | { |
Jan Tattermusch | 8c61db9 | 2015-04-02 15:22:31 -0700 | [diff] [blame] | 95 | var numbers = new List<Num> |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 96 | { |
Jan Tattermusch | 2852631 | 2015-08-03 09:21:38 -0700 | [diff] [blame] | 97 | new Num { Num_ = 1 }, |
| 98 | new Num { Num_ = 2 }, |
| 99 | new Num { Num_ = 3 } |
Jan Tattermusch | 13cd125 | 2015-03-06 10:11:50 -0800 | [diff] [blame] | 100 | }; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 101 | |
Jan Tattermusch | 9f550a3 | 2015-05-18 16:00:36 -0700 | [diff] [blame] | 102 | Num sum; |
Jan Tattermusch | e5c9b80 | 2015-07-14 17:46:58 -0700 | [diff] [blame] | 103 | using (var sumCall = client.Sum()) |
Jan Tattermusch | 9f550a3 | 2015-05-18 16:00:36 -0700 | [diff] [blame] | 104 | { |
Jan Tattermusch | f22abfb | 2015-08-09 16:15:34 -0700 | [diff] [blame] | 105 | await sumCall.RequestStream.WriteAllAsync(numbers); |
Jan Tattermusch | a236ff2 | 2015-07-21 12:33:31 -0700 | [diff] [blame] | 106 | sum = await sumCall.ResponseAsync; |
Jan Tattermusch | 9f550a3 | 2015-05-18 16:00:36 -0700 | [diff] [blame] | 107 | } |
Jan Tattermusch | 8c61db9 | 2015-04-02 15:22:31 -0700 | [diff] [blame] | 108 | |
Jan Tattermusch | 2852631 | 2015-08-03 09:21:38 -0700 | [diff] [blame] | 109 | DivReply result = await client.DivAsync(new DivArgs { Dividend = sum.Num_, Divisor = numbers.Count }); |
Jan Tattermusch | 8c61db9 | 2015-04-02 15:22:31 -0700 | [diff] [blame] | 110 | Console.WriteLine("Avg Result: " + result); |
Jan Tattermusch | 13cd125 | 2015-03-06 10:11:50 -0800 | [diff] [blame] | 111 | } |
| 112 | } |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 113 | } |