Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
ryihan committed Oct 25, 2021
1 parent 9472965 commit a305c79
Show file tree
Hide file tree
Showing 11 changed files with 632 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.grpc.binder.cpp.exampleclient;

import android.app.Application;

public class ButtonPressHandler {
static {
System.loadLibrary("app");
}

public native String native_entry(Application application);

public String onPressed(Application application) {
return native_entry(application);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.grpc.binder.cpp.exampleclient;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;

/** Main class for the example app. */
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v("Example", "hello, world");

setContentView(R.layout.activity_main);

Button clickMeButton = findViewById(R.id.clickMeButton);
TextView exampleTextView = findViewById(R.id.exampleTextView);

ButtonPressHandler h = new ButtonPressHandler();

clickMeButton.setOnClickListener(
v -> exampleTextView.setText(h.onPressed(getApplication())));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.grpc.binder.cpp.exampleserver;

import android.app.Application;

public class ButtonPressHandler {
static {
System.loadLibrary("app");
}

public String onPressed(Application application) {
return "Server Button Pressed";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.grpc.binder.cpp.exampleserver;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import io.grpc.binder.cpp.GrpcCppServerBuilder;

/** Exposes gRPC services running in the main process */
public final class ExportedEndpointService extends Service {
static {
System.loadLibrary("app");
}

public ExportedEndpointService() {
init_grpc_server();
}

@Override
public IBinder onBind(Intent intent) {
// The argument should match the URI passed into grpc::ServerBuilder::AddListeningPort
return GrpcCppServerBuilder.GetEndpointBinder("binder:example.service");
}

public native void init_grpc_server();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.grpc.binder.cpp.exampleserver;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;
import io.grpc.binder.cpp.exampleserver.R;

/** Main class for the example app. */
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v("Example", "hello, world");

setContentView(R.layout.activity_main);

Button clickMeButton = findViewById(R.id.clickMeButton);
TextView exampleTextView = findViewById(R.id.exampleTextView);

ButtonPressHandler h = new ButtonPressHandler();

clickMeButton.setOnClickListener(
v -> exampleTextView.setText(h.onPressed(getApplication())));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/*
* Copyright 2018, gRPC Authors All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.grpc.helloworldexample.cpp;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.lang.ref.WeakReference;

public class HelloworldActivity extends AppCompatActivity {

static {
System.loadLibrary("grpc-helloworld");
}

private Button sendButton;
private Button serverButton;
private EditText hostEdit;
private EditText portEdit;
private EditText messageEdit;
private EditText serverPortEdit;
private TextView resultText;
private GrpcTask grpcTask;
private RunServerTask runServerTask;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_helloworld);
sendButton = (Button) findViewById(R.id.send_button);
serverButton = (Button) findViewById(R.id.server_button);
hostEdit = (EditText) findViewById(R.id.host_edit_text);
portEdit = (EditText) findViewById(R.id.port_edit_text);
messageEdit = (EditText) findViewById(R.id.message_edit_text);
serverPortEdit = (EditText) findViewById(R.id.server_port_edit_text);
resultText = (TextView) findViewById(R.id.grpc_response_text);
resultText.setMovementMethod(new ScrollingMovementMethod());
}

@Override
protected void onPause() {
super.onPause();
if (runServerTask != null) {
runServerTask.cancel(true);
runServerTask = null;
serverButton.setText("Start gRPC Server");
}
if (grpcTask != null) {
grpcTask.cancel(true);
grpcTask = null;
}
}

public void sendMessage(View view) {
((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(hostEdit.getWindowToken(), 0);
sendButton.setEnabled(false);
resultText.setText("");
grpcTask = new GrpcTask(this);
grpcTask.executeOnExecutor(
AsyncTask.THREAD_POOL_EXECUTOR,
hostEdit.getText().toString(),
messageEdit.getText().toString(),
portEdit.getText().toString());
}

public void startOrStopServer(View view) {
if (runServerTask != null) {
runServerTask.cancel(true);
runServerTask = null;
serverButton.setText("Start gRPC Server");
Toast.makeText(this, "Server stopped", Toast.LENGTH_SHORT).show();
} else {
runServerTask = new RunServerTask(this);
String portStr = serverPortEdit.getText().toString();
int port = TextUtils.isEmpty(portStr) ? 50051 : Integer.valueOf(portStr);
runServerTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, port);
serverButton.setText("Stop gRPC Server");
Toast.makeText(this, "Server started on port " + port, Toast.LENGTH_SHORT).show();
}
}

private static class RunServerTask extends AsyncTask<Integer, Void, Void> {
private final WeakReference<HelloworldActivity> activityReference;

private RunServerTask(HelloworldActivity activity) {
this.activityReference = new WeakReference<HelloworldActivity>(activity);
}

@Override
protected Void doInBackground(Integer... params) {
int port = params[0];
HelloworldActivity activity = activityReference.get();
if (activity != null) {
activity.startServer(port);
}
return null;
}
}

private static class GrpcTask extends AsyncTask<String, Void, String> {
private final WeakReference<HelloworldActivity> activityReference;

private GrpcTask(HelloworldActivity activity) {
this.activityReference = new WeakReference<HelloworldActivity>(activity);
}

@Override
protected String doInBackground(String... params) {
String host = params[0];
String message = params[1];
String portStr = params[2];
int port = TextUtils.isEmpty(portStr) ? 50051 : Integer.valueOf(portStr);
return sayHello(host, port, message);
}

@Override
protected void onPostExecute(String result) {
HelloworldActivity activity = activityReference.get();
if (activity == null || isCancelled()) {
return;
}
TextView resultText = (TextView) activity.findViewById(R.id.grpc_response_text);
Button sendButton = (Button) activity.findViewById(R.id.send_button);
resultText.setText(result);
sendButton.setEnabled(true);
}
}

/**
* Invoked by native code to stop server when RunServerTask has been canceled, either by user
* request or upon app moving to background.
*/
public boolean isRunServerTaskCancelled() {
if (runServerTask != null) {
return runServerTask.isCancelled();
}
return false;
}

public static native String sayHello(String host, int port, String message);

public native void startServer(int port);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright 2018, gRPC Authors All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.grpc.interop.cpp;

import static junit.framework.Assert.assertTrue;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4.class)
public class InteropTest {
private String host;
private int port;
private boolean useTls;

@Before
public void setUp() throws Exception {
host =
InstrumentationRegistry.getArguments()
.getString("server_host", "grpc-test.sandbox.googleapis.com");
port = Integer.parseInt(InstrumentationRegistry.getArguments().getString("server_port", "443"));
useTls =
Boolean.parseBoolean(InstrumentationRegistry.getArguments().getString("use_tls", "true"));
}

@Test
public void emptyUnary() {
assertTrue(InteropActivity.doEmpty(host, port, useTls));
}

@Test
public void largeUnary() {
assertTrue(InteropActivity.doLargeUnary(host, port, useTls));
}

@Test
public void emptyStream() {
assertTrue(InteropActivity.doEmptyStream(host, port, useTls));
}

@Test
public void requestStreaming() {
assertTrue(InteropActivity.doRequestStreaming(host, port, useTls));
}

@Test
public void responseStreaming() {
assertTrue(InteropActivity.doResponseStreaming(host, port, useTls));
}

@Test
public void pingPong() {
assertTrue(InteropActivity.doPingPong(host, port, useTls));
}
}
Loading

0 comments on commit a305c79

Please sign in to comment.