Skip to content

Commit

Permalink
Isolated sending of putchunk command as runnable function
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelribeiro1510 committed Apr 10, 2021
1 parent 39d7dd9 commit b123442
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
2 changes: 1 addition & 1 deletion proj1/src/Channel.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private void processPacket(DatagramPacket receivedPacket) {
String command = splitHeader[1];
String fileID = splitHeader[3];
int chunkNr = splitHeader.length >= 5 ? Integer.parseInt(splitHeader[4]) : 0;
int desiredRepDegree = splitHeader.length == 6 ? Integer.parseInt(splitHeader[5]) : 0;
int desiredRepDegree = splitHeader.length == 6 ? Integer.parseInt(splitHeader[5]) : -1;

Chunk newChunk = new Chunk(fileID, chunkNr, desiredRepDegree);
switch (command) {
Expand Down
6 changes: 3 additions & 3 deletions proj1/src/Chunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@ public class Chunk implements Serializable {
public Chunk(String fileID, int chunkNumber, int desiredReplicationDegree, byte[] content){
this.fileID = fileID;
this.chunkNumber = chunkNumber;
this.desiredReplicationDegree = desiredReplicationDegree;
this.perceivedReplicationDegree = 0;
this.content = content;
this.desiredReplicationDegree = desiredReplicationDegree;
}

public Chunk(String fileID, int chunkNumber, int desiredReplicationDegree){
this.fileID = fileID;
this.chunkNumber = chunkNumber;
this.desiredReplicationDegree = desiredReplicationDegree;
this.perceivedReplicationDegree = 0;
this.content = null;
this.desiredReplicationDegree = desiredReplicationDegree;
}

public Chunk(String fileID, int chunkNumber) {
this.fileID = fileID;
this.chunkNumber = chunkNumber;
this.desiredReplicationDegree = -1;
this.perceivedReplicationDegree = 0;
this.content = null;
this.desiredReplicationDegree = -1;
}

public String getFileID() {
Expand Down
27 changes: 15 additions & 12 deletions proj1/src/Peer.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class Peer implements RemoteInterface {
*/
public static String serviceDirectory;

private String protocolVersion;
private static String protocolVersion;
private static int peerID;
public static String accessPoint;

Expand Down Expand Up @@ -91,22 +91,25 @@ public synchronized void backup(String filepath, int replicationDegree) throws E
}

for (Chunk chunk : fileObject.getChunks()) {
initiatePUTCHUNK(fileObject.getFileID(), chunk);
}

String dataHeader = this.protocolVersion + " PUTCHUNK " + peerID + " " + fileObject.getFileID() + " " + chunk.getChunkNumber() + " " + replicationDegree + " " + "\r\n" + "\r\n";
System.out.println(dataHeader);
fileStorage.initiateBackup(fileObject);
FileStorage.saveToDisk();
}

byte[] fullMessage = new byte[dataHeader.length() + chunk.getContent().length];
System.arraycopy(dataHeader.getBytes(), 0, fullMessage,0, dataHeader.getBytes().length);
System.arraycopy(chunk.getContent(), 0, fullMessage, dataHeader.getBytes().length, chunk.getContent().length);
public static void initiatePUTCHUNK(String fileID, Chunk chunk) {
String dataHeader = protocolVersion + " PUTCHUNK " + peerID + " " + fileID + " " + chunk.getChunkNumber() + " " + chunk.getDesiredReplicationDegree() + " " + "\r\n" + "\r\n";
System.out.println(dataHeader);

MDB.sendMessage(fullMessage);
byte[] fullMessage = new byte[dataHeader.length() + chunk.getContent().length];
System.arraycopy(dataHeader.getBytes(), 0, fullMessage,0, dataHeader.getBytes().length);
System.arraycopy(chunk.getContent(), 0, fullMessage, dataHeader.getBytes().length, chunk.getContent().length);

System.out.println("Entering Check Rep Degree -> Chunk nr. " + chunk.getChunkNumber());
Peer.getExec().schedule(new CheckReplicationDegree(fullMessage, chunk), 1, TimeUnit.SECONDS);
}
MDB.sendMessage(fullMessage);

fileStorage.initiateBackup(fileObject);
FileStorage.saveToDisk();
System.out.println("Entering Check Rep Degree -> Chunk nr. " + chunk.getChunkNumber());
Peer.getExec().schedule(new CheckReplicationDegree(fullMessage, chunk), 1, TimeUnit.SECONDS);
}

public void restore(String filepath) {
Expand Down
12 changes: 11 additions & 1 deletion proj1/src/Reclaim.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import java.util.Random;
import java.util.concurrent.TimeUnit;

public class Reclaim {


Expand All @@ -16,8 +19,15 @@ public static void processPacketREMOVED(String[] splitHeader) {
fileStorage.removeBackedPeer(chunkToSearch, peerId);

// check if new count is lower than desired replication degree
if (fileStorage.getPerceivedReplicationDegree(chunkToSearch) < chunkToSearch.getDesiredReplicationDegree()) {
var chunkInThisPeerOpt = fileStorage.findChunk(chunkToSearch);
if (chunkInThisPeerOpt.isEmpty()){
System.out.println("Something went wrong in chunk nr. " + chunkNr);
return;
}

if (fileStorage.getPerceivedReplicationDegree(chunkToSearch) < chunkInThisPeerOpt.get().getDesiredReplicationDegree()) {
int rand = new Random().nextInt(401);
Peer.getExec().schedule(() -> Peer.initiatePUTCHUNK(fileId, chunkInThisPeerOpt.get()), rand, TimeUnit.MILLISECONDS);
}

// if so, random delay (0-400ms) and then initiate Backup
Expand Down

0 comments on commit b123442

Please sign in to comment.