Project

General

Profile

Statistics
| Revision:

root / Client.java @ 1

History | View | Annotate | Download (2.52 KB)

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