Project

General

Profile

Statistics
| Revision:

root / DistributedBackupService / src / handlers / BackupChunkHandler.java

History | View | Annotate | Download (2.51 KB)

1
package handlers;
2

    
3
import server.Peer;
4
import sockets.Socket;
5
import utils.Utils;
6

    
7
import java.io.IOException;
8
import java.util.concurrent.atomic.AtomicIntegerArray;
9

    
10
import chunk.Chunk;
11
import protocols.BackupChunk;
12

    
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;
19

    
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
        }
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
        }
33

    
34
        @Override
35
        public void run() {
36
                int waitTime = 1000;
37
                Message msg = generatePutChunkMsg(chunk, protocolVersion);
38

    
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
                        }
49

    
50
                        try {
51
                                sleep(waitTime);
52
                        } catch (InterruptedException e) {
53
                                e.printStackTrace();
54
                        }
55

    
56
                        waitTime *= 2;
57
                }
58
        }
59

    
60
        protected boolean isReplicationtheWantedOne() {
61
                if(replication != null && replication.get(chunk.getChunkNo()) >= chunk.getReplication())
62
                        return true;
63
                else return false;
64
        }
65

    
66
        private void sleep(int waitTime) throws InterruptedException {
67
                Thread.sleep(waitTime);
68
        }
69

    
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
        }
75

    
76
        // Getters and Setters
77

    
78
        public Peer getPeer() {
79
                return peer;
80
        }
81

    
82
        public Chunk getChunk() {
83
                return chunk;
84
        }
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

    
114
}