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