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; |
Mario Emmenlauer | 47e4923 | 2020-04-07 18:43:46 +0200 | [diff] [blame] | 20 | using System.Diagnostics; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 21 | using System.IO; |
| 22 | using System.Linq; |
| 23 | using System.Net; |
| 24 | using System.Net.Security; |
| 25 | using System.Security.Cryptography.X509Certificates; |
| 26 | using System.Threading; |
| 27 | using System.Threading.Tasks; |
| 28 | using Microsoft.Extensions.Logging; |
Mario Emmenlauer | 47e4923 | 2020-04-07 18:43:46 +0200 | [diff] [blame] | 29 | using Microsoft.Extensions.DependencyInjection; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 30 | using Thrift; |
| 31 | using Thrift.Protocol; |
| 32 | using Thrift.Transport; |
| 33 | using Thrift.Transport.Client; |
| 34 | using tutorial; |
| 35 | using shared; |
| 36 | |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 37 | #pragma warning disable IDE0063 // using |
| 38 | #pragma warning disable IDE0057 // substr |
| 39 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 40 | namespace Client |
| 41 | { |
Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame^] | 42 | public static class LoggingHelper |
| 43 | { |
| 44 | public static ILoggerFactory LogFactory { get; } = LoggerFactory.Create(builder => { |
| 45 | ConfigureLogging(builder); |
| 46 | }); |
| 47 | |
| 48 | public static void ConfigureLogging(ILoggingBuilder logging) |
| 49 | { |
| 50 | logging.SetMinimumLevel(LogLevel.Trace); |
| 51 | logging.AddConsole(); |
| 52 | logging.AddDebug(); |
| 53 | } |
| 54 | |
| 55 | public static ILogger<T> CreateLogger<T>() => LogFactory.CreateLogger<T>(); |
| 56 | } |
| 57 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 58 | public class Program |
| 59 | { |
Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame^] | 60 | private static readonly ILogger Logger = LoggingHelper.CreateLogger<Program>(); |
Jens Geyer | eacd1d4 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 61 | private static readonly TConfiguration Configuration = null; // new TConfiguration() if needed |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 62 | |
| 63 | private static void DisplayHelp() |
| 64 | { |
| 65 | Logger.LogInformation(@" |
| 66 | Usage: |
Kengo Seki | bee4f2f | 2019-12-29 17:04:50 +0900 | [diff] [blame] | 67 | Client -help |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 68 | will diplay help information |
| 69 | |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 70 | Client -tr:<transport> -bf:<buffering> -pr:<protocol> [-mc:<numClients>] [-multiplex] |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 71 | will run client with specified arguments (tcp transport and binary protocol by default) and with 1 client |
| 72 | |
| 73 | Options: |
| 74 | -tr (transport): |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 75 | tcp - (default) tcp transport (localhost:9090) |
| 76 | tcptls - tcp tls transport (localhost:9090) |
| 77 | namedpipe - namedpipe transport (pipe "".test"") |
| 78 | http - http transport (http://localhost:9090) |
Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 79 | |
| 80 | -bf (buffering): |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 81 | none - (default) no buffering |
| 82 | buffered - buffered transport |
| 83 | framed - framed transport |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 84 | |
| 85 | -pr (protocol): |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 86 | binary - (default) binary protocol |
| 87 | compact - compact protocol |
| 88 | json - json protocol |
| 89 | |
| 90 | -multiplex - adds multiplexed protocol |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 91 | |
| 92 | -mc (multiple clients): |
| 93 | <numClients> - number of multiple clients to connect to server (max 100, default 1) |
| 94 | |
| 95 | Sample: |
Kengo Seki | bee4f2f | 2019-12-29 17:04:50 +0900 | [diff] [blame] | 96 | Client -tr:tcp -pr:binary |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 97 | "); |
| 98 | } |
| 99 | |
| 100 | public static void Main(string[] args) |
| 101 | { |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 102 | args ??= Array.Empty<string>(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 103 | |
Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame^] | 104 | if (args.Any(x => x.StartsWith("-help", StringComparison.OrdinalIgnoreCase))) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 105 | { |
Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame^] | 106 | DisplayHelp(); |
| 107 | return; |
| 108 | } |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 109 | |
Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame^] | 110 | Logger.LogInformation("Starting client..."); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 111 | |
Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame^] | 112 | using (var source = new CancellationTokenSource()) |
| 113 | { |
| 114 | RunAsync(args, source.Token).GetAwaiter().GetResult(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame^] | 118 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 119 | private static async Task RunAsync(string[] args, CancellationToken cancellationToken) |
| 120 | { |
| 121 | var numClients = GetNumberOfClients(args); |
| 122 | |
Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame^] | 123 | Logger.LogInformation("Selected # of clients: {numClients}", numClients); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 124 | |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 125 | var transport = GetTransport(args); |
Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame^] | 126 | Logger.LogInformation("Selected client transport: {transport}", transport); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 127 | |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 128 | var protocol = MakeProtocol( args, MakeTransport(args)); |
Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame^] | 129 | Logger.LogInformation("Selected client protocol: {GetProtocol(args)}", GetProtocol(args)); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 130 | |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 131 | var mplex = GetMultiplex(args); |
Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame^] | 132 | Logger.LogInformation("Multiplex {mplex}", mplex); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 133 | |
| 134 | var tasks = new Task[numClients]; |
| 135 | for (int i = 0; i < numClients; i++) |
| 136 | { |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 137 | var task = RunClientAsync(protocol, mplex, cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 138 | tasks[i] = task; |
| 139 | } |
| 140 | |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 141 | Task.WaitAll(tasks,cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 142 | await Task.CompletedTask; |
| 143 | } |
| 144 | |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 145 | private static bool GetMultiplex(string[] args) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 146 | { |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 147 | var mplex = args.FirstOrDefault(x => x.StartsWith("-multiplex")); |
| 148 | return !string.IsNullOrEmpty(mplex); |
| 149 | } |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 150 | |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 151 | private static Protocol GetProtocol(string[] args) |
| 152 | { |
| 153 | var protocol = args.FirstOrDefault(x => x.StartsWith("-pr"))?.Split(':')?[1]; |
| 154 | if (string.IsNullOrEmpty(protocol)) |
| 155 | return Protocol.Binary; |
| 156 | |
| 157 | protocol = protocol.Substring(0, 1).ToUpperInvariant() + protocol.Substring(1).ToLowerInvariant(); |
| 158 | if (Enum.TryParse(protocol, true, out Protocol selectedProtocol)) |
| 159 | return selectedProtocol; |
| 160 | else |
| 161 | return Protocol.Binary; |
| 162 | } |
| 163 | |
| 164 | private static Buffering GetBuffering(string[] args) |
| 165 | { |
| 166 | var buffering = args.FirstOrDefault(x => x.StartsWith("-bf"))?.Split(":")?[1]; |
| 167 | if (string.IsNullOrEmpty(buffering)) |
| 168 | return Buffering.None; |
| 169 | |
| 170 | buffering = buffering.Substring(0, 1).ToUpperInvariant() + buffering.Substring(1).ToLowerInvariant(); |
| 171 | if (Enum.TryParse<Buffering>(buffering, out var selectedBuffering)) |
| 172 | return selectedBuffering; |
| 173 | else |
| 174 | return Buffering.None; |
| 175 | } |
| 176 | |
| 177 | private static Transport GetTransport(string[] args) |
| 178 | { |
| 179 | var transport = args.FirstOrDefault(x => x.StartsWith("-tr"))?.Split(':')?[1]; |
| 180 | if (string.IsNullOrEmpty(transport)) |
| 181 | return Transport.Tcp; |
| 182 | |
| 183 | transport = transport.Substring(0, 1).ToUpperInvariant() + transport.Substring(1).ToLowerInvariant(); |
| 184 | if (Enum.TryParse(transport, true, out Transport selectedTransport)) |
| 185 | return selectedTransport; |
| 186 | else |
| 187 | return Transport.Tcp; |
| 188 | } |
| 189 | |
| 190 | |
| 191 | private static TTransport MakeTransport(string[] args) |
| 192 | { |
Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 193 | // construct endpoint transport |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 194 | TTransport transport = null; |
| 195 | Transport selectedTransport = GetTransport(args); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 196 | { |
| 197 | switch (selectedTransport) |
| 198 | { |
| 199 | case Transport.Tcp: |
Jens Geyer | eacd1d4 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 200 | transport = new TSocketTransport(IPAddress.Loopback, 9090, Configuration); |
Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 201 | break; |
| 202 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 203 | case Transport.NamedPipe: |
Jens Geyer | eacd1d4 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 204 | transport = new TNamedPipeTransport(".test", Configuration); |
Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 205 | break; |
| 206 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 207 | case Transport.Http: |
Jens Geyer | eacd1d4 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 208 | transport = new THttpTransport(new Uri("http://localhost:9090"), Configuration); |
Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 209 | break; |
| 210 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 211 | case Transport.TcpTls: |
Jens Geyer | eacd1d4 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 212 | transport = new TTlsSocketTransport(IPAddress.Loopback, 9090, Configuration, |
| 213 | GetCertificate(), CertValidator, LocalCertificateSelectionCallback); |
Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 214 | break; |
| 215 | |
| 216 | default: |
| 217 | Debug.Assert(false, "unhandled case"); |
| 218 | break; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 222 | // optionally add layered transport(s) |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 223 | Buffering selectedBuffering = GetBuffering(args); |
| 224 | switch (selectedBuffering) |
Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 225 | { |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 226 | case Buffering.Buffered: |
| 227 | transport = new TBufferedTransport(transport); |
| 228 | break; |
Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 229 | |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 230 | case Buffering.Framed: |
| 231 | transport = new TFramedTransport(transport); |
| 232 | break; |
Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 233 | |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 234 | default: // layered transport(s) are optional |
| 235 | Debug.Assert(selectedBuffering == Buffering.None, "unhandled case"); |
| 236 | break; |
Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | return transport; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | private static int GetNumberOfClients(string[] args) |
| 243 | { |
| 244 | var numClients = args.FirstOrDefault(x => x.StartsWith("-mc"))?.Split(':')?[1]; |
| 245 | |
Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame^] | 246 | Logger.LogInformation("Selected # of clients: {numClients}", numClients); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 247 | |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 248 | if (int.TryParse(numClients, out int c) && (0 < c) && (c <= 100)) |
| 249 | return c; |
| 250 | else |
| 251 | return 1; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | private static X509Certificate2 GetCertificate() |
| 255 | { |
| 256 | // due to files location in net core better to take certs from top folder |
| 257 | var certFile = GetCertPath(Directory.GetParent(Directory.GetCurrentDirectory())); |
| 258 | return new X509Certificate2(certFile, "ThriftTest"); |
| 259 | } |
| 260 | |
| 261 | private static string GetCertPath(DirectoryInfo di, int maxCount = 6) |
| 262 | { |
| 263 | var topDir = di; |
| 264 | var certFile = |
| 265 | topDir.EnumerateFiles("ThriftTest.pfx", SearchOption.AllDirectories) |
| 266 | .FirstOrDefault(); |
| 267 | if (certFile == null) |
| 268 | { |
| 269 | if (maxCount == 0) |
| 270 | throw new FileNotFoundException("Cannot find file in directories"); |
| 271 | return GetCertPath(di.Parent, maxCount - 1); |
| 272 | } |
| 273 | |
| 274 | return certFile.FullName; |
| 275 | } |
| 276 | |
| 277 | private static X509Certificate LocalCertificateSelectionCallback(object sender, |
| 278 | string targetHost, X509CertificateCollection localCertificates, |
| 279 | X509Certificate remoteCertificate, string[] acceptableIssuers) |
| 280 | { |
| 281 | return GetCertificate(); |
| 282 | } |
| 283 | |
| 284 | private static bool CertValidator(object sender, X509Certificate certificate, |
| 285 | X509Chain chain, SslPolicyErrors sslPolicyErrors) |
| 286 | { |
| 287 | return true; |
| 288 | } |
| 289 | |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 290 | private static TProtocol MakeProtocol(string[] args, TTransport transport) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 291 | { |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 292 | Protocol selectedProtocol = GetProtocol(args); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 293 | return selectedProtocol switch |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 294 | { |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 295 | Protocol.Binary => new TBinaryProtocol(transport), |
| 296 | Protocol.Compact => new TCompactProtocol(transport), |
| 297 | Protocol.Json => new TJsonProtocol(transport), |
| 298 | _ => throw new Exception("unhandled protocol"), |
| 299 | }; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 300 | } |
| 301 | |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 302 | private static async Task RunClientAsync(TProtocol protocol, bool multiplex, CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 303 | { |
| 304 | try |
| 305 | { |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 306 | try |
| 307 | { |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 308 | if( multiplex) |
| 309 | protocol = new TMultiplexedProtocol(protocol, nameof(Calculator)); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 310 | |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 311 | var client = new Calculator.Client(protocol); |
| 312 | await ExecuteCalculatorClientOperations(client, cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 313 | } |
| 314 | catch (Exception ex) |
| 315 | { |
Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame^] | 316 | Logger.LogError("{ex}",ex); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 317 | } |
| 318 | finally |
| 319 | { |
| 320 | protocol.Transport.Close(); |
| 321 | } |
| 322 | } |
| 323 | catch (TApplicationException x) |
| 324 | { |
Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame^] | 325 | Logger.LogError("{x}",x); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 329 | private static async Task ExecuteCalculatorClientOperations( Calculator.Client client, CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 330 | { |
| 331 | await client.OpenTransportAsync(cancellationToken); |
| 332 | |
| 333 | // Async version |
| 334 | |
Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame^] | 335 | Logger.LogInformation("{client.ClientId} Ping()", client.ClientId); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 336 | await client.ping(cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 337 | |
Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame^] | 338 | Logger.LogInformation("{client.ClientId} Add(1,1)", client.ClientId); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 339 | var sum = await client.add(1, 1, cancellationToken); |
Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame^] | 340 | Logger.LogInformation("{client.ClientId} Add(1,1)={sum}", client.ClientId, sum); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 341 | |
| 342 | var work = new Work |
| 343 | { |
| 344 | Op = Operation.DIVIDE, |
| 345 | Num1 = 1, |
| 346 | Num2 = 0 |
| 347 | }; |
| 348 | |
| 349 | try |
| 350 | { |
Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame^] | 351 | Logger.LogInformation("{client.ClientId} Calculate(1)", client.ClientId); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 352 | await client.calculate(1, work, cancellationToken); |
Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame^] | 353 | Logger.LogInformation("{client.ClientId} Whoa we can divide by 0", client.ClientId); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 354 | } |
| 355 | catch (InvalidOperation io) |
| 356 | { |
Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame^] | 357 | Logger.LogInformation("{client.ClientId} Invalid operation: {io}", client.ClientId, io); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | work.Op = Operation.SUBTRACT; |
| 361 | work.Num1 = 15; |
| 362 | work.Num2 = 10; |
| 363 | |
| 364 | try |
| 365 | { |
Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame^] | 366 | Logger.LogInformation("{client.ClientId} Calculate(1)", client.ClientId); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 367 | var diff = await client.calculate(1, work, cancellationToken); |
Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame^] | 368 | Logger.LogInformation("{client.ClientId} 15-10={diff}", client.ClientId, diff); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 369 | } |
| 370 | catch (InvalidOperation io) |
| 371 | { |
Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame^] | 372 | Logger.LogInformation("{client.ClientId} Invalid operation: {io}", client.ClientId, io); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 373 | } |
| 374 | |
Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame^] | 375 | Logger.LogInformation("{client.ClientId} GetStruct(1)", client.ClientId); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 376 | var log = await client.getStruct(1, cancellationToken); |
Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame^] | 377 | Logger.LogInformation("{client.ClientId} Check log: {log.Value}", client.ClientId, log.Value); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 378 | |
Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame^] | 379 | Logger.LogInformation("{client.ClientId} Zip() with delay 100mc on server side", client.ClientId); |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 380 | await client.zip(cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 381 | } |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 382 | |
| 383 | |
| 384 | private enum Transport |
| 385 | { |
| 386 | Tcp, |
| 387 | NamedPipe, |
| 388 | Http, |
| 389 | TcpBuffered, |
| 390 | Framed, |
| 391 | TcpTls |
| 392 | } |
| 393 | |
| 394 | private enum Protocol |
| 395 | { |
| 396 | Binary, |
| 397 | Compact, |
| 398 | Json, |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 399 | } |
Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 400 | |
| 401 | private enum Buffering |
| 402 | { |
| 403 | None, |
| 404 | Buffered, |
| 405 | Framed |
| 406 | } |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 407 | } |
| 408 | } |