Ajout suppression d'un SBE de son repository lorsqu'il est déconnecté

This commit is contained in:
Benjamin 2022-03-15 19:18:35 +01:00
parent 1a91271324
commit ebb5ee9b15
12 changed files with 124 additions and 43 deletions

View File

@ -8,6 +8,7 @@ import lightcontainer.protocol.ProtocolWriter;
import java.io.*; import java.io.*;
import java.net.Socket; import java.net.Socket;
import java.net.SocketException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Objects; import java.util.Objects;
@ -82,18 +83,21 @@ public class StoreProcessor extends Thread implements AutoCloseable {
this.client_run = true; this.client_run = true;
while (this.client_run) { while (this.client_run) {
try { try {
if (!store.isConnected()) {
// TODO : Gérer déconnection
break;
}
waitAction(); waitAction();
System.out.println("[SBE] Envoie commande : " + protocolResult.getCommand()); System.out.println("[SBE] Envoie commande : " + protocolResult.getCommand());
// Request // Request
this.writer.write(protocolResult.getCommand()); this.writer.write(protocolResult.getCommand());
this.writer.flush(); this.writer.flush();
protocolResult.write(this.store.getOutputStream());
try {
protocolResult.write(this.store.getOutputStream());
} catch (IOException writeException) { // Si SBE fermé
System.out.println("STOPPER");
// Envoie au client que la requête n'a pu être traitée
alertAvalaible(null);
break;
}
// Response // Response
String responseCommand = this.reader.readLine(); String responseCommand = this.reader.readLine();
@ -116,10 +120,21 @@ public class StoreProcessor extends Thread implements AutoCloseable {
alertAvalaible(null); alertAvalaible(null);
} }
} catch (IOException ignore) { } catch (IOException exception) {
// TODO : Gérer déconnection System.out.println("[ERROR] Problem with SBE (" + domain + ") : " + exception.getMessage());
this.close();
} }
} }
// Fermeture du SBE
try {
this.reader.close();
this.writer.close();
this.store.close();
this.fileFrontEnd.onStoreDisconnect(this.domain);
} catch (IOException ioException) {
System.out.println("[ERROR] Error while closing SBE (" + domain + ") : " + ioException.getMessage());
}
} }
/** /**
@ -140,6 +155,9 @@ public class StoreProcessor extends Thread implements AutoCloseable {
private void alertAvalaible(ProtocolWriter.ProtocolResult responseCommand) { private void alertAvalaible(ProtocolWriter.ProtocolResult responseCommand) {
synchronized (this) { synchronized (this) {
this.protocolResult = null; this.protocolResult = null;
if (responseCommand == null) {
this.close();
}
fileFrontEnd.onStoreAvailable(this, responseCommand); fileFrontEnd.onStoreAvailable(this, responseCommand);
} }
} }
@ -164,11 +182,8 @@ public class StoreProcessor extends Thread implements AutoCloseable {
@Override @Override
public void close() { public void close() {
if (this.client_run) { if (this.client_run) {
try { this.client_run = false;
this.client_run = false; // TODO : Gérer déconnection (enlever du repo et prévenir client et FileFrontEnd)
this.store.close();
// TODO : Gérer déconnection (enlever du repo et prévenir client et FileFrontEnd)
} catch (IOException ignored) { }
} }
} }

View File

@ -10,6 +10,7 @@ import lightcontainer.utils.NetChooser;
import java.io.IOException; import java.io.IOException;
import java.net.*; import java.net.*;
import java.util.logging.Logger;
/** /**
* StoreMulticastRunnable * StoreMulticastRunnable
@ -69,27 +70,32 @@ public class MulticastServerListener implements Runnable {
while (true) { while (true) {
// Read the packet received and build a string of characters // Read the packet received and build a string of characters
this.listener.receive(packet); this.listener.receive(packet);
String data = new String(packet.getData(), 0, packet.getLength());
// Create a new StoreBacked (try used in the case of an error to maintain the listening loop)
try {
// TODO Récupérer le port du message du packet et le setup (add description of the line).
HelloRule.Result readerResult = protocolRep.executeReader(null, data);
System.out.printf("Nouveau SBE : Domain=%s | Port=%d\n", readerResult.getDomain(), readerResult.getPort());
if (!this.repository.hasDomain(readerResult.getDomain())){ onNewSbe(packet);
Socket socket = new Socket(packet.getAddress(), readerResult.getPort());
// Create the store processor
StoreProcessor storeProcessor = new StoreProcessor(socket, readerResult.getDomain(), ffe, protocolRep); // TODO <!!!> : Voir comment on procède get via repo ou ici ?!
// Add the store processor to its repository
this.repository.addStore(storeProcessor);
}
} catch (IOException ignore) {
ignore.printStackTrace();
}
} }
} catch (Exception ignore) { } catch (IOException ioException) {
System.out.println("[ERREUR] Multicast server can't start : " + ioException.getMessage());
}
}
private void onNewSbe(DatagramPacket packet) {
try {
String data = new String(packet.getData(), 0, packet.getLength());
HelloRule.Result readerResult = protocolRep.executeReader(null, data);
System.out.printf("Nouveau SBE : Domain=%s | Port=%d\n", readerResult.getDomain(), readerResult.getPort());
if (!this.repository.hasDomain(readerResult.getDomain())){
Socket socket = new Socket(packet.getAddress(), readerResult.getPort());
// Create the store processor
StoreProcessor storeProcessor = new StoreProcessor(socket, readerResult.getDomain(), ffe, protocolRep); // TODO <!!!> : Voir comment on procède get via repo ou ici ?!
// Add the store processor to its repository
this.repository.addStore(storeProcessor);
}
} catch (IOException | ClassCastException exception) {
System.out.println("[ERREUR] Une SBE essaye de se connecter avec une mauvaise configuration : " + exception.getMessage());
} }
} }
@ -117,6 +123,7 @@ public class MulticastServerListener implements Runnable {
* @since 1.0 * @since 1.0
*/ */
public void stop() { public void stop() {
repository.disconnectDomains();
this.listener.close(); this.listener.close();
} }
} }

View File

@ -28,4 +28,15 @@ public interface MulticastSPR {
boolean hasDomain(String domain); boolean hasDomain(String domain);
int domainCount(); int domainCount();
/**
* Déconnecte tous les SBE
*/
void disconnectDomains();
/**
* Permet de déconnecter un SBE
* @param domain Le domaine du SBE à déconnecter
*/
void closeStore(String domain);
} }

View File

@ -14,4 +14,10 @@ public interface StoreProcessorFFE {
* @param responseCommand * @param responseCommand
*/ */
void onStoreAvailable(StoreProcessor store, ProtocolWriter.ProtocolResult response); void onStoreAvailable(StoreProcessor store, ProtocolWriter.ProtocolResult response);
/**
* Permet de déconnecter un SBE
* @param domain Le domaine du SBE à déconnecter
*/
void onStoreDisconnect(String domain);
} }

View File

@ -2,6 +2,7 @@ package lightcontainer.protocol;
import lightcontainer.domains.client.Context; import lightcontainer.domains.client.Context;
import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -51,7 +52,7 @@ public abstract class ProtocolWriter {
* Redéfinissez cette méthode pour l'utiliser. * Redéfinissez cette méthode pour l'utiliser.
* @param writer Buffer à remplir qui sera envoyer via le réseau * @param writer Buffer à remplir qui sera envoyer via le réseau
*/ */
public void write(OutputStream writer) {} public void write(OutputStream writer) throws IOException {}
/** /**
* Accesseur au contexte courant sur lequel opère la commande * Accesseur au contexte courant sur lequel opère la commande

View File

@ -4,6 +4,7 @@ import lightcontainer.domains.client.Context;
import lightcontainer.protocol.ProtocolWriter; import lightcontainer.protocol.ProtocolWriter;
import lightcontainer.utils.FileSender; import lightcontainer.utils.FileSender;
import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
public class GetFileOkRule extends ProtocolWriter { public class GetFileOkRule extends ProtocolWriter {
@ -40,8 +41,7 @@ public class GetFileOkRule extends ProtocolWriter {
* @param writer Buffer à remplir qui sera envoyer via le réseau * @param writer Buffer à remplir qui sera envoyer via le réseau
*/ */
@Override @Override
public void write(OutputStream writer) { public void write(OutputStream writer) throws IOException {
super.write(writer);
System.out.printf("Sauvegarde du fichier : %s %d\n", this.filename, this.filesize); System.out.printf("Sauvegarde du fichier : %s %d\n", this.filename, this.filesize);
FileSender fileSender = new FileSender(storagePath); FileSender fileSender = new FileSender(storagePath);

View File

@ -4,6 +4,7 @@ import lightcontainer.domains.client.Context;
import lightcontainer.protocol.ProtocolWriter; import lightcontainer.protocol.ProtocolWriter;
import lightcontainer.utils.FileSender; import lightcontainer.utils.FileSender;
import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
/** /**
@ -40,7 +41,7 @@ public class SendfileRule extends ProtocolWriter {
} }
@Override @Override
public void write(OutputStream writer) { public void write(OutputStream writer) throws IOException {
super.write(writer); super.write(writer);
System.out.println("Envoie du fichier au SBE"); System.out.println("Envoie du fichier au SBE");

View File

@ -63,6 +63,11 @@ public class FileFrontEnd implements ClientHandlerFFE, StoreProcessorFFE {
assignOtherTask(store); assignOtherTask(store);
} }
@Override
public void onStoreDisconnect(String domain) {
this.storeRepository.closeStore(domain);
}
private void assignOtherTask(StoreProcessor store) { private void assignOtherTask(StoreProcessor store) {
Iterator<Task> it = tasks.iterator(); Iterator<Task> it = tasks.iterator();
@ -86,7 +91,6 @@ public class FileFrontEnd implements ClientHandlerFFE, StoreProcessorFFE {
@Override @Override
public boolean canExecuteCommand(String domain) { public boolean canExecuteCommand(String domain) {
System.out.println("Peut on exécuter la commande ? " + domain);
return domain == null ? storeRepository.domainCount() > 0 : storeRepository.hasDomain(domain); return domain == null ? storeRepository.domainCount() > 0 : storeRepository.hasDomain(domain);
} }
} }

View File

@ -6,6 +6,7 @@ import lightcontainer.domains.server.MulticastServerListener;
import lightcontainer.interfaces.MulticastSPR; import lightcontainer.interfaces.MulticastSPR;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator;
import java.util.Set; import java.util.Set;
// TODO : C'est genre un ClientHandlerManager quoi hein, normal qu'il fasse blinder de chose ;) // TODO : C'est genre un ClientHandlerManager quoi hein, normal qu'il fasse blinder de chose ;)
/** /**
@ -97,6 +98,33 @@ public class StoreProcessorRepository implements AutoCloseable, MulticastSPR {
return handlers.size(); return handlers.size();
} }
@Override
public void disconnectDomains() {
for (StoreProcessor handler : handlers) {
handler.close();
}
}
/**
* Permet de déconnecter un SBE
*
* @param domain Le domaine du SBE à déconnecter
*/
@Override
public void closeStore(String domain) {
Iterator<StoreProcessor> it = this.handlers.iterator();
System.out.println("1 Nombre de SBE : " + handlers.size());
while (it.hasNext()) {
StoreProcessor storeProcessor = it.next();
if (storeProcessor.getDomain().equals(domain)) {
storeProcessor.close();
it.remove();
return;
}
}
}
/** /**
* AutoClosable Function * AutoClosable Function
* Closes all StoreProcessor stored in this repository and deallocates all resources. * Closes all StoreProcessor stored in this repository and deallocates all resources.

View File

@ -8,7 +8,7 @@ public class FileSender {
public FileSender(String path) { this.path = path; } public FileSender(String path) { this.path = path; }
public boolean sendFile(String filename, OutputStream out, int fileSize, String aesKey, String iv) { public boolean sendFile(String filename, OutputStream out, int fileSize, String aesKey, String iv) throws IOException {
BufferedInputStream bisFile; BufferedInputStream bisFile;
System.out.printf("Envoie fichier : %s - %s - %s \n", filename, aesKey, iv); System.out.printf("Envoie fichier : %s - %s - %s \n", filename, aesKey, iv);
try { try {
@ -23,12 +23,11 @@ public class FileSender {
} else } else
return false; return false;
} catch(IOException | AES_GCM.AesGcmException ex) { } catch(IOException | AES_GCM.AesGcmException ex) {
ex.printStackTrace(); throw new IOException();
return false;
} }
} }
public boolean sendFile(String filename, OutputStream out) { public boolean sendFile(String filename, OutputStream out) throws IOException {
BufferedInputStream bisFile; BufferedInputStream bisFile;
int bytesReaded = 0; int bytesReaded = 0;
@ -48,8 +47,7 @@ public class FileSender {
} else } else
return false; return false;
} catch(IOException ex) { } catch(IOException ex) {
ex.printStackTrace(); throw ex;
return false;
} }
} }

View File

@ -0,0 +1,10 @@
package lightcontainer.utils;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
public class Log {
}

View File

@ -1 +1 @@
{"unicast_port":8000,"multicast_ip":"224.66.66.1","multicast_port":15502,"network_interface":"","tls":true,"storagePath":"/home/benjamin/ffe","users":[{"name":"aaaaa","password":"$2a$10$nDCEDVwbNO/YDQ4qdRcxfuES4.aboluLzWouXXsk6vDoaWocv516W","aes_key":"kYtwHy9qJBg30WS6axWTFGVE0Ge5kpYiJJlC+COIEI4=","files":[]}]} {"unicast_port":8000,"multicast_ip":"224.66.66.1","multicast_port":15502,"network_interface":"","tls":true,"storagePath":"/home/benjamin/ffe","users":[{"name":"aaaaa","password":"$2a$10$nDCEDVwbNO/YDQ4qdRcxfuES4.aboluLzWouXXsk6vDoaWocv516W","aes_key":"kYtwHy9qJBg30WS6axWTFGVE0Ge5kpYiJJlC+COIEI4=","files":[{"name":"README.md","fileNameSalt":"QnjgVrrW2KADUYUdeD/KcQ==","size":17,"iv":"E5JtY/JrH1B447F/my8Hkg==","storage":["lightcontainerSB01"]}]}]}