Skip to content

Commit 47fc606

Browse files
committed
Update
1 parent 76df63f commit 47fc606

File tree

1,229 files changed

+344475
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,229 files changed

+344475
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
21+
#include "src/core/ext/transport/binder/client/channel_create.h"
22+
#include "src/core/ext/transport/binder/security_policy/untrusted_security_policy.h"
23+
24+
extern "C" JNIEXPORT jstring JNICALL
25+
Java_io_grpc_binder_cpp_exampleclient_ButtonPressHandler_native_1entry(
26+
JNIEnv* env, jobject /*this*/, jobject application) {
27+
static bool first = true;
28+
__android_log_print(ANDROID_LOG_INFO, "DemoClient", "Line number %d",
29+
__LINE__);
30+
if (first) {
31+
first = false;
32+
grpc::experimental::BindToOnDeviceServerService(
33+
env, application, "io.grpc.binder.cpp.exampleserver",
34+
"io.grpc.binder.cpp.exampleserver.ExportedEndpointService");
35+
return env->NewStringUTF("Clicked 1 time");
36+
} else {
37+
// TODO(mingcl): Use same signature security after it become available
38+
auto channel = grpc::experimental::CreateBinderChannel(
39+
env, application, "", "",
40+
std::make_shared<
41+
grpc::experimental::binder::UntrustedSecurityPolicy>());
42+
auto stub = helloworld::Greeter::NewStub(channel);
43+
grpc::ClientContext context;
44+
helloworld::HelloRequest request;
45+
helloworld::HelloReply response;
46+
request.set_name("BinderTransportClient");
47+
grpc::Status status = stub->SayHello(&context, request, &response);
48+
if (status.ok()) {
49+
return env->NewStringUTF(response.message().c_str());
50+
}
51+
return env->NewStringUTF("Clicked more than 1 time. Status not ok");
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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/binder_auto_utils.h>
16+
#include <android/binder_ibinder.h>
17+
#include <android/binder_ibinder_jni.h>
18+
#include <android/binder_interface_utils.h>
19+
#include <android/log.h>
20+
#include <jni.h>
21+
22+
#include "examples/protos/helloworld.grpc.pb.h"
23+
#include "examples/protos/helloworld.pb.h"
24+
25+
#include <grpcpp/grpcpp.h>
26+
27+
#include "src/core/ext/transport/binder/security_policy/untrusted_security_policy.h"
28+
#include "src/core/ext/transport/binder/server/binder_server.h"
29+
#include "src/core/ext/transport/binder/server/binder_server_credentials.h"
30+
31+
namespace {
32+
class GreeterService : public helloworld::Greeter::Service {
33+
public:
34+
grpc::Status SayHello(grpc::ServerContext*,
35+
const helloworld::HelloRequest* request,
36+
helloworld::HelloReply* response) override {
37+
__android_log_print(ANDROID_LOG_INFO, "DemoServer", "Line number %d",
38+
__LINE__);
39+
__android_log_print(ANDROID_LOG_INFO, "DemoServer", "Got hello request: %s",
40+
request->name().c_str());
41+
response->set_message("Hi, " + request->name());
42+
return grpc::Status::OK;
43+
}
44+
};
45+
46+
} // namespace
47+
48+
extern "C" JNIEXPORT void JNICALL
49+
Java_io_grpc_binder_cpp_exampleserver_ExportedEndpointService_init_1grpc_1server(
50+
JNIEnv* env, jobject /*this*/) {
51+
__android_log_print(ANDROID_LOG_INFO, "DemoServer", "Line number %d",
52+
__LINE__);
53+
static std::unique_ptr<grpc::Server> server = nullptr;
54+
55+
if (server != nullptr) {
56+
// Already initiated
57+
return;
58+
}
59+
60+
static GreeterService service;
61+
grpc::ServerBuilder server_builder;
62+
server_builder.RegisterService(&service);
63+
64+
// TODO(mingcl): Use same signature security after it become available
65+
server_builder.AddListeningPort(
66+
"binder:example.service",
67+
grpc::experimental::BinderServerCredentials(
68+
std::make_shared<
69+
grpc::experimental::binder::UntrustedSecurityPolicy>()));
70+
71+
server = server_builder.BuildAndStart();
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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;
27+
using grpc::ClientContext;
28+
using grpc::Server;
29+
using grpc::ServerBuilder;
30+
using grpc::ServerContext;
31+
using grpc::Status;
32+
using helloworld::Greeter;
33+
using helloworld::HelloReply;
34+
using helloworld::HelloRequest;
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 {
40+
Status SayHello(ServerContext* context, const HelloRequest* request,
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+
54+
GreeterServiceImpl service;
55+
ServerBuilder builder;
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+
73+
class GreeterClient {
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.
80+
std::string SayHello(const std::string& user) {
81+
// Data we are sending to the server.
82+
HelloRequest request;
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.
90+
ClientContext context;
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
107+
Java_io_grpc_helloworldexample_cpp_HelloworldActivity_sayHello(
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(
123+
grpc::CreateChannel(host_port, grpc::InsecureChannelCredentials()));
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
131+
Java_io_grpc_helloworldexample_cpp_HelloworldActivity_startServer(
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+
}
+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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 <iostream>
20+
#include <memory>
21+
#include <string>
22+
23+
#include <grpcpp/grpcpp.h>
24+
25+
#ifdef BAZEL_BUILD
26+
#include "examples/protos/helloworld.grpc.pb.h"
27+
#else
28+
#include "helloworld.grpc.pb.h"
29+
#endif
30+
31+
using grpc::Channel;
32+
using grpc::ChannelArguments;
33+
using grpc::ClientContext;
34+
using grpc::Status;
35+
using helloworld::Greeter;
36+
using helloworld::HelloReply;
37+
using helloworld::HelloRequest;
38+
39+
class GreeterClient {
40+
public:
41+
GreeterClient(std::shared_ptr<Channel> channel)
42+
: stub_(Greeter::NewStub(channel)) {}
43+
44+
// Assembles the client's payload, sends it and presents the response back
45+
// from the server.
46+
std::string SayHello(const std::string& user) {
47+
// Data we are sending to the server.
48+
HelloRequest request;
49+
request.set_name(user);
50+
51+
// Container for the data we expect from the server.
52+
HelloReply reply;
53+
54+
// Context for the client. It could be used to convey extra information to
55+
// the server and/or tweak certain RPC behaviors.
56+
ClientContext context;
57+
58+
// Overwrite the call's compression algorithm to DEFLATE.
59+
context.set_compression_algorithm(GRPC_COMPRESS_DEFLATE);
60+
61+
// The actual RPC.
62+
Status status = stub_->SayHello(&context, request, &reply);
63+
64+
// Act upon its status.
65+
if (status.ok()) {
66+
return reply.message();
67+
} else {
68+
std::cout << status.error_code() << ": " << status.error_message()
69+
<< std::endl;
70+
return "RPC failed";
71+
}
72+
}
73+
74+
private:
75+
std::unique_ptr<Greeter::Stub> stub_;
76+
};
77+
78+
int main(int argc, char** argv) {
79+
// Instantiate the client. It requires a channel, out of which the actual RPCs
80+
// are created. This channel models a connection to an endpoint (in this case,
81+
// localhost at port 50051). We indicate that the channel isn't authenticated
82+
// (use of InsecureChannelCredentials()).
83+
ChannelArguments args;
84+
// Set the default compression algorithm for the channel.
85+
args.SetCompressionAlgorithm(GRPC_COMPRESS_GZIP);
86+
GreeterClient greeter(grpc::CreateCustomChannel(
87+
"localhost:50051", grpc::InsecureChannelCredentials(), args));
88+
std::string user("world world world world");
89+
std::string reply = greeter.SayHello(user);
90+
std::cout << "Greeter received: " << reply << std::endl;
91+
92+
return 0;
93+
}

0 commit comments

Comments
 (0)