This commit is contained in:
Vu Tuan Minh
2025-04-04 12:31:51 +02:00
parent e9c518ce40
commit 775b09663f
4 changed files with 35 additions and 13 deletions

View File

@@ -11,9 +11,9 @@ public class EchoClient {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
ManagedChannel channel = ManagedChannelBuilder ManagedChannel channel = ManagedChannelBuilder
.forAddress("localhost", 8080) //serveur localhost, port 8080 .forAddress("localhost", 8080) //serveur localhost, port 8080
.usePlaintext() // on demande a ne pas utiliser tls .usePlaintext() // on demande a ne pas utiliser tls
.build(); // pattern builder .build(); // pattern builder
//On utilise un stub bloquant pour faire la requête (on bloque jusqu'à réponse) //On utilise un stub bloquant pour faire la requête (on bloque jusqu'à réponse)
EchoServiceGrpc.EchoServiceBlockingStub stub = EchoServiceGrpc.newBlockingStub(channel); EchoServiceGrpc.EchoServiceBlockingStub stub = EchoServiceGrpc.newBlockingStub(channel);
@@ -25,7 +25,6 @@ public class EchoClient {
// On affiche le résultat // On affiche le résultat
System.out.println("Reçu : " + response.getMessage()); System.out.println("Reçu : " + response.getMessage());
channel.shutdown(); channel.shutdown();
} }
} }

View File

@@ -1,9 +1,29 @@
package fr.istic.grpc.todo; package fr.istic.grpc.todo;
import fr.istic.grpc.echo.Echo;
import fr.istic.grpc.echo.EchoServiceGrpc;
import fr.istic.grpc.todo.Todo.*; import fr.istic.grpc.todo.Todo.*;
import fr.istic.grpc.todo.TodoServiceGrpc.*; import fr.istic.grpc.todo.TodoServiceGrpc.*;
import io.grpc.*; import io.grpc.*;
public class TodoClient { public class TodoClient {
public static void main(String[] args) throws Exception {
ManagedChannel channel = ManagedChannelBuilder
.forAddress("localhost", 8080) //serveur localhost, port 8080
.usePlaintext() // on demande a ne pas utiliser tls
.build(); // pattern builder
//On utilise un stub bloquant pour faire la requête (on bloque jusqu'à réponse)
TodoServiceGrpc.TodoServiceBlockingStub stub = TodoServiceGrpc.newBlockingStub(channel);
//On construit la requête :
Echo.MessageSent request = Echo.MessageSent.newBuilder().setMessage("ilililili").build();
// On appelle le serveur :
//Echo.MessageReceive response = stub.echo(request);
// On affiche le résultat
//System.out.println("Reçu : " + response.getMessage());
channel.shutdown();
}
} }

View File

@@ -1,5 +1,6 @@
package fr.istic.grpc.todo; package fr.istic.grpc.todo;
import io.grpc.stub.*;
import java.util.concurrent.*; import java.util.concurrent.*;
import java.util.concurrent.atomic.*; import java.util.concurrent.atomic.*;
@@ -12,13 +13,13 @@ public class TodoServiceImpl extends TodoServiceGrpc.TodoServiceImplBase {
return taskId.incrementAndGet(); return taskId.incrementAndGet();
} }
/*
@Override
public void createTask(Todo.CreateTaskRequest request, StreamObserver<Todo.TaskResponse> responseObserver) { public void createTask(Todo.CreateTaskRequest request, StreamObserver<Todo.TaskResponse> responseObserver) {
// Créer une tâche en générant un identifiant Todo.TaskResponse tr = Todo.TaskResponse.newBuilder().setId(Integer.toString(generateUniqueTaskId()))
.setDescription(request.getDescription()).build();
responseObserver.onNext(tr);
responseObserver.onCompleted();
} }
/*
@Override @Override
public void listTasks(Todo.ListTasksRequest request, StreamObserver<Todo.ListTasksResponse> responseObserver) { public void listTasks(Todo.ListTasksRequest request, StreamObserver<Todo.ListTasksResponse> responseObserver) {
// La réponse est une liste // La réponse est une liste

View File

@@ -5,7 +5,9 @@ option java_package = "fr.istic.grpc.todo";
// Le service TodoService définit les opérations disponibles pour gérer les tâches. // Le service TodoService définit les opérations disponibles pour gérer les tâches.
service TodoService { service TodoService {
// A COMPLETER ! rpc Create(CreateTaskRequest) returns (TaskResponse){}
rpc View(ListTasksRequest) returns (ListTasksResponse){}
rpc Delete(DeleteTaskRequest) returns (TaskResponse){}
} }
// Message de requête pour créer une nouvelle tâche. // Message de requête pour créer une nouvelle tâche.