diff --git a/README.md b/README.md index 1028b97a2ad..d7860c63d9c 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ You can find quick start guides for each language, including installation instru * [Go](https://github.com/grpc/grpc-common/tree/master/go) * [ruby](https://github.com/grpc/grpc-common/tree/master/ruby) * [Node.js](https://github.com/grpc/grpc-common/tree/master/node) +* [Android Java](https://github.com/grpc/grpc-common/tree/master/java/android) * [Python](https://github.com/grpc/grpc-common/tree/master/python/helloworld) ## What's in this repository? diff --git a/java/android/GreeterGrpc.java b/java/android/GreeterGrpc.java new file mode 100644 index 00000000000..eaef3679d2d --- /dev/null +++ b/java/android/GreeterGrpc.java @@ -0,0 +1,184 @@ +package io.grpc.examples; + +import static io.grpc.stub.Calls.createMethodDescriptor; +import static io.grpc.stub.Calls.asyncUnaryCall; +import static io.grpc.stub.Calls.asyncServerStreamingCall; +import static io.grpc.stub.Calls.asyncClientStreamingCall; +import static io.grpc.stub.Calls.duplexStreamingCall; +import static io.grpc.stub.Calls.blockingUnaryCall; +import static io.grpc.stub.Calls.blockingServerStreamingCall; +import static io.grpc.stub.Calls.unaryFutureCall; +import static io.grpc.stub.ServerCalls.createMethodDefinition; +import static io.grpc.stub.ServerCalls.asyncUnaryRequestCall; +import static io.grpc.stub.ServerCalls.asyncStreamingRequestCall; + +import java.io.IOException; + +public class GreeterGrpc { + + private static final io.grpc.stub.Method METHOD_SAY_HELLO = + io.grpc.stub.Method.create( + io.grpc.MethodType.UNARY, "SayHello", + io.grpc.nano.NanoUtils.marshaller( + new io.grpc.nano.Parser() { + @Override + public io.grpc.examples.Helloworld.HelloRequest parse(com.google.protobuf.nano.CodedInputByteBufferNano input) throws IOException { + return io.grpc.examples.Helloworld.HelloRequest.parseFrom(input); + } + }), + io.grpc.nano.NanoUtils.marshaller( + new io.grpc.nano.Parser() { + @Override + public io.grpc.examples.Helloworld.HelloReply parse(com.google.protobuf.nano.CodedInputByteBufferNano input) throws IOException { + return io.grpc.examples.Helloworld.HelloReply.parseFrom(input); + } + })); + + public static GreeterStub newStub(io.grpc.Channel channel) { + return new GreeterStub(channel, CONFIG); + } + + public static GreeterBlockingStub newBlockingStub( + io.grpc.Channel channel) { + return new GreeterBlockingStub(channel, CONFIG); + } + + public static GreeterFutureStub newFutureStub( + io.grpc.Channel channel) { + return new GreeterFutureStub(channel, CONFIG); + } + + public static final GreeterServiceDescriptor CONFIG = + new GreeterServiceDescriptor(); + + public static class GreeterServiceDescriptor extends + io.grpc.stub.AbstractServiceDescriptor { + public final io.grpc.MethodDescriptor sayHello; + + private GreeterServiceDescriptor() { + sayHello = createMethodDescriptor( + "helloworld.Greeter", METHOD_SAY_HELLO); + } + + private GreeterServiceDescriptor( + java.util.Map> methodMap) { + sayHello = (io.grpc.MethodDescriptor) methodMap.get( + CONFIG.sayHello.getName()); + } + + @java.lang.Override + protected GreeterServiceDescriptor build( + java.util.Map> methodMap) { + return new GreeterServiceDescriptor(methodMap); + } + + @java.lang.Override + public com.google.common.collect.ImmutableList> methods() { + return com.google.common.collect.ImmutableList.>of( + sayHello); + } + } + + public static interface Greeter { + + public void sayHello(io.grpc.examples.Helloworld.HelloRequest request, + io.grpc.stub.StreamObserver responseObserver); + } + + public static interface GreeterBlockingClient { + + public io.grpc.examples.Helloworld.HelloReply sayHello(io.grpc.examples.Helloworld.HelloRequest request); + } + + public static interface GreeterFutureClient { + + public com.google.common.util.concurrent.ListenableFuture sayHello( + io.grpc.examples.Helloworld.HelloRequest request); + } + + public static class GreeterStub extends + io.grpc.stub.AbstractStub + implements Greeter { + private GreeterStub(io.grpc.Channel channel, + GreeterServiceDescriptor config) { + super(channel, config); + } + + @java.lang.Override + protected GreeterStub build(io.grpc.Channel channel, + GreeterServiceDescriptor config) { + return new GreeterStub(channel, config); + } + + @java.lang.Override + public void sayHello(io.grpc.examples.Helloworld.HelloRequest request, + io.grpc.stub.StreamObserver responseObserver) { + asyncUnaryCall( + channel.newCall(config.sayHello), request, responseObserver); + } + } + + public static class GreeterBlockingStub extends + io.grpc.stub.AbstractStub + implements GreeterBlockingClient { + private GreeterBlockingStub(io.grpc.Channel channel, + GreeterServiceDescriptor config) { + super(channel, config); + } + + @java.lang.Override + protected GreeterBlockingStub build(io.grpc.Channel channel, + GreeterServiceDescriptor config) { + return new GreeterBlockingStub(channel, config); + } + + @java.lang.Override + public io.grpc.examples.Helloworld.HelloReply sayHello(io.grpc.examples.Helloworld.HelloRequest request) { + return blockingUnaryCall( + channel.newCall(config.sayHello), request); + } + } + + public static class GreeterFutureStub extends + io.grpc.stub.AbstractStub + implements GreeterFutureClient { + private GreeterFutureStub(io.grpc.Channel channel, + GreeterServiceDescriptor config) { + super(channel, config); + } + + @java.lang.Override + protected GreeterFutureStub build(io.grpc.Channel channel, + GreeterServiceDescriptor config) { + return new GreeterFutureStub(channel, config); + } + + @java.lang.Override + public com.google.common.util.concurrent.ListenableFuture sayHello( + io.grpc.examples.Helloworld.HelloRequest request) { + return unaryFutureCall( + channel.newCall(config.sayHello), request); + } + } + + public static io.grpc.ServerServiceDefinition bindService( + final Greeter serviceImpl) { + return io.grpc.ServerServiceDefinition.builder("helloworld.Greeter") + .addMethod(createMethodDefinition( + METHOD_SAY_HELLO, + asyncUnaryRequestCall( + new io.grpc.stub.ServerCalls.UnaryRequestMethod< + io.grpc.examples.Helloworld.HelloRequest, + io.grpc.examples.Helloworld.HelloReply>() { + @java.lang.Override + public void invoke( + io.grpc.examples.Helloworld.HelloRequest request, + io.grpc.stub.StreamObserver responseObserver) { + serviceImpl.sayHello(request, responseObserver); + } + }))).build(); + } +} diff --git a/java/android/Helloworld.java b/java/android/Helloworld.java new file mode 100644 index 00000000000..35dd5345129 --- /dev/null +++ b/java/android/Helloworld.java @@ -0,0 +1,175 @@ +// Generated by the protocol buffer compiler. DO NOT EDIT! + +package io.grpc.examples; + +@SuppressWarnings("hiding") +public interface Helloworld { + + public static final class HelloRequest extends + com.google.protobuf.nano.MessageNano { + + private static volatile HelloRequest[] _emptyArray; + public static HelloRequest[] emptyArray() { + // Lazily initializes the empty array + if (_emptyArray == null) { + synchronized ( + com.google.protobuf.nano.InternalNano.LAZY_INIT_LOCK) { + if (_emptyArray == null) { + _emptyArray = new HelloRequest[0]; + } + } + } + return _emptyArray; + } + + // optional string name = 1; + public java.lang.String name; + + public HelloRequest() { + clear(); + } + + public HelloRequest clear() { + name = ""; + cachedSize = -1; + return this; + } + + @Override + public void writeTo(com.google.protobuf.nano.CodedOutputByteBufferNano output) + throws java.io.IOException { + if (!this.name.equals("")) { + output.writeString(1, this.name); + } + super.writeTo(output); + } + + @Override + protected int computeSerializedSize() { + int size = super.computeSerializedSize(); + if (!this.name.equals("")) { + size += com.google.protobuf.nano.CodedOutputByteBufferNano + .computeStringSize(1, this.name); + } + return size; + } + + @Override + public HelloRequest mergeFrom( + com.google.protobuf.nano.CodedInputByteBufferNano input) + throws java.io.IOException { + while (true) { + int tag = input.readTag(); + switch (tag) { + case 0: + return this; + default: { + if (!com.google.protobuf.nano.WireFormatNano.parseUnknownField(input, tag)) { + return this; + } + break; + } + case 10: { + this.name = input.readString(); + break; + } + } + } + } + + public static HelloRequest parseFrom(byte[] data) + throws com.google.protobuf.nano.InvalidProtocolBufferNanoException { + return com.google.protobuf.nano.MessageNano.mergeFrom(new HelloRequest(), data); + } + + public static HelloRequest parseFrom( + com.google.protobuf.nano.CodedInputByteBufferNano input) + throws java.io.IOException { + return new HelloRequest().mergeFrom(input); + } + } + + public static final class HelloReply extends + com.google.protobuf.nano.MessageNano { + + private static volatile HelloReply[] _emptyArray; + public static HelloReply[] emptyArray() { + // Lazily initializes the empty array + if (_emptyArray == null) { + synchronized ( + com.google.protobuf.nano.InternalNano.LAZY_INIT_LOCK) { + if (_emptyArray == null) { + _emptyArray = new HelloReply[0]; + } + } + } + return _emptyArray; + } + + // optional string message = 1; + public java.lang.String message; + + public HelloReply() { + clear(); + } + + public HelloReply clear() { + message = ""; + cachedSize = -1; + return this; + } + + @Override + public void writeTo(com.google.protobuf.nano.CodedOutputByteBufferNano output) + throws java.io.IOException { + if (!this.message.equals("")) { + output.writeString(1, this.message); + } + super.writeTo(output); + } + + @Override + protected int computeSerializedSize() { + int size = super.computeSerializedSize(); + if (!this.message.equals("")) { + size += com.google.protobuf.nano.CodedOutputByteBufferNano + .computeStringSize(1, this.message); + } + return size; + } + + @Override + public HelloReply mergeFrom( + com.google.protobuf.nano.CodedInputByteBufferNano input) + throws java.io.IOException { + while (true) { + int tag = input.readTag(); + switch (tag) { + case 0: + return this; + default: { + if (!com.google.protobuf.nano.WireFormatNano.parseUnknownField(input, tag)) { + return this; + } + break; + } + case 10: { + this.message = input.readString(); + break; + } + } + } + } + + public static HelloReply parseFrom(byte[] data) + throws com.google.protobuf.nano.InvalidProtocolBufferNanoException { + return com.google.protobuf.nano.MessageNano.mergeFrom(new HelloReply(), data); + } + + public static HelloReply parseFrom( + com.google.protobuf.nano.CodedInputByteBufferNano input) + throws java.io.IOException { + return new HelloReply().mergeFrom(input); + } + } +} diff --git a/java/android/HelloworldActivity.java b/java/android/HelloworldActivity.java new file mode 100644 index 00000000000..a64d3f4d6aa --- /dev/null +++ b/java/android/HelloworldActivity.java @@ -0,0 +1,93 @@ +package io.grpc.helloworldexample; + +import android.content.Context; +import android.support.v7.app.ActionBarActivity; +import android.os.Bundle; +import android.os.AsyncTask; +import android.text.TextUtils; +import android.view.Menu; +import android.view.MenuItem; +import android.view.View; +import android.view.inputmethod.InputMethodManager; +import android.widget.Button; +import android.widget.EditText; +import android.widget.TextView; + +import io.grpc.ChannelImpl; +import ex.grpc.GreeterGrpc; +import ex.grpc.Helloworld.HelloRequest; +import ex.grpc.Helloworld.HelloReply; +import io.grpc.transport.okhttp.OkHttpChannelBuilder; + +import java.util.concurrent.TimeUnit; + +public class Helloworld extends ActionBarActivity { + private Button mSendButton; + private EditText mHostEdit; + private EditText mPortEdit; + private EditText mMessageEdit; + private TextView mResultText; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_helloworld); + mSendButton = (Button) findViewById(R.id.send_button); + mHostEdit = (EditText) findViewById(R.id.host_edit_text); + mPortEdit = (EditText) findViewById(R.id.port_edit_text); + mMessageEdit = (EditText) findViewById(R.id.message_edit_text); + mResultText = (TextView) findViewById(R.id.grpc_response_text); + } + + public void sendMessage(View view) { + ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)) + .hideSoftInputFromWindow(mHostEdit.getWindowToken(), 0); + mSendButton.setEnabled(false); + new GrpcTask().execute(); + } + + private class GrpcTask extends AsyncTask { + private String mHost; + private String mMessage; + private int mPort; + private ChannelImpl mChannel; + + @Override + protected void onPreExecute() { + mHost = mHostEdit.getText().toString(); + mMessage = mMessageEdit.getText().toString(); + String portStr = mPortEdit.getText().toString(); + mPort = TextUtils.isEmpty(portStr) ? 0 : Integer.valueOf(portStr); + mResultText.setText(""); + } + + private String sayHello(ChannelImpl channel) { + GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(channel); + HelloRequest message = new HelloRequest(); + message.name = mMessage; + HelloReply reply = stub.sayHello(message); + return reply.message; + } + + @Override + protected String doInBackground(Void... nothing) { + try { + mChannel = OkHttpChannelBuilder.forAddress(mHost, mPort).build(); + return sayHello(mChannel); + } catch (Exception e) { + return "Failed... : " + e.getMessage(); + } + } + + @Override + protected void onPostExecute(String result) { + try { + mChannel.shutdown().awaitTerminated(1, TimeUnit.SECONDS); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + mResultText.setText(result); + mSendButton.setEnabled(true); + } + } +} diff --git a/java/android/README.md b/java/android/README.md new file mode 100644 index 00000000000..a231faf9817 --- /dev/null +++ b/java/android/README.md @@ -0,0 +1,71 @@ +gRPC Hello World Tutorial (Android Java) +======================== + +BACKGROUND +------------- +For this sample, we've already generated the server and client stubs from [helloworld.proto](https://github.com/grpc/grpc-common/blob/master/protos/helloworld.proto). + +PREREQUISITES +------------- +- [Java gRPC](https://github.com/grpc/grpc-java) + +- [Android Tutorial](https://developer.android.com/training/basics/firstapp/index.html) If you're new to Android development + +- We only have Android gRPC client in this example. Please follow examples in other languages to build and run a gRPC server. + +INSTALL +------- +1 Clone the gRPC Java git repo +```sh +$ git clone https://github.com/grpc/grpc-java +``` + +2 Install gRPC Java, as described in [How to Build](https://github.com/grpc/grpc-java#how-to-build) +```sh +$ # from this dir +$ cd grpc-java +$ # follow the instructions in 'How to Build' +``` + +3 [Create an Android project](https://developer.android.com/training/basics/firstapp/creating-project.html) under your working directory. +- Set Application name to "Helloworld Example" and set Company Domain to "grpc.io". Make sure your package name is "io.grpc.helloworldexample" +- Choose appropriate minimum SDK +- Use Blank Activity +- Set Activity Name to HelloworldActivity +- Set Layout Name to activity_helloworld + +4 Prepare the app +- Clone this git repo +```sh +$ git clone https://github.com/grpc/grpc-common + +``` +- Replace the generated HelloworldActivity.java and activity_helloworld.xml with the two files in this repo +- Copy GreeterGrpc.java and Helloworld.java under your_app_dir/app/src/main/java/io/grpc/examples/ +- In your AndroidManifest.xml, make sure you have +```sh + +``` +added outside your appplication tag + +5 Add dependencies. gRPC Java on Android depends on grpc-java, protobuf nano, okhttp +- Copy grpc-java .jar files to your_app_dir/app/libs/: + - grpc-java/core/build/libs/*.jar + - grpc-java/stub/build/libs/*.jar + - grpc-java/nano/build/libs/*.jar + - grpc-java/okhttp/build/libs/*.jar +- Copy or download other dependencies to your_app_dir/app/libs/: + - [Guava 18](http://search.maven.org/remotecontent?filepath=com/google/guava/guava/18.0/guava-18.0.jar) + - [okhttp 2.2.0](http://repo1.maven.org/maven2/com/squareup/okhttp/okhttp/2.2.0/okhttp-2.2.0.jar) + - protobuf nano: +```sh +$ cp ~/.m2/repository/com/google/protobuf/nano/protobuf-javanano/2.6.2-pre/protobuf-javanano-2.6.2-pre.jar your_app_dir/app/libs/ +``` +- Make sure your_app_dir/app/build.gradle contains: +```sh +dependencies { + compile fileTree(dir: 'libs', include: ['*.jar']) +} +``` + +6 [Run your example app](https://developer.android.com/training/basics/firstapp/running-app.html) diff --git a/java/android/activity_helloworld.xml b/java/android/activity_helloworld.xml new file mode 100644 index 00000000000..41411d11aa5 --- /dev/null +++ b/java/android/activity_helloworld.xml @@ -0,0 +1,54 @@ + + + + + + + + + + +