blob: be99f92d0b32bb2c96b3377a18f3672d7caf64b3 [file] [log] [blame]
Jan Tattermusch503bbac2015-02-26 18:19:47 -08001#region Copyright notice and license
2
3// Copyright 2015, Google Inc.
4// All rights reserved.
5//
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are
8// met:
9//
10// * Redistributions of source code must retain the above copyright
11// notice, this list of conditions and the following disclaimer.
12// * Redistributions in binary form must reproduce the above
13// copyright notice, this list of conditions and the following disclaimer
14// in the documentation and/or other materials provided with the
15// distribution.
16// * Neither the name of Google Inc. nor the names of its
17// contributors may be used to endorse or promote products derived from
18// this software without specific prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32#endregion
33
34using System;
35using System.Collections.Generic;
36using System.Diagnostics;
Jan Tattermuschb0829eb2015-03-03 09:30:55 -080037using System.IO;
Jan Tattermusch503bbac2015-02-26 18:19:47 -080038using System.Text.RegularExpressions;
39using System.Threading.Tasks;
Jan Tattermuschb26972f2015-09-03 17:47:14 -070040
41using CommandLine;
42using CommandLine.Text;
Jan Tattermusch503bbac2015-02-26 18:19:47 -080043using Grpc.Core;
Jan Tattermusch66f85782017-02-24 19:44:16 +010044using Grpc.Core.Logging;
Jan Tattermusch503bbac2015-02-26 18:19:47 -080045using Grpc.Core.Utils;
Jan Tattermusch8644aea2015-08-03 10:21:18 -070046using Grpc.Testing;
Jan Tattermusch503bbac2015-02-26 18:19:47 -080047using NUnit.Framework;
Jan Tattermusch503bbac2015-02-26 18:19:47 -080048
49namespace Grpc.IntegrationTesting
50{
51 public class InteropServer
52 {
53 private class ServerOptions
54 {
Jan Tattermusch678ec902016-08-08 12:54:16 +080055 [Option("port", Default = 8070)]
Jan Tattermuschb26972f2015-09-03 17:47:14 -070056 public int Port { get; set; }
57
Jan Tattermusch7828e812015-10-07 17:27:48 -070058 // Deliberately using nullable bool type to allow --use_tls=true syntax (as opposed to --use_tls)
Jan Tattermusch678ec902016-08-08 12:54:16 +080059 [Option("use_tls", Default = false)]
Jan Tattermusch7828e812015-10-07 17:27:48 -070060 public bool? UseTls { get; set; }
Jan Tattermusch503bbac2015-02-26 18:19:47 -080061 }
62
63 ServerOptions options;
64
65 private InteropServer(ServerOptions options)
66 {
67 this.options = options;
68 }
69
70 public static void Run(string[] args)
71 {
Jan Tattermusch66f85782017-02-24 19:44:16 +010072 GrpcEnvironment.SetLogger(new ConsoleLogger());
Jan Tattermusch90cbde82016-08-08 14:33:28 +080073 var parserResult = Parser.Default.ParseArguments<ServerOptions>(args)
74 .WithNotParsed(errors => Environment.Exit(1))
75 .WithParsed(options =>
76 {
77 var interopServer = new InteropServer(options);
78 interopServer.Run();
79 });
Jan Tattermusch503bbac2015-02-26 18:19:47 -080080 }
81
82 private void Run()
83 {
Jan Tattermusch021df8a2015-08-04 20:31:11 -070084 var server = new Server
85 {
86 Services = { TestService.BindService(new TestServiceImpl()) }
87 };
Jan Tattermusch503bbac2015-02-26 18:19:47 -080088
Jan Tattermusch03e82e22015-05-06 16:37:12 -070089 string host = "0.0.0.0";
Jan Tattermuschb26972f2015-09-03 17:47:14 -070090 int port = options.Port;
Jan Tattermusch7828e812015-10-07 17:27:48 -070091 if (options.UseTls.Value)
Jan Tattermuschb0829eb2015-03-03 09:30:55 -080092 {
Jan Tattermuschbeffc772015-10-22 13:28:22 -070093 server.Ports.Add(host, port, TestCredentials.CreateSslServerCredentials());
Jan Tattermuschb0829eb2015-03-03 09:30:55 -080094 }
95 else
96 {
Jan Tattermuschb26972f2015-09-03 17:47:14 -070097 server.Ports.Add(host, options.Port, ServerCredentials.Insecure);
Jan Tattermuschb0829eb2015-03-03 09:30:55 -080098 }
Jan Tattermusch03e82e22015-05-06 16:37:12 -070099 Console.WriteLine("Running server on " + string.Format("{0}:{1}", host, port));
Jan Tattermusch503bbac2015-02-26 18:19:47 -0800100 server.Start();
101
102 server.ShutdownTask.Wait();
Jan Tattermusch503bbac2015-02-26 18:19:47 -0800103 }
Jan Tattermusch503bbac2015-02-26 18:19:47 -0800104 }
105}