Updated code is below. Still doesn't work after calling .addMessage2(str)
On Thursday, October 6, 2016 at 6:25:57 PM UTC-5, Louis Ryan wrote:
>
>
>
> On Wed, Oct 5, 2016 at 2:46 PM, Tim McManamey <[email protected]
> <javascript:>> wrote:
>
>> I want to return a List of strings using gRPC. In my program it seams to
>> add a string to the repeated value fine, but when I return the value to the
>> calling program it's empty. Here is my code:
>>
>> my.proto
>>
>> syntax = "proto3";
>>
>> option java_multiple_files = true;
>> option java_package = my.package.name.testsend";
>> option java_outer_classname = "TestSendProto";
>> option objc_class_prefix = "TS";
>>
>> package testsend;
>>
>> // The greeting service definition.
>> service Greeter {
>> // Sends a greeting
>> rpc SayHello (HelloRequest) returns (HelloReply) {}
>> rpc SayHelloAgain (HelloRequest) returns (ArrayHelloReply) {}
>> }
>>
>> // The request message containing the user's name.
>> message HelloRequest {
>> string name = 1;
>> }
>>
>> // The response message containing the greetings
>> message HelloReply {
>> string message = 1;
>> }
>>
>> message ArrayHelloReply {
>> repeated string message2 = 2;
>> }
>>
>> ////////////////////////////// server /////////////////////////////////
>> import io.grpc.Server;
>> import io.grpc.ServerBuilder;
>> import io.grpc.stub.StreamObserver;
>> import java.io.IOException;
>> import java.util.logging.Logger;
>> import java.util.ArrayList;
>> import java.io.*;
>>
>>
>> /**
>> *
>> * @author yotimbo
>> */
>> public class yoRecvo {
>> private static final Logger logger =
>> Logger.getLogger(yoRecvo.class.getName());
>>
>> /* The port on which the server should run */
>> private int port = 50051;
>> private Server server;
>>
>> private void start() throws IOException {
>> server = ServerBuilder.forPort(port)
>> .addService(new GreeterImpl())
>> .build()
>> .start();
>> logger.info("Server started, listening on " + port);
>> Runtime.getRuntime().addShutdownHook(new Thread() {
>> @Override
>> public void run() {
>> // Use stderr here since the logger may have been reset by its
>> JVM shutdown hook.
>> System.err.println("*** shutting down gRPC server since JVM is
>> shutting down");
>> yoRecvo.this.stop();
>> System.err.println("*** server shut down");
>> }
>> });
>> }
>>
>> private void stop() {
>> if (server != null) {
>> server.shutdown();
>> }
>> }
>>
>> /**
>> * Await termination on the main thread since the grpc library uses
>> daemon threads.
>> */
>> private void blockUntilShutdown() throws InterruptedException {
>> if (server != null) {
>> server.awaitTermination();
>> }
>> }
>>
>> /**
>> * Main launches the server from the command line.
>> */
>> public static void main(String[] args) throws IOException,
>> InterruptedException {
>> final yoRecvo server = new yoRecvo();
>> server.start();
>> server.blockUntilShutdown();
>> }
>>
>> private class GreeterImpl extends GreeterGrpc.GreeterImplBase {
>>
>> @Override
>> public void sayHello(HelloRequest req, StreamObserver<HelloReply>
>> responseObserver) {
>> HelloReply reply = HelloReply.newBuilder().setMessage("Hello " +
>> req.getName()).build();
>> responseObserver.onNext(reply);
>> responseObserver.onCompleted();
>> }
>> @Override
>> public void sayHelloAgain(HelloRequest req,
>> StreamObserver<ArrayHelloReply> responseObserver) {
>> String str = new String("Yo Dude");
>> final Logger logger = Logger.getLogger(yoRecvo.class.getName());
>>
>> ArrayHelloReply arrayHelloReply =
>> ArrayHelloReply.newBuilder().build();
>>
> ArrayHelloReply.Builder builder = arrayHelloReply.toBuilder();
>
arrayHelloReply = builder.addMessage2(str).build();
>
> Did you mean to make the reply empty, would have expected to see calls to
> add elements to the message2 list here
>
>
>>
>> logger.info("number of elements " +
>> arrayHelloReply.getMessage2Count());
>>
>>
>> responseObserver.onNext(arrayHelloReply);
>> responseObserver.onCompleted();
>> }
>> }
>> }
>>
>> /////////////////////// client //////////////////////////
>>
>> import com.google.protobuf.Descriptors;
>> import io.grpc.ManagedChannel;
>> import io.grpc.ManagedChannelBuilder;
>> import io.grpc.StatusRuntimeException;
>> import java.util.List;
>>
>> import java.util.concurrent.TimeUnit;
>> import java.util.logging.Level;
>> import java.util.logging.Logger;
>>
>>
>> /**
>> *
>> * @author yotimbo
>> */
>> public class yoSendo {
>> private static final Logger logger =
>> Logger.getLogger(yoSendo.class.getName());
>> private final ManagedChannel channel; //gRPC class, only needed for
>> server
>> private final GreeterGrpc.GreeterBlockingStub blockingStub;
>>
>> public yoSendo(String host, int port) {
>> channel = ManagedChannelBuilder.forAddress(host, port)
>> .usePlaintext(true)
>> .build();
>> blockingStub = GreeterGrpc.newBlockingStub(channel); //Greeter
>> is from proto service, Grpc must be appended
>> }
>>
>> public void shutdown() throws InterruptedException {
>> channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
>> }
>>
>> /** Say hello to server. */
>> public void greet(String name) {
>> List mylist;
>> logger.info("Will try to greet " + name + " ...");
>> HelloRequest request =
>> HelloRequest.newBuilder().setName(name).build();
>> HelloReply response;
>> ArrayHelloReply arrayResponse;
>> try {
>> response = blockingStub.sayHello(request);
>> } catch (StatusRuntimeException e) {
>> logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
>> return;
>> }
>> logger.info("Greeting: " + response.getMessage());
>> try {
>> arrayResponse = blockingStub.sayHelloAgain(request);
>> logger.info(arrayResponse.getAllFields().toString());
>> } catch (StatusRuntimeException e) {
>> logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
>> return;
>> }
>> logger.info("Greeting: " + arrayResponse.getMessage2Count());
>>
>> }
>>
>> public static void main(String[] args) throws Exception{
>> yoSendo client = new yoSendo("localhost", 50051);
>> try {
>> /* Access a service running on the local machine on port 50051 */
>> String user = "world";
>> if (args.length > 0) {
>> user = args[0]; /* Use the arg as the name to greet if provided */
>> }
>> client.greet(user);
>> } finally {
>> client.shutdown();
>> }
>> }
>> }
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "grpc.io" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected] <javascript:>.
>> To post to this group, send email to [email protected]
>> <javascript:>.
>> Visit this group at https://groups.google.com/group/grpc-io.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/grpc-io/2b745870-9ab4-4de5-8bdc-e1a3322cb238%40googlegroups.com
>>
>> <https://groups.google.com/d/msgid/grpc-io/2b745870-9ab4-4de5-8bdc-e1a3322cb238%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
--
You received this message because you are subscribed to the Google Groups
"grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/grpc-io.
To view this discussion on the web visit
https://groups.google.com/d/msgid/grpc-io/6bd01f07-c4df-4291-9a07-04dac59b9f3e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.