blob: 97c91b1b1b586248b03e4d94200e7a9629627fef [file] [log] [blame]
Jan Tattermuscha7fff862015-02-13 11:08:08 -08001#region Copyright notice and license
2
Jan Tattermusch07002d72015-02-13 11:24:27 -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 Tattermuscha7608b02015-02-03 17:54:38 -080034using System;
Jan Tattermuscha7608b02015-02-03 17:54:38 -080035using System.Collections.Generic;
36using System.Reactive.Linq;
Jan Tattermusch30868622015-02-19 09:22:33 -080037using System.Threading.Tasks;
38using Grpc.Core.Utils;
Jan Tattermuscha7608b02015-02-03 17:54:38 -080039
40namespace math
41{
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -080042 public static class MathExamples
Jan Tattermuscha7608b02015-02-03 17:54:38 -080043 {
Jan Tattermusch15111f52015-02-05 18:15:14 -080044 public static void DivExample(MathGrpc.IMathServiceClient stub)
Jan Tattermuscha7608b02015-02-03 17:54:38 -080045 {
46 DivReply result = stub.Div(new DivArgs.Builder { Dividend = 10, Divisor = 3 }.Build());
47 Console.WriteLine("Div Result: " + result);
48 }
49
Jan Tattermusch15111f52015-02-05 18:15:14 -080050 public static void DivAsyncExample(MathGrpc.IMathServiceClient stub)
Jan Tattermuscha7608b02015-02-03 17:54:38 -080051 {
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 Tattermusch15111f52015-02-05 18:15:14 -080057 public static void DivAsyncWithCancellationExample(MathGrpc.IMathServiceClient stub)
Jan Tattermuscha7608b02015-02-03 17:54:38 -080058 {
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 Tattermusch15111f52015-02-05 18:15:14 -080064 public static void FibExample(MathGrpc.IMathServiceClient stub)
Jan Tattermuscha7608b02015-02-03 17:54:38 -080065 {
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 Tattermusch15111f52015-02-05 18:15:14 -080073 public static void SumExample(MathGrpc.IMathServiceClient stub)
Jan Tattermuscha7608b02015-02-03 17:54:38 -080074 {
Craig Tiller190d3602015-02-18 09:23:38 -080075 List<Num> numbers = new List<Num>{new Num.Builder { Num_ = 1 }.Build(),
Jan Tattermuscha7608b02015-02-03 17:54:38 -080076 new Num.Builder { Num_ = 2 }.Build(),
77 new Num.Builder { Num_ = 3 }.Build()};
Craig Tiller190d3602015-02-18 09:23:38 -080078
Jan Tattermuscha7608b02015-02-03 17:54:38 -080079 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 Tattermusch15111f52015-02-05 18:15:14 -080088 public static void DivManyExample(MathGrpc.IMathServiceClient stub)
Jan Tattermuscha7608b02015-02-03 17:54:38 -080089 {
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 Tiller190d3602015-02-18 09:23:38 -080097
Jan Tattermuscha7608b02015-02-03 17:54:38 -080098 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 Tattermusch15111f52015-02-05 18:15:14 -0800108 public static void DependendRequestsExample(MathGrpc.IMathServiceClient stub)
Jan Tattermuscha7608b02015-02-03 17:54:38 -0800109 {
110 var numberList = new List<Num>
Craig Tiller190d3602015-02-18 09:23:38 -0800111 { new Num.Builder{ Num_ = 1 }.Build(),
Jan Tattermuscha7608b02015-02-03 17:54:38 -0800112 new Num.Builder{ Num_ = 2 }.Build(), new Num.Builder{ Num_ = 3 }.Build()
113 };
114
115 numberList.ToObservable();
116
117 //IObserver<Num> numbers;
Craig Tiller190d3602015-02-18 09:23:38 -0800118 //Task<Num> call = stub.Sum(out numbers);
Jan Tattermuscha7608b02015-02-03 17:54:38 -0800119 //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