Project

General

Profile

Revision 2

Fixed many bugs

View differences:

Client.java
4 4

  
5 5
public class Client {
6 6

  
7
    private Client() {
8

  
9
    }
10

  
11 7
    public static void main(String[] args) {
12 8

  
13 9
        // Command parameters
......
52 48
        }
53 49
        // Any more arguments are invalid too
54 50
        else {
55
            System.out.println("Invalid number of arguments");
51
            System.out.println("java Client <peer_ap> <protocol> <opnd_1> <opnd_2");
56 52
            return;
57 53
        }
58 54

  
59
        // Peer Id to takeover
60
        String idPeer = args[0];
55
        // Host id
56
        String[] host = new String(args[0]).split("/");
61 57

  
62 58
        try {
63 59

  
60
            PeerInterface stub;
61

  
64 62
            // Lookup for RMI
65
            Registry registry = LocateRegistry.getRegistry();
63
            if(host.length > 2){
64
                Registry registry = LocateRegistry.getRegistry(host[2]);
65
                stub = (PeerInterface) registry.lookup(host[3]);
66
            }else{
67
                Registry registry = LocateRegistry.getRegistry();
68
                stub = (PeerInterface) registry.lookup(args[0]);
69
            }                     
66 70

  
67
            // Lookup for peer access point
68
            PeerInterface stub = (PeerInterface) registry.lookup(idPeer);
69

  
70 71
            // Identify action
71 72
            switch (opnd[0]) {
72 73
            case "BACKUP":
FileManager.java
77 77
        file.delete();
78 78
    }
79 79

  
80
    // Create metafile(TODO add more useful arguments)
81
    // Used to reconstruct the file name when Restoring
82
    public static void metaFile(String path, String fileId) throws IOException {
83

  
84
        // Extract file name
85
        int index = path.lastIndexOf("\\");
86
        String fileName = path.substring(index + 1);
87

  
88
        // Create actual file
89
        File file = new File(fileName + ".meta");
90
        if (file.createNewFile()) {
91
            // TODO File created
92
        } else {
93
            // TODO File already exists
94
        }
95

  
96
        // Write data to file
97
        FileWriter writer = new FileWriter(file);
98
        writer.write("Id:" + fileId);
99
        writer.close();
100

  
101
    }
102

  
103 80
    // Read all bytes of file(generic use)
104 81
    public static byte[] readFile(String path) {
105 82

  
Server.java
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

  
clientBackup.bat
1 1
@echo off 
2
java Client 1 BACKUP test.png 2
2
java Client //localhost/1 BACKUP test.png 2
clientDelete.bat
1 1
@echo off 
2
java Client 1 DELETE test.png
2
java Client //localhost/1 DELETE test.png
clientReclaim.bat
1 1
@echo off 
2
java Client 2 RECLAIM 0
2
java Client //localhost/2 RECLAIM 0
clientRestore.bat
1 1
@echo off 
2
java Client 1 RESTORE test.png
2
java Client //localhost/1 RESTORE test.png
peer1.bat
1 1
@echo off 
2
java Server 1
2
java Server 1 1 1 8000 224.0.0.1 8001 224.0.0.2 8002 224.0.0.3
peer2.bat
1 1
@echo off 
2
java Server 2
2
java Server 1 2 2 8000 224.0.0.1 8001 224.0.0.2 8002 224.0.0.3
peer3.bat
1 1
@echo off 
2
java Server 3
2
java Server 1 3 3 8000 224.0.0.1 8001 224.0.0.2 8002 224.0.0.3
peer4.bat
1 1
@echo off 
2
java Server 4
2
java Server 1 4 4 8000 224.0.0.1 8001 224.0.0.2 8002 224.0.0.3

Also available in: Unified diff