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 | { |
| 91 | GrpcEnvironment.Initialize(); |
| 92 | |
| 93 | var server = new Server(); |
Jan Tattermusch | 7eb3a76 | 2015-05-07 14:26:13 -0700 | [diff] [blame^] | 94 | server.AddServiceDefinition(TestService.BindService(new TestServiceImpl())); |
Jan Tattermusch | 503bbac | 2015-02-26 18:19:47 -0800 | [diff] [blame] | 95 | |
Jan Tattermusch | 03e82e2 | 2015-05-06 16:37:12 -0700 | [diff] [blame] | 96 | string host = "0.0.0.0"; |
| 97 | int port = options.port.Value; |
Jan Tattermusch | b0829eb | 2015-03-03 09:30:55 -0800 | [diff] [blame] | 98 | if (options.useTls) |
| 99 | { |
Jan Tattermusch | 03e82e2 | 2015-05-06 16:37:12 -0700 | [diff] [blame] | 100 | server.AddListeningPort(host, port, TestCredentials.CreateTestServerCredentials()); |
Jan Tattermusch | b0829eb | 2015-03-03 09:30:55 -0800 | [diff] [blame] | 101 | } |
| 102 | else |
| 103 | { |
Jan Tattermusch | 03e82e2 | 2015-05-06 16:37:12 -0700 | [diff] [blame] | 104 | server.AddListeningPort(host, options.port.Value); |
Jan Tattermusch | b0829eb | 2015-03-03 09:30:55 -0800 | [diff] [blame] | 105 | } |
Jan Tattermusch | 03e82e2 | 2015-05-06 16:37:12 -0700 | [diff] [blame] | 106 | Console.WriteLine("Running server on " + string.Format("{0}:{1}", host, port)); |
Jan Tattermusch | 503bbac | 2015-02-26 18:19:47 -0800 | [diff] [blame] | 107 | server.Start(); |
| 108 | |
| 109 | server.ShutdownTask.Wait(); |
| 110 | |
| 111 | GrpcEnvironment.Shutdown(); |
| 112 | } |
| 113 | |
| 114 | private static ServerOptions ParseArguments(string[] args) |
| 115 | { |
| 116 | var options = new ServerOptions(); |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 117 | foreach (string arg in args) |
Jan Tattermusch | 503bbac | 2015-02-26 18:19:47 -0800 | [diff] [blame] | 118 | { |
| 119 | ParseArgument(arg, options); |
| 120 | if (options.help) |
| 121 | { |
| 122 | break; |
| 123 | } |
| 124 | } |
| 125 | return options; |
| 126 | } |
| 127 | |
| 128 | private static void ParseArgument(string arg, ServerOptions options) |
| 129 | { |
| 130 | Match match; |
| 131 | match = Regex.Match(arg, "--port=(.*)"); |
| 132 | if (match.Success) |
| 133 | { |
| 134 | options.port = int.Parse(match.Groups[1].Value.Trim()); |
| 135 | return; |
| 136 | } |
| 137 | |
| 138 | match = Regex.Match(arg, "--use_tls=(.*)"); |
| 139 | if (match.Success) |
| 140 | { |
| 141 | options.useTls = bool.Parse(match.Groups[1].Value.Trim()); |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | Console.WriteLine(string.Format("Unrecognized argument \"{0}\"", arg)); |
| 146 | options.help = true; |
| 147 | } |
| 148 | } |
| 149 | } |