| Jan Tattermusch | 503bbac | 2015-02-26 18:19:47 -0800 | [diff] [blame] | 1 | #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 | |
| 34 | using System; |
| 35 | using System.Collections.Generic; |
| 36 | using System.Diagnostics; |
| Jan Tattermusch | b0829eb | 2015-03-03 09:30:55 -0800 | [diff] [blame] | 37 | using System.IO; |
| Jan Tattermusch | 503bbac | 2015-02-26 18:19:47 -0800 | [diff] [blame] | 38 | using System.Text.RegularExpressions; |
| 39 | using System.Threading.Tasks; |
| 40 | using Google.ProtocolBuffers; |
| Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 41 | using grpc.testing; |
| Jan Tattermusch | 503bbac | 2015-02-26 18:19:47 -0800 | [diff] [blame] | 42 | using Grpc.Core; |
| 43 | using Grpc.Core.Utils; |
| 44 | using NUnit.Framework; |
| Jan Tattermusch | 503bbac | 2015-02-26 18:19:47 -0800 | [diff] [blame] | 45 | |
| 46 | namespace Grpc.IntegrationTesting |
| 47 | { |
| 48 | public class InteropServer |
| 49 | { |
| 50 | private class ServerOptions |
| 51 | { |
| 52 | public bool help; |
| Jan Tattermusch | b0829eb | 2015-03-03 09:30:55 -0800 | [diff] [blame] | 53 | public int? port = 8070; |
| Jan Tattermusch | 503bbac | 2015-02-26 18:19:47 -0800 | [diff] [blame] | 54 | public bool useTls; |
| 55 | } |
| 56 | |
| 57 | ServerOptions options; |
| 58 | |
| 59 | private InteropServer(ServerOptions options) |
| 60 | { |
| 61 | this.options = options; |
| 62 | } |
| 63 | |
| 64 | public static void Run(string[] args) |
| 65 | { |
| 66 | Console.WriteLine("gRPC C# interop testing server"); |
| 67 | ServerOptions options = ParseArguments(args); |
| 68 | |
| 69 | if (!options.port.HasValue) |
| 70 | { |
| 71 | Console.WriteLine("Missing required argument."); |
| 72 | Console.WriteLine(); |
| 73 | options.help = true; |
| 74 | } |
| 75 | |
| 76 | if (options.help) |
| 77 | { |
| 78 | Console.WriteLine("Usage:"); |
| 79 | Console.WriteLine(" --port=PORT"); |
| 80 | Console.WriteLine(" --use_tls=BOOLEAN"); |
| 81 | Console.WriteLine(); |
| 82 | Environment.Exit(1); |
| 83 | } |
| 84 | |
| 85 | var interopServer = new InteropServer(options); |
| 86 | interopServer.Run(); |
| 87 | } |
| 88 | |
| 89 | private void Run() |
| 90 | { |
| Jan Tattermusch | 503bbac | 2015-02-26 18:19:47 -0800 | [diff] [blame] | 91 | var server = new Server(); |
| Jan Tattermusch | 7eb3a76 | 2015-05-07 14:26:13 -0700 | [diff] [blame] | 92 | server.AddServiceDefinition(TestService.BindService(new TestServiceImpl())); |
| Jan Tattermusch | 503bbac | 2015-02-26 18:19:47 -0800 | [diff] [blame] | 93 | |
| Jan Tattermusch | 03e82e2 | 2015-05-06 16:37:12 -0700 | [diff] [blame] | 94 | string host = "0.0.0.0"; |
| 95 | int port = options.port.Value; |
| Jan Tattermusch | b0829eb | 2015-03-03 09:30:55 -0800 | [diff] [blame] | 96 | if (options.useTls) |
| 97 | { |
| Jan Tattermusch | a96ac05 | 2015-07-24 14:49:30 -0700 | [diff] [blame] | 98 | server.AddPort(host, port, TestCredentials.CreateTestServerCredentials()); |
| Jan Tattermusch | b0829eb | 2015-03-03 09:30:55 -0800 | [diff] [blame] | 99 | } |
| 100 | else |
| 101 | { |
| Jan Tattermusch | a96ac05 | 2015-07-24 14:49:30 -0700 | [diff] [blame] | 102 | server.AddPort(host, options.port.Value, ServerCredentials.Insecure); |
| Jan Tattermusch | b0829eb | 2015-03-03 09:30:55 -0800 | [diff] [blame] | 103 | } |
| Jan Tattermusch | 03e82e2 | 2015-05-06 16:37:12 -0700 | [diff] [blame] | 104 | Console.WriteLine("Running server on " + string.Format("{0}:{1}", host, port)); |
| Jan Tattermusch | 503bbac | 2015-02-26 18:19:47 -0800 | [diff] [blame] | 105 | server.Start(); |
| 106 | |
| 107 | server.ShutdownTask.Wait(); |
| 108 | |
| 109 | GrpcEnvironment.Shutdown(); |
| 110 | } |
| 111 | |
| 112 | private static ServerOptions ParseArguments(string[] args) |
| 113 | { |
| 114 | var options = new ServerOptions(); |
| Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 115 | foreach (string arg in args) |
| Jan Tattermusch | 503bbac | 2015-02-26 18:19:47 -0800 | [diff] [blame] | 116 | { |
| 117 | ParseArgument(arg, options); |
| 118 | if (options.help) |
| 119 | { |
| 120 | break; |
| 121 | } |
| 122 | } |
| 123 | return options; |
| 124 | } |
| 125 | |
| 126 | private static void ParseArgument(string arg, ServerOptions options) |
| 127 | { |
| 128 | Match match; |
| 129 | match = Regex.Match(arg, "--port=(.*)"); |
| 130 | if (match.Success) |
| 131 | { |
| 132 | options.port = int.Parse(match.Groups[1].Value.Trim()); |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | match = Regex.Match(arg, "--use_tls=(.*)"); |
| 137 | if (match.Success) |
| 138 | { |
| 139 | options.useTls = bool.Parse(match.Groups[1].Value.Trim()); |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | Console.WriteLine(string.Format("Unrecognized argument \"{0}\"", arg)); |
| 144 | options.help = true; |
| 145 | } |
| 146 | } |
| 147 | } |