mirror of https://github.com/grpc/grpc.git
Add BinderTransport example server app (#27067)
Some code are commented out because the server interface has not been merged yet. After this, user should be able to install both apps on Android device and test if basic unary call works or notpull/27183/head
parent
013a45ccc4
commit
73003f8527
13 changed files with 303 additions and 4 deletions
@ -0,0 +1,21 @@ |
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" |
||||
package="io.grpc.binder.cpp.exampleserver" |
||||
android:versionCode="1" |
||||
android:versionName="1.0" > |
||||
|
||||
<uses-sdk |
||||
android:minSdkVersion="29" |
||||
android:targetSdkVersion="30" /> |
||||
|
||||
<application |
||||
android:label="gRPC BinderTransport Server Cpp"> |
||||
<activity |
||||
android:name=".MainActivity" |
||||
android:label="gRPC BinderTransport Server Cpp" > |
||||
<intent-filter> |
||||
<action android:name="android.intent.action.MAIN" /> |
||||
<category android:name="android.intent.category.LAUNCHER" /> |
||||
</intent-filter> |
||||
</activity> |
||||
</application> |
||||
</manifest> |
@ -0,0 +1,10 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" |
||||
package="io.grpc.binder.cpp.exampleserver"> |
||||
<uses-sdk android:minSdkVersion="29" android:targetSdkVersion="30"/> |
||||
<application> |
||||
<service |
||||
android:name=".ExportedEndpointService" |
||||
android:exported="true"/> |
||||
</application> |
||||
</manifest> |
@ -0,0 +1,65 @@ |
||||
# 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_binary", "android_library") |
||||
|
||||
cc_library( |
||||
name = "jni_lib", |
||||
srcs = ["native.cc"], |
||||
linkopts = [ |
||||
"-ldl", |
||||
"-llog", |
||||
"-lm", |
||||
"-lbinder_ndk", |
||||
"-Wl,--no-undefined", |
||||
], |
||||
deps = [ |
||||
# Temporarily directly depend on this target before we expose a public API |
||||
# TODO(mingcl): Uncomment this after server interfaces are merged |
||||
# "//src/core/ext/transport/binder/server:grpc_transport_binder_server", |
||||
"//:grpc++", |
||||
"//examples/protos:helloworld_cc_grpc", |
||||
], |
||||
alwayslink = True, |
||||
) |
||||
|
||||
android_library( |
||||
name = "activity", |
||||
srcs = [ |
||||
"ButtonPressHandler.java", |
||||
"MainActivity.java", |
||||
], |
||||
manifest = "AndroidManifest.xml", |
||||
resource_files = glob(["res/**"]), |
||||
deps = [ |
||||
":endpoint", |
||||
":jni_lib", |
||||
], |
||||
) |
||||
|
||||
android_library( |
||||
name = "endpoint", |
||||
srcs = ["ExportedEndpointService.java"], |
||||
exports_manifest = True, |
||||
manifest = "AndroidManifest_endpoint.xml", |
||||
deps = [], |
||||
) |
||||
|
||||
android_binary( |
||||
name = "app", |
||||
manifest = "AndroidManifest.xml", |
||||
deps = [ |
||||
":activity", |
||||
], |
||||
) |
@ -0,0 +1,13 @@ |
||||
package io.grpc.binder.cpp.exampleserver; |
||||
|
||||
import android.app.Application; |
||||
|
||||
public class ButtonPressHandler { |
||||
static { |
||||
System.loadLibrary("app"); |
||||
} |
||||
|
||||
public String onPressed(Application application) { |
||||
return "Server Button Pressed"; |
||||
} |
||||
} |
@ -0,0 +1,28 @@ |
||||
package io.grpc.binder.cpp.exampleserver; |
||||
|
||||
import android.app.Service; |
||||
import android.os.IBinder; |
||||
import android.content.Intent; |
||||
|
||||
/** Exposes gRPC services running in the main process */ |
||||
public final class ExportedEndpointService extends Service { |
||||
private final IBinder binder; |
||||
|
||||
static { |
||||
System.loadLibrary("app"); |
||||
} |
||||
|
||||
public ExportedEndpointService() { |
||||
init_grpc_server(); |
||||
binder = get_endpoint_binder(); |
||||
} |
||||
|
||||
@Override |
||||
public IBinder onBind(Intent intent) { |
||||
return binder; |
||||
} |
||||
|
||||
public native void init_grpc_server(); |
||||
|
||||
public native IBinder get_endpoint_binder(); |
||||
} |
@ -0,0 +1,27 @@ |
||||
package io.grpc.binder.cpp.exampleserver; |
||||
|
||||
import android.app.Activity; |
||||
import android.os.Bundle; |
||||
import android.util.Log; |
||||
import android.widget.Button; |
||||
import android.widget.TextView; |
||||
import io.grpc.binder.cpp.exampleserver.R; |
||||
|
||||
/** Main class for the example app. */ |
||||
public class MainActivity extends Activity { |
||||
@Override |
||||
public void onCreate(Bundle savedInstanceState) { |
||||
super.onCreate(savedInstanceState); |
||||
Log.v("Example", "hello, world"); |
||||
|
||||
setContentView(R.layout.activity_main); |
||||
|
||||
Button clickMeButton = findViewById(R.id.clickMeButton); |
||||
TextView exampleTextView = findViewById(R.id.exampleTextView); |
||||
|
||||
ButtonPressHandler h = new ButtonPressHandler(); |
||||
|
||||
clickMeButton.setOnClickListener( |
||||
v -> exampleTextView.setText(h.onPressed(getApplication()))); |
||||
} |
||||
} |
@ -0,0 +1,87 @@ |
||||
// 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 <android/binder_auto_utils.h> |
||||
#include <android/binder_ibinder.h> |
||||
#include <android/binder_ibinder_jni.h> |
||||
#include <android/binder_interface_utils.h> |
||||
#include <android/log.h> |
||||
#include <grpcpp/grpcpp.h> |
||||
#include <jni.h> |
||||
|
||||
#include "examples/protos/helloworld.grpc.pb.h" |
||||
#include "examples/protos/helloworld.pb.h" |
||||
|
||||
// TODO(mingcl): Uncomment this after server interfaces are merged
|
||||
// #include "src/core/ext/transport/binder/server/binder_server.h"
|
||||
// #include "src/core/ext/transport/binder/server/binder_server_credentials.h"
|
||||
|
||||
namespace { |
||||
class GreeterService : public helloworld::Greeter::Service { |
||||
public: |
||||
grpc::Status SayHello(grpc::ServerContext*, |
||||
const helloworld::HelloRequest* request, |
||||
helloworld::HelloReply* response) override { |
||||
__android_log_print(ANDROID_LOG_INFO, "DemoServer", "Line number %d", |
||||
__LINE__); |
||||
__android_log_print(ANDROID_LOG_INFO, "DemoServer", "Got hello request: %s", |
||||
request->name().c_str()); |
||||
response->set_message("Hi, " + request->name()); |
||||
return grpc::Status::OK; |
||||
} |
||||
}; |
||||
|
||||
} // namespace
|
||||
|
||||
extern "C" JNIEXPORT void JNICALL |
||||
Java_io_grpc_binder_cpp_exampleserver_ExportedEndpointService_init_1grpc_1server( |
||||
JNIEnv* env, jobject /*this*/) { |
||||
__android_log_print(ANDROID_LOG_INFO, "DemoServer", "Line number %d", |
||||
__LINE__); |
||||
static std::unique_ptr<grpc::Server> server = nullptr; |
||||
|
||||
if (server != nullptr) { |
||||
// Already initiated
|
||||
return; |
||||
} |
||||
|
||||
static GreeterService service; |
||||
grpc::ServerBuilder server_builder; |
||||
server_builder.RegisterService(&service); |
||||
|
||||
// TODO(mingcl): Uncomment this after server interfaces are merged
|
||||
//
|
||||
// grpc_endpoint_binder_pool_init();
|
||||
// server_builder.AddListeningPort("binder://example.service",
|
||||
// grpc::experimental::BinderServerCredentials());
|
||||
|
||||
server = server_builder.BuildAndStart(); |
||||
} |
||||
|
||||
extern "C" JNIEXPORT jobject JNICALL |
||||
Java_io_grpc_binder_cpp_exampleserver_ExportedEndpointService_get_1endpoint_1binder( |
||||
JNIEnv* env, jobject /*this*/) { |
||||
__android_log_print(ANDROID_LOG_INFO, "DemoServer", "Line number %d", |
||||
__LINE__); |
||||
|
||||
// TODO(mingcl): Uncomment this after server interfaces are merged
|
||||
// auto ai_binder =
|
||||
// static_cast<AIBinder*>(grpc::experimental::binder::GetEndpointBinder("example.service"));
|
||||
|
||||
AIBinder* ai_binder = nullptr; |
||||
|
||||
__android_log_print(ANDROID_LOG_INFO, "DemoServer", "ai_binder = %p", |
||||
ai_binder); |
||||
return AIBinder_toJavaBinder(env, ai_binder); |
||||
} |
@ -0,0 +1,24 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
android:padding="16dp" |
||||
android:orientation="vertical" |
||||
android:gravity="center_vertical" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent"> |
||||
|
||||
<TextView |
||||
android:id="@+id/exampleTextView" |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:layout_gravity="center_horizontal" |
||||
android:textSize="32dp" |
||||
android:text="@string/thinking_face"/> |
||||
|
||||
<Button |
||||
android:id="@+id/clickMeButton" |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:layout_gravity="center_horizontal" |
||||
android:layout_marginTop="16dp" |
||||
android:text="@string/click_me_button"/> |
||||
</LinearLayout> |
@ -0,0 +1,5 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<resources> |
||||
<string name="click_me_button">Run example</string> |
||||
<string name="thinking_face">🤔</string> |
||||
</resources> |
Loading…
Reference in new issue