sdis1819-t7g02 / channels / BackupChannel.java @ 1
History | View | Annotate | Download (3.37 KB)
1 |
package channels; |
---|---|
2 |
|
3 |
import java.io.IOException; |
4 |
|
5 |
import protocols.SendSoredMessageProtocol; |
6 |
import service.Cloud; |
7 |
import service.Constants; |
8 |
|
9 |
public class BackupChannel extends Channel { |
10 |
private Message receivedMessage;
|
11 |
|
12 |
public BackupChannel(String addr, String p, Cloud f) throws IOException { |
13 |
super(addr, p, f.getID(), f);
|
14 |
} |
15 |
|
16 |
public void printLog(String msg) { |
17 |
System.out.println("MDB " + serverId + msg); |
18 |
} |
19 |
|
20 |
// This message is used to ensure that the chunk is backed up with the desired replication degree as follows. The initiator-peer collects the confirmation messages during a time interval of one second. If the number of confirmation messages it received up to the end of that interval is lower than the desired replication degree, it retransmits the backup message on the MDB channel, and doubles the time interval for receiving confirmation messages. This procedure is repeated up to a maximum number of five times, i.e. the initiator will send at most 5 PUTCHUNK messages per chunk.
|
21 |
//
|
22 |
// Hint: Because UDP is not reliable, a peer that has stored a chunk must reply with a STORED message to every PUTCHUNK message it receives. Furthermore, the initiator-peer needs to keep track of which peers have responded.
|
23 |
//
|
24 |
// A peer should also count the number of confirmation messages for each of the chunks it has stored and keep that count in non-volatile memory. This information can be useful if the peer runs out of disk space: in that event, the peer may try to free some space by evicting chunks whose actual replication degree is higher than the desired replication degree.
|
25 |
|
26 |
@Override
|
27 |
public void run () { |
28 |
printLog(": UP");
|
29 |
int messageSenderId = -1; |
30 |
String messageType;
|
31 |
|
32 |
try {
|
33 |
joinChannelGroup(); |
34 |
} catch (IOException e1) { |
35 |
e1.printStackTrace(); |
36 |
} |
37 |
|
38 |
do {
|
39 |
try {
|
40 |
|
41 |
|
42 |
receivedMessage = captureData(); |
43 |
|
44 |
if(receivedMessage == null) { |
45 |
printLog(" passing message.");
|
46 |
continue;
|
47 |
} |
48 |
|
49 |
messageType = receivedMessage.getHeader().getMessageType(); |
50 |
|
51 |
messageSenderId = Integer.parseInt(receivedMessage.getHeader().getSenderId());
|
52 |
|
53 |
if(messageType.equals(Constants.PUTCHUNK) && messageSenderId != getServerId()) {
|
54 |
//printLog(" received: " + receivedMessage.getHeader().toString());
|
55 |
|
56 |
if(father.storeChunk(receivedMessage)) {
|
57 |
SendSoredMessageProtocol mp = new SendSoredMessageProtocol(buildMessage(), father);
|
58 |
|
59 |
mp.start(); |
60 |
} |
61 |
else {
|
62 |
printLog(" ERROR: couldn't store chunk");
|
63 |
} |
64 |
} |
65 |
else {
|
66 |
//printLog(" ERROR: Invalid message header received -> " + receivedMessage.getHeader().toString());
|
67 |
} |
68 |
|
69 |
// leaveGroupChannel();
|
70 |
} |
71 |
catch (IOException e) { |
72 |
printLog(" ERROR: " + "IOException -> " + e.getMessage()); |
73 |
} |
74 |
} while(true); |
75 |
} |
76 |
|
77 |
public MessageHeader buildMessage() {
|
78 |
MessageHeader response; |
79 |
|
80 |
response = buildResponseMessage(); |
81 |
|
82 |
return response;
|
83 |
} |
84 |
|
85 |
public MessageHeader buildResponseMessage() {
|
86 |
MessageHeader response; |
87 |
String[] headerElements = new String[Constants.STORED_N_ARGS]; |
88 |
|
89 |
headerElements[0] = Constants.STORED;
|
90 |
headerElements[1] = father.getProtocolVersion();
|
91 |
headerElements[2] = Integer.toString(father.getID()); |
92 |
headerElements[3] = receivedMessage.getHeader().getFileId();
|
93 |
headerElements[4] = receivedMessage.getHeader().getChunkNumber();
|
94 |
|
95 |
response = new MessageHeader(headerElements);
|
96 |
|
97 |
return response;
|
98 |
} |
99 |
} |