Commit 77d85ac5 authored by Gabriele Civitarese's avatar Gabriele Civitarese

Refactor import

parent bcf4ae00
package chat;
import com.example.chat.ChatServiceGrpc.*;
import com.example.chat.ChatServiceGrpc;
import com.example.chat.ChatServiceOuterClass;
import com.example.chat.ChatServiceOuterClass.*;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.stub.StreamObserver;
......@@ -40,17 +41,17 @@ public class ChatClient {
//creating the asynchronous stub
ChatServiceGrpc.ChatServiceStub stub = ChatServiceGrpc.newStub(channel);
ChatServiceStub stub = ChatServiceGrpc.newStub(channel);
//the stub returns a stream (to communicate with the server, and thus with all the other clients).
//the argument is the stream of messages which are transmitted by the server.
StreamObserver<ChatServiceOuterClass.ChatMessage> serverStream = stub.chat(new StreamObserver<ChatServiceOuterClass.ChatMessage>() {
StreamObserver<ChatMessage> serverStream = stub.chat(new StreamObserver<ChatMessage>() {
//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(ChatServiceOuterClass.ChatMessage chatMessage) {
public void onNext(ChatMessage chatMessage) {
String from = chatMessage.getFrom();
String message = chatMessage.getMessage();
......@@ -86,7 +87,7 @@ public class ChatClient {
}
//we use the stream to communicate to the server our message.
serverStream.onNext(ChatServiceOuterClass.ChatMessage.newBuilder().setFrom(nickname).setMessage(message).build());
serverStream.onNext(ChatMessage.newBuilder().setFrom(nickname).setMessage(message).build());
}
......
package chat;
import com.example.chat.ChatServiceGrpc;
import com.example.chat.ChatServiceOuterClass;
import com.example.chat.ChatServiceGrpc.*;
import com.example.chat.ChatServiceOuterClass.*;
import io.grpc.stub.StreamObserver;
import java.util.HashSet;
import java.util.LinkedHashSet;
public class ChatServiceImpl extends ChatServiceGrpc.ChatServiceImplBase {
public class ChatServiceImpl extends ChatServiceImplBase {
//an hashset to store all the streams which the server uses to communicate with each client
HashSet<StreamObserver> observers = new LinkedHashSet<StreamObserver>();
@Override public StreamObserver<ChatServiceOuterClass.ChatMessage> chat(final StreamObserver<ChatServiceOuterClass.ChatMessage> responseObserver){
@Override public StreamObserver<ChatMessage> chat(final StreamObserver<ChatMessage> responseObserver){
//the stream used to communicate with a specific client is stored in a hash set (avoiding duplicates)
synchronized (observers) {
......@@ -22,10 +22,10 @@ public class ChatServiceImpl extends ChatServiceGrpc.ChatServiceImplBase {
}
//it returns the stream that will be used by the clients to send messages.
//the client will write on this stream
return new StreamObserver<ChatServiceOuterClass.ChatMessage>() {
return new StreamObserver<ChatMessage>() {
//receiving a message from a specific client
public void onNext(ChatServiceOuterClass.ChatMessage chatMessage) {
public void onNext(ChatMessage chatMessage) {
//unwrapping message
String message = chatMessage.getMessage();
......@@ -43,12 +43,12 @@ public class ChatServiceImpl extends ChatServiceGrpc.ChatServiceImplBase {
}
//iterating on all the streams to communicate with all the clients
for(StreamObserver<ChatServiceOuterClass.ChatMessage> observer: copy){
for(StreamObserver<ChatMessage> observer: copy){
//we exclude the one which is sending the message
if(!observer.equals(responseObserver))
//we simply forward the message
observer.onNext(ChatServiceOuterClass.ChatMessage.newBuilder().setMessage(message).setFrom(from).build());
observer.onNext(ChatMessage.newBuilder().setMessage(message).setFrom(from).build());
}
......
package grpchelloserver;
import com.example.grpc.GreetingServiceGrpc;
import com.example.grpc.GreetingServiceOuterClass;
import com.example.grpc.GreetingServiceOuterClass.*;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.stub.StreamObserver;
......@@ -39,10 +39,10 @@ public class GreetingServiceClient {
GreetingServiceGrpc.GreetingServiceBlockingStub stub = GreetingServiceGrpc.newBlockingStub(channel);
//creating the HelloResponse object which will be provided as input to the RPC method
GreetingServiceOuterClass.HelloRequest request = GreetingServiceOuterClass.HelloRequest.newBuilder().setName("Civi").build();
HelloRequest request = HelloRequest.newBuilder().setName("Pippo").build();
//calling the method. it returns an instance of HelloResponse
GreetingServiceOuterClass.HelloResponse response = stub.greeting(request);
HelloResponse response = stub.greeting(request);
//printing the answer
System.out.println(response.getGreeting());
......@@ -62,13 +62,13 @@ public class GreetingServiceClient {
GreetingServiceGrpc.GreetingServiceStub stub = GreetingServiceGrpc.newStub(channel);
//creating the HelloResponse object which will be provided as input to the RPC method
GreetingServiceOuterClass.HelloRequest request = GreetingServiceOuterClass.HelloRequest.newBuilder().setName("Civi").build();
HelloRequest request = HelloRequest.newBuilder().setName("Pippo").build();
//calling the RPC method. since it is asynchronous, we need to define handlers
stub.streamGreeting(request, new StreamObserver<GreetingServiceOuterClass.HelloResponse>() {
stub.streamGreeting(request, new StreamObserver<HelloResponse>() {
//this hanlder takes care of each item received in the stream
public void onNext(GreetingServiceOuterClass.HelloResponse helloResponse) {
public void onNext(HelloResponse helloResponse) {
//each item is just printed
System.out.println(helloResponse.getGreeting());
......
package grpchelloserver;
import com.example.grpc.GreetingServiceGrpc;
import com.example.grpc.GreetingServiceOuterClass;
import com.example.grpc.GreetingServiceGrpc.GreetingServiceImplBase;
import com.example.grpc.GreetingServiceOuterClass.*;
import io.grpc.stub.StreamObserver;
public class GreetingServiceImpl extends GreetingServiceGrpc.GreetingServiceImplBase {
public class GreetingServiceImpl extends GreetingServiceImplBase {
@Override
public void greeting(GreetingServiceOuterClass.HelloRequest request, StreamObserver<GreetingServiceOuterClass.HelloResponse> responseObserver){
public void greeting(HelloRequest request, StreamObserver<HelloResponse> responseObserver){
//la richiesta è di tipo HelloRequest (definito in .proto)
System.out.println(request);
//costruisco la richiesta di tipo HelloResponse (sempre definito in .proto)
GreetingServiceOuterClass.HelloResponse response = GreetingServiceOuterClass.HelloResponse.newBuilder().setGreeting("Hello there, "+request.getName()).build();
HelloResponse response = HelloResponse.newBuilder().setGreeting("Hello there, "+request.getName()).build();
//passo la risposta nello stream
responseObserver.onNext(response);
......@@ -24,7 +24,7 @@ public class GreetingServiceImpl extends GreetingServiceGrpc.GreetingServiceImpl
}
@Override
public void streamGreeting(GreetingServiceOuterClass.HelloRequest request, StreamObserver<GreetingServiceOuterClass.HelloResponse> responseObserver){
public void streamGreeting(HelloRequest request, StreamObserver<HelloResponse> responseObserver){
System.out.println("Metodo stream chiamato!");
......@@ -32,7 +32,7 @@ public class GreetingServiceImpl extends GreetingServiceGrpc.GreetingServiceImpl
System.out.println(request);
//costruisco la richiesta di tipo HelloResponse (sempre definito in .proto)
GreetingServiceOuterClass.HelloResponse response = GreetingServiceOuterClass.HelloResponse.newBuilder().setGreeting("Hello there, "+request.getName()).build();
HelloResponse response = HelloResponse.newBuilder().setGreeting("Hello there, "+request.getName()).build();
//passo la risposta nello stream
responseObserver.onNext(response);
......
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