root / DistributedBackupService / src / protocols / BackupChunk.java
History | View | Annotate | Download (2.22 KB)
1 | 1 | up20130859 | package protocols; |
---|---|---|---|
2 | |||
3 | import server.Peer; |
||
4 | import utils.Utils; |
||
5 | |||
6 | import static chunk.FolderManager.fileSplit; |
||
7 | |||
8 | import java.io.File; |
||
9 | import java.util.HashMap; |
||
10 | import java.io.IOException; |
||
11 | import java.nio.file.Files; |
||
12 | import java.util.ArrayList; |
||
13 | import java.io.FileNotFoundException; |
||
14 | import java.nio.file.attribute.BasicFileAttributes; |
||
15 | import java.security.NoSuchAlgorithmException; |
||
16 | |||
17 | import chunk.Chunk; |
||
18 | import chunk.FileManager; |
||
19 | import chunk.FolderManager; |
||
20 | import handlers.BackupChunkHandler; |
||
21 | |||
22 | public class BackupChunk implements Runnable { |
||
23 | |||
24 | private String fileID; |
||
25 | private Peer peer;
|
||
26 | private String version; |
||
27 | |||
28 | private byte[] data; |
||
29 | private int replication; |
||
30 | private File file; |
||
31 | |||
32 | |||
33 | public BackupChunk(String version, File file, int replication, Peer peer) { |
||
34 | this.version = version;
|
||
35 | this.file = file;
|
||
36 | this.replication = replication;
|
||
37 | this.peer = peer;
|
||
38 | } |
||
39 | |||
40 | |||
41 | public Peer getPeer() {
|
||
42 | return peer;
|
||
43 | } |
||
44 | |||
45 | public String getVersion() { |
||
46 | return version;
|
||
47 | } |
||
48 | |||
49 | @Override
|
||
50 | public void run() { |
||
51 | try {
|
||
52 | data = FolderManager.loadFile(file); |
||
53 | } catch (IOException e) { |
||
54 | e.printStackTrace(); |
||
55 | } |
||
56 | |||
57 | try {
|
||
58 | fileID = hashFileID(file); |
||
59 | } catch (NoSuchAlgorithmException e) { |
||
60 | e.printStackTrace(); |
||
61 | } |
||
62 | |||
63 | ArrayList<Chunk> chunks;
|
||
64 | chunks = fileSplit(data, fileID, replication); |
||
65 | |||
66 | HashMap<String, Chunk> chunksInfo = new HashMap<>(); |
||
67 | |||
68 | peer.getPeerInformation().startChunkReplication(fileID, chunks.size()); |
||
69 | |||
70 | for (Chunk chunk : chunks) {
|
||
71 | new Thread(new BackupChunkHandler(this, chunk)).start(); |
||
72 | chunksInfo.put(Integer.toString(chunk.getChunkNo()), new Chunk(chunk.getChunkNo(), chunk.getReplication())); |
||
73 | } |
||
74 | |||
75 | peer.addFileToFileManager(file.getPath(), new FileManager(file, fileID, replication, chunksInfo));
|
||
76 | peer.getPeerInformation().resetChunkReplication(fileID); |
||
77 | |||
78 | System.out.println("Finished backupInitiator!"); |
||
79 | } |
||
80 | |||
81 | private String hashFileID(File file) throws NoSuchAlgorithmException { |
||
82 | BasicFileAttributes attributes; |
||
83 | try {
|
||
84 | attributes = Files.readAttributes(file.toPath(), BasicFileAttributes.class); |
||
85 | } catch (IOException e) { |
||
86 | System.out.println("Couldn't read file's metadata: " + e.getMessage()); |
||
87 | return null; |
||
88 | } |
||
89 | |||
90 | String fileId = file.getName() + attributes.lastModifiedTime() + attributes.size();
|
||
91 | return Utils.hash(fileId);
|
||
92 | } |
||
93 | |||
94 | } |