50 |
50 |
// State Restore
|
51 |
51 |
public boolean restoring;
|
52 |
52 |
public AtomicBoolean waitRestoreReply = new AtomicBoolean(false);
|
53 |
|
public ConcurrentHashMap<String, Chunk> recoveredChunks = new ConcurrentHashMap<String, Chunk>(); // ChunkNo -> Chunk
|
|
53 |
public ConcurrentHashMap<String, Chunk> recoveredChunks = new ConcurrentHashMap<String, Chunk>(); // ChunkNo ->
|
|
54 |
// Chunk
|
54 |
55 |
|
55 |
56 |
// Service Management Information
|
56 |
57 |
public ConcurrentHashMap<String, Chunk> chunks = new ConcurrentHashMap<String, Chunk>(); // fileId-ChunkNo
|
... | ... | |
85 |
86 |
// Backup Protocol
|
86 |
87 |
public void backup(String path, int replications) throws RemoteException {
|
87 |
88 |
|
88 |
|
// TODO is file backed up already?
|
89 |
|
|
90 |
89 |
// Start backup
|
91 |
90 |
this.doingBackUp = true;
|
92 |
91 |
|
... | ... | |
94 |
93 |
int index = path.lastIndexOf("\\");
|
95 |
94 |
String fileName = path.substring(index + 1);
|
96 |
95 |
|
97 |
|
// Generate Hash
|
98 |
|
// TODO dont use just file name for hashing(multiple file versions with same
|
99 |
|
// name problem)
|
100 |
|
String fileId = FileManager.hash(fileName);
|
|
96 |
// Generate Hash from file name and date
|
|
97 |
String fileId = FileManager.hash(fileName + Long.toString(new File(path).lastModified()));
|
101 |
98 |
|
|
99 |
// Is file backed up already?
|
|
100 |
if (this.files.get(fileId) != null) {
|
|
101 |
System.out.println("File already backed up");
|
|
102 |
return;
|
|
103 |
}
|
|
104 |
|
102 |
105 |
// Add to file list
|
103 |
106 |
this.files.put(fileId, fileName);
|
104 |
107 |
|
... | ... | |
161 |
164 |
int index = path.lastIndexOf("\\");
|
162 |
165 |
String fileName = path.substring(index + 1);
|
163 |
166 |
|
164 |
|
// Generate Hash
|
165 |
|
// TODO dont use just file name for hashing(multiple file versions with same
|
166 |
|
// name problem)
|
167 |
|
String fileId = FileManager.hash(fileName);
|
|
167 |
// Generate Hash from file name and date
|
|
168 |
String fileId = FileManager.hash(fileName + Long.toString(new File(path).lastModified()));
|
168 |
169 |
|
169 |
170 |
// Find in list of backed up
|
170 |
171 |
String name = this.files.get(fileId);
|
171 |
172 |
|
172 |
173 |
// File not backed up yet, just return
|
173 |
174 |
if (name == null) {
|
|
175 |
System.out.println("File not backed up by this peer");
|
174 |
176 |
return;
|
175 |
177 |
}
|
176 |
178 |
|
... | ... | |
200 |
202 |
|
201 |
203 |
// Wait for response
|
202 |
204 |
this.waitRestoreReply.set(true);
|
|
205 |
long time = System.nanoTime();
|
203 |
206 |
while (this.waitRestoreReply.get()) {
|
|
207 |
// Restore timeout
|
|
208 |
if (TimeUnit.NANOSECONDS.toMillis(time) - TimeUnit.NANOSECONDS.toMillis(System.nanoTime()) > 1000) {
|
|
209 |
System.out.println("Restore timeout, missing chunks");
|
|
210 |
return;
|
|
211 |
}
|
204 |
212 |
}
|
205 |
213 |
|
206 |
|
}
|
|
214 |
}
|
207 |
215 |
|
208 |
|
//Get simple list from recovered chunks
|
|
216 |
// Get simple list from recovered chunks
|
209 |
217 |
List<Chunk> list = new ArrayList<Chunk>(this.recoveredChunks.values());
|
210 |
218 |
|
211 |
219 |
// Sort chunks
|
... | ... | |
248 |
256 |
int index = path.lastIndexOf("\\");
|
249 |
257 |
String fileName = path.substring(index + 1);
|
250 |
258 |
|
251 |
|
// Generate Hash
|
252 |
|
// TODO dont use just file name for hashing(multiple file versions with same
|
253 |
|
// name problem)
|
254 |
|
String fileId = FileManager.hash(fileName);
|
|
259 |
// Generate Hash from file name and date
|
|
260 |
String fileId = FileManager.hash(fileName + Long.toString(new File(path).lastModified()));
|
255 |
261 |
|
|
262 |
// Remove from list of backed up
|
|
263 |
String name = this.files.get(fileId);
|
|
264 |
if (name != null) {
|
|
265 |
this.files.remove(fileId);
|
|
266 |
}
|
|
267 |
|
256 |
268 |
try {
|
257 |
269 |
|
258 |
270 |
// Prepare message
|
... | ... | |
307 |
319 |
|
308 |
320 |
// Remove from file system
|
309 |
321 |
Path current = Paths.get("");
|
310 |
|
String place = current.toAbsolutePath().toString() + "\\peer" + id + "\\backup\\" + entry.getValue().fileId + "\\chk" + entry.getValue().chunkN + ".chk";
|
|
322 |
String place = current.toAbsolutePath().toString() + "\\peer" + id + "\\backup\\"
|
|
323 |
+ entry.getValue().fileId + "\\chk" + entry.getValue().chunkN + ".chk";
|
311 |
324 |
File toDelete = new File(place);
|
312 |
325 |
toDelete.delete();
|
313 |
326 |
|
... | ... | |
437 |
450 |
|
438 |
451 |
public static void main(String args[]) {
|
439 |
452 |
|
440 |
|
// TODO Arguments are not correct, need to receive all the specific socket
|
441 |
|
// arguments too, version and server(RMI)
|
|
453 |
try {
|
442 |
454 |
|
443 |
|
// Check arguments
|
444 |
|
if (args.length < 1) {
|
445 |
|
System.err.println("Invalid number of arguments");
|
446 |
|
return;
|
447 |
|
}
|
|
455 |
// Server object
|
|
456 |
Server obj;
|
448 |
457 |
|
449 |
|
try {
|
|
458 |
// Basic (version id)
|
|
459 |
if (args.length == 2) {
|
|
460 |
obj = new Server(args[1]);
|
|
461 |
}
|
|
462 |
// Full (version id access port mc port mdb port mdr)
|
|
463 |
else if (args.length == 9) {
|
|
464 |
obj = new Server(args[1], Integer.parseInt(args[3]), args[4], Integer.parseInt(args[5]), args[6],
|
|
465 |
Integer.parseInt(args[7]), args[8]);
|
|
466 |
}
|
|
467 |
// Invalid arguments
|
|
468 |
else {
|
|
469 |
System.out.println("java Server <version> <server id>");
|
|
470 |
System.out.println(
|
|
471 |
"java Server <version> <server id> <access_point> <MC_port> <MC_IP_address> <MDB_port> <MDB_IP_address> <MDR_port> <MDR_IP_address>");
|
|
472 |
return;
|
|
473 |
}
|
450 |
474 |
|
451 |
|
// Create Server
|
452 |
|
Server obj = new Server(args[0]);
|
453 |
|
|
454 |
475 |
// Bind the server as a registry
|
455 |
476 |
PeerInterface stub = (PeerInterface) UnicastRemoteObject.exportObject(obj, 0);
|
456 |
477 |
|
457 |
478 |
// Bind the remote object's stub in the registry
|
458 |
|
|
459 |
|
// TODO Get registry what port?
|
460 |
479 |
Registry registry = LocateRegistry.getRegistry();
|
461 |
|
registry.bind(args[0], stub);
|
|
480 |
registry.bind(obj.id, stub);
|
462 |
481 |
|
463 |
482 |
System.err.println("Server ready");
|
464 |
483 |
|