1 #region Copyright notice and license 2 3 // Copyright 2015 gRPC authors. 4 // 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 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 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. 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> 33 public class WorkerServiceImpl : WorkerService.WorkerServiceBase 34 { 35 readonly Action stopRequestHandler; 36 WorkerServiceImpl(Action stopRequestHandler)37 public WorkerServiceImpl(Action stopRequestHandler) 38 { 39 this.stopRequestHandler = GrpcPreconditions.CheckNotNull(stopRequestHandler); 40 } 41 RunServer(IAsyncStreamReader<ServerArgs> requestStream, IServerStreamWriter<ServerStatus> responseStream, ServerCallContext context)42 public override async Task RunServer(IAsyncStreamReader<ServerArgs> requestStream, IServerStreamWriter<ServerStatus> responseStream, ServerCallContext context) 43 { 44 GrpcPreconditions.CheckState(await requestStream.MoveNext()); 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, 52 Cores = Environment.ProcessorCount, 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 RunClient(IAsyncStreamReader<ClientArgs> requestStream, IServerStreamWriter<ClientStatus> responseStream, ServerCallContext context)66 public override async Task RunClient(IAsyncStreamReader<ClientArgs> requestStream, IServerStreamWriter<ClientStatus> responseStream, ServerCallContext context) 67 { 68 GrpcPreconditions.CheckState(await requestStream.MoveNext()); 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 } 87 CoreCount(CoreRequest request, ServerCallContext context)88 public override Task<CoreResponse> CoreCount(CoreRequest request, ServerCallContext context) 89 { 90 return Task.FromResult(new CoreResponse { Cores = Environment.ProcessorCount }); 91 } 92 QuitWorker(Void request, ServerCallContext context)93 public override Task<Void> QuitWorker(Void request, ServerCallContext context) 94 { 95 stopRequestHandler(); 96 return Task.FromResult(new Void()); 97 } 98 } 99 } 100