Ajout suppression d'un SBE de son repository lorsqu'il est déconnecté
This commit is contained in:
parent
1a91271324
commit
ebb5ee9b15
@ -8,6 +8,7 @@ import lightcontainer.protocol.ProtocolWriter;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.Socket;
|
||||
import java.net.SocketException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Objects;
|
||||
|
||||
@ -82,18 +83,21 @@ public class StoreProcessor extends Thread implements AutoCloseable {
|
||||
this.client_run = true;
|
||||
while (this.client_run) {
|
||||
try {
|
||||
if (!store.isConnected()) {
|
||||
// TODO : Gérer déconnection
|
||||
break;
|
||||
}
|
||||
waitAction();
|
||||
System.out.println("[SBE] Envoie commande : " + protocolResult.getCommand());
|
||||
|
||||
// Request
|
||||
this.writer.write(protocolResult.getCommand());
|
||||
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
|
||||
String responseCommand = this.reader.readLine();
|
||||
@ -116,10 +120,21 @@ public class StoreProcessor extends Thread implements AutoCloseable {
|
||||
alertAvalaible(null);
|
||||
}
|
||||
|
||||
} catch (IOException ignore) {
|
||||
// TODO : Gérer déconnection
|
||||
} catch (IOException exception) {
|
||||
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) {
|
||||
synchronized (this) {
|
||||
this.protocolResult = null;
|
||||
if (responseCommand == null) {
|
||||
this.close();
|
||||
}
|
||||
fileFrontEnd.onStoreAvailable(this, responseCommand);
|
||||
}
|
||||
}
|
||||
@ -164,11 +182,8 @@ public class StoreProcessor extends Thread implements AutoCloseable {
|
||||
@Override
|
||||
public void close() {
|
||||
if (this.client_run) {
|
||||
try {
|
||||
this.client_run = false;
|
||||
this.store.close();
|
||||
// TODO : Gérer déconnection (enlever du repo et prévenir client et FileFrontEnd)
|
||||
} catch (IOException ignored) { }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -10,6 +10,7 @@ import lightcontainer.utils.NetChooser;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.*;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* StoreMulticastRunnable
|
||||
@ -69,11 +70,19 @@ public class MulticastServerListener implements Runnable {
|
||||
while (true) {
|
||||
// Read the packet received and build a string of characters
|
||||
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)
|
||||
|
||||
onNewSbe(packet);
|
||||
}
|
||||
} catch (IOException ioException) {
|
||||
System.out.println("[ERREUR] Multicast server can't start : " + ioException.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private void onNewSbe(DatagramPacket packet) {
|
||||
try {
|
||||
// TODO Récupérer le port du message du packet et le setup (add description of the line).
|
||||
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())){
|
||||
@ -85,11 +94,8 @@ public class MulticastServerListener implements Runnable {
|
||||
// Add the store processor to its repository
|
||||
this.repository.addStore(storeProcessor);
|
||||
}
|
||||
} catch (IOException ignore) {
|
||||
ignore.printStackTrace();
|
||||
}
|
||||
}
|
||||
} catch (Exception ignore) {
|
||||
} 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
|
||||
*/
|
||||
public void stop() {
|
||||
repository.disconnectDomains();
|
||||
this.listener.close();
|
||||
}
|
||||
}
|
||||
|
@ -28,4 +28,15 @@ public interface MulticastSPR {
|
||||
boolean hasDomain(String domain);
|
||||
|
||||
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);
|
||||
}
|
||||
|
@ -14,4 +14,10 @@ public interface StoreProcessorFFE {
|
||||
* @param responseCommand
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package lightcontainer.protocol;
|
||||
|
||||
import lightcontainer.domains.client.Context;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
@ -51,7 +52,7 @@ public abstract class ProtocolWriter {
|
||||
* Redéfinissez cette méthode pour l'utiliser.
|
||||
* @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
|
||||
|
@ -4,6 +4,7 @@ import lightcontainer.domains.client.Context;
|
||||
import lightcontainer.protocol.ProtocolWriter;
|
||||
import lightcontainer.utils.FileSender;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
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
|
||||
*/
|
||||
@Override
|
||||
public void write(OutputStream writer) {
|
||||
super.write(writer);
|
||||
public void write(OutputStream writer) throws IOException {
|
||||
System.out.printf("Sauvegarde du fichier : %s %d\n", this.filename, this.filesize);
|
||||
|
||||
FileSender fileSender = new FileSender(storagePath);
|
||||
|
@ -4,6 +4,7 @@ import lightcontainer.domains.client.Context;
|
||||
import lightcontainer.protocol.ProtocolWriter;
|
||||
import lightcontainer.utils.FileSender;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
/**
|
||||
@ -40,7 +41,7 @@ public class SendfileRule extends ProtocolWriter {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(OutputStream writer) {
|
||||
public void write(OutputStream writer) throws IOException {
|
||||
super.write(writer);
|
||||
System.out.println("Envoie du fichier au SBE");
|
||||
|
||||
|
@ -63,6 +63,11 @@ public class FileFrontEnd implements ClientHandlerFFE, StoreProcessorFFE {
|
||||
assignOtherTask(store);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStoreDisconnect(String domain) {
|
||||
this.storeRepository.closeStore(domain);
|
||||
}
|
||||
|
||||
private void assignOtherTask(StoreProcessor store) {
|
||||
Iterator<Task> it = tasks.iterator();
|
||||
|
||||
@ -86,7 +91,6 @@ public class FileFrontEnd implements ClientHandlerFFE, StoreProcessorFFE {
|
||||
|
||||
@Override
|
||||
public boolean canExecuteCommand(String domain) {
|
||||
System.out.println("Peut on exécuter la commande ? " + domain);
|
||||
return domain == null ? storeRepository.domainCount() > 0 : storeRepository.hasDomain(domain);
|
||||
}
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ import lightcontainer.domains.server.MulticastServerListener;
|
||||
import lightcontainer.interfaces.MulticastSPR;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
// 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();
|
||||
}
|
||||
|
||||
@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
|
||||
* Closes all StoreProcessor stored in this repository and deallocates all resources.
|
||||
|
@ -8,7 +8,7 @@ public class FileSender {
|
||||
|
||||
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;
|
||||
System.out.printf("Envoie fichier : %s - %s - %s \n", filename, aesKey, iv);
|
||||
try {
|
||||
@ -23,12 +23,11 @@ public class FileSender {
|
||||
} else
|
||||
return false;
|
||||
} catch(IOException | AES_GCM.AesGcmException ex) {
|
||||
ex.printStackTrace();
|
||||
return false;
|
||||
throw new IOException();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean sendFile(String filename, OutputStream out) {
|
||||
public boolean sendFile(String filename, OutputStream out) throws IOException {
|
||||
BufferedInputStream bisFile;
|
||||
int bytesReaded = 0;
|
||||
|
||||
@ -48,8 +47,7 @@ public class FileSender {
|
||||
} else
|
||||
return false;
|
||||
} catch(IOException ex) {
|
||||
ex.printStackTrace();
|
||||
return false;
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
|
10
app/src/main/java/lightcontainer/utils/Log.java
Normal file
10
app/src/main/java/lightcontainer/utils/Log.java
Normal 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 {
|
||||
|
||||
|
||||
}
|
@ -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"]}]}]}
|
Loading…
Reference in New Issue
Block a user