diff --git a/src/main/java/chat/ChatClient.java b/src/main/java/chat/ChatClient.java index 10a7dc1f8401939eba43a380d9090bdd1fc57d43..2961f5ad335ec6ec2185cd62f492713aa23f92de 100644 --- a/src/main/java/chat/ChatClient.java +++ b/src/main/java/chat/ChatClient.java @@ -1,7 +1,8 @@ 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 serverStream = stub.chat(new StreamObserver() { + StreamObserver serverStream = stub.chat(new StreamObserver() { //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()); } diff --git a/src/main/java/chat/ChatServiceImpl.java b/src/main/java/chat/ChatServiceImpl.java index b851ee09c49660ffd5ce0ea2ac73ea3920cb5321..ef490adf90cff2fa8753b35e0313438d4984af9b 100644 --- a/src/main/java/chat/ChatServiceImpl.java +++ b/src/main/java/chat/ChatServiceImpl.java @@ -1,18 +1,18 @@ 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 observers = new LinkedHashSet(); - @Override public StreamObserver chat(final StreamObserver responseObserver){ + @Override public StreamObserver chat(final StreamObserver 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() { + return new StreamObserver() { //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 observer: copy){ + for(StreamObserver 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()); } diff --git a/src/main/java/grpchelloserver/GreetingServiceClient.java b/src/main/java/grpchelloserver/GreetingServiceClient.java index e51e80e369da902bf2351600ee1bd71841c7f4aa..1f5c9927fba9afa253fdce672cc8009000abe537 100644 --- a/src/main/java/grpchelloserver/GreetingServiceClient.java +++ b/src/main/java/grpchelloserver/GreetingServiceClient.java @@ -1,7 +1,7 @@ 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() { + stub.streamGreeting(request, new StreamObserver() { //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()); diff --git a/src/main/java/grpchelloserver/GreetingServiceImpl.java b/src/main/java/grpchelloserver/GreetingServiceImpl.java index dfd09948029d4af3f837eb57e1785c633a0d205f..03e1d96bb725525e924da87e6d10b6ec193229ef 100644 --- a/src/main/java/grpchelloserver/GreetingServiceImpl.java +++ b/src/main/java/grpchelloserver/GreetingServiceImpl.java @@ -1,19 +1,19 @@ 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 responseObserver){ + public void greeting(HelloRequest request, StreamObserver 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 responseObserver){ + public void streamGreeting(HelloRequest request, StreamObserver 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);