root / project / src / main / java / protocols / Restore.java @ 1
History | View | Annotate | Download (3.6 KB)
1 |
package main.java.protocols; |
---|---|
2 |
|
3 |
import main.java.file.FileChunk; |
4 |
import main.java.file.FileChunkID; |
5 |
import main.java.file.FileID; |
6 |
import main.java.listeners.Broker; |
7 |
import main.java.peer.Peer; |
8 |
|
9 |
import java.io.*; |
10 |
import java.util.ArrayList; |
11 |
|
12 |
import static main.java.utils.Constants.*; |
13 |
import static main.java.utils.Utilities.*; |
14 |
|
15 |
public class Restore implements Runnable { |
16 |
|
17 |
private File file; |
18 |
|
19 |
|
20 |
public Restore(File file) { |
21 |
this.file = file;
|
22 |
} |
23 |
|
24 |
@Override
|
25 |
public void run() { |
26 |
byte[] fileData = new byte[0]; |
27 |
|
28 |
|
29 |
String finalName = file.getName();
|
30 |
|
31 |
FileID fID = new FileID(sha256(file.getName()), -1); |
32 |
String filename = fID.toString();
|
33 |
|
34 |
|
35 |
Peer.restoring = true;
|
36 |
|
37 |
|
38 |
|
39 |
System.out.println("\t Restoring file: " + filename + "\n"); |
40 |
Peer.getMDRListener().chunksReceived.put(filename, new ArrayList<>()); |
41 |
ArrayList<FileChunk> chunks = new ArrayList<>(); |
42 |
|
43 |
ArrayList<FileChunk> fileChunks;
|
44 |
|
45 |
int fileParts = Peer.getDb().getNumChunksOfFile(fID);
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
for (int i = 0; i < fileParts; i++) { |
51 |
|
52 |
FileChunkID chunkID = new FileChunkID(filename, i);
|
53 |
|
54 |
|
55 |
//send get chunk
|
56 |
Broker.sendGETCHUNK(chunkID); |
57 |
|
58 |
//receive chunk
|
59 |
fileChunks = Peer.getMDRListener().chunksReceived.get(filename); |
60 |
|
61 |
|
62 |
FileChunk chunkAUX = fileChunks.isEmpty() ? null : Peer.getMDRListener().chunksReceived.get(filename).remove(0); |
63 |
|
64 |
while (chunkAUX == null) { |
65 |
try {
|
66 |
Thread.sleep(500); |
67 |
} catch (InterruptedException e) { |
68 |
e.printStackTrace(); |
69 |
} |
70 |
|
71 |
chunkAUX = fileChunks.isEmpty() ? null : Peer.getMDRListener().chunksReceived.get(filename).remove(0); |
72 |
} |
73 |
|
74 |
chunks.add(chunkAUX); |
75 |
|
76 |
|
77 |
//System.out.println("DATABASE : ");
|
78 |
//Peer.getDb().printDatabase();
|
79 |
|
80 |
} |
81 |
|
82 |
|
83 |
|
84 |
|
85 |
for (int i = 0; i < fileParts; i++) { |
86 |
FileChunk chunkTmp = null;
|
87 |
ArrayList<FileChunk> chunkAUX = null; |
88 |
for(FileChunk chunk : chunks) {
|
89 |
if(chunk.getChunkNo() == i) {
|
90 |
chunkTmp = chunk; |
91 |
break;
|
92 |
} |
93 |
} |
94 |
|
95 |
if(chunkTmp == null) { |
96 |
System.out.println("Missing chunk file!!"); |
97 |
} else {
|
98 |
try {
|
99 |
fileData = concatBytes(fileData, chunkTmp.getChunkData()); |
100 |
} catch (IOException e) { |
101 |
e.printStackTrace(); |
102 |
} |
103 |
|
104 |
} |
105 |
|
106 |
File dir = new File("peer"+Peer.getID()+"/restored/"); |
107 |
|
108 |
if(!dir.exists()) {
|
109 |
dir.mkdirs(); |
110 |
} |
111 |
|
112 |
FileOutputStream out = null; |
113 |
try {
|
114 |
out = new FileOutputStream("peer"+Peer.getID()+"/restored/" + finalName); |
115 |
|
116 |
} catch (FileNotFoundException e) { |
117 |
e.printStackTrace(); |
118 |
|
119 |
} |
120 |
try {
|
121 |
assert out != null; |
122 |
out.write(fileData); |
123 |
|
124 |
} catch (IOException e) { |
125 |
e.printStackTrace(); |
126 |
|
127 |
} |
128 |
try {
|
129 |
out.close(); |
130 |
|
131 |
} catch (IOException e) { |
132 |
e.printStackTrace(); |
133 |
|
134 |
} |
135 |
|
136 |
|
137 |
} |
138 |
|
139 |
|
140 |
Peer.restoring = false;
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
} |
146 |
|
147 |
private static byte[] concatBytes(byte[] a, byte[] b) throws IOException { |
148 |
|
149 |
|
150 |
ByteArrayOutputStream outputStream = new ByteArrayOutputStream( ); |
151 |
outputStream.write( a ); |
152 |
outputStream.write( b ); |
153 |
|
154 |
byte result[] = outputStream.toByteArray( ); |
155 |
|
156 |
return result;
|
157 |
} |
158 |
} |