https://gist.github.com/tmcmanameyunl/e3b3a8ec34c35c47ff4d8b378f643ef5





On Friday, October 7, 2016 at 11:05:20 AM UTC-5, Louis Ryan wrote:
>
> Tim,
>
> Any chance you can put your code into a PR on Github or a Gist to make it 
> easier to reivew?
>
> - Louis
>
> On Fri, Oct 7, 2016 at 6:11 AM, Tim McManamey <tjmcm...@gmail.com 
> <javascript:>> wrote:
>
>> 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 <tjmcm...@gmail.com> 
>>> 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 grpc-io+u...@googlegroups.com.
>>>> To post to this group, send email to grp...@googlegroups.com.
>>>> 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 grpc-io+u...@googlegroups.com <javascript:>.
>> To post to this group, send email to grp...@googlegroups.com 
>> <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/6bd01f07-c4df-4291-9a07-04dac59b9f3e%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/grpc-io/6bd01f07-c4df-4291-9a07-04dac59b9f3e%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 grpc-io+unsubscr...@googlegroups.com.
To post to this group, send email to grpc-io@googlegroups.com.
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/c1ba32fb-227d-4e1c-a404-2bbd6de58301%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to