InteropServer.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #region Copyright notice and license
  2. // Copyright 2015, Google Inc.
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #endregion
  31. using System;
  32. using System.Collections.Generic;
  33. using System.Diagnostics;
  34. using System.IO;
  35. using System.Text.RegularExpressions;
  36. using System.Threading.Tasks;
  37. using Grpc.Core;
  38. using Grpc.Core.Utils;
  39. using Grpc.Testing;
  40. using NUnit.Framework;
  41. namespace Grpc.IntegrationTesting
  42. {
  43. public class InteropServer
  44. {
  45. private class ServerOptions
  46. {
  47. public bool help;
  48. public int? port = 8070;
  49. public bool useTls;
  50. }
  51. ServerOptions options;
  52. private InteropServer(ServerOptions options)
  53. {
  54. this.options = options;
  55. }
  56. public static void Run(string[] args)
  57. {
  58. Console.WriteLine("gRPC C# interop testing server");
  59. ServerOptions options = ParseArguments(args);
  60. if (!options.port.HasValue)
  61. {
  62. Console.WriteLine("Missing required argument.");
  63. Console.WriteLine();
  64. options.help = true;
  65. }
  66. if (options.help)
  67. {
  68. Console.WriteLine("Usage:");
  69. Console.WriteLine(" --port=PORT");
  70. Console.WriteLine(" --use_tls=BOOLEAN");
  71. Console.WriteLine();
  72. Environment.Exit(1);
  73. }
  74. var interopServer = new InteropServer(options);
  75. interopServer.Run();
  76. }
  77. private void Run()
  78. {
  79. var server = new Server
  80. {
  81. Services = { TestService.BindService(new TestServiceImpl()) }
  82. };
  83. string host = "0.0.0.0";
  84. int port = options.port.Value;
  85. if (options.useTls)
  86. {
  87. server.Ports.Add(host, port, TestCredentials.CreateTestServerCredentials());
  88. }
  89. else
  90. {
  91. server.Ports.Add(host, options.port.Value, ServerCredentials.Insecure);
  92. }
  93. Console.WriteLine("Running server on " + string.Format("{0}:{1}", host, port));
  94. server.Start();
  95. server.ShutdownTask.Wait();
  96. }
  97. private static ServerOptions ParseArguments(string[] args)
  98. {
  99. var options = new ServerOptions();
  100. foreach (string arg in args)
  101. {
  102. ParseArgument(arg, options);
  103. if (options.help)
  104. {
  105. break;
  106. }
  107. }
  108. return options;
  109. }
  110. private static void ParseArgument(string arg, ServerOptions options)
  111. {
  112. Match match;
  113. match = Regex.Match(arg, "--port=(.*)");
  114. if (match.Success)
  115. {
  116. options.port = int.Parse(match.Groups[1].Value.Trim());
  117. return;
  118. }
  119. match = Regex.Match(arg, "--use_tls=(.*)");
  120. if (match.Success)
  121. {
  122. options.useTls = bool.Parse(match.Groups[1].Value.Trim());
  123. return;
  124. }
  125. Console.WriteLine(string.Format("Unrecognized argument \"{0}\"", arg));
  126. options.help = true;
  127. }
  128. }
  129. }