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 <jni.h>
20
21 #include "absl/strings/str_format.h"
22
23 #include <grpcpp/grpcpp.h>
24
25 #include "test/cpp/interop/interop_client.h"
26
GetClient(const char * host,int port,bool use_tls)27 std::shared_ptr<grpc::testing::InteropClient> GetClient(const char* host,
28 int port,
29 bool use_tls) {
30 std::shared_ptr<grpc::ChannelCredentials> credentials;
31 if (use_tls) {
32 credentials = grpc::SslCredentials(grpc::SslCredentialsOptions());
33 } else {
34 credentials = grpc::InsecureChannelCredentials();
35 }
36 std::string host_port = absl::StrFormat("%s:%d", host, port);
37 return std::make_shared<grpc::testing::InteropClient>(
38 [host_port, credentials](grpc::ChannelArguments args) {
39 return grpc::CreateCustomChannel(host_port, credentials, args);
40 },
41 true, false);
42 }
43
44 extern "C" JNIEXPORT jboolean JNICALL
Java_io_grpc_interop_cpp_InteropActivity_doEmpty(JNIEnv * env,jobject obj_this,jstring host_raw,jint port_raw,jboolean use_tls_raw)45 Java_io_grpc_interop_cpp_InteropActivity_doEmpty(JNIEnv* env, jobject obj_this,
46 jstring host_raw,
47 jint port_raw,
48 jboolean use_tls_raw) {
49 const char* host = env->GetStringUTFChars(host_raw, (jboolean*)0);
50 int port = static_cast<int>(port_raw);
51 bool use_tls = static_cast<bool>(use_tls_raw);
52
53 jboolean result = GetClient(host, port, use_tls)->DoEmpty();
54 env->ReleaseStringUTFChars(host_raw, host);
55 return result;
56 }
57
58 extern "C" JNIEXPORT jboolean JNICALL
Java_io_grpc_interop_cpp_InteropActivity_doLargeUnary(JNIEnv * env,jobject obj_this,jstring host_raw,jint port_raw,jboolean use_tls_raw)59 Java_io_grpc_interop_cpp_InteropActivity_doLargeUnary(JNIEnv* env,
60 jobject obj_this,
61 jstring host_raw,
62 jint port_raw,
63 jboolean use_tls_raw) {
64 const char* host = env->GetStringUTFChars(host_raw, (jboolean*)0);
65 int port = static_cast<int>(port_raw);
66 bool use_tls = static_cast<bool>(use_tls_raw);
67
68 jboolean result = GetClient(host, port, use_tls)->DoLargeUnary();
69 env->ReleaseStringUTFChars(host_raw, host);
70 return result;
71 }
72
73 extern "C" JNIEXPORT jboolean JNICALL
Java_io_grpc_interop_cpp_InteropActivity_doEmptyStream(JNIEnv * env,jobject obj_this,jstring host_raw,jint port_raw,jboolean use_tls_raw)74 Java_io_grpc_interop_cpp_InteropActivity_doEmptyStream(JNIEnv* env,
75 jobject obj_this,
76 jstring host_raw,
77 jint port_raw,
78 jboolean use_tls_raw) {
79 const char* host = env->GetStringUTFChars(host_raw, (jboolean*)0);
80 int port = static_cast<int>(port_raw);
81 bool use_tls = static_cast<bool>(use_tls_raw);
82
83 jboolean result = GetClient(host, port, use_tls)->DoEmptyStream();
84 env->ReleaseStringUTFChars(host_raw, host);
85 return result;
86 }
87
88 extern "C" JNIEXPORT jboolean JNICALL
Java_io_grpc_interop_cpp_InteropActivity_doRequestStreaming(JNIEnv * env,jobject obj_this,jstring host_raw,jint port_raw,jboolean use_tls_raw)89 Java_io_grpc_interop_cpp_InteropActivity_doRequestStreaming(
90 JNIEnv* env, jobject obj_this, jstring host_raw, jint port_raw,
91 jboolean use_tls_raw) {
92 const char* host = env->GetStringUTFChars(host_raw, (jboolean*)0);
93 int port = static_cast<int>(port_raw);
94 bool use_tls = static_cast<bool>(use_tls_raw);
95
96 jboolean result = GetClient(host, port, use_tls)->DoRequestStreaming();
97 env->ReleaseStringUTFChars(host_raw, host);
98 return result;
99 }
100
101 extern "C" JNIEXPORT jboolean JNICALL
Java_io_grpc_interop_cpp_InteropActivity_doResponseStreaming(JNIEnv * env,jobject obj_this,jstring host_raw,jint port_raw,jboolean use_tls_raw)102 Java_io_grpc_interop_cpp_InteropActivity_doResponseStreaming(
103 JNIEnv* env, jobject obj_this, jstring host_raw, jint port_raw,
104 jboolean use_tls_raw) {
105 const char* host = env->GetStringUTFChars(host_raw, (jboolean*)0);
106 int port = static_cast<int>(port_raw);
107 bool use_tls = static_cast<bool>(use_tls_raw);
108
109 jboolean result = GetClient(host, port, use_tls)->DoResponseStreaming();
110 env->ReleaseStringUTFChars(host_raw, host);
111 return result;
112 }
113
114 extern "C" JNIEXPORT jboolean JNICALL
Java_io_grpc_interop_cpp_InteropActivity_doPingPong(JNIEnv * env,jobject obj_this,jstring host_raw,jint port_raw,jboolean use_tls_raw)115 Java_io_grpc_interop_cpp_InteropActivity_doPingPong(JNIEnv* env,
116 jobject obj_this,
117 jstring host_raw,
118 jint port_raw,
119 jboolean use_tls_raw) {
120 const char* host = env->GetStringUTFChars(host_raw, (jboolean*)0);
121 int port = static_cast<int>(port_raw);
122 bool use_tls = static_cast<bool>(use_tls_raw);
123
124 jboolean result = GetClient(host, port, use_tls)->DoPingPong();
125 env->ReleaseStringUTFChars(host_raw, host);
126 return result;
127 }
128