Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 1 | #region Copyright notice and license |
| 2 | |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | // Copyright 2015 gRPC authors. |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 4 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 8 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 10 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 16 | |
| 17 | #endregion |
| 18 | |
| 19 | using System; |
| 20 | using System.Collections.Generic; |
| 21 | using System.Threading; |
| 22 | using System.Threading.Tasks; |
| 23 | using Google.Protobuf; |
| 24 | using Grpc.Core; |
| 25 | using Grpc.Core.Utils; |
| 26 | using Grpc.IntegrationTesting; |
| 27 | |
| 28 | namespace Grpc.Testing |
| 29 | { |
| 30 | /// <summary> |
| 31 | /// Implementation of WorkerService server |
| 32 | /// </summary> |
Jan Tattermusch | d39426d | 2016-03-14 16:07:52 -0700 | [diff] [blame] | 33 | public class WorkerServiceImpl : WorkerService.WorkerServiceBase |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 34 | { |
Jan Tattermusch | 1cbb567 | 2016-02-18 14:27:28 -0800 | [diff] [blame] | 35 | readonly Action stopRequestHandler; |
| 36 | |
| 37 | public WorkerServiceImpl(Action stopRequestHandler) |
| 38 | { |
Jan Tattermusch | a493353 | 2016-02-23 10:45:58 -0800 | [diff] [blame] | 39 | this.stopRequestHandler = GrpcPreconditions.CheckNotNull(stopRequestHandler); |
Jan Tattermusch | 1cbb567 | 2016-02-18 14:27:28 -0800 | [diff] [blame] | 40 | } |
| 41 | |
Jan Tattermusch | d39426d | 2016-03-14 16:07:52 -0700 | [diff] [blame] | 42 | public override async Task RunServer(IAsyncStreamReader<ServerArgs> requestStream, IServerStreamWriter<ServerStatus> responseStream, ServerCallContext context) |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 43 | { |
Jan Tattermusch | 7a3ee6a | 2016-02-18 10:36:02 -0800 | [diff] [blame] | 44 | GrpcPreconditions.CheckState(await requestStream.MoveNext()); |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 45 | var serverConfig = requestStream.Current.Setup; |
| 46 | var runner = ServerRunners.CreateStarted(serverConfig); |
| 47 | |
| 48 | await responseStream.WriteAsync(new ServerStatus |
| 49 | { |
| 50 | Stats = runner.GetStats(false), |
| 51 | Port = runner.BoundPort, |
Jan Tattermusch | d0f5f72 | 2016-05-18 21:44:15 -0700 | [diff] [blame] | 52 | Cores = Environment.ProcessorCount, |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 53 | }); |
| 54 | |
| 55 | while (await requestStream.MoveNext()) |
| 56 | { |
| 57 | var reset = requestStream.Current.Mark.Reset; |
| 58 | await responseStream.WriteAsync(new ServerStatus |
| 59 | { |
| 60 | Stats = runner.GetStats(reset) |
| 61 | }); |
| 62 | } |
| 63 | await runner.StopAsync(); |
| 64 | } |
| 65 | |
Jan Tattermusch | d39426d | 2016-03-14 16:07:52 -0700 | [diff] [blame] | 66 | public override async Task RunClient(IAsyncStreamReader<ClientArgs> requestStream, IServerStreamWriter<ClientStatus> responseStream, ServerCallContext context) |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 67 | { |
Jan Tattermusch | 7a3ee6a | 2016-02-18 10:36:02 -0800 | [diff] [blame] | 68 | GrpcPreconditions.CheckState(await requestStream.MoveNext()); |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 69 | var clientConfig = requestStream.Current.Setup; |
| 70 | var runner = ClientRunners.CreateStarted(clientConfig); |
| 71 | |
| 72 | await responseStream.WriteAsync(new ClientStatus |
| 73 | { |
| 74 | Stats = runner.GetStats(false) |
| 75 | }); |
| 76 | |
| 77 | while (await requestStream.MoveNext()) |
| 78 | { |
| 79 | var reset = requestStream.Current.Mark.Reset; |
| 80 | await responseStream.WriteAsync(new ClientStatus |
| 81 | { |
| 82 | Stats = runner.GetStats(reset) |
| 83 | }); |
| 84 | } |
| 85 | await runner.StopAsync(); |
| 86 | } |
Jan Tattermusch | 1cbb567 | 2016-02-18 14:27:28 -0800 | [diff] [blame] | 87 | |
Jan Tattermusch | d39426d | 2016-03-14 16:07:52 -0700 | [diff] [blame] | 88 | public override Task<CoreResponse> CoreCount(CoreRequest request, ServerCallContext context) |
Jan Tattermusch | 1cbb567 | 2016-02-18 14:27:28 -0800 | [diff] [blame] | 89 | { |
| 90 | return Task.FromResult(new CoreResponse { Cores = Environment.ProcessorCount }); |
| 91 | } |
| 92 | |
Jan Tattermusch | d39426d | 2016-03-14 16:07:52 -0700 | [diff] [blame] | 93 | public override Task<Void> QuitWorker(Void request, ServerCallContext context) |
Jan Tattermusch | 1cbb567 | 2016-02-18 14:27:28 -0800 | [diff] [blame] | 94 | { |
| 95 | stopRequestHandler(); |
| 96 | return Task.FromResult(new Void()); |
| 97 | } |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 98 | } |
| 99 | } |