Project

General

Profile

Statistics
| Revision:

root / src / Client.java

History | View | Annotate | Download (2.73 KB)

1 3 up20160792
2
import java.rmi.registry.LocateRegistry;
3
import java.rmi.registry.Registry;
4
5
public class Client {
6
7
    public static void main(String[] args) {
8
9
        // Command parameters
10
        String[] opnd = { "operand", "opnd1", "opnd2" };
11
12
        // Process Arguments
13
14
        // 2 arguments can only be STATE check
15
        if (args.length == 2) {
16
            opnd[0] = "STATE";
17
        }
18
        // 3 arguments can only be RECLAIM/DELETE/RESTORE
19
        else if (args.length == 3) {
20
            switch (args[1]) {
21
            case "RESTORE":
22
                opnd[0] = "RESTORE";
23
                // File
24
                opnd[1] = args[2];
25
                break;
26
            case "DELETE":
27
                opnd[0] = "DELETE";
28
                // File
29
                opnd[1] = args[2];
30
                break;
31
            case "RECLAIM":
32
                opnd[0] = "RECLAIM";
33
                // Reclaim size
34
                opnd[1] = args[2];
35
                break;
36
            default:
37
                System.out.println("Error: Invalid operation name");
38
                return;
39
            }
40
        }
41
        // 4 arguments can only be BACKUP
42
        else if (args.length == 4) {
43
            opnd[0] = "BACKUP";
44
            // File
45
            opnd[1] = args[2];
46
            // Replication level
47
            opnd[2] = args[3];
48
        }
49
        // Any more arguments are invalid too
50
        else {
51
            System.out.println("java Client <peer_ap> <protocol> <opnd_1> <opnd_2");
52
            return;
53
        }
54
55
        // Host id
56
        String[] host = new String(args[0]).split("/");
57
58
        try {
59
60
            PeerInterface stub;
61
62
            // Lookup for RMI
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
            }
70
71
            // Identify action
72
            switch (opnd[0]) {
73
            case "BACKUP":
74
                stub.backup(opnd[1], Integer.parseInt(opnd[2]));
75
                break;
76
            case "RESTORE":
77
                stub.restore(opnd[1]);
78
                break;
79
            case "DELETE":
80
                stub.delete(opnd[1]);
81
                break;
82
            case "RECLAIM":
83
                stub.reclaim(Integer.parseInt(opnd[1]));
84
                break;
85
            default:
86
                break;
87
            }
88
89
        } catch (Exception e) {
90
91
            System.err.println("Client exception: " + e.toString());
92
            e.printStackTrace();
93
94
        }
95
96
    }
97
98
}