blob: 06d81a4d83d5095a6b39096af4ed47b93235170d [file] [log] [blame]
Jan Tattermuscha7fff862015-02-13 11:08:08 -08001#region Copyright notice and license
Jan Tattermusch07002d72015-02-13 11:24:27 -08002// Copyright 2015, 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
37namespace math
38{
Jan Tattermusch13cd1252015-03-06 10:11:50 -080039 public static class MathExamples
40 {
Jan Tattermusche5c9b802015-07-14 17:46:58 -070041 public static void DivExample(Math.IMathClient client)
Jan Tattermusch13cd1252015-03-06 10:11:50 -080042 {
Jan Tattermusche5c9b802015-07-14 17:46:58 -070043 DivReply result = client.Div(new DivArgs.Builder { Dividend = 10, Divisor = 3 }.Build());
Jan Tattermusch13cd1252015-03-06 10:11:50 -080044 Console.WriteLine("Div Result: " + result);
45 }
Jan Tattermuscha7608b02015-02-03 17:54:38 -080046
Jan Tattermusche5c9b802015-07-14 17:46:58 -070047 public static async Task DivAsyncExample(Math.IMathClient client)
Jan Tattermusch13cd1252015-03-06 10:11:50 -080048 {
Jan Tattermusch25bb2ef2015-07-21 12:15:53 -070049 DivReply result = await client.DivAsync(new DivArgs.Builder { Dividend = 4, Divisor = 5 }.Build());
Jan Tattermusch8c61db92015-04-02 15:22:31 -070050 Console.WriteLine("DivAsync Result: " + result);
51 }
52
Jan Tattermusche5c9b802015-07-14 17:46:58 -070053 public static async Task FibExample(Math.IMathClient client)
Jan Tattermusch13cd1252015-03-06 10:11:50 -080054 {
Jan Tattermusche5c9b802015-07-14 17:46:58 -070055 using (var call = client.Fib(new FibArgs.Builder { Limit = 5 }.Build()))
Jan Tattermusch9f550a32015-05-18 16:00:36 -070056 {
57 List<Num> result = await call.ResponseStream.ToList();
58 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 Tattermusche5c9b802015-07-14 17:46:58 -070062 public static async Task SumExample(Math.IMathClient 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 {
66 new Num.Builder { Num_ = 1 }.Build(),
Jan Tattermusch13cd1252015-03-06 10:11:50 -080067 new Num.Builder { Num_ = 2 }.Build(),
68 new Num.Builder { Num_ = 3 }.Build()
69 };
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 {
73 await call.RequestStream.WriteAll(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 Tattermusche5c9b802015-07-14 17:46:58 -070078 public static async Task DivManyExample(Math.IMathClient 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 {
82 new DivArgs.Builder { Dividend = 10, Divisor = 3 }.Build(),
83 new DivArgs.Builder { Dividend = 100, Divisor = 21 }.Build(),
84 new DivArgs.Builder { Dividend = 7, Divisor = 2 }.Build()
85 };
Jan Tattermusche5c9b802015-07-14 17:46:58 -070086 using (var call = client.DivMany())
Jan Tattermusch9f550a32015-05-18 16:00:36 -070087 {
88 await call.RequestStream.WriteAll(divArgsList);
89 Console.WriteLine("DivMany Result: " + string.Join("|", await call.ResponseStream.ToList()));
90 }
Jan Tattermusch13cd1252015-03-06 10:11:50 -080091 }
Jan Tattermuscha7608b02015-02-03 17:54:38 -080092
Jan Tattermusche5c9b802015-07-14 17:46:58 -070093 public static async Task DependendRequestsExample(Math.IMathClient 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 Tattermusch8c61db92015-04-02 15:22:31 -070097 new Num.Builder { Num_ = 1 }.Build(),
98 new Num.Builder { Num_ = 2 }.Build(),
99 new Num.Builder { Num_ = 3 }.Build()
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 {
105 await sumCall.RequestStream.WriteAll(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 Tattermusche5c9b802015-07-14 17:46:58 -0700109 DivReply result = await client.DivAsync(new DivArgs.Builder { Dividend = sum.Num_, Divisor = numbers.Count }.Build());
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}