blob: 6bd997d1f469f39d9f4803c80989cdf00463e810 [file] [log] [blame]
Jan Tattermusch20831382015-02-24 14:16:04 -08001#region Copyright notice and license
2
3// Copyright 2015, Google Inc.
4// All rights reserved.
5//
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are
8// met:
9//
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.
19//
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
34using System;
35using System.Collections.Generic;
36using System.Threading;
37using System.Threading.Tasks;
38using Google.ProtocolBuffers;
Jan Tattermuscha5272b62015-04-30 11:56:46 -070039using Grpc.Core;
Jan Tattermusch20831382015-02-24 14:16:04 -080040using Grpc.Core.Utils;
41
42namespace grpc.testing
43{
44 /// <summary>
45 /// Implementation of TestService server
46 /// </summary>
Jan Tattermusch7eb3a762015-05-07 14:26:13 -070047 public class TestServiceImpl : TestService.ITestService
Jan Tattermusch20831382015-02-24 14:16:04 -080048 {
Jan Tattermusch8ab1f7e2015-05-06 15:56:30 -070049 public Task<Empty> EmptyCall(ServerCallContext context, Empty request)
Jan Tattermusch20831382015-02-24 14:16:04 -080050 {
Jan Tattermuscha5272b62015-04-30 11:56:46 -070051 return Task.FromResult(Empty.DefaultInstance);
Jan Tattermusch20831382015-02-24 14:16:04 -080052 }
53
Jan Tattermusch8ab1f7e2015-05-06 15:56:30 -070054 public Task<SimpleResponse> UnaryCall(ServerCallContext context, SimpleRequest request)
Jan Tattermusch20831382015-02-24 14:16:04 -080055 {
56 var response = SimpleResponse.CreateBuilder()
57 .SetPayload(CreateZerosPayload(request.ResponseSize)).Build();
Jan Tattermuscha5272b62015-04-30 11:56:46 -070058 return Task.FromResult(response);
Jan Tattermusch20831382015-02-24 14:16:04 -080059 }
60
Jan Tattermusch8ab1f7e2015-05-06 15:56:30 -070061 public async Task StreamingOutputCall(ServerCallContext context, StreamingOutputCallRequest request, IServerStreamWriter<StreamingOutputCallResponse> responseStream)
Jan Tattermusch20831382015-02-24 14:16:04 -080062 {
Jan Tattermusch075dde42015-03-11 18:21:00 -070063 foreach (var responseParam in request.ResponseParametersList)
Jan Tattermusch20831382015-02-24 14:16:04 -080064 {
65 var response = StreamingOutputCallResponse.CreateBuilder()
66 .SetPayload(CreateZerosPayload(responseParam.Size)).Build();
Jan Tattermusch43dc50b2015-05-18 15:27:19 -070067 await responseStream.WriteAsync(response);
Jan Tattermusch20831382015-02-24 14:16:04 -080068 }
Jan Tattermusch20831382015-02-24 14:16:04 -080069 }
70
Jan Tattermusch8ab1f7e2015-05-06 15:56:30 -070071 public async Task<StreamingInputCallResponse> StreamingInputCall(ServerCallContext context, IAsyncStreamReader<StreamingInputCallRequest> requestStream)
Jan Tattermusch20831382015-02-24 14:16:04 -080072 {
Jan Tattermuscha5272b62015-04-30 11:56:46 -070073 int sum = 0;
74 await requestStream.ForEach(async request =>
Jan Tattermusch075dde42015-03-11 18:21:00 -070075 {
Jan Tattermuscha5272b62015-04-30 11:56:46 -070076 sum += request.Payload.Body.Length;
Jan Tattermusch20831382015-02-24 14:16:04 -080077 });
Jan Tattermuscha5272b62015-04-30 11:56:46 -070078 return StreamingInputCallResponse.CreateBuilder().SetAggregatedPayloadSize(sum).Build();
Jan Tattermusch20831382015-02-24 14:16:04 -080079 }
80
Jan Tattermusch8ab1f7e2015-05-06 15:56:30 -070081 public async Task FullDuplexCall(ServerCallContext context, IAsyncStreamReader<StreamingOutputCallRequest> requestStream, IServerStreamWriter<StreamingOutputCallResponse> responseStream)
Jan Tattermusch20831382015-02-24 14:16:04 -080082 {
Jan Tattermuscha5272b62015-04-30 11:56:46 -070083 await requestStream.ForEach(async request =>
Jan Tattermusch20831382015-02-24 14:16:04 -080084 {
Jan Tattermuscha5272b62015-04-30 11:56:46 -070085 foreach (var responseParam in request.ResponseParametersList)
Jan Tattermusch075dde42015-03-11 18:21:00 -070086 {
87 var response = StreamingOutputCallResponse.CreateBuilder()
88 .SetPayload(CreateZerosPayload(responseParam.Size)).Build();
Jan Tattermusch43dc50b2015-05-18 15:27:19 -070089 await responseStream.WriteAsync(response);
Jan Tattermusch075dde42015-03-11 18:21:00 -070090 }
Jan Tattermuscha5272b62015-04-30 11:56:46 -070091 });
92 }
93
Jan Tattermusch8ab1f7e2015-05-06 15:56:30 -070094 public async Task HalfDuplexCall(ServerCallContext context, IAsyncStreamReader<StreamingOutputCallRequest> requestStream, IServerStreamWriter<StreamingOutputCallResponse> responseStream)
Jan Tattermuscha5272b62015-04-30 11:56:46 -070095 {
96 throw new NotImplementedException();
Jan Tattermusch20831382015-02-24 14:16:04 -080097 }
98
Jan Tattermusch075dde42015-03-11 18:21:00 -070099 private static Payload CreateZerosPayload(int size)
100 {
Jan Tattermusch20831382015-02-24 14:16:04 -0800101 return Payload.CreateBuilder().SetBody(ByteString.CopyFrom(new byte[size])).Build();
102 }
103 }
104}