public Task <SaunaResponse> FetchCurrentState(SaunaRequest request) { if (_cachedResponse == null) { _cachedResponse = CreateSaunaResponse(0, request); } return(Task.FromResult(_cachedResponse)); }
public override Task <SaunaResponse> FetchCurrentState(SaunaRequest request, ServerCallContext context) { return(Task.FromResult(new SaunaResponse { TimeStamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds(), IsDrySauna = _randomGenerator.Next(0, 2) == 1, IsInfraRed = _randomGenerator.Next(0, 2) == 1, Temperature = GetTemperature(request.TemperatureUnit), Description = "" })); }
static async Task Main(string[] args) { await Task.Delay(5000); var grpcChannel = GrpcChannel.ForAddress("https://localhost:5001"); var restClient = new RestClient("https://localhost:5001"); var grpcClient = new Sauna.SaunaClient(grpcChannel); var grpcRequest = new SaunaRequest { TemperatureUnit = "C" }; var restRequest = new RestRequest("sauna", Method.GET); await grpcClient.FetchCurrentStateAsync(grpcRequest); await restClient.GetAsync <SaunaResponse>(restRequest); var numberOfSamples = 10; long totalGrpcMilliseconds = 0; long totalRestMilliseconds = 0; for (int i = 0; i < numberOfSamples; i++) { WriteLine(); var sw1 = Stopwatch.StartNew(); var grpcResponse = await grpcClient.FetchCurrentStateAsync(grpcRequest); sw1.Stop(); ForegroundColor = ConsoleColor.Green; totalGrpcMilliseconds += sw1.ElapsedMilliseconds; WriteLine($"gRPC: {sw1.ElapsedMilliseconds}ms"); var sw2 = Stopwatch.StartNew(); var restResponse = await restClient.GetAsync <SaunaResponse>(restRequest); sw2.Stop(); ForegroundColor = ConsoleColor.Yellow; totalRestMilliseconds += sw2.ElapsedMilliseconds; WriteLine($"REST: {sw2.ElapsedMilliseconds}ms"); } WriteLine(); ForegroundColor = ConsoleColor.Green; WriteLine($"Total gRPC: {totalGrpcMilliseconds}ms"); WriteLine($"Average gRPC: {totalGrpcMilliseconds / numberOfSamples}ms"); ForegroundColor = ConsoleColor.Yellow; WriteLine($"Total REST: {totalRestMilliseconds}ms"); WriteLine($"Average REST: {totalRestMilliseconds / numberOfSamples}ms"); }
private SaunaResponse CreateSaunaResponse(int counter, SaunaRequest request) { if (counter > 30) { return(null); } return(new SaunaResponse { TimeStamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds(), IsDrySauna = _randomGenerator.Next(0, 2) == 1, IsInfraRed = _randomGenerator.Next(0, 2) == 1, Temperature = GetTemperature(request.TemperatureUnit), Description = $"{typeof(SaunaManager)}", Deeper = CreateSaunaResponse(counter + 1, request) }); }
public override async Task FetchStateStream(SaunaRequest request, IServerStreamWriter <SaunaResponse> responseStream, ServerCallContext context) { while (!context.CancellationToken.IsCancellationRequested) { await Task.Delay(1000); var message = new SaunaResponse { TimeStamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds(), IsDrySauna = _randomGenerator.Next(0, 2) == 1, IsInfraRed = _randomGenerator.Next(0, 2) == 1, Temperature = GetTemperature(request.TemperatureUnit), Description = "" }; if (!context.CancellationToken.IsCancellationRequested) { await responseStream.WriteAsync(message); } } }
static async Task Main(string[] args) { await Task.Delay(1000); var channel = GrpcChannel.ForAddress("https://localhost:5001"); var client = new Sauna.SaunaClient(channel); var request = new SaunaRequest { TemperatureUnit = "C" }; // Fetch a single value. var response = await client.FetchCurrentStateAsync(request); var timeStamp = DateTimeOffset.FromUnixTimeSeconds(response.TimeStamp); WriteLine( $"[{timeStamp:T}] <{(response.IsInfraRed ? "I" : " ")}> <{(response.IsDrySauna ? "S" : " ")}> {response.Temperature}°C"); // Fetch a stream of values. var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10)); using var streamingCall = client.FetchStateStream(request, cancellationToken: cts.Token); try { await foreach (var saunaMessage in streamingCall.ResponseStream.ReadAllAsync(cts.Token)) { timeStamp = DateTimeOffset.FromUnixTimeSeconds(saunaMessage.TimeStamp); WriteLine( $"[{timeStamp:T}] <{(saunaMessage.IsInfraRed ? "I" : " ")}> <{(saunaMessage.IsDrySauna ? "S" : " ")}> {saunaMessage.Temperature}°C"); } } catch (RpcException ex) when(ex.StatusCode == StatusCode.Cancelled) { WriteLine("Stream cancelled."); } ReadKey(); }
public override Task <SaunaResponse> FetchCurrentState(SaunaRequest request, ServerCallContext context) { return(_saunaManager.FetchCurrentState(request)); }