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.Diagnostics; 22 using System.IO; 23 using System.Text.RegularExpressions; 24 using System.Threading.Tasks; 25 26 using CommandLine; 27 using CommandLine.Text; 28 using Grpc.Core; 29 using Grpc.Core.Logging; 30 using Grpc.Core.Utils; 31 using Grpc.Testing; 32 using NUnit.Framework; 33 34 namespace Grpc.IntegrationTesting 35 { 36 public class InteropServer 37 { 38 private class ServerOptions 39 { 40 [Option("port", Default = 8070)] 41 public int Port { get; set; } 42 43 // Deliberately using nullable bool type to allow --use_tls=true syntax (as opposed to --use_tls) 44 [Option("use_tls", Default = false)] 45 public bool? UseTls { get; set; } 46 } 47 48 ServerOptions options; 49 InteropServer(ServerOptions options)50 private InteropServer(ServerOptions options) 51 { 52 this.options = options; 53 } 54 Run(string[] args)55 public static void Run(string[] args) 56 { 57 GrpcEnvironment.SetLogger(new ConsoleLogger()); 58 var parserResult = Parser.Default.ParseArguments<ServerOptions>(args) 59 .WithNotParsed(errors => Environment.Exit(1)) 60 .WithParsed(options => 61 { 62 var interopServer = new InteropServer(options); 63 interopServer.Run(); 64 }); 65 } 66 Run()67 private void Run() 68 { 69 var server = new Server 70 { 71 Services = { TestService.BindService(new TestServiceImpl()) } 72 }; 73 74 string host = "0.0.0.0"; 75 int port = options.Port; 76 if (options.UseTls.Value) 77 { 78 server.Ports.Add(host, port, TestCredentials.CreateSslServerCredentials()); 79 } 80 else 81 { 82 server.Ports.Add(host, options.Port, ServerCredentials.Insecure); 83 } 84 Console.WriteLine("Running server on " + string.Format("{0}:{1}", host, port)); 85 server.Start(); 86 87 server.ShutdownTask.Wait(); 88 } 89 } 90 } 91