Add a class for generating readable connection id for binder (#27771)

* Add a class for generating readable connection id for binder

This class will be used to generate a connection IDs that are used to
identify binder transport connections. For now we have not migrated to
client channel yet so we simply use this new class to generate a
connection id for the only connection we support.

* Regenerate projects.
pull/27818/head
Ming-Chuan 3 years ago committed by GitHub
parent 6d2a641dd3
commit 57af085d87
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      BUILD
  2. 8
      CMakeLists.txt
  3. 16
      build_autogenerated.yaml
  4. 19
      src/core/ext/transport/binder/client/channel_create.cc
  5. 67
      src/core/ext/transport/binder/client/connection_id_generator.cc
  6. 52
      src/core/ext/transport/binder/client/connection_id_generator.h

@ -499,6 +499,7 @@ grpc_cc_library(
srcs = [
"src/core/ext/transport/binder/client/channel_create.cc",
"src/core/ext/transport/binder/client/channel_create_impl.cc",
"src/core/ext/transport/binder/client/connection_id_generator.cc",
"src/core/ext/transport/binder/client/endpoint_binder_pool.cc",
"src/core/ext/transport/binder/client/jni_utils.cc",
"src/core/ext/transport/binder/security_policy/internal_only_security_policy.cc",
@ -516,6 +517,7 @@ grpc_cc_library(
hdrs = [
"src/core/ext/transport/binder/client/channel_create.h",
"src/core/ext/transport/binder/client/channel_create_impl.h",
"src/core/ext/transport/binder/client/connection_id_generator.h",
"src/core/ext/transport/binder/client/endpoint_binder_pool.h",
"src/core/ext/transport/binder/client/jni_utils.h",
"src/core/ext/transport/binder/security_policy/internal_only_security_policy.h",

8
CMakeLists.txt generated

@ -8349,6 +8349,7 @@ add_executable(binder_server_test
${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/simple_messages.grpc.pb.h
src/core/ext/transport/binder/client/channel_create.cc
src/core/ext/transport/binder/client/channel_create_impl.cc
src/core/ext/transport/binder/client/connection_id_generator.cc
src/core/ext/transport/binder/client/endpoint_binder_pool.cc
src/core/ext/transport/binder/client/jni_utils.cc
src/core/ext/transport/binder/security_policy/internal_only_security_policy.cc
@ -8402,6 +8403,7 @@ if(gRPC_BUILD_TESTS)
add_executable(binder_transport_test
src/core/ext/transport/binder/client/channel_create.cc
src/core/ext/transport/binder/client/channel_create_impl.cc
src/core/ext/transport/binder/client/connection_id_generator.cc
src/core/ext/transport/binder/client/endpoint_binder_pool.cc
src/core/ext/transport/binder/client/jni_utils.cc
src/core/ext/transport/binder/security_policy/internal_only_security_policy.cc
@ -10018,6 +10020,7 @@ add_executable(end2end_binder_transport_test
${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/simple_messages.grpc.pb.h
src/core/ext/transport/binder/client/channel_create.cc
src/core/ext/transport/binder/client/channel_create_impl.cc
src/core/ext/transport/binder/client/connection_id_generator.cc
src/core/ext/transport/binder/client/endpoint_binder_pool.cc
src/core/ext/transport/binder/client/jni_utils.cc
src/core/ext/transport/binder/security_policy/internal_only_security_policy.cc
@ -10126,6 +10129,7 @@ if(gRPC_BUILD_TESTS)
add_executable(endpoint_binder_pool_test
src/core/ext/transport/binder/client/channel_create.cc
src/core/ext/transport/binder/client/channel_create_impl.cc
src/core/ext/transport/binder/client/connection_id_generator.cc
src/core/ext/transport/binder/client/endpoint_binder_pool.cc
src/core/ext/transport/binder/client/jni_utils.cc
src/core/ext/transport/binder/security_policy/internal_only_security_policy.cc
@ -10542,6 +10546,7 @@ if(gRPC_BUILD_TESTS)
add_executable(fake_binder_test
src/core/ext/transport/binder/client/channel_create.cc
src/core/ext/transport/binder/client/channel_create_impl.cc
src/core/ext/transport/binder/client/connection_id_generator.cc
src/core/ext/transport/binder/client/endpoint_binder_pool.cc
src/core/ext/transport/binder/client/jni_utils.cc
src/core/ext/transport/binder/security_policy/internal_only_security_policy.cc
@ -16176,6 +16181,7 @@ if(gRPC_BUILD_TESTS)
add_executable(transport_stream_receiver_test
src/core/ext/transport/binder/client/channel_create.cc
src/core/ext/transport/binder/client/channel_create_impl.cc
src/core/ext/transport/binder/client/connection_id_generator.cc
src/core/ext/transport/binder/client/endpoint_binder_pool.cc
src/core/ext/transport/binder/client/jni_utils.cc
src/core/ext/transport/binder/security_policy/internal_only_security_policy.cc
@ -16491,6 +16497,7 @@ if(gRPC_BUILD_TESTS)
add_executable(wire_reader_test
src/core/ext/transport/binder/client/channel_create.cc
src/core/ext/transport/binder/client/channel_create_impl.cc
src/core/ext/transport/binder/client/connection_id_generator.cc
src/core/ext/transport/binder/client/endpoint_binder_pool.cc
src/core/ext/transport/binder/client/jni_utils.cc
src/core/ext/transport/binder/security_policy/internal_only_security_policy.cc
@ -16590,6 +16597,7 @@ if(gRPC_BUILD_TESTS)
add_executable(wire_writer_test
src/core/ext/transport/binder/client/channel_create.cc
src/core/ext/transport/binder/client/channel_create_impl.cc
src/core/ext/transport/binder/client/connection_id_generator.cc
src/core/ext/transport/binder/client/endpoint_binder_pool.cc
src/core/ext/transport/binder/client/jni_utils.cc
src/core/ext/transport/binder/security_policy/internal_only_security_policy.cc

@ -4629,6 +4629,7 @@ targets:
headers:
- src/core/ext/transport/binder/client/channel_create.h
- src/core/ext/transport/binder/client/channel_create_impl.h
- src/core/ext/transport/binder/client/connection_id_generator.h
- src/core/ext/transport/binder/client/endpoint_binder_pool.h
- src/core/ext/transport/binder/client/jni_utils.h
- src/core/ext/transport/binder/security_policy/internal_only_security_policy.h
@ -4655,6 +4656,7 @@ targets:
- src/proto/grpc/testing/simple_messages.proto
- src/core/ext/transport/binder/client/channel_create.cc
- src/core/ext/transport/binder/client/channel_create_impl.cc
- src/core/ext/transport/binder/client/connection_id_generator.cc
- src/core/ext/transport/binder/client/endpoint_binder_pool.cc
- src/core/ext/transport/binder/client/jni_utils.cc
- src/core/ext/transport/binder/security_policy/internal_only_security_policy.cc
@ -4681,6 +4683,7 @@ targets:
headers:
- src/core/ext/transport/binder/client/channel_create.h
- src/core/ext/transport/binder/client/channel_create_impl.h
- src/core/ext/transport/binder/client/connection_id_generator.h
- src/core/ext/transport/binder/client/endpoint_binder_pool.h
- src/core/ext/transport/binder/client/jni_utils.h
- src/core/ext/transport/binder/security_policy/internal_only_security_policy.h
@ -4714,6 +4717,7 @@ targets:
src:
- src/core/ext/transport/binder/client/channel_create.cc
- src/core/ext/transport/binder/client/channel_create_impl.cc
- src/core/ext/transport/binder/client/connection_id_generator.cc
- src/core/ext/transport/binder/client/endpoint_binder_pool.cc
- src/core/ext/transport/binder/client/jni_utils.cc
- src/core/ext/transport/binder/security_policy/internal_only_security_policy.cc
@ -5453,6 +5457,7 @@ targets:
headers:
- src/core/ext/transport/binder/client/channel_create.h
- src/core/ext/transport/binder/client/channel_create_impl.h
- src/core/ext/transport/binder/client/connection_id_generator.h
- src/core/ext/transport/binder/client/endpoint_binder_pool.h
- src/core/ext/transport/binder/client/jni_utils.h
- src/core/ext/transport/binder/security_policy/internal_only_security_policy.h
@ -5480,6 +5485,7 @@ targets:
- src/proto/grpc/testing/simple_messages.proto
- src/core/ext/transport/binder/client/channel_create.cc
- src/core/ext/transport/binder/client/channel_create_impl.cc
- src/core/ext/transport/binder/client/connection_id_generator.cc
- src/core/ext/transport/binder/client/endpoint_binder_pool.cc
- src/core/ext/transport/binder/client/jni_utils.cc
- src/core/ext/transport/binder/security_policy/internal_only_security_policy.cc
@ -5526,6 +5532,7 @@ targets:
headers:
- src/core/ext/transport/binder/client/channel_create.h
- src/core/ext/transport/binder/client/channel_create_impl.h
- src/core/ext/transport/binder/client/connection_id_generator.h
- src/core/ext/transport/binder/client/endpoint_binder_pool.h
- src/core/ext/transport/binder/client/jni_utils.h
- src/core/ext/transport/binder/security_policy/internal_only_security_policy.h
@ -5559,6 +5566,7 @@ targets:
src:
- src/core/ext/transport/binder/client/channel_create.cc
- src/core/ext/transport/binder/client/channel_create_impl.cc
- src/core/ext/transport/binder/client/connection_id_generator.cc
- src/core/ext/transport/binder/client/endpoint_binder_pool.cc
- src/core/ext/transport/binder/client/jni_utils.cc
- src/core/ext/transport/binder/security_policy/internal_only_security_policy.cc
@ -5756,6 +5764,7 @@ targets:
headers:
- src/core/ext/transport/binder/client/channel_create.h
- src/core/ext/transport/binder/client/channel_create_impl.h
- src/core/ext/transport/binder/client/connection_id_generator.h
- src/core/ext/transport/binder/client/endpoint_binder_pool.h
- src/core/ext/transport/binder/client/jni_utils.h
- src/core/ext/transport/binder/security_policy/internal_only_security_policy.h
@ -5789,6 +5798,7 @@ targets:
src:
- src/core/ext/transport/binder/client/channel_create.cc
- src/core/ext/transport/binder/client/channel_create_impl.cc
- src/core/ext/transport/binder/client/connection_id_generator.cc
- src/core/ext/transport/binder/client/endpoint_binder_pool.cc
- src/core/ext/transport/binder/client/jni_utils.cc
- src/core/ext/transport/binder/security_policy/internal_only_security_policy.cc
@ -8139,6 +8149,7 @@ targets:
headers:
- src/core/ext/transport/binder/client/channel_create.h
- src/core/ext/transport/binder/client/channel_create_impl.h
- src/core/ext/transport/binder/client/connection_id_generator.h
- src/core/ext/transport/binder/client/endpoint_binder_pool.h
- src/core/ext/transport/binder/client/jni_utils.h
- src/core/ext/transport/binder/security_policy/internal_only_security_policy.h
@ -8171,6 +8182,7 @@ targets:
src:
- src/core/ext/transport/binder/client/channel_create.cc
- src/core/ext/transport/binder/client/channel_create_impl.cc
- src/core/ext/transport/binder/client/connection_id_generator.cc
- src/core/ext/transport/binder/client/endpoint_binder_pool.cc
- src/core/ext/transport/binder/client/jni_utils.cc
- src/core/ext/transport/binder/security_policy/internal_only_security_policy.cc
@ -8329,6 +8341,7 @@ targets:
headers:
- src/core/ext/transport/binder/client/channel_create.h
- src/core/ext/transport/binder/client/channel_create_impl.h
- src/core/ext/transport/binder/client/connection_id_generator.h
- src/core/ext/transport/binder/client/endpoint_binder_pool.h
- src/core/ext/transport/binder/client/jni_utils.h
- src/core/ext/transport/binder/security_policy/internal_only_security_policy.h
@ -8362,6 +8375,7 @@ targets:
src:
- src/core/ext/transport/binder/client/channel_create.cc
- src/core/ext/transport/binder/client/channel_create_impl.cc
- src/core/ext/transport/binder/client/connection_id_generator.cc
- src/core/ext/transport/binder/client/endpoint_binder_pool.cc
- src/core/ext/transport/binder/client/jni_utils.cc
- src/core/ext/transport/binder/security_policy/internal_only_security_policy.cc
@ -8435,6 +8449,7 @@ targets:
headers:
- src/core/ext/transport/binder/client/channel_create.h
- src/core/ext/transport/binder/client/channel_create_impl.h
- src/core/ext/transport/binder/client/connection_id_generator.h
- src/core/ext/transport/binder/client/endpoint_binder_pool.h
- src/core/ext/transport/binder/client/jni_utils.h
- src/core/ext/transport/binder/security_policy/internal_only_security_policy.h
@ -8468,6 +8483,7 @@ targets:
src:
- src/core/ext/transport/binder/client/channel_create.cc
- src/core/ext/transport/binder/client/channel_create_impl.cc
- src/core/ext/transport/binder/client/connection_id_generator.cc
- src/core/ext/transport/binder/client/endpoint_binder_pool.cc
- src/core/ext/transport/binder/client/jni_utils.cc
- src/core/ext/transport/binder/security_policy/internal_only_security_policy.cc

@ -44,6 +44,7 @@
#include <grpcpp/impl/grpc_library.h>
#include "src/core/ext/transport/binder/client/channel_create_impl.h"
#include "src/core/ext/transport/binder/client/connection_id_generator.h"
#include "src/core/ext/transport/binder/client/endpoint_binder_pool.h"
#include "src/core/ext/transport/binder/client/jni_utils.h"
#include "src/core/ext/transport/binder/transport/binder_transport.h"
@ -56,10 +57,13 @@
namespace grpc {
namespace experimental {
namespace {
// TODO(mingcl): To support multiple binder transport connection at the same
// time, we will need to generate unique connection id for each connection.
// For now we use a fixed connection id. This will be fixed in the next PR
std::string kConnectionId = "connection_id_placeholder";
// For now we just use a single connection id globally. This will be fixed after
// we drop the BindToOnDeviceServerService interface
std::string g_connection_id;
} // namespace
void BindToOnDeviceServerService(void* jni_env_void, jobject application,
absl::string_view package_name,
@ -70,12 +74,19 @@ void BindToOnDeviceServerService(void* jni_env_void, jobject application,
JNIEnv* jni_env = static_cast<JNIEnv*>(jni_env_void);
{
GPR_ASSERT(g_connection_id == "");
g_connection_id = grpc_binder::GetConnectionIdGenerator()->Generate(
std::string(package_name), std::string(class_name));
GPR_ASSERT(g_connection_id != "");
}
// clang-format off
grpc_binder::CallStaticJavaMethod(jni_env,
"io/grpc/binder/cpp/NativeConnectionHelper",
"tryEstablishConnection",
"(Landroid/content/Context;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V",
application, std::string(package_name), std::string(class_name), kConnectionId);
application, std::string(package_name), std::string(class_name), g_connection_id);
// clang-format on
}
@ -104,7 +115,7 @@ std::shared_ptr<grpc::Channel> CreateCustomBinderChannel(
std::unique_ptr<grpc_binder::Binder> endpoint_binder;
grpc_binder::GetEndpointBinderPool()->GetEndpointBinder(
kConnectionId, [&](std::unique_ptr<grpc_binder::Binder> e) {
g_connection_id, [&](std::unique_ptr<grpc_binder::Binder> e) {
endpoint_binder = std::move(e);
});
// This assumes the above callback will be called immediately before

@ -0,0 +1,67 @@
// Copyright 2021 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <grpc/support/port_platform.h>
#include "src/core/ext/transport/binder/client/connection_id_generator.h"
#include "absl/strings/str_cat.h"
namespace {
// Make sure `s` does not contain characters other than numbers, alphabets,
// period and underscore
std::string Normalize(absl::string_view str_view) {
std::string s = std::string(str_view);
for (size_t i = 0; i < s.length(); i++) {
if (!isalnum(s[i]) && s[i] != '.') {
s[i] = '_';
}
}
return s;
}
// Remove prefix of the string if the string is longer than len
std::string StripToLength(const std::string& s, size_t len) {
if (s.length() > len) {
return s.substr(s.length() - len, len);
}
return s;
}
} // namespace
namespace grpc_binder {
std::string ConnectionIdGenerator::Generate(absl::string_view package_name,
absl::string_view class_name) {
// reserve some room for serial number
const size_t kReserveForNumbers = 15;
std::string s = StripToLength(
absl::StrCat(Normalize(package_name), "-", Normalize(class_name)),
kPathLengthLimit - kReserveForNumbers);
std::string ret;
{
grpc_core::MutexLock l(&m_);
// Insert a hyphen before serial number
ret = absl::StrCat(s, "-", ++count_);
}
GPR_ASSERT(ret.length() < kPathLengthLimit);
return ret;
}
ConnectionIdGenerator* GetConnectionIdGenerator() {
static ConnectionIdGenerator* cig = new ConnectionIdGenerator();
return cig;
}
} // namespace grpc_binder

@ -0,0 +1,52 @@
// Copyright 2021 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef GRPC_CORE_EXT_TRANSPORT_BINDER_CLIENT_CONNECTION_ID_GENERATOR_H
#define GRPC_CORE_EXT_TRANSPORT_BINDER_CLIENT_CONNECTION_ID_GENERATOR_H
#include <grpc/support/port_platform.h>
#include <map>
#include "absl/strings/string_view.h"
#include "src/core/lib/gprpp/sync.h"
namespace grpc_binder {
// Generates somewhat human-readable unique identifiers from package name and
// class name. We will generate a Id that only contains unreserved URI
// characters (uppercase and lowercase letters, decimal digits, hyphen, period,
// underscore, and tilde).
class ConnectionIdGenerator {
public:
std::string Generate(absl::string_view package_name,
absl::string_view class_name);
private:
// Our generated Id need to fit in unix socket path length limit. We use 100
// here to be safe.
const size_t kPathLengthLimit = 100;
grpc_core::Mutex m_;
// Every generated identifier will followed by the value of this counter to
// make sure every generated id is unique.
int count_ ABSL_GUARDED_BY(m_);
};
ConnectionIdGenerator* GetConnectionIdGenerator();
} // namespace grpc_binder
#endif // GRPC_CORE_EXT_TRANSPORT_BINDER_CLIENT_CONNECTION_ID_GENERATOR_H
Loading…
Cancel
Save