Make ServerBuilder accept (dns:///) URIs instead of just dns names
diff --git a/include/grpc++/server_builder.h b/include/grpc++/server_builder.h
index a56f81d..b29d960 100644
--- a/include/grpc++/server_builder.h
+++ b/include/grpc++/server_builder.h
@@ -142,14 +142,15 @@
///
/// It can be invoked multiple times.
///
- /// \param addr The address to try to bind to the server (eg, localhost:1234,
- /// 192.168.1.1:31416, [::1]:27182, etc.).
+ /// \param addr_uri The address to try to bind to the server in URI form. If
+ /// the scheme name is omitted, "dns:///" is assumed. Valid values include
+ /// dns:///localhost:1234, / 192.168.1.1:31416, dns:///[::1]:27182, etc.).
/// \params creds The credentials associated with the server.
/// \param selected_port[out] Upon success, updated to contain the port
/// number. \a nullptr otherwise.
///
// TODO(dgq): the "port" part seems to be a misnomer.
- ServerBuilder& AddListeningPort(const grpc::string& addr,
+ ServerBuilder& AddListeningPort(const grpc::string& addr_uri,
std::shared_ptr<ServerCredentials> creds,
int* selected_port = nullptr);
diff --git a/src/cpp/server/server_builder.cc b/src/cpp/server/server_builder.cc
index 2ead048..87fa1df 100644
--- a/src/cpp/server/server_builder.cc
+++ b/src/cpp/server/server_builder.cc
@@ -172,8 +172,15 @@
}
ServerBuilder& ServerBuilder::AddListeningPort(
- const grpc::string& addr, std::shared_ptr<ServerCredentials> creds,
+ const grpc::string& addr_uri, std::shared_ptr<ServerCredentials> creds,
int* selected_port) {
+ const grpc::string uri_scheme = "dns:";
+ grpc::string addr = addr_uri;
+ if (addr_uri.compare(0, uri_scheme.size(), uri_scheme) == 0) {
+ size_t pos = uri_scheme.size();
+ while (addr_uri[pos] == '/') ++pos; // Skip slashes.
+ addr = addr_uri.substr(pos);
+ }
Port port = {addr, creds, selected_port};
ports_.push_back(port);
return *this;