grpc-helloworld.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2018 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #include <atomic>
20 
21 #include <grpc++/grpc++.h>
22 #include <jni.h>
23 
24 #include "helloworld.grpc.pb.h"
25 
26 using grpc::Channel;
28 using grpc::Server;
31 using grpc::Status;
35 
36 std::atomic<bool> stop_server(false);
37 
38 // Logic and data behind the server's behavior.
39 class GreeterServiceImpl final : public Greeter::Service {
41  HelloReply* reply) override {
42  std::string prefix("Hello ");
43  reply->set_message(prefix + request->name());
44  return Status::OK;
45  }
46 };
47 
48 void StartServer(JNIEnv* env, jobject obj, jmethodID is_cancelled_mid,
49  int port) {
50  const int host_port_buf_size = 1024;
51  char host_port[host_port_buf_size];
52  snprintf(host_port, host_port_buf_size, "0.0.0.0:%d", port);
53 
56  // Listen on the given address without any authentication mechanism.
57  builder.AddListeningPort(host_port, grpc::InsecureServerCredentials());
58  // Register "service" as the instance through which we'll communicate with
59  // clients. In this case it corresponds to an *synchronous* service.
60  builder.RegisterService(&service);
61  // Finally assemble the server.
62  std::unique_ptr<Server> server(builder.BuildAndStart());
63  while (!stop_server.load()) {
64  // Check with the Java code to see if the user has requested the server stop or the app is no
65  // longer in the foreground.
66  jboolean is_cancelled = env->CallBooleanMethod(obj, is_cancelled_mid);
67  if (is_cancelled == JNI_TRUE) {
68  stop_server = true;
69  }
70  }
71 }
72 
74  public:
75  GreeterClient(std::shared_ptr<Channel> channel)
76  : stub_(Greeter::NewStub(channel)) {}
77 
78  // Assembles the client's payload, sends it and presents the response back
79  // from the server.
81  // Data we are sending to the server.
83  request.set_name(user);
84 
85  // Container for the data we expect from the server.
86  HelloReply reply;
87 
88  // Context for the client. It could be used to convey extra information to
89  // the server and/or tweak certain RPC behaviors.
91  // The actual RPC.
92  Status status = stub_->SayHello(&context, request, &reply);
93 
94  if (status.ok()) {
95  return reply.message();
96  } else {
97  return status.error_message();
98  }
99  }
100 
101  private:
102  std::unique_ptr<Greeter::Stub> stub_;
103 };
104 
105 // Send an RPC and return the response. Invoked from Java code.
106 extern "C" JNIEXPORT jstring JNICALL
108  JNIEnv* env, jobject obj_unused, jstring host_raw, jint port_raw,
109  jstring message_raw) {
110  const char* host_chars = env->GetStringUTFChars(host_raw, (jboolean*)0);
111  std::string host(host_chars, env->GetStringUTFLength(host_raw));
112 
113  int port = static_cast<int>(port_raw);
114 
115  const char* message_chars = env->GetStringUTFChars(message_raw, (jboolean*)0);
116  std::string message(message_chars, env->GetStringUTFLength(message_raw));
117 
118  const int host_port_buf_size = 1024;
119  char host_port[host_port_buf_size];
120  snprintf(host_port, host_port_buf_size, "%s:%d", host.c_str(), port);
121 
122  GreeterClient greeter(
124  std::string reply = greeter.SayHello(message);
125 
126  return env->NewStringUTF(reply.c_str());
127 }
128 
129 // Start the server. Invoked from Java code.
130 extern "C" JNIEXPORT void JNICALL
132  JNIEnv* env, jobject obj_this, jint port_raw) {
133  int port = static_cast<int>(port_raw);
134 
135  jclass cls = env->GetObjectClass(obj_this);
136  jmethodID is_cancelled_mid =
137  env->GetMethodID(cls, "isRunServerTaskCancelled", "()Z");
138 
139  stop_server = false;
140 
141  StartServer(env, obj_this, is_cancelled_mid, port);
142 }
obj
OPENSSL_EXPORT const ASN1_OBJECT * obj
Definition: x509.h:1671
grpc::ServerContext
Definition: grpcpp/impl/codegen/server_context.h:566
benchmark.request
request
Definition: benchmark.py:77
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
stop_server
std::atomic< bool > stop_server(false)
framework.rpc.grpc_channelz.Server
Server
Definition: grpc_channelz.py:42
hellostreamingworld_pb2.HelloRequest
HelloRequest
Definition: hellostreamingworld_pb2.py:102
status
absl::Status status
Definition: rls.cc:251
OK
@ OK
Definition: cronet_status.h:43
message
char * message
Definition: libuv/docs/code/tty-gravity/main.c:12
server
std::unique_ptr< Server > server
Definition: channelz_service_test.cc:330
framework.rpc.grpc_channelz.Channel
Channel
Definition: grpc_channelz.py:32
profile_analyzer.builder
builder
Definition: profile_analyzer.py:159
channel
wrapped_grpc_channel * channel
Definition: src/php/ext/grpc/call.h:33
GreeterServiceImpl::SayHello
Status SayHello(ServerContext *context, const HelloRequest *request, HelloReply *reply) override
Definition: grpc-helloworld.cc:40
grpc::ServerBuilder
A builder class for the creation and startup of grpc::Server instances.
Definition: grpcpp/server_builder.h:86
GreeterClient
Definition: grpc-helloworld.cc:73
GreeterClient::GreeterClient
GreeterClient(std::shared_ptr< Channel > channel)
Definition: grpc-helloworld.cc:75
helloworld.Greeter
Definition: helloworld.py:32
grpc++.h
grpc::CreateChannel
std::shared_ptr< Channel > CreateChannel(const grpc::string &target, const std::shared_ptr< ChannelCredentials > &creds)
grpc::ClientContext
Definition: grpcpp/impl/codegen/client_context.h:195
tests.unit._exit_scenarios.port
port
Definition: _exit_scenarios.py:179
Java_io_grpc_helloworldexample_cpp_HelloworldActivity_startServer
JNIEXPORT void JNICALL Java_io_grpc_helloworldexample_cpp_HelloworldActivity_startServer(JNIEnv *env, jobject obj_this, jint port_raw)
Definition: grpc-helloworld.cc:131
GreeterServiceImpl
Definition: grpc-helloworld.cc:39
hellostreamingworld_pb2.HelloReply
HelloReply
Definition: hellostreamingworld_pb2.py:109
Java_io_grpc_helloworldexample_cpp_HelloworldActivity_sayHello
JNIEXPORT jstring JNICALL Java_io_grpc_helloworldexample_cpp_HelloworldActivity_sayHello(JNIEnv *env, jobject obj_unused, jstring host_raw, jint port_raw, jstring message_raw)
Definition: grpc-helloworld.cc:107
StartServer
void StartServer(JNIEnv *env, jobject obj, jmethodID is_cancelled_mid, int port)
Definition: grpc-helloworld.cc:48
GreeterClient::SayHello
std::string SayHello(const std::string &user)
Definition: grpc-helloworld.cc:80
env
Definition: env.py:1
prefix
static const char prefix[]
Definition: head_of_line_blocking.cc:28
grpc::protobuf::util::Status
GRPC_CUSTOM_UTIL_STATUS Status
Definition: include/grpcpp/impl/codegen/config_protobuf.h:93
grpc::Status
Definition: include/grpcpp/impl/codegen/status.h:35
absl::Status::ok
ABSL_MUST_USE_RESULT bool ok() const
Definition: third_party/abseil-cpp/absl/status/status.h:802
grpc::InsecureServerCredentials
std::shared_ptr< ServerCredentials > InsecureServerCredentials()
Definition: insecure_server_credentials.cc:52
context
grpc::ClientContext context
Definition: istio_echo_server_lib.cc:61
grpc::InsecureChannelCredentials
std::shared_ptr< ChannelCredentials > InsecureChannelCredentials()
Credentials for an unencrypted, unauthenticated channel.
Definition: cpp/client/insecure_credentials.cc:69
service
__attribute__((deprecated("Please use GRPCProtoMethod."))) @interface ProtoMethod NSString * service
Definition: ProtoMethod.h:25
GreeterClient::stub_
std::unique_ptr< Greeter::Stub > stub_
Definition: grpc-helloworld.cc:102


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:59:47