Commit ecb0d6b6 authored by Luca Arrotta's avatar Luca Arrotta

bidirectional stream example added

parent 864fdcff
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/Lab3-Examples.iml" filepath="$PROJECT_DIR$/.idea/Lab3-Examples.iml" />
</modules>
</component>
</project>
\ No newline at end of file
syntax = "proto3";
package com.example.grpc;
// Request payload
message ClientRequest {
string stringRequest = 1;
}
message ServerResponse {
string stringResponse = 1;
}
service BidirectionalService {
rpc bidirectional(stream ClientRequest) returns (stream ServerResponse);
}
......@@ -3,17 +3,8 @@ package com.example.grpc;
// Request payload
message HelloRequest {
// Each message attribute is strongly typed.
// You also must assign a "tag" number.
// Each tag number is unique within the message.
string name = 1;
// This defines a strongly typed list of String
repeated string hobbies = 2;
// There are many more basics types, like Enum, Map
// See https://developers.google.com/protocol-buffers/docs/proto3
// for more information.
}
message HelloResponse {
......
......@@ -19,23 +19,11 @@ public final class GreetingServiceOuterClass {
com.google.protobuf.MessageOrBuilder {
/**
* <pre>
* Each message attribute is strongly typed.
* You also must assign a "tag" number.
* Each tag number is unique within the message.
* </pre>
*
* <code>string name = 1;</code>
* @return The name.
*/
java.lang.String getName();
/**
* <pre>
* Each message attribute is strongly typed.
* You also must assign a "tag" number.
* Each tag number is unique within the message.
* </pre>
*
* <code>string name = 1;</code>
* @return The bytes for name.
*/
......@@ -43,39 +31,23 @@ public final class GreetingServiceOuterClass {
getNameBytes();
/**
* <pre>
* This defines a strongly typed list of String
* </pre>
*
* <code>repeated string hobbies = 2;</code>
* @return A list containing the hobbies.
*/
java.util.List<java.lang.String>
getHobbiesList();
/**
* <pre>
* This defines a strongly typed list of String
* </pre>
*
* <code>repeated string hobbies = 2;</code>
* @return The count of hobbies.
*/
int getHobbiesCount();
/**
* <pre>
* This defines a strongly typed list of String
* </pre>
*
* <code>repeated string hobbies = 2;</code>
* @param index The index of the element to return.
* @return The hobbies at the given index.
*/
java.lang.String getHobbies(int index);
/**
* <pre>
* This defines a strongly typed list of String
* </pre>
*
* <code>repeated string hobbies = 2;</code>
* @param index The index of the value to return.
* @return The bytes of the hobbies at the given index.
......@@ -188,12 +160,6 @@ public final class GreetingServiceOuterClass {
public static final int NAME_FIELD_NUMBER = 1;
private volatile java.lang.Object name_;
/**
* <pre>
* Each message attribute is strongly typed.
* You also must assign a "tag" number.
* Each tag number is unique within the message.
* </pre>
*
* <code>string name = 1;</code>
* @return The name.
*/
......@@ -210,12 +176,6 @@ public final class GreetingServiceOuterClass {
}
}
/**
* <pre>
* Each message attribute is strongly typed.
* You also must assign a "tag" number.
* Each tag number is unique within the message.
* </pre>
*
* <code>string name = 1;</code>
* @return The bytes for name.
*/
......@@ -236,10 +196,6 @@ public final class GreetingServiceOuterClass {
public static final int HOBBIES_FIELD_NUMBER = 2;
private com.google.protobuf.LazyStringList hobbies_;
/**
* <pre>
* This defines a strongly typed list of String
* </pre>
*
* <code>repeated string hobbies = 2;</code>
* @return A list containing the hobbies.
*/
......@@ -248,10 +204,6 @@ public final class GreetingServiceOuterClass {
return hobbies_;
}
/**
* <pre>
* This defines a strongly typed list of String
* </pre>
*
* <code>repeated string hobbies = 2;</code>
* @return The count of hobbies.
*/
......@@ -259,10 +211,6 @@ public final class GreetingServiceOuterClass {
return hobbies_.size();
}
/**
* <pre>
* This defines a strongly typed list of String
* </pre>
*
* <code>repeated string hobbies = 2;</code>
* @param index The index of the element to return.
* @return The hobbies at the given index.
......@@ -271,10 +219,6 @@ public final class GreetingServiceOuterClass {
return hobbies_.get(index);
}
/**
* <pre>
* This defines a strongly typed list of String
* </pre>
*
* <code>repeated string hobbies = 2;</code>
* @param index The index of the value to return.
* @return The bytes of the hobbies at the given index.
......@@ -628,12 +572,6 @@ public final class GreetingServiceOuterClass {
private java.lang.Object name_ = "";
/**
* <pre>
* Each message attribute is strongly typed.
* You also must assign a "tag" number.
* Each tag number is unique within the message.
* </pre>
*
* <code>string name = 1;</code>
* @return The name.
*/
......@@ -650,12 +588,6 @@ public final class GreetingServiceOuterClass {
}
}
/**
* <pre>
* Each message attribute is strongly typed.
* You also must assign a "tag" number.
* Each tag number is unique within the message.
* </pre>
*
* <code>string name = 1;</code>
* @return The bytes for name.
*/
......@@ -673,12 +605,6 @@ public final class GreetingServiceOuterClass {
}
}
/**
* <pre>
* Each message attribute is strongly typed.
* You also must assign a "tag" number.
* Each tag number is unique within the message.
* </pre>
*
* <code>string name = 1;</code>
* @param value The name to set.
* @return This builder for chaining.
......@@ -694,12 +620,6 @@ public final class GreetingServiceOuterClass {
return this;
}
/**
* <pre>
* Each message attribute is strongly typed.
* You also must assign a "tag" number.
* Each tag number is unique within the message.
* </pre>
*
* <code>string name = 1;</code>
* @return This builder for chaining.
*/
......@@ -710,12 +630,6 @@ public final class GreetingServiceOuterClass {
return this;
}
/**
* <pre>
* Each message attribute is strongly typed.
* You also must assign a "tag" number.
* Each tag number is unique within the message.
* </pre>
*
* <code>string name = 1;</code>
* @param value The bytes for name to set.
* @return This builder for chaining.
......@@ -740,10 +654,6 @@ public final class GreetingServiceOuterClass {
}
}
/**
* <pre>
* This defines a strongly typed list of String
* </pre>
*
* <code>repeated string hobbies = 2;</code>
* @return A list containing the hobbies.
*/
......@@ -752,10 +662,6 @@ public final class GreetingServiceOuterClass {
return hobbies_.getUnmodifiableView();
}
/**
* <pre>
* This defines a strongly typed list of String
* </pre>
*
* <code>repeated string hobbies = 2;</code>
* @return The count of hobbies.
*/
......@@ -763,10 +669,6 @@ public final class GreetingServiceOuterClass {
return hobbies_.size();
}
/**
* <pre>
* This defines a strongly typed list of String
* </pre>
*
* <code>repeated string hobbies = 2;</code>
* @param index The index of the element to return.
* @return The hobbies at the given index.
......@@ -775,10 +677,6 @@ public final class GreetingServiceOuterClass {
return hobbies_.get(index);
}
/**
* <pre>
* This defines a strongly typed list of String
* </pre>
*
* <code>repeated string hobbies = 2;</code>
* @param index The index of the value to return.
* @return The bytes of the hobbies at the given index.
......@@ -788,10 +686,6 @@ public final class GreetingServiceOuterClass {
return hobbies_.getByteString(index);
}
/**
* <pre>
* This defines a strongly typed list of String
* </pre>
*
* <code>repeated string hobbies = 2;</code>
* @param index The index to set the value at.
* @param value The hobbies to set.
......@@ -808,10 +702,6 @@ public final class GreetingServiceOuterClass {
return this;
}
/**
* <pre>
* This defines a strongly typed list of String
* </pre>
*
* <code>repeated string hobbies = 2;</code>
* @param value The hobbies to add.
* @return This builder for chaining.
......@@ -827,10 +717,6 @@ public final class GreetingServiceOuterClass {
return this;
}
/**
* <pre>
* This defines a strongly typed list of String
* </pre>
*
* <code>repeated string hobbies = 2;</code>
* @param values The hobbies to add.
* @return This builder for chaining.
......@@ -844,10 +730,6 @@ public final class GreetingServiceOuterClass {
return this;
}
/**
* <pre>
* This defines a strongly typed list of String
* </pre>
*
* <code>repeated string hobbies = 2;</code>
* @return This builder for chaining.
*/
......@@ -858,10 +740,6 @@ public final class GreetingServiceOuterClass {
return this;
}
/**
* <pre>
* This defines a strongly typed list of String
* </pre>
*
* <code>repeated string hobbies = 2;</code>
* @param value The bytes of the hobbies to add.
* @return This builder for chaining.
......
syntax = "proto3";
package com.example.grpc;
message ClientRequest {
string stringRequest = 1;
}
message ServerResponse {
string stringResponse = 1;
}
service BidirectionalService {
rpc bidirectional(stream ClientRequest) returns (stream ServerResponse);
}
......@@ -3,17 +3,8 @@ package com.example.grpc;
// Request payload
message HelloRequest {
// Each message attribute is strongly typed.
// You also must assign a "tag" number.
// Each tag number is unique within the message.
string name = 1;
// This defines a strongly typed list of String
repeated string hobbies = 2;
// There are many more basics types, like Enum, Map
// See https://developers.google.com/protocol-buffers/docs/proto3
// for more information.
}
message HelloResponse {
......
......@@ -10,6 +10,9 @@ com/example/chat/ChatServiceOuterClass.java
chat/ChatClient.java
chat.ChatClient
chat.ChatClient$1
bidirectional/BidirectionalServiceImpl.java
bidirectional.BidirectionalServiceImpl
bidirectional.BidirectionalServiceImpl$1
chat/ChatServiceImpl.java
chat.ChatServiceImpl
chat.ChatServiceImpl$1
......@@ -26,6 +29,17 @@ com/example/grpc/GreetingServiceGrpc.java
com.example.grpc.GreetingServiceGrpc$GreetingServiceMethodDescriptorSupplier
com.example.grpc.GreetingServiceGrpc$GreetingServiceStub
com.example.grpc.GreetingServiceGrpc$MethodHandlers
com/example/grpc/BidirectionalServiceGrpc.java
com.example.grpc.BidirectionalServiceGrpc
com.example.grpc.BidirectionalServiceGrpc$1
com.example.grpc.BidirectionalServiceGrpc$BidirectionalServiceBaseDescriptorSupplier
com.example.grpc.BidirectionalServiceGrpc$BidirectionalServiceBlockingStub
com.example.grpc.BidirectionalServiceGrpc$BidirectionalServiceFileDescriptorSupplier
com.example.grpc.BidirectionalServiceGrpc$BidirectionalServiceFutureStub
com.example.grpc.BidirectionalServiceGrpc$BidirectionalServiceImplBase
com.example.grpc.BidirectionalServiceGrpc$BidirectionalServiceMethodDescriptorSupplier
com.example.grpc.BidirectionalServiceGrpc$BidirectionalServiceStub
com.example.grpc.BidirectionalServiceGrpc$MethodHandlers
com/example/grpc/GreetingServiceOuterClass.java
com.example.grpc.GreetingServiceOuterClass
com.example.grpc.GreetingServiceOuterClass$1
......@@ -37,6 +51,8 @@ com/example/grpc/GreetingServiceOuterClass.java
com.example.grpc.GreetingServiceOuterClass$HelloResponse$1
com.example.grpc.GreetingServiceOuterClass$HelloResponse$Builder
com.example.grpc.GreetingServiceOuterClass$HelloResponseOrBuilder
bidirectional/Server.java
bidirectional.Server
com/example/chat/ChatServiceGrpc.java
com.example.chat.ChatServiceGrpc
com.example.chat.ChatServiceGrpc$1
......@@ -48,6 +64,9 @@ com/example/chat/ChatServiceGrpc.java
com.example.chat.ChatServiceGrpc$ChatServiceMethodDescriptorSupplier
com.example.chat.ChatServiceGrpc$ChatServiceStub
com.example.chat.ChatServiceGrpc$MethodHandlers
bidirectional/Client.java
bidirectional.Client
bidirectional.Client$1
grpchelloserver/App.java
grpchelloserver.App
it/ewlab/actor/ActorOuterClass.java
......@@ -63,10 +82,21 @@ it/ewlab/actor/ActorOuterClass.java
it.ewlab.actor.ActorOuterClass$Actor$Sex
it.ewlab.actor.ActorOuterClass$Actor$Sex$1
it.ewlab.actor.ActorOuterClass$ActorOrBuilder
com/example/grpc/BidirectionalServiceOuterClass.java
com.example.grpc.BidirectionalServiceOuterClass
com.example.grpc.BidirectionalServiceOuterClass$1
com.example.grpc.BidirectionalServiceOuterClass$ClientRequest
com.example.grpc.BidirectionalServiceOuterClass$ClientRequest$1
com.example.grpc.BidirectionalServiceOuterClass$ClientRequest$Builder
com.example.grpc.BidirectionalServiceOuterClass$ClientRequestOrBuilder
com.example.grpc.BidirectionalServiceOuterClass$ServerResponse
com.example.grpc.BidirectionalServiceOuterClass$ServerResponse$1
com.example.grpc.BidirectionalServiceOuterClass$ServerResponse$Builder
com.example.grpc.BidirectionalServiceOuterClass$ServerResponseOrBuilder
grpchelloserver/GreetingServiceImpl.java
grpchelloserver.GreetingServiceImpl
actor/Server.java
actor.Server
grpchelloserver/GreetingServiceClient.java
grpchelloserver.GreetingServiceClient
grpchelloserver.GreetingServiceClient$1
actor/Server.java
actor.Server
......@@ -10,6 +10,7 @@ public class Server {
public static void main(String[] args) throws IOException {
System.out.println("The server is running...\n");
ServerSocket serverSocket = new ServerSocket(9999);
......
package bidirectional;
import com.example.grpc.BidirectionalServiceGrpc.*;
import com.example.grpc.BidirectionalServiceOuterClass.*;
import io.grpc.stub.StreamObserver;
public class BidirectionalServiceImpl extends BidirectionalServiceImplBase {
@Override
public StreamObserver<ClientRequest> bidirectional(StreamObserver<ServerResponse> responseObserver){
//it returns the stream that will be used by the clients to send messages. The client will write on this stream
return new StreamObserver<ClientRequest>() {
//receiving a message from the client
public void onNext(ClientRequest clientRequest) {
String clientStringRequest = clientRequest.getStringRequest();
System.out.println("[FROM CLIENT] " + clientStringRequest);
// sending the response to the client
System.out.println("Sending the response to the client...\n");
responseObserver.onNext(ServerResponse.newBuilder().setStringResponse("I've received this message: '" + clientStringRequest + "'").build());
}
public void onError(Throwable throwable) {
}
public void onCompleted() {
}
};
}
}
package bidirectional;
import com.example.grpc.BidirectionalServiceGrpc.*;
import com.example.grpc.BidirectionalServiceGrpc;
import com.example.grpc.BidirectionalServiceOuterClass.*;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.stub.StreamObserver;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
public class Client {
public final static String IP = "localhost";
public final static int PORT = 8888;
public static void main(String[] args) throws IOException {
//opening a connection with server
final ManagedChannel channel = ManagedChannelBuilder.forTarget(IP+":"+PORT).usePlaintext().build();
//creating the asynchronous stub
BidirectionalServiceStub stub = BidirectionalServiceGrpc.newStub(channel);
//the stub returns a stream to communicate with the server.
//the argument is the stream of messages which are transmitted by the server.
StreamObserver<ClientRequest> serverStream = stub.bidirectional(new StreamObserver<ServerResponse>() {
//remember: all the methods here are CALLBACKS which are handled in an asynchronous manner.
//we define what to do when a message from the server arrives (just print the message)
public void onNext(ServerResponse serverResponse) {
System.out.println("[FROM SERVER] " + serverResponse.getStringResponse());
}
public void onError(Throwable throwable) {
}
public void onCompleted() {
}
});
String msg = "First request from the client";
System.out.println("Sending the message '" + msg + "' to the server...");
serverStream.onNext(ClientRequest.newBuilder().setStringRequest(msg).build());
msg = "Second request from the client";
System.out.println("Sending the message '" + msg + "' to the server...");
serverStream.onNext(ClientRequest.newBuilder().setStringRequest(msg).build());
msg = "Third request from the client";
System.out.println("Sending the message '" + msg + "' to the server...");
serverStream.onNext(ClientRequest.newBuilder().setStringRequest(msg).build());
try {
//you need this. otherwise the method will terminate before that answers from the server are received
channel.awaitTermination(10, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
package bidirectional;
import io.grpc.ServerBuilder;
import java.io.IOException;
public class Server {
public static void main( String[] args ) {
try {
io.grpc.Server server = ServerBuilder.forPort(8888).addService(new BidirectionalServiceImpl()).build();
server.start();
System.out.println("Server started!\n");
server.awaitTermination();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
syntax = "proto3";
package com.example.grpc;
message ClientRequest {
string stringRequest = 1;
}
message ServerResponse {
string stringResponse = 1;
}
service BidirectionalService {
rpc bidirectional(stream ClientRequest) returns (stream ServerResponse);
}
......@@ -3,17 +3,8 @@ package com.example.grpc;
// Request payload
message HelloRequest {
// Each message attribute is strongly typed.
// You also must assign a "tag" number.
// Each tag number is unique within the message.
string name = 1;
// This defines a strongly typed list of String
repeated string hobbies = 2;
// There are many more basics types, like Enum, Map
// See https://developers.google.com/protocol-buffers/docs/proto3
// for more information.
}
message HelloResponse {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment