Modify helloworld example to allow a target string
This commit is contained in:
parent
c564d28f9a
commit
2bf8cbb424
|
|
@ -255,6 +255,10 @@ main loop in `HandleRpcs` to query the queue.
|
|||
|
||||
For a working example, refer to [greeter_async_server.cc](greeter_async_server.cc).
|
||||
|
||||
#### Flags for the client
|
||||
|
||||
```sh
|
||||
./greeter_client --target="a target string used to create a GRPC client channel"
|
||||
```
|
||||
|
||||
|
||||
The Default value for --target is "localhost:50051".
|
||||
|
|
|
|||
|
|
@ -73,11 +73,32 @@ class GreeterClient {
|
|||
|
||||
int main(int argc, char** argv) {
|
||||
// Instantiate the client. It requires a channel, out of which the actual RPCs
|
||||
// are created. This channel models a connection to an endpoint (in this case,
|
||||
// localhost at port 50051). We indicate that the channel isn't authenticated
|
||||
// (use of InsecureChannelCredentials()).
|
||||
// are created. This channel models a connection to an endpoint specified by
|
||||
// the argument "--target=" which is the only expected argument.
|
||||
// We indicate that the channel isn't authenticated (use of
|
||||
// InsecureChannelCredentials()).
|
||||
std::string target_str;
|
||||
std::string arg_str("--target");
|
||||
if (argc > 1) {
|
||||
std::string arg_val = argv[1];
|
||||
size_t start_pos = arg_val.find(arg_str);
|
||||
if (start_pos != std::string::npos) {
|
||||
start_pos += arg_str.size();
|
||||
if (arg_val[start_pos] == '=') {
|
||||
target_str = arg_val.substr(start_pos + 1);
|
||||
} else {
|
||||
std::cout << "The only correct argument syntax is --target=" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
std::cout << "The only acceptable argument is --target=" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
target_str = "localhost:50051";
|
||||
}
|
||||
GreeterClient greeter(grpc::CreateChannel(
|
||||
"localhost:50051", grpc::InsecureChannelCredentials()));
|
||||
target_str, grpc::InsecureChannelCredentials()));
|
||||
std::string user("world");
|
||||
std::string reply = greeter.SayHello(user);
|
||||
std::cout << "Greeter received: " << reply << std::endl;
|
||||
|
|
|
|||
Loading…
Reference in New Issue