exampleserver/native.cc
Go to the documentation of this file.
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/log.h>
16 #include <jni.h>
17 
18 #include "examples/protos/helloworld.grpc.pb.h"
19 #include "examples/protos/helloworld.pb.h"
20 
22 #include <grpcpp/grpcpp.h>
25 
26 namespace {
27 class GreeterService : public helloworld::Greeter::Service {
28  public:
31  helloworld::HelloReply* response) override {
32  __android_log_print(ANDROID_LOG_INFO, "DemoServer", "Line number %d",
33  __LINE__);
34  __android_log_print(ANDROID_LOG_INFO, "DemoServer", "Got hello request: %s",
35  request->name().c_str());
36  response->set_message("Hi, " + request->name());
37  return grpc::Status::OK;
38  }
39 };
40 
41 } // namespace
42 
43 extern "C" JNIEXPORT void JNICALL
45  JNIEnv* env, jobject /*this*/, jobject context) {
46  // Lower the gRPC logging level, here it is just for demo and debugging
47  // purpose.
48  setenv("GRPC_VERBOSITY", "INFO", true);
49  __android_log_print(ANDROID_LOG_INFO, "DemoServer", "Line number %d",
50  __LINE__);
51  static std::unique_ptr<grpc::Server> server = nullptr;
52 
53  if (server != nullptr) {
54  // Already initiated
55  return;
56  }
57 
58  if (grpc::experimental::InitializeBinderChannelJavaClass(env)) {
59  __android_log_print(ANDROID_LOG_INFO, "DemoServer",
60  "InitializeBinderChannelJavaClass succeed");
61  } else {
62  __android_log_print(ANDROID_LOG_INFO, "DemoServer",
63  "InitializeBinderChannelJavaClass failed");
64  }
65 
66  static GreeterService service;
67  grpc::ServerBuilder server_builder;
68  server_builder.RegisterService(&service);
69 
70  JavaVM* jvm;
71  {
72  jint result = env->GetJavaVM(&jvm);
73  assert(result == 0);
74  }
75  server_builder.AddListeningPort(
76  "binder:example.service",
78  std::make_shared<
79  grpc::experimental::binder::SameSignatureSecurityPolicy>(
80  jvm, context)));
81 
82  server = server_builder.BuildAndStart();
83 }
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
grpc::ServerContext
Definition: grpcpp/impl/codegen/server_context.h:566
grpc::ServerBuilder::BuildAndStart
virtual std::unique_ptr< grpc::Server > BuildAndStart()
Definition: server_builder.cc:275
grpc::experimental::BinderServerCredentials
std::shared_ptr< grpc::ServerCredentials > BinderServerCredentials(std::shared_ptr< grpc::experimental::binder::SecurityPolicy > security_policy)
Definition: binder_server_credentials.cc:66
benchmark.request
request
Definition: benchmark.py:77
hellostreamingworld_pb2.HelloRequest
HelloRequest
Definition: hellostreamingworld_pb2.py:102
grpc::ServerBuilder::RegisterService
ServerBuilder & RegisterService(grpc::Service *service)
Definition: server_builder.cc:101
grpc::ServerBuilder::AddListeningPort
ServerBuilder & AddListeningPort(const std::string &addr_uri, std::shared_ptr< grpc::ServerCredentials > creds, int *selected_port=nullptr)
Definition: server_builder.cc:222
binder_security_policy.h
binder_credentials.h
grpc::ServerBuilder
A builder class for the creation and startup of grpc::Server instances.
Definition: grpcpp/server_builder.h:86
Java_io_grpc_binder_cpp_exampleserver_ExportedEndpointService_init_1grpc_1server
JNIEXPORT void JNICALL Java_io_grpc_binder_cpp_exampleserver_ExportedEndpointService_init_1grpc_1server(JNIEnv *env, jobject, jobject context)
Definition: exampleserver/native.cc:44
grpcpp.h
grpc::Status::OK
static const Status & OK
An OK pre-defined instance.
Definition: include/grpcpp/impl/codegen/status.h:113
hellostreamingworld_pb2.HelloReply
HelloReply
Definition: hellostreamingworld_pb2.py:109
server
Definition: examples/python/async_streaming/server.py:1
asyncio_get_stats.response
response
Definition: asyncio_get_stats.py:28
env
Definition: env.py:1
grpc::Status
Definition: include/grpcpp/impl/codegen/status.h:35
context
grpc::ClientContext context
Definition: istio_echo_server_lib.cc:61
service
__attribute__((deprecated("Please use GRPCProtoMethod."))) @interface ProtoMethod NSString * service
Definition: ProtoMethod.h:25
create_channel_binder.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:42