• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 The gRPC Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 using System;
16 using System.Net;
17 using System.Threading.Tasks;
18 using Grpc.Core;
19 using Grpc.HealthCheck;
20 using Helloworld;
21 using Grpc.Health;
22 using Grpc.Health.V1;
23 using Grpc.Reflection;
24 using Grpc.Reflection.V1Alpha;
25 using CommandLine;
26 
27 namespace GreeterServer
28 {
29     class GreeterImpl : Greeter.GreeterBase
30     {
31         private string hostname;
32 
GreeterImpl(string hostname)33         public GreeterImpl(string hostname)
34         {
35             this.hostname = hostname;
36         }
37 
38         // Server side handler of the SayHello RPC
SayHello(HelloRequest request, ServerCallContext context)39         public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
40         {
41             return Task.FromResult(new HelloReply { Message = $"Hello {request.Name} from {hostname}!"});
42         }
43     }
44 
45     class Program
46     {
47         class Options
48         {
49             [Option("port", Default = 50051, HelpText = "The port to listen on.")]
50             public int Port { get; set; }
51 
52             [Option("hostname", Required = false, HelpText = "The name clients will see in responses. If not specified, machine's hostname will obtain automatically.")]
53             public string Hostname { get; set; }
54         }
55 
Main(string[] args)56         public static void Main(string[] args)
57         {
58             Parser.Default.ParseArguments<Options>(args)
59                    .WithParsed<Options>(options => RunServer(options));
60         }
61 
RunServer(Options options)62         private static void RunServer(Options options)
63         {
64             var hostName = options.Hostname ?? Dns.GetHostName();
65 
66             var serviceDescriptors = new [] {Greeter.Descriptor, Health.Descriptor, ServerReflection.Descriptor};
67             var greeterImpl = new GreeterImpl(hostName);
68             var healthServiceImpl = new HealthServiceImpl();
69             var reflectionImpl = new ReflectionServiceImpl(serviceDescriptors);
70 
71             Server server = new Server
72             {
73                 Services = { Greeter.BindService(greeterImpl), Health.BindService(healthServiceImpl), ServerReflection.BindService(reflectionImpl) },
74                 Ports = { new ServerPort("[::]", options.Port, ServerCredentials.Insecure) }
75             };
76             server.Start();
77 
78             // Mark all services as healthy.
79             foreach (var serviceDescriptor in serviceDescriptors)
80             {
81                 healthServiceImpl.SetStatus(serviceDescriptor.FullName, HealthCheckResponse.Types.ServingStatus.Serving);
82             }
83             // Mark overall server status as healthy.
84             healthServiceImpl.SetStatus("", HealthCheckResponse.Types.ServingStatus.Serving);
85 
86             Console.WriteLine("Greeter server listening on port " + options.Port);
87             Console.WriteLine("Press any key to stop the server...");
88             Console.ReadKey();
89 
90             server.ShutdownAsync().Wait();
91         }
92     }
93 }
94