Imported Upstream version 1.41.0
[platform/upstream/grpc.git] / examples / android / binder / java / io / grpc / binder / cpp / exampleserver / native.cc
1 // Copyright 2021 gRPC authors.
2 //
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
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14
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>
20 #include <jni.h>
21
22 #include "examples/protos/helloworld.grpc.pb.h"
23 #include "examples/protos/helloworld.pb.h"
24
25 #include <grpcpp/grpcpp.h>
26
27 #include "src/core/ext/transport/binder/server/binder_server.h"
28 #include "src/core/ext/transport/binder/server/binder_server_credentials.h"
29
30 namespace {
31 class GreeterService : public helloworld::Greeter::Service {
32  public:
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",
37                         __LINE__);
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;
42   }
43 };
44
45 }  // namespace
46
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",
51                       __LINE__);
52   static std::unique_ptr<grpc::Server> server = nullptr;
53
54   if (server != nullptr) {
55     // Already initiated
56     return;
57   }
58
59   static GreeterService service;
60   grpc::ServerBuilder server_builder;
61   server_builder.RegisterService(&service);
62
63   server_builder.AddListeningPort(
64       "binder://example.service",
65       grpc::experimental::BinderServerCredentials());
66
67   server = server_builder.BuildAndStart();
68 }
69
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",
74                       __LINE__);
75
76   auto ai_binder = static_cast<AIBinder*>(
77       grpc::experimental::binder::GetEndpointBinder("example.service"));
78
79   __android_log_print(ANDROID_LOG_INFO, "DemoServer", "ai_binder = %p",
80                       ai_binder);
81   return AIBinder_toJavaBinder(env, ai_binder);
82 }