Project

General

Profile

Statistics
| Revision:

sdis1819-t7g02 / protocols / recoverChunkRepDegreeProtocol.java

History | View | Annotate | Download (1.2 KB)

1
package protocols;
2

    
3
import java.util.Random;
4

    
5
import channels.Message;
6
import service.Cloud;
7
import service.Constants;
8

    
9
public class recoverChunkRepDegreeProtocol extends Thread {
10
        Cloud father;
11
        Message toSend;
12
        String fileId;
13
        String chunkNo;
14

    
15
        public recoverChunkRepDegreeProtocol(Cloud c, Message m) {
16
                father = c;
17
                toSend = m;
18
                fileId = m.getHeader().getFileId();
19
                chunkNo = m.getHeader().getChunkNumber();
20
        }
21
        
22
        @Override
23
        public void run () {
24
                int numberOfTries = 0, multiplier = 1;
25
                
26
                try {
27
                        waitRandomTime();
28
                        while(numberOfTries < Constants.MAX_PUTCHUNK_MESSAGES) {
29
                                father.sendBackupMessage(toSend);
30
                                
31
                                Thread.sleep(1000 * multiplier);
32
                                
33
                                if(father.getChunkCurrentRepDegree(fileId, chunkNo) >= Integer.parseInt(toSend.getHeader().getDegree())) {
34
                                        System.out.println("replication degree for " + chunkNo + " restored.");
35
                                        return;
36
                                }
37
                                
38
                                multiplier *= 2;
39
                                numberOfTries++;
40
                        }                        
41
                }
42
                catch(InterruptedException e) {
43
                        System.out.println(e.getMessage());
44
                        return;
45
                }
46
        }
47
        
48
        public void waitRandomTime() throws InterruptedException {
49
                Random r = new Random();
50
                int low = 1;
51
                int high = 401;
52
                int result = r.nextInt(high-low) + low;
53
                
54
                Thread.sleep(result);
55
        }
56
}