Project

General

Profile

Statistics
| Revision:

root / DistributedBackupService / src / client / TestApp.java

History | View | Annotate | Download (2.43 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", () -> {
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
        }
103
}