Skip to content

Commit

Permalink
Se ha añadido lógica para trabajar con varios servidores en el archivo
Browse files Browse the repository at this point in the history
de configuración. Ahora si tienes varios servidores en la configuración
salva el archivo y hace una copia de seguridad en otro (mirroring).
  • Loading branch information
luislorenzom committed Jan 10, 2016
1 parent 809a7d1 commit ef0e36b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 24 deletions.
64 changes: 47 additions & 17 deletions src/main/java/es/udc/fic/tic/nautilus/connection/ClientImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,48 @@ public void saveFileInNetwork(String filePath, int downloadLimit,
int index = 0;

for (NautilusMessage msg : msgs) {
String host = connectionUtilities.getHostAndBackupFromConfig().get(0);
int result = startClient(host, msg);

if (result == 1) {
// Save the host in the key file
nautilusKey.get(index).setHost(host);
List<String> serverPreferences = connectionUtilities.getHostAndBackupFromConfig();
for (String host : serverPreferences) {
//String host = serverPreferences.get(0);
int result = startClient(host, msg);

// Delete the split file
if (path != null) {
new File(path + "/"+ nautilusKey.get(index).getFileName()).delete();
} else {
new File(nautilusKey.get(index).getFileName()).delete();
}
} else {
//TODO: comprobar si se guardo bien, y si no probar con mas host de la config
String backup = connectionUtilities.getHostAndBackupFromConfig().get(1);
startClient(backup, msg);
if (result == 1) {
// Save the host in the key file
nautilusKey.get(index).setHost(host);

// Delete the split file
if (path != null) {
new File(path + "/"+ nautilusKey.get(index).getFileName()).delete();
} else {
new File(nautilusKey.get(index).getFileName()).delete();
}

// Check if exist one mirror (another server). if exist then copy the file
if (serverPreferences.size() > 1) {
int tmpIndex = 0;
for (String hostBackup : serverPreferences) {
if (hostBackup != host) {
int resultMirroring = startClient(hostBackup, msg);

if (resultMirroring == 1) {
// Success, now save the hostBackup in the keyFile
nautilusKey.get(index).setHostBackup(hostBackup);
break;
}

tmpIndex++;

if (tmpIndex == serverPreferences.size()) {
break;
}

}
}
}
break;
}
}

index++;
}
keysHandler.generateKeys(nautilusKey);
Expand Down Expand Up @@ -146,7 +170,13 @@ private int startClient(String ipAddress, NautilusMessage msgObject) throws Exce

Collection<PeerAddress> addressList = client.peerBean().peerMap().all();
System.out.println("=====> "+addressList.size());


// if can not connect with the server
if (addressList.size() == 0) {
client.shutdown();
return -1;
}

if (futureDiscover.isSuccess()) {
System.out.println("found that my outside address is " + futureDiscover.peerAddress());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;
import java.util.Random;

Expand Down Expand Up @@ -153,12 +154,10 @@ public List<NautilusMessage> prepareFileToSend(String filePath,

public List<String> getHostAndBackupFromConfig() {
List<String> preferences = configHandler.getConfig().getServerPreferences();
List<String> hostAndBackup = new ArrayList<String>();
Random randomizer = new Random();
for (int i = 0; i < 2; i++) {
hostAndBackup.add(preferences.get(randomizer.nextInt(preferences.size())));
}
return hostAndBackup;
long seed = System.nanoTime();

Collections.shuffle(preferences, new Random(seed));
return preferences;
}


Expand All @@ -179,7 +178,6 @@ public void restoreFile(List<File> files, List<NautilusKey> keys) throws Excepti
}

// Get the baseName for make the join operation
//TODO: revisar esto con un archivo "loquesea.pdf.0"
String[] baseNameArray = keys.get(0).getFileName().split("\\.");
String baseName = "";
index = 0;
Expand Down

0 comments on commit ef0e36b

Please sign in to comment.