root / src / Protocols / BackUpProtocol.java @ 2
History | View | Annotate | Download (3.41 KB)
1 | 2 | up20160340 | package Protocols; |
---|---|---|---|
2 | |||
3 | import Message.*; |
||
4 | import Peers.Peer; |
||
5 | import Storage.Chunk; |
||
6 | import Storage.ChunksManager; |
||
7 | |||
8 | import java.io.*; |
||
9 | import java.nio.file.Files; |
||
10 | import java.nio.file.Path; |
||
11 | import java.nio.file.Paths; |
||
12 | import java.nio.file.attribute.BasicFileAttributes; |
||
13 | import java.security.NoSuchAlgorithmException; |
||
14 | import java.util.ArrayList; |
||
15 | import java.util.List; |
||
16 | |||
17 | import static Global.Globals.MAX_TRIES; |
||
18 | |||
19 | |||
20 | public class BackUpProtocol implements Runnable { |
||
21 | |||
22 | int repDegree;
|
||
23 | Peer peer; |
||
24 | String filepath;
|
||
25 | ChunksManager chunks; |
||
26 | |||
27 | public BackUpProtocol(Peer peer, int repDegree, String filepath) throws IOException { |
||
28 | |||
29 | this.repDegree = repDegree;
|
||
30 | this.peer = peer;
|
||
31 | this.filepath = filepath;
|
||
32 | this.chunks = new ChunksManager(filepath, repDegree, peer.getId()); |
||
33 | } |
||
34 | |||
35 | String calculateFileId(Peer peer, String filepath) throws NoSuchAlgorithmException, IOException { |
||
36 | |||
37 | String[] split = filepath.split("\\."); |
||
38 | String ext = split[split.length - 1]; |
||
39 | |||
40 | Path path = Paths.get(filepath); |
||
41 | BasicFileAttributes metadata = Files.readAttributes(path, BasicFileAttributes.class); |
||
42 | FileInputStream in = new FileInputStream(filepath); |
||
43 | byte[] info = new byte[(int) metadata.size()]; |
||
44 | in.read(info); |
||
45 | |||
46 | String sha = peer.getStorage().createFileID(filepath, metadata, info);
|
||
47 | |||
48 | return sha;
|
||
49 | |||
50 | } |
||
51 | |||
52 | ArrayList<Message> constructMessages(List<Chunk> list, String fileID) { |
||
53 | ArrayList<Message> messages = new ArrayList<Message>(); |
||
54 | for (Chunk i : list) {
|
||
55 | Message msg = new PutChunkMsg(MessageType.PUTCHUNK, peer.getVersionProtocol(), peer.getId(), fileID, i.getId(), repDegree, i.getInfo());
|
||
56 | messages.add(msg); |
||
57 | } |
||
58 | |||
59 | return messages;
|
||
60 | } |
||
61 | |||
62 | @Override
|
||
63 | public void run() { |
||
64 | |||
65 | try {
|
||
66 | String id = calculateFileId(peer, filepath);
|
||
67 | String[] name = filepath.split("/"); |
||
68 | peer.getControl().getBackedUpFiles().put(name[name.length - 1], id);
|
||
69 | |||
70 | |||
71 | |||
72 | ArrayList<Message> messages = constructMessages(chunks.getChunks(), id);
|
||
73 | |||
74 | |||
75 | int i = 1; |
||
76 | int sleep = 500; |
||
77 | |||
78 | do {
|
||
79 | sleep *= 2;
|
||
80 | //send putchunks
|
||
81 | |||
82 | if (i > MAX_TRIES) {
|
||
83 | System.out.println("MAX TRIES ARCHIEVED!"); |
||
84 | return;
|
||
85 | } |
||
86 | for (Message k : messages) {
|
||
87 | peer.getMdb_channel().send(k); |
||
88 | } |
||
89 | i++; |
||
90 | } while (confirmMessage(messages, sleep));
|
||
91 | } catch (Exception e) { |
||
92 | e.printStackTrace(); |
||
93 | } |
||
94 | |||
95 | System.out.println("Backup over"); |
||
96 | |||
97 | } |
||
98 | |||
99 | |||
100 | boolean confirmMessage(ArrayList<Message> messages, int sleep) { |
||
101 | |||
102 | try {
|
||
103 | Thread.sleep(sleep);
|
||
104 | } catch (InterruptedException e) { |
||
105 | e.printStackTrace(); |
||
106 | } |
||
107 | |||
108 | try {
|
||
109 | for (Message i : messages) {
|
||
110 | String key = i.getChunkNo() + ";" + i.getFileID(); |
||
111 | Integer value = peer.getControl().getStoredMessages().get(key);
|
||
112 | |||
113 | if (value == null) { |
||
114 | return true; |
||
115 | } |
||
116 | |||
117 | if (i.getRepDegree() >= value) {
|
||
118 | messages.remove(i); |
||
119 | continue;
|
||
120 | } |
||
121 | |||
122 | return true; |
||
123 | } |
||
124 | |||
125 | |||
126 | } catch (Exception e) { |
||
127 | e.printStackTrace(); |
||
128 | } |
||
129 | return false; |
||
130 | |||
131 | } |
||
132 | } |