Ajout possibilité de déconnecter un client

This commit is contained in:
Benjamin 2022-03-11 17:02:52 +01:00
parent ef8f0016bb
commit 58c8d7f514
5 changed files with 43 additions and 4 deletions

View File

@ -1,10 +1,13 @@
package lightcontainer.domains.client; package lightcontainer.domains.client;
import lightcontainer.domains.server.UnicastServerListener;
import lightcontainer.interfaces.ClientHandlerFFE; import lightcontainer.interfaces.ClientHandlerFFE;
import lightcontainer.interfaces.ProtocolRepository; import lightcontainer.interfaces.ProtocolRepository;
import lightcontainer.interfaces.UnicastCHR;
import lightcontainer.protocol.ProtocolReader; import lightcontainer.protocol.ProtocolReader;
import lightcontainer.protocol.ProtocolWriter; import lightcontainer.protocol.ProtocolWriter;
import lightcontainer.protocol.rules.reader.SigninRule; import lightcontainer.protocol.rules.reader.SigninRule;
import lightcontainer.protocol.rules.reader.SignoutRule;
import lightcontainer.protocol.rules.reader.SignupRule; import lightcontainer.protocol.rules.reader.SignupRule;
import lightcontainer.protocol.rules.writer.SignErrorRule; import lightcontainer.protocol.rules.writer.SignErrorRule;
import lightcontainer.protocol.rules.writer.SignOkRule; import lightcontainer.protocol.rules.writer.SignOkRule;
@ -34,6 +37,7 @@ public class ClientHandler implements Runnable, AutoCloseable {
private ProtocolRepository protocolRep; private ProtocolRepository protocolRep;
private Context context; private Context context;
private boolean client_run; private boolean client_run;
private UnicastCHR repository;
private BufferedReader reader; private BufferedReader reader;
@ -41,7 +45,8 @@ public class ClientHandler implements Runnable, AutoCloseable {
private ProtocolWriter.ProtocolResult response; private ProtocolWriter.ProtocolResult response;
// Constructor // Constructor
public ClientHandler(Socket client, ClientHandlerFFE ffe, ProtocolRepository protocolRep, Context context) { public ClientHandler(UnicastCHR repository, Socket client, ClientHandlerFFE ffe, ProtocolRepository protocolRep, Context context) {
this.repository = repository;
this.fileFrontEnd = ffe; this.fileFrontEnd = ffe;
this.client = client; this.client = client;
this.protocolRep = protocolRep; this.protocolRep = protocolRep;
@ -87,11 +92,13 @@ public class ClientHandler implements Runnable, AutoCloseable {
String command = this.reader.readLine(); String command = this.reader.readLine();
if (command != null) { if (command != null) {
System.out.println("Client: " + command); System.out.println("Client: " + command);
} else this.client.close(); } else {
repository.disconnect(this);
}
ProtocolReader.ProtocolResult ruleResult = protocolRep.executeReader(context, command + "\r\n"); ProtocolReader.ProtocolResult ruleResult = protocolRep.executeReader(context, command + "\r\n");
if (ruleResult == null) { if (ruleResult == null) {
this.close(); repository.disconnect(this);
return; return;
} }
@ -120,6 +127,7 @@ public class ClientHandler implements Runnable, AutoCloseable {
} }
} catch (IOException ignore) { } catch (IOException ignore) {
ignore.printStackTrace(); ignore.printStackTrace();
repository.disconnect(this);
} }
} }
@ -131,6 +139,7 @@ public class ClientHandler implements Runnable, AutoCloseable {
* @return TRUE si le client possède l'accès demandé * @return TRUE si le client possède l'accès demandé
*/ */
private boolean checkAccess(ProtocolReader.ProtocolResult ruleResult) { private boolean checkAccess(ProtocolReader.ProtocolResult ruleResult) {
checkSignout(ruleResult);
if (context.isConnected()) if (context.isConnected())
return true; return true;
@ -158,6 +167,17 @@ public class ClientHandler implements Runnable, AutoCloseable {
writer.flush(); writer.flush();
} }
/**
* Vérifie s'il s'âgit d'une demande de déconnexion
* @param ruleResult
*/
private void checkSignout(ProtocolReader.ProtocolResult ruleResult) {
try {
ruleResult.getClass().asSubclass(SignoutRule.Result.class);
repository.disconnect(this);
} catch (ClassCastException e2) { }
}
/** /**
* Permet au Client d'attendre la fin de la réalisation de sa tâche * Permet au Client d'attendre la fin de la réalisation de sa tâche
*/ */
@ -194,6 +214,7 @@ public class ClientHandler implements Runnable, AutoCloseable {
try { try {
this.client_run = false; this.client_run = false;
this.client.close(); this.client.close();
System.out.printf("[CLIENT] %s s'est déconnecté\n", context.getLogin());
} catch (IOException ignored) { } } catch (IOException ignored) { }
} }
} }

View File

@ -51,7 +51,7 @@ public class UnicastServerListener implements Runnable {
Socket client = this.server.accept(); Socket client = this.server.accept();
System.out.println("New Client"); System.out.println("New Client");
// Create a new Handler client by passing these dependencies to it // Create a new Handler client by passing these dependencies to it
ClientHandler clientHandler = new ClientHandler(client, ffe, protocolRep, new Context(AppData.getInstance())); // TODO passer FileFrontEnd ou faire ca dans le repository ?! ClientHandler clientHandler = new ClientHandler(this.repository, client, ffe, protocolRep, new Context(AppData.getInstance()));
// Add the client handler to its repository (clienthandlerrepository) // Add the client handler to its repository (clienthandlerrepository)
this.repository.addClient(clientHandler); this.repository.addClient(clientHandler);
// Start the thread // Start the thread

View File

@ -22,5 +22,11 @@ public interface UnicastCHR {
*/ */
void addClient(ClientHandler client); void addClient(ClientHandler client);
/**
* Permet de demander la déconnection d'un client
* @param client Le client à déconnecter
*/
void disconnect(ClientHandler client);
void respondToClient(String client, ProtocolWriter.ProtocolResult response); void respondToClient(String client, ProtocolWriter.ProtocolResult response);
} }

View File

@ -17,9 +17,15 @@ public class SignoutRule extends ProtocolReader {
super(NAME, PATTERN); super(NAME, PATTERN);
} }
public class Result extends ProtocolResult {
public Result(Context context) {
super(context);
}
}
@Override @Override
protected <T extends ProtocolResult> T onExecuted(Context context, String... data) { protected <T extends ProtocolResult> T onExecuted(Context context, String... data) {
return null; return null;
} }
} }

View File

@ -55,6 +55,12 @@ public class ClientHandlerRepository implements AutoCloseable, UnicastCHR {
this.handlers.add(client); this.handlers.add(client);
} }
@Override
public void disconnect(ClientHandler client) {
if (handlers.remove(client))
client.close();
}
@Override @Override
public void respondToClient(String login, ProtocolWriter.ProtocolResult response) { public void respondToClient(String login, ProtocolWriter.ProtocolResult response) {
for (ClientHandler client : handlers) { for (ClientHandler client : handlers) {