Revision 2
Updated some classes
BackupChunkHandler.java | ||
---|---|---|
11 | 11 |
import protocols.BackupChunk; |
12 | 12 |
|
13 | 13 |
public class BackupChunkHandler implements Runnable { |
14 |
private Peer peer; |
|
15 |
private Chunk chunk; |
|
16 |
private final String protocolVersion; |
|
17 |
private AtomicIntegerArray replication; |
|
18 |
private int retries = 5; |
|
14 | 19 |
|
15 |
private final String protocolVersion; |
|
16 |
private Peer parentPeer; |
|
17 |
private Chunk chunk; |
|
18 |
private AtomicIntegerArray chunkReplication; |
|
19 |
private int retries = 5; |
|
20 |
public BackupChunkHandler(BackupChunk backupChunk, Chunk chunk) { |
|
21 |
this.chunk = chunk; |
|
22 |
this.peer = backupChunk.getPeer(); |
|
23 |
this.protocolVersion = backupChunk.getVersion(); |
|
24 |
this.replication = peer.getPeerInformation().getChunkReplication(chunk.getFileID()); |
|
25 |
} |
|
20 | 26 |
|
21 |
public BackupChunkHandler(BackupChunk backupInitiator, Chunk chunk) {
|
|
22 |
this.chunk = chunk;
|
|
23 |
this.parentPeer = backupInitiator.getPeer();
|
|
24 |
this.protocolVersion = backupInitiator.getVersion();
|
|
25 |
this.chunkReplication = parentPeer.getPeerInformation().getChunkReplication(chunk.getFileID());
|
|
26 |
}
|
|
27 |
BackupChunkHandler(Peer peer, Chunk chunk) {
|
|
28 |
this.chunk = chunk;
|
|
29 |
this.peer = peer;
|
|
30 |
this.protocolVersion = Utils.VERSION;
|
|
31 |
this.replication = null;
|
|
32 |
}
|
|
27 | 33 |
|
28 |
BackupChunkHandler(Peer parentPeer, Chunk chunk) { |
|
29 |
this.chunk = chunk; |
|
30 |
this.parentPeer = parentPeer; |
|
31 |
this.protocolVersion = Utils.VERSION; |
|
32 |
this.chunkReplication = null; |
|
33 |
} |
|
34 |
@Override |
|
35 |
public void run() { |
|
36 |
int waitTime = 1000; |
|
37 |
Message msg = generatePutChunkMsg(chunk, protocolVersion); |
|
34 | 38 |
|
35 |
@Override |
|
36 |
public void run() { |
|
39 |
for (int i = 0; i < retries; ++i) { |
|
40 |
if (isReplicationtheWantedOne() == true) { |
|
41 |
System.out.println("Achieved desired replication at i=" + i); |
|
42 |
break; |
|
43 |
} |
|
44 |
try { |
|
45 |
peer.sendMsg(Socket.SocketType.MDB, msg); |
|
46 |
} catch (IOException e1) { |
|
47 |
e1.printStackTrace(); |
|
48 |
} |
|
37 | 49 |
|
38 |
int waitTime = 1000; |
|
39 |
Message msg = generatePutChunkMsg(chunk, protocolVersion); |
|
50 |
try { |
|
51 |
sleep(waitTime); |
|
52 |
} catch (InterruptedException e) { |
|
53 |
e.printStackTrace(); |
|
54 |
} |
|
40 | 55 |
|
41 |
for (int i = 0; i < retries; ++i) { |
|
42 |
if (isDesiredReplicationDegree()) { |
|
43 |
System.out.println("Achieved desired replication at i=" + i); |
|
44 |
break; |
|
45 |
} |
|
56 |
waitTime *= 2; |
|
57 |
} |
|
58 |
} |
|
46 | 59 |
|
47 |
try {
|
|
48 |
parentPeer.sendMsg(Socket.SocketType.MDB, msg);
|
|
49 |
} catch (IOException e) {
|
|
50 |
System.out.println(e.getMessage());
|
|
51 |
}
|
|
60 |
protected boolean isReplicationtheWantedOne() {
|
|
61 |
if(replication != null && replication.get(chunk.getChunkNo()) >= chunk.getReplication())
|
|
62 |
return true;
|
|
63 |
else return false;
|
|
64 |
}
|
|
52 | 65 |
|
53 |
sleep(waitTime); |
|
54 |
waitTime *= 2; |
|
55 |
} |
|
56 |
} |
|
66 |
private void sleep(int waitTime) throws InterruptedException { |
|
67 |
Thread.sleep(waitTime); |
|
68 |
} |
|
57 | 69 |
|
58 |
protected boolean isDesiredReplicationDegree() { |
|
59 |
return chunkReplication != null && chunkReplication.get(chunk.getChunkNo()) >= chunk.getReplication(); |
|
60 |
} |
|
70 |
private Message generatePutChunkMsg(Chunk chunk, String protocolVersion) { |
|
71 |
String[] args = { protocolVersion, Integer.toString(peer.getID()), chunk.getFileID(), Integer.toString(chunk.getChunkNo()), Integer.toString(chunk.getReplication()) }; |
|
72 |
|
|
73 |
return new Message(Utils.MessageType.PUTCHUNK, args, chunk.getData()); |
|
74 |
} |
|
61 | 75 |
|
62 |
private void sleep(int waitTime) { |
|
63 |
try { |
|
64 |
Thread.sleep(waitTime); |
|
65 |
} catch (InterruptedException e) { |
|
66 |
e.printStackTrace(); |
|
67 |
} |
|
68 |
} |
|
76 |
// Getters and Setters |
|
69 | 77 |
|
70 |
private Message generatePutChunkMsg(Chunk chunk, String protocolVersion) {
|
|
71 |
String[] args = { protocolVersion, Integer.toString(parentPeer.getID()), chunk.getFileID(), Integer.toString(chunk.getChunkNo()), Integer.toString(chunk.getReplication())
|
|
72 |
};
|
|
78 |
public Peer getPeer() {
|
|
79 |
return peer;
|
|
80 |
}
|
|
73 | 81 |
|
74 |
return new Message(Utils.MessageType.PUTCHUNK, args, chunk.getData()); |
|
75 |
} |
|
82 |
public Chunk getChunk() { |
|
83 |
return chunk; |
|
84 |
} |
|
76 | 85 |
|
86 |
public String getProtocolVersion() { |
|
87 |
return protocolVersion; |
|
88 |
} |
|
89 |
|
|
90 |
public AtomicIntegerArray getReplication() { |
|
91 |
return replication; |
|
92 |
} |
|
93 |
|
|
94 |
public int getRetries() { |
|
95 |
return retries; |
|
96 |
} |
|
97 |
|
|
98 |
public void setPeer(Peer peer) { |
|
99 |
this.peer = peer; |
|
100 |
} |
|
101 |
|
|
102 |
public void setChunk(Chunk chunk) { |
|
103 |
this.chunk = chunk; |
|
104 |
} |
|
105 |
|
|
106 |
public void setReplication(AtomicIntegerArray replication) { |
|
107 |
this.replication = replication; |
|
108 |
} |
|
109 |
|
|
110 |
public void setRetries(int retries) { |
|
111 |
this.retries = retries; |
|
112 |
} |
|
113 |
|
|
77 | 114 |
} |
Also available in: Unified diff