blob: 6075420974889cd4cf26e08cb8c088a8e6ff5d12 [file] [log] [blame]
Jan Tattermuscha7fff862015-02-13 11:08:08 -08001#region Copyright notice and license
Jan Tattermuschacb842c2016-03-25 16:54:14 -07002// Copyright 2015-2016, Google Inc.
Jan Tattermuscha7fff862015-02-13 11:08:08 -08003// All rights reserved.
Craig Tiller190d3602015-02-18 09:23:38 -08004//
Jan Tattermuscha7fff862015-02-13 11:08:08 -08005// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
Craig Tiller190d3602015-02-18 09:23:38 -08008//
Jan Tattermuscha7fff862015-02-13 11:08:08 -08009// * 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 Tiller190d3602015-02-18 09:23:38 -080018//
Jan Tattermuscha7fff862015-02-13 11:08:08 -080019// 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 Tattermuscha7fff862015-02-13 11:08:08 -080030#endregion
31
Jan Tattermuscha7608b02015-02-03 17:54:38 -080032using System;
Jan Tattermuscha7608b02015-02-03 17:54:38 -080033using System.Collections.Generic;
Jan Tattermusch30868622015-02-19 09:22:33 -080034using System.Threading.Tasks;
35using Grpc.Core.Utils;
Jan Tattermuscha7608b02015-02-03 17:54:38 -080036
Jan Tattermusch28526312015-08-03 09:21:38 -070037namespace Math
Jan Tattermuscha7608b02015-02-03 17:54:38 -080038{
Jan Tattermusch13cd1252015-03-06 10:11:50 -080039 public static class MathExamples
40 {
Jan Tattermusch809148d2016-03-22 15:09:41 -070041 public static void DivExample(Math.MathClient client)
Jan Tattermusch13cd1252015-03-06 10:11:50 -080042 {
Jan Tattermusch28526312015-08-03 09:21:38 -070043 DivReply result = client.Div(new DivArgs { Dividend = 10, Divisor = 3 });
Jan Tattermusch13cd1252015-03-06 10:11:50 -080044 Console.WriteLine("Div Result: " + result);
45 }
Jan Tattermuscha7608b02015-02-03 17:54:38 -080046
Jan Tattermusch809148d2016-03-22 15:09:41 -070047 public static async Task DivAsyncExample(Math.MathClient client)
Jan Tattermusch13cd1252015-03-06 10:11:50 -080048 {
Jan Tattermusch28526312015-08-03 09:21:38 -070049 DivReply result = await client.DivAsync(new DivArgs { Dividend = 4, Divisor = 5 });
Jan Tattermusch8c61db92015-04-02 15:22:31 -070050 Console.WriteLine("DivAsync Result: " + result);
51 }
52
Jan Tattermusch809148d2016-03-22 15:09:41 -070053 public static async Task FibExample(Math.MathClient client)
Jan Tattermusch13cd1252015-03-06 10:11:50 -080054 {
Jan Tattermusch28526312015-08-03 09:21:38 -070055 using (var call = client.Fib(new FibArgs { Limit = 5 }))
Jan Tattermusch9f550a32015-05-18 16:00:36 -070056 {
Jan Tattermuschf22abfb2015-08-09 16:15:34 -070057 List<Num> result = await call.ResponseStream.ToListAsync();
Jan Tattermusch9f550a32015-05-18 16:00:36 -070058 Console.WriteLine("Fib Result: " + string.Join("|", result));
59 }
Jan Tattermusch13cd1252015-03-06 10:11:50 -080060 }
Jan Tattermuscha7608b02015-02-03 17:54:38 -080061
Jan Tattermusch809148d2016-03-22 15:09:41 -070062 public static async Task SumExample(Math.MathClient client)
Jan Tattermusch13cd1252015-03-06 10:11:50 -080063 {
Jan Tattermusch8c61db92015-04-02 15:22:31 -070064 var numbers = new List<Num>
Jan Tattermusch075dde42015-03-11 18:21:00 -070065 {
Jan Tattermusch28526312015-08-03 09:21:38 -070066 new Num { Num_ = 1 },
67 new Num { Num_ = 2 },
68 new Num { Num_ = 3 }
Jan Tattermusch13cd1252015-03-06 10:11:50 -080069 };
Craig Tiller190d3602015-02-18 09:23:38 -080070
Jan Tattermusche5c9b802015-07-14 17:46:58 -070071 using (var call = client.Sum())
Jan Tattermusch9f550a32015-05-18 16:00:36 -070072 {
Jan Tattermuschf22abfb2015-08-09 16:15:34 -070073 await call.RequestStream.WriteAllAsync(numbers);
Jan Tattermuscha236ff22015-07-21 12:33:31 -070074 Console.WriteLine("Sum Result: " + await call.ResponseAsync);
Jan Tattermusch9f550a32015-05-18 16:00:36 -070075 }
Jan Tattermusch13cd1252015-03-06 10:11:50 -080076 }
Jan Tattermuscha7608b02015-02-03 17:54:38 -080077
Jan Tattermusch809148d2016-03-22 15:09:41 -070078 public static async Task DivManyExample(Math.MathClient client)
Jan Tattermusch13cd1252015-03-06 10:11:50 -080079 {
Jan Tattermusch8c61db92015-04-02 15:22:31 -070080 var divArgsList = new List<DivArgs>
Jan Tattermusch13cd1252015-03-06 10:11:50 -080081 {
Jan Tattermusch28526312015-08-03 09:21:38 -070082 new DivArgs { Dividend = 10, Divisor = 3 },
83 new DivArgs { Dividend = 100, Divisor = 21 },
84 new DivArgs { Dividend = 7, Divisor = 2 }
Jan Tattermusch13cd1252015-03-06 10:11:50 -080085 };
Jan Tattermusche5c9b802015-07-14 17:46:58 -070086 using (var call = client.DivMany())
Jan Tattermusch9f550a32015-05-18 16:00:36 -070087 {
Jan Tattermuschf22abfb2015-08-09 16:15:34 -070088 await call.RequestStream.WriteAllAsync(divArgsList);
89 Console.WriteLine("DivMany Result: " + string.Join("|", await call.ResponseStream.ToListAsync()));
Jan Tattermusch9f550a32015-05-18 16:00:36 -070090 }
Jan Tattermusch13cd1252015-03-06 10:11:50 -080091 }
Jan Tattermuscha7608b02015-02-03 17:54:38 -080092
Jan Tattermusch809148d2016-03-22 15:09:41 -070093 public static async Task DependendRequestsExample(Math.MathClient client)
Jan Tattermusch13cd1252015-03-06 10:11:50 -080094 {
Jan Tattermusch8c61db92015-04-02 15:22:31 -070095 var numbers = new List<Num>
Jan Tattermusch075dde42015-03-11 18:21:00 -070096 {
Jan Tattermusch28526312015-08-03 09:21:38 -070097 new Num { Num_ = 1 },
98 new Num { Num_ = 2 },
99 new Num { Num_ = 3 }
Jan Tattermusch13cd1252015-03-06 10:11:50 -0800100 };
Jan Tattermuscha7608b02015-02-03 17:54:38 -0800101
Jan Tattermusch9f550a32015-05-18 16:00:36 -0700102 Num sum;
Jan Tattermusche5c9b802015-07-14 17:46:58 -0700103 using (var sumCall = client.Sum())
Jan Tattermusch9f550a32015-05-18 16:00:36 -0700104 {
Jan Tattermuschf22abfb2015-08-09 16:15:34 -0700105 await sumCall.RequestStream.WriteAllAsync(numbers);
Jan Tattermuscha236ff22015-07-21 12:33:31 -0700106 sum = await sumCall.ResponseAsync;
Jan Tattermusch9f550a32015-05-18 16:00:36 -0700107 }
Jan Tattermusch8c61db92015-04-02 15:22:31 -0700108
Jan Tattermusch28526312015-08-03 09:21:38 -0700109 DivReply result = await client.DivAsync(new DivArgs { Dividend = sum.Num_, Divisor = numbers.Count });
Jan Tattermusch8c61db92015-04-02 15:22:31 -0700110 Console.WriteLine("Avg Result: " + result);
Jan Tattermusch13cd1252015-03-06 10:11:50 -0800111 }
112 }
Jan Tattermuscha7608b02015-02-03 17:54:38 -0800113}