1 // Copyright 2021 gRPC authors.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 #include <android/binder_auto_utils.h>
16 #include <android/binder_ibinder.h>
17 #include <android/binder_ibinder_jni.h>
18 #include <android/binder_interface_utils.h>
19 #include <android/log.h>
22 #include "examples/protos/helloworld.grpc.pb.h"
23 #include "examples/protos/helloworld.pb.h"
25 #include <grpcpp/grpcpp.h>
27 #include "src/core/ext/transport/binder/server/binder_server.h"
28 #include "src/core/ext/transport/binder/server/binder_server_credentials.h"
31 class GreeterService : public helloworld::Greeter::Service {
33 grpc::Status SayHello(grpc::ServerContext*,
34 const helloworld::HelloRequest* request,
35 helloworld::HelloReply* response) override {
36 __android_log_print(ANDROID_LOG_INFO, "DemoServer", "Line number %d",
38 __android_log_print(ANDROID_LOG_INFO, "DemoServer", "Got hello request: %s",
39 request->name().c_str());
40 response->set_message("Hi, " + request->name());
41 return grpc::Status::OK;
47 extern "C" JNIEXPORT void JNICALL
48 Java_io_grpc_binder_cpp_exampleserver_ExportedEndpointService_init_1grpc_1server(
49 JNIEnv* env, jobject /*this*/) {
50 __android_log_print(ANDROID_LOG_INFO, "DemoServer", "Line number %d",
52 static std::unique_ptr<grpc::Server> server = nullptr;
54 if (server != nullptr) {
59 static GreeterService service;
60 grpc::ServerBuilder server_builder;
61 server_builder.RegisterService(&service);
63 server_builder.AddListeningPort(
64 "binder://example.service",
65 grpc::experimental::BinderServerCredentials());
67 server = server_builder.BuildAndStart();
70 extern "C" JNIEXPORT jobject JNICALL
71 Java_io_grpc_binder_cpp_exampleserver_ExportedEndpointService_get_1endpoint_1binder(
72 JNIEnv* env, jobject /*this*/) {
73 __android_log_print(ANDROID_LOG_INFO, "DemoServer", "Line number %d",
76 auto ai_binder = static_cast<AIBinder*>(
77 grpc::experimental::binder::GetEndpointBinder("example.service"));
79 __android_log_print(ANDROID_LOG_INFO, "DemoServer", "ai_binder = %p",
81 return AIBinder_toJavaBinder(env, ai_binder);