Project

General

Profile

Statistics
| Revision:

root / DistributedBackupService / src / handlers / BackupChunkHandler.java

History | View | Annotate | Download (2.51 KB)

1 1 up20130859
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 2 up20130859
        private Peer peer;
15
        private Chunk chunk;
16
        private final String protocolVersion;
17
        private AtomicIntegerArray replication;
18
        private int retries = 5;
19 1 up20130859
20 2 up20130859
        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 1 up20130859
27 2 up20130859
        BackupChunkHandler(Peer peer, Chunk chunk) {
28
                this.chunk = chunk;
29
                this.peer = peer;
30
                this.protocolVersion = Utils.VERSION;
31
                this.replication = null;
32
        }
33 1 up20130859
34 2 up20130859
        @Override
35
        public void run() {
36
                int waitTime = 1000;
37
                Message msg = generatePutChunkMsg(chunk, protocolVersion);
38 1 up20130859
39 2 up20130859
                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 1 up20130859
50 2 up20130859
                        try {
51
                                sleep(waitTime);
52
                        } catch (InterruptedException e) {
53
                                e.printStackTrace();
54
                        }
55 1 up20130859
56 2 up20130859
                        waitTime *= 2;
57
                }
58
        }
59 1 up20130859
60 2 up20130859
        protected boolean isReplicationtheWantedOne() {
61
                if(replication != null && replication.get(chunk.getChunkNo()) >= chunk.getReplication())
62
                        return true;
63
                else return false;
64
        }
65 1 up20130859
66 2 up20130859
        private void sleep(int waitTime) throws InterruptedException {
67
                Thread.sleep(waitTime);
68
        }
69 1 up20130859
70 2 up20130859
        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 1 up20130859
76 2 up20130859
        // Getters and Setters
77 1 up20130859
78 2 up20130859
        public Peer getPeer() {
79
                return peer;
80
        }
81 1 up20130859
82 2 up20130859
        public Chunk getChunk() {
83
                return chunk;
84
        }
85 1 up20130859
86 2 up20130859
        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 1 up20130859
}