blob: 87c3cbe1d4cf3565dd5ba5e8d95d22e2aa186350 [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;
40using Google.ProtocolBuffers;
Jan Tattermusch075dde42015-03-11 18:21:00 -070041using grpc.testing;
Jan Tattermusch503bbac2015-02-26 18:19:47 -080042using Grpc.Core;
43using Grpc.Core.Utils;
44using NUnit.Framework;
Jan Tattermusch503bbac2015-02-26 18:19:47 -080045
46namespace Grpc.IntegrationTesting
47{
48 public class InteropServer
49 {
50 private class ServerOptions
51 {
52 public bool help;
Jan Tattermuschb0829eb2015-03-03 09:30:55 -080053 public int? port = 8070;
Jan Tattermusch503bbac2015-02-26 18:19:47 -080054 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 Tattermusch7eb3a762015-05-07 14:26:13 -070094 server.AddServiceDefinition(TestService.BindService(new TestServiceImpl()));
Jan Tattermusch503bbac2015-02-26 18:19:47 -080095
Jan Tattermusch03e82e22015-05-06 16:37:12 -070096 string host = "0.0.0.0";
97 int port = options.port.Value;
Jan Tattermuschb0829eb2015-03-03 09:30:55 -080098 if (options.useTls)
99 {
Jan Tattermusch03e82e22015-05-06 16:37:12 -0700100 server.AddListeningPort(host, port, TestCredentials.CreateTestServerCredentials());
Jan Tattermuschb0829eb2015-03-03 09:30:55 -0800101 }
102 else
103 {
Jan Tattermusch03e82e22015-05-06 16:37:12 -0700104 server.AddListeningPort(host, options.port.Value);
Jan Tattermuschb0829eb2015-03-03 09:30:55 -0800105 }
Jan Tattermusch03e82e22015-05-06 16:37:12 -0700106 Console.WriteLine("Running server on " + string.Format("{0}:{1}", host, port));
Jan Tattermusch503bbac2015-02-26 18:19:47 -0800107 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 Tattermusch075dde42015-03-11 18:21:00 -0700117 foreach (string arg in args)
Jan Tattermusch503bbac2015-02-26 18:19:47 -0800118 {
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}