From 77d85ac583aaa289986ee7e3af757350f63ac531 Mon Sep 17 00:00:00 2001 From: Gabriele Civitarese Date: Thu, 4 Apr 2019 15:38:52 +0200 Subject: [PATCH] Refactor import --- src/main/java/chat/ChatClient.java | 11 ++++++----- src/main/java/chat/ChatServiceImpl.java | 16 ++++++++-------- .../grpchelloserver/GreetingServiceClient.java | 12 ++++++------ .../grpchelloserver/GreetingServiceImpl.java | 14 +++++++------- 4 files changed, 27 insertions(+), 26 deletions(-) diff --git a/src/main/java/chat/ChatClient.java b/src/main/java/chat/ChatClient.java index 10a7dc1..2961f5a 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 b851ee0..ef490ad 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 e51e80e..1f5c992 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 dfd0994..03e1d96 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); -- 2.18.1