Project

General

Profile

Statistics
| Revision:

root / DistributedBackupService / src / client / TestApp.java @ 1

History | View | Annotate | Download (2.7 KB)

1
package client;
2

    
3
import java.io.File;
4
import java.rmi.RemoteException;
5
import java.rmi.registry.LocateRegistry;
6
import java.rmi.registry.Registry;
7
import java.util.HashMap;
8
import java.util.Map;
9

    
10
import server.InitiatorPeer;
11

    
12
public class TestApp implements Runnable {
13

    
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

    
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
                        System.out.println("<peer_ap> - peer access point");
28
                        System.out.println("<sub_protocol> - BACKUP, RESTORE, DELETE, RECLAIM");
29
                        System.out.println("<opnd_1> - path name of the file to backup/restore/delete");
30
                        System.out.println("<opnd_2> - replication degree");
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;
48

    
49
        TestApp app = new TestApp(peer_ap, sub_protocol, opnd_1, opnd_2);
50
        new Thread(app).start();
51
    }
52

    
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;
60

    
61
        protocols.put("BACKUP", this::handleBackup);
62
        protocols.put("DELETE", this::handleDelete);
63

    
64
    }
65

    
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
    }
87

    
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
    }
96
}