blob: 9475e66c4087f14ef0947f2309618d2497d1078a [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 {
Jan Tattermusch503bbac2015-02-26 18:19:47 -080091 var server = new Server();
Jan Tattermusch7eb3a762015-05-07 14:26:13 -070092 server.AddServiceDefinition(TestService.BindService(new TestServiceImpl()));
Jan Tattermusch503bbac2015-02-26 18:19:47 -080093
Jan Tattermusch03e82e22015-05-06 16:37:12 -070094 string host = "0.0.0.0";
95 int port = options.port.Value;
Jan Tattermuschb0829eb2015-03-03 09:30:55 -080096 if (options.useTls)
97 {
Jan Tattermusch03e82e22015-05-06 16:37:12 -070098 server.AddListeningPort(host, port, TestCredentials.CreateTestServerCredentials());
Jan Tattermuschb0829eb2015-03-03 09:30:55 -080099 }
100 else
101 {
Jan Tattermusch03e82e22015-05-06 16:37:12 -0700102 server.AddListeningPort(host, options.port.Value);
Jan Tattermuschb0829eb2015-03-03 09:30:55 -0800103 }
Jan Tattermusch03e82e22015-05-06 16:37:12 -0700104 Console.WriteLine("Running server on " + string.Format("{0}:{1}", host, port));
Jan Tattermusch503bbac2015-02-26 18:19:47 -0800105 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 Tattermusch075dde42015-03-11 18:21:00 -0700115 foreach (string arg in args)
Jan Tattermusch503bbac2015-02-26 18:19:47 -0800116 {
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}