Skip to content

Commit

Permalink
Fixed backup
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelribeiro1510 committed Apr 12, 2021
1 parent 9ff51ad commit 19d96c6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
7 changes: 4 additions & 3 deletions proj1/src/Backup.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ private static void sendSTOREDMessage(byte[] storedMessage, Chunk chunk) {
}
}

fileStorage.storeChunk(chunk);
Peer.getMC().sendMessage(storedMessage);
System.out.println("Sending STORED ->" + chunk.getChunkNumber());
if (fileStorage.storeChunk(chunk)){
Peer.getMC().sendMessage(storedMessage);
System.out.println("Sending STORED ->" + chunk.getChunkNumber());
}
}

public static void processPacketSTORED(Chunk chunk, String[] splitHeader) {
Expand Down
15 changes: 9 additions & 6 deletions proj1/src/FileStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,23 @@ public FileStorage() throws IOException {
}

/**
* Tries to store new chunk as a file locally,
* @param chunk New chunk
* Tries to store new chunk as a file locally, incrementing rep degree either it is stored or not
* @param chunk
* @return
*/
public void storeChunk(Chunk chunk) {
addStoredChunk(chunk);
public boolean storeChunk(Chunk chunk) {
if(!addStoredChunk(chunk)) return false;
incrementReplicationDegree(chunk);

FileOutputStream fos;
try {
fos = new FileOutputStream( chunksDir + "/" + chunk.getChunkID());
fos.write(chunk.getContent());
fos.close();
return true;
} catch (IOException e) {
System.out.println("Error writing chunk to file locally");
return false;
}
}

Expand Down Expand Up @@ -208,15 +211,15 @@ public void initiateBackup(FileObject file) {
* Adds stored chunk to peers knowledge, taking into account the chunks perceived replication degree if its already in the system
* @param chunk
*/
private void addStoredChunk(Chunk chunk){
private boolean addStoredChunk(Chunk chunk){
if(allChunks.contains(chunk)){
for (Chunk c : allChunks) {
if (c.equals(chunk)) {
chunk.setPerceivedReplicationDegree(c.getPerceivedReplicationDegree());
}
}
}
storedChunks.add(chunk);
return storedChunks.add(chunk);
}

/**
Expand Down

0 comments on commit 19d96c6

Please sign in to comment.