Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 1 | // Licensed to the Apache Software Foundation(ASF) under one |
| 2 | // or more contributor license agreements.See the NOTICE file |
| 3 | // distributed with this work for additional information |
| 4 | // regarding copyright ownership.The ASF licenses this file |
| 5 | // to you under the Apache License, Version 2.0 (the |
| 6 | // "License"); you may not use this file except in compliance |
| 7 | // with the License. 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, |
| 12 | // software distributed under the License is distributed on an |
| 13 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | // KIND, either express or implied. See the License for the |
| 15 | // specific language governing permissions and limitations |
| 16 | // under the License. |
| 17 | |
| 18 | using System; |
| 19 | using System.Collections.Generic; |
| 20 | using System.IO; |
| 21 | using System.Linq; |
| 22 | using System.Net.Security; |
| 23 | using System.Security.Cryptography.X509Certificates; |
| 24 | using System.Threading; |
| 25 | using System.Threading.Tasks; |
| 26 | using Microsoft.AspNetCore.Builder; |
| 27 | using Microsoft.AspNetCore.Hosting; |
| 28 | using Microsoft.Extensions.Configuration; |
| 29 | using Microsoft.Extensions.DependencyInjection; |
| 30 | using Microsoft.Extensions.Logging; |
| 31 | using Thrift; |
| 32 | using Thrift.Protocol; |
| 33 | using Thrift.Server; |
| 34 | using Thrift.Transport; |
| 35 | using Thrift.Transport.Server; |
| 36 | using tutorial; |
| 37 | using shared; |
| 38 | using Thrift.Processor; |
Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 39 | using System.Diagnostics; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 40 | |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 41 | #pragma warning disable IDE0057 // substr |
| 42 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 43 | namespace Server |
| 44 | { |
Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame] | 45 | public static class LoggingHelper |
| 46 | { |
| 47 | public static ILoggerFactory LogFactory { get; } = LoggerFactory.Create(builder => { |
| 48 | ConfigureLogging(builder); |
| 49 | }); |
| 50 | |
| 51 | public static void ConfigureLogging(ILoggingBuilder logging) |
| 52 | { |
| 53 | logging.SetMinimumLevel(LogLevel.Trace); |
| 54 | logging.AddConsole(); |
| 55 | logging.AddDebug(); |
| 56 | } |
| 57 | |
| 58 | public static ILogger<T> CreateLogger<T>() => LogFactory.CreateLogger<T>(); |
| 59 | } |
| 60 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 61 | public class Program |
| 62 | { |
Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame] | 63 | private static readonly ILogger Logger = LoggingHelper.CreateLogger<Program>(); |
Jens Geyer | eacd1d4 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 64 | private static readonly TConfiguration Configuration = null; // new TConfiguration() if needed |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 65 | |
| 66 | public static void Main(string[] args) |
| 67 | { |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 68 | args ??= Array.Empty<string>(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 69 | |
Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame] | 70 | if (args.Any(x => x.StartsWith("-help", StringComparison.OrdinalIgnoreCase))) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 71 | { |
Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame] | 72 | DisplayHelp(); |
| 73 | return; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 74 | } |
Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame] | 75 | |
| 76 | using (var source = new CancellationTokenSource()) |
| 77 | { |
| 78 | RunAsync(args, source.Token).GetAwaiter().GetResult(); |
| 79 | |
| 80 | Logger.LogInformation("Press any key to stop..."); |
| 81 | |
| 82 | Console.ReadLine(); |
| 83 | source.Cancel(); |
| 84 | } |
| 85 | |
| 86 | Logger.LogInformation("Server stopped"); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 87 | } |
| 88 | |
Jens Geyer | b11f63c | 2019-03-14 21:12:38 +0100 | [diff] [blame] | 89 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 90 | private static void DisplayHelp() |
| 91 | { |
| 92 | Logger.LogInformation(@" |
| 93 | Usage: |
Kengo Seki | bee4f2f | 2019-12-29 17:04:50 +0900 | [diff] [blame] | 94 | Server -help |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 95 | will diplay help information |
| 96 | |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 97 | Server -tr:<transport> -bf:<buffering> -pr:<protocol> [-multiplex] |
Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 98 | will run server with specified arguments (tcp transport, no buffering, and binary protocol by default) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 99 | |
| 100 | Options: |
| 101 | -tr (transport): |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 102 | tcp - (default) tcp transport (localhost:9090) |
| 103 | tcptls - tcp transport with tls (localhost:9090) |
| 104 | namedpipe - namedpipe transport (pipe "".test"") |
| 105 | http - http transport (localhost:9090) |
Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 106 | |
| 107 | -bf (buffering): |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 108 | none - (default) no buffering |
| 109 | buffered - buffered transport |
| 110 | framed - framed transport |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 111 | |
| 112 | -pr (protocol): |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 113 | binary - (default) binary protocol |
| 114 | compact - compact protocol |
| 115 | json - json protocol |
| 116 | |
| 117 | -multiplex - adds multiplexed protocol |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 118 | |
| 119 | Sample: |
Kengo Seki | bee4f2f | 2019-12-29 17:04:50 +0900 | [diff] [blame] | 120 | Server -tr:tcp |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 121 | "); |
| 122 | } |
| 123 | |
| 124 | private static async Task RunAsync(string[] args, CancellationToken cancellationToken) |
| 125 | { |
| 126 | var selectedTransport = GetTransport(args); |
Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 127 | var selectedBuffering = GetBuffering(args); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 128 | var selectedProtocol = GetProtocol(args); |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 129 | var multiplex = GetMultiplex(args); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 130 | |
| 131 | if (selectedTransport == Transport.Http) |
| 132 | { |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 133 | if (multiplex) |
Jens Geyer | 3cac320 | 2022-01-31 18:04:35 +0100 | [diff] [blame^] | 134 | throw new Exception("This tutorial sample code does not yet allow multiplex over http (although Thrift itself of course does)"); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 135 | new HttpServerSample().Run(cancellationToken); |
| 136 | } |
| 137 | else |
| 138 | { |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 139 | await RunSelectedConfigurationAsync(selectedTransport, selectedBuffering, selectedProtocol, multiplex, cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 143 | |
| 144 | private static bool GetMultiplex(string[] args) |
| 145 | { |
| 146 | var mplex = args.FirstOrDefault(x => x.StartsWith("-multiplex")); |
| 147 | return !string.IsNullOrEmpty(mplex); |
| 148 | } |
| 149 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 150 | private static Protocol GetProtocol(string[] args) |
| 151 | { |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 152 | var protocol = args.FirstOrDefault(x => x.StartsWith("-pr"))?.Split(':')?[1]; |
| 153 | if (string.IsNullOrEmpty(protocol)) |
| 154 | return Protocol.Binary; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 155 | |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 156 | protocol = protocol.Substring(0, 1).ToUpperInvariant() + protocol.Substring(1).ToLowerInvariant(); |
| 157 | if (Enum.TryParse(protocol, true, out Protocol selectedProtocol)) |
| 158 | return selectedProtocol; |
| 159 | else |
| 160 | return Protocol.Binary; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 161 | } |
| 162 | |
Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 163 | private static Buffering GetBuffering(string[] args) |
| 164 | { |
| 165 | var buffering = args.FirstOrDefault(x => x.StartsWith("-bf"))?.Split(":")?[1]; |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 166 | if (string.IsNullOrEmpty(buffering)) |
| 167 | return Buffering.None; |
Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 168 | |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 169 | buffering = buffering.Substring(0, 1).ToUpperInvariant() + buffering.Substring(1).ToLowerInvariant(); |
| 170 | if( Enum.TryParse<Buffering>(buffering, out var selectedBuffering)) |
| 171 | return selectedBuffering; |
| 172 | else |
| 173 | return Buffering.None; |
Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 174 | } |
| 175 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 176 | private static Transport GetTransport(string[] args) |
| 177 | { |
| 178 | var transport = args.FirstOrDefault(x => x.StartsWith("-tr"))?.Split(':')?[1]; |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 179 | if (string.IsNullOrEmpty(transport)) |
| 180 | return Transport.Tcp; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 181 | |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 182 | transport = transport.Substring(0, 1).ToUpperInvariant() + transport.Substring(1).ToLowerInvariant(); |
| 183 | if( Enum.TryParse(transport, true, out Transport selectedTransport)) |
| 184 | return selectedTransport; |
| 185 | else |
| 186 | return Transport.Tcp; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 187 | } |
| 188 | |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 189 | private static async Task RunSelectedConfigurationAsync(Transport transport, Buffering buffering, Protocol protocol, bool multiplex, CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 190 | { |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 191 | TServerTransport serverTransport = transport switch |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 192 | { |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 193 | Transport.Tcp => new TServerSocketTransport(9090, Configuration), |
| 194 | Transport.NamedPipe => new TNamedPipeServerTransport(".test", Configuration, NamedPipeClientFlags.None), |
| 195 | Transport.TcpTls => new TTlsServerSocketTransport(9090, Configuration, GetCertificate(), ClientCertValidator, LocalCertificateSelectionCallback), |
| 196 | _ => throw new ArgumentException("unsupported value $transport", nameof(transport)), |
| 197 | }; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 198 | |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 199 | TTransportFactory transportFactory = buffering switch |
Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 200 | { |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 201 | Buffering.Buffered => new TBufferedTransport.Factory(), |
| 202 | Buffering.Framed => new TFramedTransport.Factory(), |
| 203 | // layered transport(s) are optional |
| 204 | Buffering.None => null, |
| 205 | _ => throw new ArgumentException("unsupported value $buffering", nameof(buffering)), |
| 206 | }; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 207 | |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 208 | TProtocolFactory protocolFactory = protocol switch |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 209 | { |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 210 | Protocol.Binary => new TBinaryProtocol.Factory(), |
| 211 | Protocol.Compact => new TCompactProtocol.Factory(), |
| 212 | Protocol.Json => new TJsonProtocol.Factory(), |
| 213 | _ => throw new ArgumentException("unsupported value $protocol", nameof(protocol)), |
| 214 | }; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 215 | |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 216 | var handler = new CalculatorAsyncHandler(); |
| 217 | ITAsyncProcessor processor = new Calculator.AsyncProcessor(handler); |
| 218 | |
| 219 | if (multiplex) |
| 220 | { |
| 221 | var multiplexedProcessor = new TMultiplexedProcessor(); |
| 222 | multiplexedProcessor.RegisterProcessor(nameof(Calculator), processor); |
| 223 | |
| 224 | processor = multiplexedProcessor; |
| 225 | } |
| 226 | |
Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 227 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 228 | try |
| 229 | { |
| 230 | Logger.LogInformation( |
Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame] | 231 | "TSimpleAsyncServer with \n{transport} transport\n{buffering} buffering\nmultiplex = {multiplex}\n{protocol} protocol", |
| 232 | transport, |
| 233 | buffering, |
| 234 | multiplex ? "yes" : "no", |
| 235 | protocol |
| 236 | ); |
Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 237 | |
| 238 | var server = new TSimpleAsyncServer( |
| 239 | itProcessorFactory: new TSingletonProcessorFactory(processor), |
| 240 | serverTransport: serverTransport, |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 241 | inputTransportFactory: transportFactory, |
| 242 | outputTransportFactory: transportFactory, |
| 243 | inputProtocolFactory: protocolFactory, |
| 244 | outputProtocolFactory: protocolFactory, |
Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame] | 245 | logger: LoggingHelper.CreateLogger<TSimpleAsyncServer >()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 246 | |
| 247 | Logger.LogInformation("Starting the server..."); |
Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 248 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 249 | await server.ServeAsync(cancellationToken); |
| 250 | } |
| 251 | catch (Exception x) |
| 252 | { |
Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame] | 253 | Logger.LogInformation("{x}",x); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
| 257 | private static X509Certificate2 GetCertificate() |
| 258 | { |
| 259 | // due to files location in net core better to take certs from top folder |
| 260 | var certFile = GetCertPath(Directory.GetParent(Directory.GetCurrentDirectory())); |
| 261 | return new X509Certificate2(certFile, "ThriftTest"); |
| 262 | } |
| 263 | |
| 264 | private static string GetCertPath(DirectoryInfo di, int maxCount = 6) |
| 265 | { |
| 266 | var topDir = di; |
| 267 | var certFile = |
| 268 | topDir.EnumerateFiles("ThriftTest.pfx", SearchOption.AllDirectories) |
| 269 | .FirstOrDefault(); |
| 270 | if (certFile == null) |
| 271 | { |
| 272 | if (maxCount == 0) |
| 273 | throw new FileNotFoundException("Cannot find file in directories"); |
| 274 | return GetCertPath(di.Parent, maxCount - 1); |
| 275 | } |
| 276 | |
| 277 | return certFile.FullName; |
| 278 | } |
| 279 | |
| 280 | private static X509Certificate LocalCertificateSelectionCallback(object sender, |
| 281 | string targetHost, X509CertificateCollection localCertificates, |
| 282 | X509Certificate remoteCertificate, string[] acceptableIssuers) |
| 283 | { |
| 284 | return GetCertificate(); |
| 285 | } |
| 286 | |
| 287 | private static bool ClientCertValidator(object sender, X509Certificate certificate, |
| 288 | X509Chain chain, SslPolicyErrors sslPolicyErrors) |
| 289 | { |
| 290 | return true; |
| 291 | } |
| 292 | |
| 293 | private enum Transport |
| 294 | { |
| 295 | Tcp, |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 296 | NamedPipe, |
| 297 | Http, |
| 298 | TcpTls, |
Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | private enum Buffering |
| 302 | { |
| 303 | None, |
| 304 | Buffered, |
| 305 | Framed, |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | private enum Protocol |
| 309 | { |
| 310 | Binary, |
| 311 | Compact, |
| 312 | Json, |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | public class HttpServerSample |
| 316 | { |
| 317 | public void Run(CancellationToken cancellationToken) |
| 318 | { |
| 319 | var config = new ConfigurationBuilder() |
| 320 | .AddEnvironmentVariables(prefix: "ASPNETCORE_") |
| 321 | .Build(); |
| 322 | |
| 323 | var host = new WebHostBuilder() |
| 324 | .UseConfiguration(config) |
| 325 | .UseKestrel() |
| 326 | .UseUrls("http://localhost:9090") |
| 327 | .UseContentRoot(Directory.GetCurrentDirectory()) |
| 328 | .UseStartup<Startup>() |
Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame] | 329 | .ConfigureLogging((ctx,logging) => LoggingHelper.ConfigureLogging(logging)) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 330 | .Build(); |
| 331 | |
Jens Geyer | b11f63c | 2019-03-14 21:12:38 +0100 | [diff] [blame] | 332 | Logger.LogTrace("test"); |
| 333 | Logger.LogCritical("test"); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 334 | host.RunAsync(cancellationToken).GetAwaiter().GetResult(); |
| 335 | } |
| 336 | |
| 337 | public class Startup |
| 338 | { |
Jens Geyer | ec43954 | 2019-11-01 19:19:44 +0100 | [diff] [blame] | 339 | public Startup(IWebHostEnvironment env) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 340 | { |
| 341 | var builder = new ConfigurationBuilder() |
| 342 | .SetBasePath(env.ContentRootPath) |
| 343 | .AddEnvironmentVariables(); |
| 344 | |
| 345 | Configuration = builder.Build(); |
| 346 | } |
| 347 | |
| 348 | public IConfigurationRoot Configuration { get; } |
| 349 | |
| 350 | // This method gets called by the runtime. Use this method to add services to the container. |
| 351 | public void ConfigureServices(IServiceCollection services) |
| 352 | { |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 353 | // NOTE: this is not really the recommended way to do it |
| 354 | // because the HTTP server cannot be configured properly to e.g. accept framed or multiplex |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 355 | services.AddTransient<Calculator.IAsync, CalculatorAsyncHandler>(); |
| 356 | services.AddTransient<ITAsyncProcessor, Calculator.AsyncProcessor>(); |
| 357 | services.AddTransient<THttpServerTransport, THttpServerTransport>(); |
| 358 | } |
| 359 | |
| 360 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. |
Jens Geyer | ec43954 | 2019-11-01 19:19:44 +0100 | [diff] [blame] | 361 | public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 362 | { |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 363 | _ = env; |
| 364 | _ = loggerFactory; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 365 | app.UseMiddleware<THttpServerTransport>(); |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | public class CalculatorAsyncHandler : Calculator.IAsync |
| 371 | { |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 372 | private readonly Dictionary<int, SharedStruct> _log = new(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 373 | |
| 374 | public CalculatorAsyncHandler() |
| 375 | { |
| 376 | } |
| 377 | |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 378 | public async Task<SharedStruct> getStruct(int key, |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 379 | CancellationToken cancellationToken) |
| 380 | { |
Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame] | 381 | Logger.LogInformation("GetStruct({key})", key); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 382 | return await Task.FromResult(_log[key]); |
| 383 | } |
| 384 | |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 385 | public async Task ping(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 386 | { |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 387 | Logger.LogInformation("Ping()"); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 388 | await Task.CompletedTask; |
| 389 | } |
| 390 | |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 391 | public async Task<int> add(int num1, int num2, CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 392 | { |
Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame] | 393 | Logger.LogInformation("Add({num1},{num2})", num1, num2); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 394 | return await Task.FromResult(num1 + num2); |
| 395 | } |
| 396 | |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 397 | public async Task<int> calculate(int logid, Work w, CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 398 | { |
Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame] | 399 | Logger.LogInformation("Calculate({logid}, [{w.Op},{w.Num1},{w.Num2}])", logid, w.Op, w.Num1, w.Num2); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 400 | |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 401 | int val; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 402 | switch (w.Op) |
| 403 | { |
| 404 | case Operation.ADD: |
| 405 | val = w.Num1 + w.Num2; |
| 406 | break; |
| 407 | |
| 408 | case Operation.SUBTRACT: |
| 409 | val = w.Num1 - w.Num2; |
| 410 | break; |
| 411 | |
| 412 | case Operation.MULTIPLY: |
| 413 | val = w.Num1 * w.Num2; |
| 414 | break; |
| 415 | |
| 416 | case Operation.DIVIDE: |
| 417 | if (w.Num2 == 0) |
| 418 | { |
| 419 | var io = new InvalidOperation |
| 420 | { |
| 421 | WhatOp = (int) w.Op, |
| 422 | Why = "Cannot divide by 0" |
| 423 | }; |
| 424 | |
| 425 | throw io; |
| 426 | } |
| 427 | val = w.Num1 / w.Num2; |
| 428 | break; |
| 429 | |
| 430 | default: |
| 431 | { |
| 432 | var io = new InvalidOperation |
| 433 | { |
| 434 | WhatOp = (int) w.Op, |
| 435 | Why = "Unknown operation" |
| 436 | }; |
| 437 | |
| 438 | throw io; |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | var entry = new SharedStruct |
| 443 | { |
| 444 | Key = logid, |
| 445 | Value = val.ToString() |
| 446 | }; |
| 447 | |
| 448 | _log[logid] = entry; |
| 449 | |
| 450 | return await Task.FromResult(val); |
| 451 | } |
| 452 | |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 453 | public async Task zip(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 454 | { |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 455 | Logger.LogInformation("Zip() with delay 100mc"); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 456 | await Task.Delay(100, CancellationToken.None); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | public class SharedServiceAsyncHandler : SharedService.IAsync |
| 461 | { |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 462 | public async Task<SharedStruct> getStruct(int key, CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 463 | { |
Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame] | 464 | Logger.LogInformation("GetStruct({key})", key); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 465 | return await Task.FromResult(new SharedStruct() |
| 466 | { |
| 467 | Key = key, |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 468 | Value = "GetStruct" |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 469 | }); |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | } |