Imported Upstream version 1.27.0
[platform/upstream/grpc.git] / src / android / test / interop / app / src / main / cpp / grpc-interop.cc
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
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
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   return GetClient(host, port, use_tls)->DoEmpty();
55 }
56
57 extern "C" JNIEXPORT jboolean JNICALL
58 Java_io_grpc_interop_cpp_InteropActivity_doLargeUnary(JNIEnv* env,
59                                                       jobject obj_this,
60                                                       jstring host_raw,
61                                                       jint port_raw,
62                                                       jboolean use_tls_raw) {
63   const char* host = env->GetStringUTFChars(host_raw, (jboolean*)0);
64   int port = static_cast<int>(port_raw);
65   bool use_tls = static_cast<bool>(use_tls_raw);
66
67   return GetClient(host, port, use_tls)->DoLargeUnary();
68 }
69
70 extern "C" JNIEXPORT jboolean JNICALL
71 Java_io_grpc_interop_cpp_InteropActivity_doEmptyStream(JNIEnv* env,
72                                                        jobject obj_this,
73                                                        jstring host_raw,
74                                                        jint port_raw,
75                                                        jboolean use_tls_raw) {
76   const char* host = env->GetStringUTFChars(host_raw, (jboolean*)0);
77   int port = static_cast<int>(port_raw);
78   bool use_tls = static_cast<bool>(use_tls_raw);
79
80   return GetClient(host, port, use_tls)->DoEmptyStream();
81 }
82
83 extern "C" JNIEXPORT jboolean JNICALL
84 Java_io_grpc_interop_cpp_InteropActivity_doRequestStreaming(
85     JNIEnv* env, jobject obj_this, jstring host_raw, jint port_raw,
86     jboolean use_tls_raw) {
87   const char* host = env->GetStringUTFChars(host_raw, (jboolean*)0);
88   int port = static_cast<int>(port_raw);
89   bool use_tls = static_cast<bool>(use_tls_raw);
90
91   return GetClient(host, port, use_tls)->DoRequestStreaming();
92 }
93
94 extern "C" JNIEXPORT jboolean JNICALL
95 Java_io_grpc_interop_cpp_InteropActivity_doResponseStreaming(
96     JNIEnv* env, jobject obj_this, jstring host_raw, jint port_raw,
97     jboolean use_tls_raw) {
98   const char* host = env->GetStringUTFChars(host_raw, (jboolean*)0);
99   int port = static_cast<int>(port_raw);
100   bool use_tls = static_cast<bool>(use_tls_raw);
101
102   return GetClient(host, port, use_tls)->DoResponseStreaming();
103 }
104
105 extern "C" JNIEXPORT jboolean JNICALL
106 Java_io_grpc_interop_cpp_InteropActivity_doPingPong(JNIEnv* env,
107                                                     jobject obj_this,
108                                                     jstring host_raw,
109                                                     jint port_raw,
110                                                     jboolean use_tls_raw) {
111   const char* host = env->GetStringUTFChars(host_raw, (jboolean*)0);
112   int port = static_cast<int>(port_raw);
113   bool use_tls = static_cast<bool>(use_tls_raw);
114
115   return GetClient(host, port, use_tls)->DoPingPong();
116 }