blob: aca73b15ea691f965914c32cb9e0124dbc8e21d7 [file] [log] [blame]
Jan Tattermusch503bbac2015-02-26 18:19:47 -08001#region Copyright notice and license
2
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003// Copyright 2015 gRPC authors.
Jan Tattermusch503bbac2015-02-26 18:19:47 -08004//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005// 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
Jan Tattermusch503bbac2015-02-26 18:19:47 -08008//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009// http://www.apache.org/licenses/LICENSE-2.0
Jan Tattermusch503bbac2015-02-26 18:19:47 -080010//
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011// 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.
Jan Tattermusch503bbac2015-02-26 18:19:47 -080016
17#endregion
18
19using System;
20using System.Collections.Generic;
21using System.Diagnostics;
Jan Tattermuschb0829eb2015-03-03 09:30:55 -080022using System.IO;
Jan Tattermusch503bbac2015-02-26 18:19:47 -080023using System.Text.RegularExpressions;
24using System.Threading.Tasks;
Jan Tattermuschb26972f2015-09-03 17:47:14 -070025
26using CommandLine;
27using CommandLine.Text;
Jan Tattermusch503bbac2015-02-26 18:19:47 -080028using Grpc.Core;
Jan Tattermusch66f85782017-02-24 19:44:16 +010029using Grpc.Core.Logging;
Jan Tattermusch503bbac2015-02-26 18:19:47 -080030using Grpc.Core.Utils;
Jan Tattermusch8644aea2015-08-03 10:21:18 -070031using Grpc.Testing;
Jan Tattermusch503bbac2015-02-26 18:19:47 -080032using NUnit.Framework;
Jan Tattermusch503bbac2015-02-26 18:19:47 -080033
34namespace Grpc.IntegrationTesting
35{
36 public class InteropServer
37 {
38 private class ServerOptions
39 {
Jan Tattermusch678ec902016-08-08 12:54:16 +080040 [Option("port", Default = 8070)]
Jan Tattermuschb26972f2015-09-03 17:47:14 -070041 public int Port { get; set; }
42
Jan Tattermusch7828e812015-10-07 17:27:48 -070043 // Deliberately using nullable bool type to allow --use_tls=true syntax (as opposed to --use_tls)
Jan Tattermusch678ec902016-08-08 12:54:16 +080044 [Option("use_tls", Default = false)]
Jan Tattermusch7828e812015-10-07 17:27:48 -070045 public bool? UseTls { get; set; }
Jan Tattermusch503bbac2015-02-26 18:19:47 -080046 }
47
48 ServerOptions options;
49
50 private InteropServer(ServerOptions options)
51 {
52 this.options = options;
53 }
54
55 public static void Run(string[] args)
56 {
Jan Tattermusch66f85782017-02-24 19:44:16 +010057 GrpcEnvironment.SetLogger(new ConsoleLogger());
Jan Tattermusch90cbde82016-08-08 14:33:28 +080058 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 });
Jan Tattermusch503bbac2015-02-26 18:19:47 -080065 }
66
67 private void Run()
68 {
Jan Tattermusch021df8a2015-08-04 20:31:11 -070069 var server = new Server
70 {
71 Services = { TestService.BindService(new TestServiceImpl()) }
72 };
Jan Tattermusch503bbac2015-02-26 18:19:47 -080073
Jan Tattermusch03e82e22015-05-06 16:37:12 -070074 string host = "0.0.0.0";
Jan Tattermuschb26972f2015-09-03 17:47:14 -070075 int port = options.Port;
Jan Tattermusch7828e812015-10-07 17:27:48 -070076 if (options.UseTls.Value)
Jan Tattermuschb0829eb2015-03-03 09:30:55 -080077 {
Jan Tattermuschbeffc772015-10-22 13:28:22 -070078 server.Ports.Add(host, port, TestCredentials.CreateSslServerCredentials());
Jan Tattermuschb0829eb2015-03-03 09:30:55 -080079 }
80 else
81 {
Jan Tattermuschb26972f2015-09-03 17:47:14 -070082 server.Ports.Add(host, options.Port, ServerCredentials.Insecure);
Jan Tattermuschb0829eb2015-03-03 09:30:55 -080083 }
Jan Tattermusch03e82e22015-05-06 16:37:12 -070084 Console.WriteLine("Running server on " + string.Format("{0}:{1}", host, port));
Jan Tattermusch503bbac2015-02-26 18:19:47 -080085 server.Start();
86
87 server.ShutdownTask.Wait();
Jan Tattermusch503bbac2015-02-26 18:19:47 -080088 }
Jan Tattermusch503bbac2015-02-26 18:19:47 -080089 }
90}