add commandline parsing

pull/23114/head
Jan Tattermusch 5 years ago
parent 8049266e64
commit f5ecc0adc8
  1. 1
      examples/csharp/Xds/Greeter/Greeter.csproj
  2. 15
      examples/csharp/Xds/GreeterClient/Program.cs
  3. 21
      examples/csharp/Xds/GreeterServer/Program.cs
  4. 2
      examples/csharp/Xds/README.md

@ -9,6 +9,7 @@
<PackageReference Include="Grpc.Core" Version="2.29.0" />
<PackageReference Include="Grpc.HealthCheck" Version="2.29.0" />
<PackageReference Include="Grpc.Reflection" Version="2.29.0"/>
<PackageReference Include="CommandLineParser" Version="2.8.0" />
<PackageReference Include="Grpc.Tools" Version="2.29.0" PrivateAssets="All" />
</ItemGroup>

@ -15,16 +15,27 @@
using System;
using Grpc.Core;
using Helloworld;
using CommandLine;
namespace GreeterClient
{
class Program
{
private class Options
{
[Option("server", Default = "localhost:50051", HelpText = "The address of the server")]
public string Server { get; set; }
}
public static void Main(string[] args)
{
// TODO: specify server address..
Parser.Default.ParseArguments<Options>(args)
.WithParsed<Options>(options => RunClient(options));
}
Channel channel = new Channel("127.0.0.1:50051", ChannelCredentials.Insecure);
private static void RunClient(Options options)
{
Channel channel = new Channel(options.Server, ChannelCredentials.Insecure);
var client = new Greeter.GreeterClient(channel);
String user = "you";

@ -22,6 +22,7 @@ using Grpc.Health;
using Grpc.Health.V1;
using Grpc.Reflection;
using Grpc.Reflection.V1Alpha;
using CommandLine;
namespace GreeterServer
{
@ -30,16 +31,28 @@ namespace GreeterServer
// Server side handler of the SayHello RPC
public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
{
String hostName = Dns.GetHostName(); // TODO: make hostname configurable
String hostName = Dns.GetHostName();
return Task.FromResult(new HelloReply { Message = $"Hello {request.Name} from {hostName}!"});
}
}
class Program
{
const int Port = 50051; // TODO: make port configurable
class Options
{
[Option("port", Default = 50051, HelpText = "The port to listen on.")]
public int Port { get; set; }
// TODO: make hostname configurable
}
public static void Main(string[] args)
{
Parser.Default.ParseArguments<Options>(args)
.WithParsed<Options>(options => RunServer(options));
}
private static void RunServer(Options options)
{
var serviceDescriptors = new [] {Greeter.Descriptor, Health.Descriptor, ServerReflection.Descriptor};
var greeterImpl = new GreeterImpl();
@ -49,7 +62,7 @@ namespace GreeterServer
Server server = new Server
{
Services = { Greeter.BindService(greeterImpl), Health.BindService(healthServiceImpl), ServerReflection.BindService(reflectionImpl) },
Ports = { new ServerPort("localhost", Port, ServerCredentials.Insecure) } // TODO: don't listen on just localhost
Ports = { new ServerPort("[::]", options.Port, ServerCredentials.Insecure) }
};
server.Start();
@ -61,7 +74,7 @@ namespace GreeterServer
// Mark overall server status as healthy.
healthServiceImpl.SetStatus("", HealthCheckResponse.Types.ServingStatus.Serving);
Console.WriteLine("Greeter server listening on port " + Port);
Console.WriteLine("Greeter server listening on port " + options.Port);
Console.WriteLine("Press any key to stop the server...");
Console.ReadKey();

@ -54,7 +54,7 @@ Finally, run your client:
```
cd GreeterClient
dotnet run -- xds-experimental:///my-backend
dotnet run --server xds-experimental:///my-backend
```
VERIFYING THE SERVER

Loading…
Cancel
Save