Revision 2
Updated some classes
DistributedBackupService/src/chunk/FileManager.java | ||
---|---|---|
38 | 38 |
this.chunkMap = chunksInfo; |
39 | 39 |
} |
40 | 40 |
|
41 |
public String getFileID() { |
|
42 |
return fileID; |
|
43 |
} |
|
44 |
|
|
45 |
public int getNumChunks() { |
|
46 |
return numChunks; |
|
47 |
} |
|
48 |
|
|
49 |
public String getPathname() { |
|
50 |
return pathName; |
|
51 |
} |
|
52 |
|
|
53 |
public String getFileName() { |
|
54 |
return fileName; |
|
55 |
} |
|
56 |
|
|
57 |
public int getWantedReplication() { |
|
58 |
return wantedReplication; |
|
59 |
} |
|
60 |
|
|
61 |
public HashMap<String, Chunk> getChunks() { |
|
62 |
return chunkMap; |
|
63 |
} |
|
64 |
|
|
65 | 41 |
synchronized public static FileManager loadDatabase(File file) throws ClassNotFoundException { |
66 | 42 |
FileManager db = null; |
67 | 43 |
|
... | ... | |
125 | 101 |
chunkRepository.remove(fileID); |
126 | 102 |
} |
127 | 103 |
|
128 |
public int getNumChunks(String pathname) { |
|
129 |
return fileRepository.get(pathname).getNumChunks(); |
|
130 |
} |
|
131 |
|
|
132 | 104 |
public Integer getRecognizedReplication(String fileID, int chunkNo) |
133 | 105 |
{ |
134 | 106 |
int ret; |
... | ... | |
141 | 113 |
return ret; |
142 | 114 |
} |
143 | 115 |
|
116 |
// Getters e Setters |
|
117 |
|
|
144 | 118 |
public boolean chunkExists(String fileID) { |
145 | 119 |
return chunkRepository.containsKey(fileID); |
146 | 120 |
} |
147 | 121 |
|
122 |
public ConcurrentMap<String, FileManager> getFileRepository() { |
|
123 |
return fileRepository; |
|
124 |
} |
|
125 |
|
|
126 |
public void setFileRepository(ConcurrentMap<String, FileManager> fileRepository) { |
|
127 |
this.fileRepository = fileRepository; |
|
128 |
} |
|
129 |
|
|
130 |
public ConcurrentMap<String, ConcurrentMap<Integer, Chunk>> getChunkRepository() { |
|
131 |
return chunkRepository; |
|
132 |
} |
|
133 |
|
|
134 |
public void setChunkRepository(ConcurrentMap<String, ConcurrentMap<Integer, Chunk>> chunkRepository) { |
|
135 |
this.chunkRepository = chunkRepository; |
|
136 |
} |
|
137 |
|
|
138 |
public String getPathName() { |
|
139 |
return pathName; |
|
140 |
} |
|
141 |
|
|
142 |
public void setPathName(String pathName) { |
|
143 |
this.pathName = pathName; |
|
144 |
} |
|
145 |
|
|
146 |
public HashMap<String, Chunk> getChunkMap() { |
|
147 |
return chunkMap; |
|
148 |
} |
|
149 |
|
|
150 |
public void setChunkMap(HashMap<String, Chunk> chunkMap) { |
|
151 |
this.chunkMap = chunkMap; |
|
152 |
} |
|
153 |
|
|
154 |
public void setFileID(String fileID) { |
|
155 |
this.fileID = fileID; |
|
156 |
} |
|
157 |
|
|
158 |
public void setFileName(String fileName) { |
|
159 |
this.fileName = fileName; |
|
160 |
} |
|
161 |
|
|
162 |
public void setNumChunks(int numChunks) { |
|
163 |
this.numChunks = numChunks; |
|
164 |
} |
|
165 |
|
|
166 |
public void setWantedReplication(int wantedReplication) { |
|
167 |
this.wantedReplication = wantedReplication; |
|
168 |
} |
|
169 |
|
|
148 | 170 |
public Set<Integer> getFileChunksKey(String fileID) { |
149 | 171 |
return chunkRepository.get(fileID).keySet(); |
150 | 172 |
} |
173 |
|
|
174 |
public String getFileID() { |
|
175 |
return fileID; |
|
176 |
} |
|
151 | 177 |
|
178 |
public int getNumChunks() { |
|
179 |
return numChunks; |
|
180 |
} |
|
181 |
|
|
182 |
public String getPathname() { |
|
183 |
return pathName; |
|
184 |
} |
|
185 |
|
|
186 |
public String getFileName() { |
|
187 |
return fileName; |
|
188 |
} |
|
189 |
|
|
190 |
public int getWantedReplication() { |
|
191 |
return wantedReplication; |
|
192 |
} |
|
193 |
|
|
194 |
public HashMap<String, Chunk> getChunks() { |
|
195 |
return chunkMap; |
|
196 |
} |
|
197 |
|
|
198 |
public int getNumChunks(String pathname) { |
|
199 |
return fileRepository.get(pathname).getNumChunks(); |
|
200 |
} |
|
201 |
|
|
152 | 202 |
} |
DistributedBackupService/src/chunk/FolderManager.java | ||
---|---|---|
38 | 38 |
File file = new File(name); |
39 | 39 |
file.mkdirs(); |
40 | 40 |
} |
41 |
|
|
42 |
public static ArrayList<Chunk> loadChunks(String path, int chunksNumber) throws IOException { |
|
43 |
ArrayList<Chunk> chunks = new ArrayList<>(); |
|
41 | 44 |
|
45 |
for (int i = 0; i <= chunksNumber; i++) { |
|
46 |
byte[] information = loadFile(new File(path + "/" + i)); |
|
47 |
Chunk chunk = new Chunk("", i, 1, information); |
|
48 |
chunks.add(chunk); |
|
49 |
} |
|
50 |
|
|
51 |
return chunks; |
|
52 |
} |
|
53 |
|
|
42 | 54 |
synchronized public static boolean backupFile(String name, String path, byte[] information) throws IOException { |
43 | 55 |
if (getFreeMemory() < information.length) { |
44 | 56 |
System.out.println("Without space to backup file"); |
... | ... | |
65 | 77 |
return information; |
66 | 78 |
} |
67 | 79 |
|
68 |
public static ArrayList<Chunk> loadChunks(String path, int chunksNumber) throws IOException { |
|
69 |
ArrayList<Chunk> chunks = new ArrayList<>(); |
|
70 |
|
|
71 |
for (int i = 0; i <= chunksNumber; i++) { |
|
72 |
byte[] information = loadFile(new File(path + "/" + i)); |
|
73 |
Chunk chunk = new Chunk("", i, 1, information); |
|
74 |
chunks.add(chunk); |
|
75 |
} |
|
76 |
|
|
77 |
return chunks; |
|
78 |
} |
|
79 |
|
|
80 | 80 |
public static ArrayList<Chunk> fileSplit(byte[] fileInformation, String fileID, int replication) { |
81 | 81 |
ArrayList<Chunk> chunks; |
82 | 82 |
int numChunks; |
... | ... | |
110 | 110 |
} |
111 | 111 |
|
112 | 112 |
|
113 |
public String getChunkPath(String fileID, int chunkNo) { |
|
114 |
return getChunksPath() + fileID + "/" + chunkNo; |
|
115 |
} |
|
116 |
|
|
117 | 113 |
public byte[] loadChunk(String fileID, int chunkNo) { |
118 | 114 |
byte[] chunkinformation = null; |
119 | 115 |
String chunkPath = getChunksPath() + fileID + "/" + chunkNo; |
... | ... | |
127 | 123 |
return chunkinformation; |
128 | 124 |
} |
129 | 125 |
|
130 |
|
|
131 |
public String getRootPath() { |
|
132 |
return path; |
|
133 |
} |
|
134 |
|
|
135 |
public String getChunksPath() { |
|
136 |
return path + Utils.CHUNKS; |
|
137 |
} |
|
138 |
|
|
139 |
public String getRestoresPath() { |
|
140 |
return path + Utils.RESTORES; |
|
141 |
} |
|
142 |
|
|
143 |
public FileManager getDatabase() { |
|
144 |
return fileManager; |
|
145 |
} |
|
146 |
|
|
147 | 126 |
public void deleteChunk(String fileID, int chunkNo) { |
148 |
String chunkPath = getChunkPath(fileID, chunkNo);
|
|
127 |
String chunkPath = getChunksPath() + fileID + "/" + chunkNo;
|
|
149 | 128 |
File file = new File(chunkPath); |
150 | 129 |
|
151 | 130 |
long chunkSize = file.length(); |
... | ... | |
154 | 133 |
fileManager.deleteChunk(fileID, chunkNo); |
155 | 134 |
} |
156 | 135 |
|
157 |
public static long getMaxMemory() { |
|
158 |
return maxMemory; |
|
159 |
} |
|
160 |
|
|
161 |
public static void setMaxMemory(int maxMemory) { |
|
162 |
FolderManager.maxMemory = maxMemory; |
|
163 |
} |
|
164 |
|
|
165 |
public static long getUsedMemory() { |
|
166 |
return FolderManager.usedMemory; |
|
167 |
} |
|
168 |
|
|
169 |
public static long getFreeMemory() { |
|
170 |
return maxMemory - usedMemory; |
|
171 |
} |
|
172 |
|
|
173 | 136 |
private static void decUsedMemory(long n) { |
174 | 137 |
usedMemory -= n; |
175 | 138 |
if (usedMemory < 0) { |
... | ... | |
187 | 150 |
System.out.println("Used memory: " + usedMemory + " / " + maxMemory); |
188 | 151 |
return true; |
189 | 152 |
} |
153 |
|
|
154 |
// Getters and setters |
|
155 |
|
|
156 |
public static long getMaxMemory() { return maxMemory; } |
|
157 |
|
|
158 |
public static void setMaxMemory(int maxMemory) { FolderManager.maxMemory = maxMemory; } |
|
159 |
|
|
160 |
public static long getUsedMemory() { return FolderManager.usedMemory; } |
|
161 |
|
|
162 |
public static long getFreeMemory() { return maxMemory - usedMemory; } |
|
163 |
|
|
164 |
public String getRootPath() { return path; } |
|
165 |
|
|
166 |
public String getChunksPath() { return path + Utils.CHUNKS; } |
|
167 |
|
|
168 |
public String getRestoresPath() { return path + Utils.RESTORES; } |
|
169 |
|
|
170 |
public FileManager getDatabase() { return fileManager; } |
|
171 |
|
|
172 |
public Peer getPeer() { return peer; } |
|
173 |
|
|
174 |
public void setPeer(Peer peer) { this.peer = peer; } |
|
175 |
|
|
176 |
public String getPath() { return path; } |
|
177 |
|
|
178 |
public void setPath(String path) { this.path = path; } |
|
179 |
|
|
180 |
public FileManager getFileManager() { return fileManager; } |
|
181 |
|
|
182 |
public void setFileManager(FileManager fileManager) { this.fileManager = fileManager; } |
|
183 |
|
|
184 |
public static int getChunkSizeLimit() { return chunkSizeLimit; } |
|
185 |
|
|
186 |
public static void setChunkSizeLimit(int chunkSizeLimit) { FolderManager.chunkSizeLimit = chunkSizeLimit; } |
|
187 |
|
|
188 |
public static void setMaxMemory(long maxMemory) { FolderManager.maxMemory = maxMemory; } |
|
189 |
|
|
190 |
public static void setUsedMemory(long usedMemory) { FolderManager.usedMemory = usedMemory; } |
|
190 | 191 |
} |
DistributedBackupService/src/client/TestApp.java | ||
---|---|---|
12 | 12 |
public class TestApp implements Runnable { |
13 | 13 |
|
14 | 14 |
private InitiatorPeer initiatorPeer; |
15 |
|
|
16 |
private String peer_ap; |
|
17 |
private String sub_protocol; |
|
18 |
private String opnd_1; |
|
19 |
private String opnd_2; |
|
20 | 15 |
|
21 |
private Map<String, Runnable> protocols; |
|
22 |
|
|
16 |
private String peer_ap; |
|
17 |
private String sub_protocol; |
|
18 |
private String opnd_1; |
|
19 |
private String opnd_2; |
|
23 | 20 |
|
24 |
public static void main(String[] args) { |
|
25 |
if (args.length < 2 || args.length > 4) { |
|
26 |
System.out.println("Usage: <java TestApp <peer_ap> <sub_protocol> <opnd_1> <opnd_2> >"); |
|
21 |
private Map<String, Runnable> protocols; |
|
22 |
|
|
23 |
|
|
24 |
public static void main(String[] args) { |
|
25 |
if (args.length < 2 || args.length > 4) { |
|
26 |
System.out.println("Usage: <java TestApp <peer_ap> <sub_protocol> <opnd_1> <opnd_2> >"); |
|
27 | 27 |
System.out.println("<peer_ap> - peer access point"); |
28 | 28 |
System.out.println("<sub_protocol> - BACKUP, RESTORE, DELETE, RECLAIM"); |
29 | 29 |
System.out.println("<opnd_1> - path name of the file to backup/restore/delete"); |
30 | 30 |
System.out.println("<opnd_2> - replication degree"); |
31 | 31 |
System.exit(1); |
32 |
} |
|
33 |
|
|
34 |
String peer_ap = args[0]; |
|
35 |
String sub_protocol = args[1]; |
|
36 |
String opnd_1; |
|
37 |
String opnd_2; |
|
38 |
|
|
39 |
if(args.length > 2) |
|
40 |
opnd_1 = args[2]; |
|
41 |
else |
|
42 |
opnd_1 = null; |
|
43 |
|
|
44 |
if(args.length > 3) |
|
45 |
opnd_2 = args[3]; |
|
46 |
else |
|
47 |
opnd_2 = null; |
|
32 |
} |
|
48 | 33 |
|
49 |
TestApp app = new TestApp(peer_ap, sub_protocol, opnd_1, opnd_2); |
|
50 |
new Thread(app).start(); |
|
51 |
} |
|
34 |
String peer_ap = args[0]; |
|
35 |
String sub_protocol = args[1]; |
|
36 |
String opnd_1; |
|
37 |
String opnd_2; |
|
52 | 38 |
|
53 |
public TestApp(String peer_ap, String sub_protocol, String opnd_1, String opnd_2) { |
|
54 |
protocols = new HashMap<>(); |
|
55 |
|
|
56 |
this.peer_ap = peer_ap; |
|
57 |
this.sub_protocol = sub_protocol; |
|
58 |
this.opnd_1 = opnd_1; |
|
59 |
this.opnd_2 = opnd_2; |
|
39 |
if(args.length > 2) |
|
40 |
opnd_1 = args[2]; |
|
41 |
else |
|
42 |
opnd_1 = null; |
|
60 | 43 |
|
61 |
protocols.put("BACKUP", this::handleBackup); |
|
62 |
protocols.put("DELETE", this::handleDelete); |
|
44 |
if(args.length > 3) |
|
45 |
opnd_2 = args[3]; |
|
46 |
else |
|
47 |
opnd_2 = null; |
|
63 | 48 |
|
64 |
} |
|
49 |
TestApp app = new TestApp(peer_ap, sub_protocol, opnd_1, opnd_2); |
|
50 |
new Thread(app).start(); |
|
51 |
} |
|
65 | 52 |
|
66 |
@Override |
|
67 |
public void run() { |
|
68 |
try { |
|
69 |
Registry registry = LocateRegistry.getRegistry(null); |
|
70 |
initiatorPeer = (InitiatorPeer) registry.lookup(peer_ap); |
|
71 |
} catch (Exception e) { |
|
72 |
System.out.println("Error when opening RMI stub"); |
|
73 |
e.printStackTrace(); |
|
74 |
} |
|
75 |
protocols.get(sub_protocol).run(); |
|
76 |
} |
|
77 |
|
|
78 |
private void handleBackup() { |
|
79 |
File file = new File(this.opnd_1); |
|
80 |
System.out.println("--> BACKUP :: Saving chunks at " + file.getAbsolutePath() + "\""); |
|
81 |
try { |
|
82 |
initiatorPeer.backup(file, Integer.parseInt(this.opnd_2)); |
|
83 |
} catch (RemoteException e) { |
|
84 |
e.printStackTrace(); |
|
85 |
} |
|
86 |
} |
|
53 |
public TestApp(String peer_ap, String sub_protocol, String opnd_1, String opnd_2) { |
|
54 |
protocols = new HashMap<>(); |
|
87 | 55 |
|
88 |
private void handleDelete() { |
|
89 |
System.out.println("--> DELETE :: delete file: " + opnd_1 + "\""); |
|
90 |
try { |
|
91 |
initiatorPeer.delete(this.opnd_1); |
|
92 |
} catch (RemoteException e) { |
|
93 |
e.printStackTrace(); |
|
94 |
} |
|
95 |
} |
|
56 |
this.peer_ap = peer_ap; |
|
57 |
this.sub_protocol = sub_protocol; |
|
58 |
this.opnd_1 = opnd_1; |
|
59 |
this.opnd_2 = opnd_2; |
|
60 |
|
|
61 |
protocols.put("BACKUP", () -> { |
|
62 |
try { |
|
63 |
backup(); |
|
64 |
} catch (NumberFormatException e) { |
|
65 |
e.printStackTrace(); |
|
66 |
} catch (RemoteException e) { |
|
67 |
e.printStackTrace(); |
|
68 |
} |
|
69 |
}); |
|
70 |
protocols.put("DELETE", () -> { |
|
71 |
try { |
|
72 |
delete(); |
|
73 |
} catch (RemoteException e) { |
|
74 |
e.printStackTrace(); |
|
75 |
} |
|
76 |
}); |
|
77 |
|
|
78 |
} |
|
79 |
|
|
80 |
@Override |
|
81 |
public void run() { |
|
82 |
try { |
|
83 |
Registry registry = LocateRegistry.getRegistry(null); |
|
84 |
initiatorPeer = (InitiatorPeer) registry.lookup(peer_ap); |
|
85 |
} catch (Exception e) { |
|
86 |
e.printStackTrace(); |
|
87 |
} |
|
88 |
protocols.get(sub_protocol).run(); |
|
89 |
} |
|
90 |
|
|
91 |
private void backup() throws NumberFormatException, RemoteException { |
|
92 |
File file = new File(this.opnd_1); |
|
93 |
System.out.println("--> BACKUP :: Saving chunks at " + file.getAbsolutePath() + "\""); |
|
94 |
initiatorPeer.backup(file, Integer.parseInt(this.opnd_2)); |
|
95 |
|
|
96 |
} |
|
97 |
|
|
98 |
private void delete() throws RemoteException { |
|
99 |
System.out.println("--> DELETE :: delete file: " + opnd_1 + "\""); |
|
100 |
initiatorPeer.delete(this.opnd_1); |
|
101 |
|
|
102 |
} |
|
96 | 103 |
} |
DistributedBackupService/src/handlers/BackupChunkHandler.java | ||
---|---|---|
11 | 11 |
import protocols.BackupChunk; |
12 | 12 |
|
13 | 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; |
|
14 | 19 |
|
15 |
private final String protocolVersion; |
|
16 |
private Peer parentPeer; |
|
17 |
private Chunk chunk; |
|
18 |
private AtomicIntegerArray chunkReplication; |
|
19 |
private int retries = 5; |
|
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 |
} |
|
20 | 26 |
|
21 |
public BackupChunkHandler(BackupChunk backupInitiator, Chunk chunk) {
|
|
22 |
this.chunk = chunk;
|
|
23 |
this.parentPeer = backupInitiator.getPeer();
|
|
24 |
this.protocolVersion = backupInitiator.getVersion();
|
|
25 |
this.chunkReplication = parentPeer.getPeerInformation().getChunkReplication(chunk.getFileID());
|
|
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 |
}
|
|
27 | 33 |
|
28 |
BackupChunkHandler(Peer parentPeer, Chunk chunk) { |
|
29 |
this.chunk = chunk; |
|
30 |
this.parentPeer = parentPeer; |
|
31 |
this.protocolVersion = Utils.VERSION; |
|
32 |
this.chunkReplication = null; |
|
33 |
} |
|
34 |
@Override |
|
35 |
public void run() { |
|
36 |
int waitTime = 1000; |
|
37 |
Message msg = generatePutChunkMsg(chunk, protocolVersion); |
|
34 | 38 |
|
35 |
@Override |
|
36 |
public void run() { |
|
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 |
} |
|
37 | 49 |
|
38 |
int waitTime = 1000; |
|
39 |
Message msg = generatePutChunkMsg(chunk, protocolVersion); |
|
50 |
try { |
|
51 |
sleep(waitTime); |
|
52 |
} catch (InterruptedException e) { |
|
53 |
e.printStackTrace(); |
|
54 |
} |
|
40 | 55 |
|
41 |
for (int i = 0; i < retries; ++i) { |
|
42 |
if (isDesiredReplicationDegree()) { |
|
43 |
System.out.println("Achieved desired replication at i=" + i); |
|
44 |
break; |
|
45 |
} |
|
56 |
waitTime *= 2; |
|
57 |
} |
|
58 |
} |
|
46 | 59 |
|
47 |
try {
|
|
48 |
parentPeer.sendMsg(Socket.SocketType.MDB, msg);
|
|
49 |
} catch (IOException e) {
|
|
50 |
System.out.println(e.getMessage());
|
|
51 |
}
|
|
60 |
protected boolean isReplicationtheWantedOne() {
|
|
61 |
if(replication != null && replication.get(chunk.getChunkNo()) >= chunk.getReplication())
|
|
62 |
return true;
|
|
63 |
else return false;
|
|
64 |
}
|
|
52 | 65 |
|
53 |
sleep(waitTime); |
|
54 |
waitTime *= 2; |
|
55 |
} |
|
56 |
} |
|
66 |
private void sleep(int waitTime) throws InterruptedException { |
|
67 |
Thread.sleep(waitTime); |
|
68 |
} |
|
57 | 69 |
|
58 |
protected boolean isDesiredReplicationDegree() { |
|
59 |
return chunkReplication != null && chunkReplication.get(chunk.getChunkNo()) >= chunk.getReplication(); |
|
60 |
} |
|
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 |
} |
|
61 | 75 |
|
62 |
private void sleep(int waitTime) { |
|
63 |
try { |
|
64 |
Thread.sleep(waitTime); |
|
65 |
} catch (InterruptedException e) { |
|
66 |
e.printStackTrace(); |
|
67 |
} |
|
68 |
} |
|
76 |
// Getters and Setters |
|
69 | 77 |
|
70 |
private Message generatePutChunkMsg(Chunk chunk, String protocolVersion) {
|
|
71 |
String[] args = { protocolVersion, Integer.toString(parentPeer.getID()), chunk.getFileID(), Integer.toString(chunk.getChunkNo()), Integer.toString(chunk.getReplication())
|
|
72 |
};
|
|
78 |
public Peer getPeer() {
|
|
79 |
return peer;
|
|
80 |
}
|
|
73 | 81 |
|
74 |
return new Message(Utils.MessageType.PUTCHUNK, args, chunk.getData()); |
|
75 |
} |
|
82 |
public Chunk getChunk() { |
|
83 |
return chunk; |
|
84 |
} |
|
76 | 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 |
|
|
77 | 114 |
} |
DistributedBackupService/src/handlers/Handler.java | ||
---|---|---|
10 | 10 |
import chunk.FileManager; |
11 | 11 |
|
12 | 12 |
public class Handler implements Runnable { |
13 |
private Peer parentPeer; |
|
13 |
private BlockingQueue<Message> msgQueue; |
|
14 |
private ScheduledExecutorService agent; |
|
15 |
private Peer peer; |
|
14 | 16 |
private Chunk peerInformation; |
15 |
private BlockingQueue<Message> msgQueue; |
|
16 |
private ScheduledExecutorService executor; |
|
17 |
|
|
17 | 18 |
private int delayLimit; |
18 | 19 |
|
19 | 20 |
private Random random; |
20 | 21 |
|
21 |
public Handler(Peer parentPeer) {
|
|
22 |
this.parentPeer = parentPeer;
|
|
23 |
this.peerInformation = parentPeer.getPeerInformation();
|
|
22 |
public Handler(Peer peer) { |
|
23 |
this.peer = peer;
|
|
24 |
this.peerInformation = peer.getPeerInformation(); |
|
24 | 25 |
msgQueue = new LinkedBlockingQueue<>(); |
25 |
executor = Executors.newScheduledThreadPool(5);
|
|
26 |
agent = Executors.newScheduledThreadPool(5);
|
|
26 | 27 |
delayLimit = 300; |
27 | 28 |
|
28 | 29 |
this.random = new Random(); |
... | ... | |
36 | 37 |
try { |
37 | 38 |
msg = msgQueue.take(); |
38 | 39 |
if (msg == null) { |
39 |
System.out.println("Null Message Received");
|
|
40 |
System.out.println("No msg received!");
|
|
40 | 41 |
return; |
41 | 42 |
} |
42 |
|
|
43 |
System.out.println("R: " + msg.toString()); |
|
44 |
|
|
43 |
System.out.println("R:: " + msg.toString()); |
|
45 | 44 |
switch (msg.getType()) { |
46 | 45 |
case PUTCHUNK: |
47 |
Backup backup = new Backup(parentPeer, msg);
|
|
48 |
executor.execute(backup);
|
|
46 |
Backup backup = new Backup(peer, msg); |
|
47 |
agent.execute(backup);
|
|
49 | 48 |
break; |
50 | 49 |
case STORED: |
51 | 50 |
peerInformation.addChunkReplication(msg.getFileID(), msg.getChunkNo()); |
52 | 51 |
break; |
53 |
case GETCHUNK: |
|
54 |
|
|
55 |
break; |
|
56 |
case CHUNK: |
|
57 |
|
|
58 |
break; |
|
59 | 52 |
case REMOVED: |
60 |
FileManager database = parentPeer.getDatabase();
|
|
53 |
FileManager database = peer.getDatabase(); |
|
61 | 54 |
String fileID = msg.getFileID(); |
62 | 55 |
int chunkNo = msg.getChunkNo(); |
63 | 56 |
|
... | ... | |
67 | 60 |
int targetReplication = chunkInfo.getReplication(); |
68 | 61 |
|
69 | 62 |
if (perceivedReplication < targetReplication) { |
70 |
byte[] chunkData = parentPeer.loadChunk(fileID, chunkNo);
|
|
63 |
byte[] chunkData = peer.loadChunk(fileID, chunkNo); |
|
71 | 64 |
|
72 |
executor.schedule(
|
|
73 |
new RemovedChunkHandler(parentPeer, chunkInfo, chunkData),
|
|
65 |
agent.schedule(
|
|
66 |
new RemovedChunkHandler(peer, chunkInfo, chunkData), |
|
74 | 67 |
this.random.nextInt(delayLimit + 1), |
75 | 68 |
TimeUnit.MILLISECONDS |
76 | 69 |
); |
77 | 70 |
} |
78 | 71 |
break; |
79 | 72 |
case DELETE: |
80 |
Delete delete = new Delete(parentPeer, msg);
|
|
81 |
executor.execute(delete);
|
|
73 |
Delete delete = new Delete(peer, msg); |
|
74 |
agent.execute(delete);
|
|
82 | 75 |
break; |
83 | 76 |
default: |
84 | 77 |
return; |
... | ... | |
89 | 82 |
} |
90 | 83 |
} |
91 | 84 |
|
92 |
|
|
93 | 85 |
public void pushMessage(byte[] data, int length) { |
94 | 86 |
Message msgParsed = new Message(data, length); |
95 | 87 |
msgQueue.add(msgParsed); |
96 | 88 |
} |
89 |
|
|
90 |
// Getters and setters |
|
91 |
|
|
92 |
public BlockingQueue<Message> getMsgQueue() { |
|
93 |
return msgQueue; |
|
94 |
} |
|
95 |
|
|
96 |
public ScheduledExecutorService getAgent() { |
|
97 |
return agent; |
|
98 |
} |
|
99 |
|
|
100 |
public Peer getPeer() { |
|
101 |
return peer; |
|
102 |
} |
|
103 |
|
|
104 |
public Chunk getPeerInformation() { |
|
105 |
return peerInformation; |
|
106 |
} |
|
107 |
|
|
108 |
public int getDelayLimit() { |
|
109 |
return delayLimit; |
|
110 |
} |
|
111 |
|
|
112 |
public Random getRandom() { |
|
113 |
return random; |
|
114 |
} |
|
115 |
|
|
116 |
public void setMsgQueue(BlockingQueue<Message> msgQueue) { |
|
117 |
this.msgQueue = msgQueue; |
|
118 |
} |
|
119 |
|
|
120 |
public void setAgent(ScheduledExecutorService agent) { |
|
121 |
this.agent = agent; |
|
122 |
} |
|
123 |
|
|
124 |
public void setPeer(Peer peer) { |
|
125 |
this.peer = peer; |
|
126 |
} |
|
127 |
|
|
128 |
public void setPeerInformation(Chunk peerInformation) { |
|
129 |
this.peerInformation = peerInformation; |
|
130 |
} |
|
131 |
|
|
132 |
public void setDelayLimit(int delayLimit) { |
|
133 |
this.delayLimit = delayLimit; |
|
134 |
} |
|
135 |
|
|
136 |
public void setRandom(Random random) { |
|
137 |
this.random = random; |
|
138 |
} |
|
97 | 139 |
} |
DistributedBackupService/src/handlers/Message.java | ||
---|---|---|
86 | 86 |
} |
87 | 87 |
|
88 | 88 |
private void translateHeader(String[] headerSplit) { |
89 |
|
|
90 | 89 |
switch (headerSplit[0]) { |
91 | 90 |
case "PUTCHUNK": |
92 | 91 |
{ |
... | ... | |
162 | 161 |
} |
163 | 162 |
} |
164 | 163 |
|
164 |
@Override |
|
165 |
public String toString() { |
|
166 |
String str; |
|
167 |
if(msgType == MessageType.PUTCHUNK) |
|
168 |
{ |
|
169 |
str = "<" + msgType +">" + " " + "<" + version+">" + " " + "<" + senderID+">" + " " +"<" + fileID+">" + " " + "<" +chunkNo+">"; |
|
170 |
} |
|
171 |
else if(msgType == MessageType.DELETE) |
|
172 |
{ |
|
173 |
str = "<" + msgType +">" + " " + "<" + version+">" + " " + "<" + senderID+">" + " " +"<" + fileID+">"; |
|
174 |
} |
|
175 |
else |
|
176 |
{ |
|
177 |
str = "<" + msgType +">" + " " + "<" + version+">" + " " + "<" + senderID+">" + " " +"<" + fileID+">" + " " + "<" +chunkNo+">"; |
|
178 |
} |
|
179 |
return str; |
|
180 |
} |
|
181 |
|
|
182 |
// Getters and Setters |
|
183 |
|
|
165 | 184 |
public String getVersion() { |
166 | 185 |
return version; |
167 | 186 |
} |
... | ... | |
190 | 209 |
return msgType; |
191 | 210 |
} |
192 | 211 |
|
193 |
@Override |
|
194 |
public String toString() { |
|
195 |
String str; |
|
196 |
if(msgType == MessageType.PUTCHUNK) |
|
197 |
{ |
|
198 |
str = "<" + msgType +">" + " " + "<" + version+">" + " " + "<" + senderID+">" + " " +"<" + fileID+">" + " " + "<" +chunkNo+">"; |
|
199 |
} |
|
200 |
else if(msgType == MessageType.DELETE) |
|
201 |
{ |
|
202 |
str = "<" + msgType +">" + " " + "<" + version+">" + " " + "<" + senderID+">" + " " +"<" + fileID+">"; |
|
203 |
} |
|
204 |
else |
|
205 |
{ |
|
206 |
str = "<" + msgType +">" + " " + "<" + version+">" + " " + "<" + senderID+">" + " " +"<" + fileID+">" + " " + "<" +chunkNo+">"; |
|
207 |
} |
|
208 |
return str; |
|
212 |
public int getArgsNo() { |
|
213 |
return argsNo; |
|
209 | 214 |
} |
210 | 215 |
|
216 |
public int getReplication() { |
|
217 |
return replication; |
|
218 |
} |
|
219 |
|
|
220 |
public MessageType getMsgType() { |
|
221 |
return msgType; |
|
222 |
} |
|
223 |
|
|
224 |
public void setArgsNo(int argsNo) { |
|
225 |
this.argsNo = argsNo; |
|
226 |
} |
|
227 |
|
|
228 |
public void setChunkNo(int chunkNo) { |
|
229 |
this.chunkNo = chunkNo; |
|
230 |
} |
|
231 |
|
|
232 |
public void setSenderID(int senderID) { |
|
233 |
this.senderID = senderID; |
|
234 |
} |
|
235 |
|
|
236 |
public void setFileID(String fileID) { |
|
237 |
this.fileID = fileID; |
|
238 |
} |
|
239 |
|
|
240 |
public void setVersion(String version) { |
|
241 |
this.version = version; |
|
242 |
} |
|
243 |
|
|
244 |
public void setReplication(int replication) { |
|
245 |
this.replication = replication; |
|
246 |
} |
|
247 |
|
|
248 |
public void setMsgType(MessageType msgType) { |
|
249 |
this.msgType = msgType; |
|
250 |
} |
|
251 |
|
|
252 |
public void setBody(byte[] body) { |
|
253 |
this.body = body; |
|
254 |
} |
|
255 |
|
|
211 | 256 |
} |
DistributedBackupService/src/handlers/RemovedChunkHandler.java | ||
---|---|---|
4 | 4 |
import server.Peer; |
5 | 5 |
|
6 | 6 |
public class RemovedChunkHandler extends BackupChunkHandler { |
7 |
private Chunk chunkInfo; |
|
7 |
private Chunk chunkInformation;
|
|
8 | 8 |
|
9 |
public RemovedChunkHandler(Peer parentPeer, Chunk chunkInfo, byte[] chunkData) {
|
|
10 |
super(parentPeer, new Chunk(chunkInfo, chunkData));
|
|
9 |
public RemovedChunkHandler(Peer peer, Chunk chunkInformation, byte[] chunkData) {
|
|
10 |
super(peer, new Chunk(chunkInformation, chunkData));
|
|
11 | 11 |
|
12 |
this.chunkInfo = chunkInfo;
|
|
12 |
this.chunkInformation = chunkInformation;
|
|
13 | 13 |
} |
14 | 14 |
|
15 | 15 |
@Override |
16 |
protected boolean isDesiredReplicationDegree() { |
|
17 |
return chunkInfo.getNumChunks() >= chunkInfo.getReplication(); |
|
16 |
protected boolean isReplicationtheWantedOne() { |
|
17 |
if(chunkInformation.getNumChunks() >= chunkInformation.getReplication()) |
|
18 |
return true; |
|
19 |
else |
|
20 |
return false; |
|
18 | 21 |
} |
22 |
|
|
23 |
// Getters and setters |
|
24 |
public Chunk getChunkInfo() { |
|
25 |
return chunkInformation; |
|
26 |
} |
|
27 |
|
|
28 |
public void setChunkInfo(Chunk chunkInfo) { |
|
29 |
this.chunkInformation = chunkInfo; |
|
30 |
} |
|
19 | 31 |
} |
Also available in: Unified diff