mirror of https://github.com/grpc/grpc.git
Add Java helper code for establishing binder transport connection (#26862)
These Java code will be invoked by binder transport C++ implementation through JNI to establish the connection between client and server. The code is locally tested with other pending changes to make sure it works correctly. For now we only make sure it builds in CI. We will port proper tests from internal repository later. A new local repository is created for the Android-only Java code because 1. The analysis of its BUILD will fail without Android SDK configured 2. We want to prevent clang-tidy (and maybe other scripts)'s automatic expansion of '...' to include it as they typically don't have Android SDK installedpull/27006/head
parent
3e34e3aac7
commit
f0489c8f5f
6 changed files with 156 additions and 0 deletions
@ -0,0 +1,27 @@ |
||||
# 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. |
||||
|
||||
load("@build_bazel_rules_android//android:rules.bzl", "android_library") |
||||
|
||||
licenses(["notice"]) |
||||
|
||||
android_library( |
||||
name = "connection_helper", |
||||
srcs = [ |
||||
"NativeConnectionHelper.java", |
||||
"SyncServiceConnection.java", |
||||
], |
||||
visibility = ["//visibility:public"], |
||||
deps = [], |
||||
) |
@ -0,0 +1,43 @@ |
||||
// 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.
|
||||
|
||||
package io.grpc.binder.cpp; |
||||
|
||||
import android.content.Context; |
||||
import android.os.IBinder; |
||||
import android.os.Parcel; |
||||
|
||||
/** |
||||
* This class will be invoked by gRPC binder transport internal implementation to perform operations |
||||
* that are only possible in Java |
||||
*/ |
||||
final class NativeConnectionHelper { |
||||
static SyncServiceConnection s; |
||||
|
||||
static void tryEstablishConnection(Context context) { |
||||
s = new SyncServiceConnection(context); |
||||
s.tryConnect(); |
||||
} |
||||
|
||||
// TODO(mingcl): We should notify C++ once we got the service binder so they don't need to call
|
||||
// this function to check. For now we assume that this function will only be called after
|
||||
// successful connection
|
||||
static IBinder getServiceBinder() { |
||||
return s.getIBinder(); |
||||
} |
||||
|
||||
static Parcel getEmptyParcel() { |
||||
return Parcel.obtain(); |
||||
} |
||||
} |
@ -0,0 +1,71 @@ |
||||
// 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.
|
||||
|
||||
package io.grpc.binder.cpp; |
||||
|
||||
import android.content.ComponentName; |
||||
import android.content.Context; |
||||
import android.content.Intent; |
||||
import android.content.ServiceConnection; |
||||
import android.os.IBinder; |
||||
import android.util.Log; |
||||
|
||||
/* Connects to a service synchronously */ |
||||
public class SyncServiceConnection implements ServiceConnection { |
||||
private final String logTag = "SyncServiceConnection"; |
||||
|
||||
private Context mContext; |
||||
private IBinder mService; |
||||
|
||||
public SyncServiceConnection(Context context) { |
||||
mContext = context; |
||||
} |
||||
|
||||
@Override |
||||
public void onServiceConnected(ComponentName className, IBinder service) { |
||||
Log.e(logTag, "Service has connected: "); |
||||
synchronized (this) { |
||||
mService = service; |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void onServiceDisconnected(ComponentName className) { |
||||
Log.e(logTag, "Service has disconnected: "); |
||||
} |
||||
|
||||
public void tryConnect() { |
||||
synchronized (this) { |
||||
Intent intent = new Intent("grpc.io.action.BIND"); |
||||
// TODO(mingcl): The component name is currently hard-coded here and should be changed
|
||||
// manually before compile. We should pump the component name from C++ to here instead after
|
||||
// we have a server ready for integration test.
|
||||
ComponentName compName = new ComponentName("redacted", "redacted"); |
||||
intent.setComponent(compName); |
||||
// Will return true if the system is in the process of bringing up a service that your client
|
||||
// has permission to bind to; false if the system couldn't find the service or if your client
|
||||
// doesn't have permission to bind to it
|
||||
boolean result = mContext.bindService(intent, this, Context.BIND_AUTO_CREATE); |
||||
if (result) { |
||||
Log.e(logTag, "bindService ok"); |
||||
} else { |
||||
Log.e(logTag, "bindService not ok"); |
||||
} |
||||
} |
||||
} |
||||
|
||||
public IBinder getIBinder() { |
||||
return mService; |
||||
} |
||||
} |
Loading…
Reference in new issue