sdis1819-t7g02 / service / Client.java @ 1
History | View | Annotate | Download (4.34 KB)
1 | 1 | up20150366 | package service; |
---|---|---|---|
2 | |||
3 | import java.rmi.registry.LocateRegistry; |
||
4 | import java.rmi.registry.Registry; |
||
5 | |||
6 | public class Client { |
||
7 | |||
8 | private Client() {}
|
||
9 | |||
10 | public static void main(String[] args) { |
||
11 | |||
12 | System.out.println("CLIENT"); |
||
13 | try
|
||
14 | { |
||
15 | int argc = args.length;
|
||
16 | |||
17 | if(argc < 2 || argc > 4) |
||
18 | { |
||
19 | print_usage(); |
||
20 | return;
|
||
21 | } |
||
22 | |||
23 | |||
24 | String aux = args[0]; |
||
25 | |||
26 | String[] acess_point = aux.split(":"); |
||
27 | |||
28 | String ip = acess_point[0]; |
||
29 | String id = acess_point[1]; |
||
30 | |||
31 | String sub_protocol = args[1]; |
||
32 | |||
33 | Registry registry = LocateRegistry.getRegistry(ip); |
||
34 | RMI rmi = (RMI) registry.lookup(id); |
||
35 | |||
36 | String file_path;
|
||
37 | |||
38 | String response;
|
||
39 | |||
40 | switch(sub_protocol)
|
||
41 | { |
||
42 | case "BACKUP": |
||
43 | if(argc != 4) |
||
44 | { |
||
45 | print_error_ops(sub_protocol); |
||
46 | } |
||
47 | |||
48 | |||
49 | file_path = args[2];
|
||
50 | int replication_degree = Integer.parseInt(args[3]); |
||
51 | |||
52 | |||
53 | response = rmi.backup(file_path, replication_degree); |
||
54 | System.out.println("response: " + response); |
||
55 | break;
|
||
56 | |||
57 | case "RESTORE": |
||
58 | if(argc != 3) |
||
59 | { |
||
60 | print_error_ops(sub_protocol); |
||
61 | } |
||
62 | |||
63 | file_path = args[2];
|
||
64 | response = rmi.restore(file_path); |
||
65 | System.out.println("response: " + response); |
||
66 | break;
|
||
67 | |||
68 | case "DELETE": |
||
69 | if(argc != 3) |
||
70 | { |
||
71 | print_error_ops(sub_protocol); |
||
72 | } |
||
73 | |||
74 | |||
75 | file_path = args[2];
|
||
76 | response = rmi.delete(file_path); |
||
77 | System.out.println("response: " + response); |
||
78 | break;
|
||
79 | |||
80 | case "RECLAIM": |
||
81 | if(argc != 3) |
||
82 | { |
||
83 | print_error_ops(sub_protocol); |
||
84 | } |
||
85 | |||
86 | long disk_space = Long.parseLong(args[2]); |
||
87 | response = rmi.reclaim(disk_space); |
||
88 | System.out.println("response: " + response); |
||
89 | break;
|
||
90 | |||
91 | case "STATE": |
||
92 | if(argc != 2) |
||
93 | { |
||
94 | print_error_ops(sub_protocol); |
||
95 | } |
||
96 | |||
97 | response = rmi.state(); |
||
98 | System.out.println("response: " + response); |
||
99 | break;
|
||
100 | } |
||
101 | |||
102 | } catch (Exception e) { |
||
103 | System.err.println("Client exception: " + e.toString()); |
||
104 | } |
||
105 | } |
||
106 | |||
107 | public static void print_usage() |
||
108 | { |
||
109 | System.out.println("Usage: :\n"); |
||
110 | System.out.println(" java Client <peer_ap> <operation> <opnd_1> <opnd_2> \n"); |
||
111 | System.out.println(" - <peer_ap> -> peer's acess point\n"); |
||
112 | System.out.println(" - <operation> -> Subprotocol ops: BACKUP, RESTORE, DELETE, RECLAIM; Enhancements: ops with ENH in the end. State: STATE \n"); |
||
113 | System.out.println(" - <opnd_1> -> path name of the file or in case of op RECLAIM the max amout of disk space\n"); |
||
114 | System.out.println(" - <opnd_2>* -> integer that specifies the desired replication degree\n"); |
||
115 | } |
||
116 | |||
117 | public static void print_error_ops(String error) |
||
118 | { |
||
119 | switch(error)
|
||
120 | { |
||
121 | case "BACKUP": |
||
122 | System.out.println("ERROR on BACKUP format. Must be:\n"); |
||
123 | System.out.println("> BACKUP <file_path> <replication_degree>"); |
||
124 | break;
|
||
125 | |||
126 | case "RESTORE": |
||
127 | System.out.println("ERROR on RESTORE format. Must be:\n"); |
||
128 | System.out.println("> RESTORE <file_path>"); |
||
129 | break;
|
||
130 | |||
131 | case "DELETE": |
||
132 | System.out.println("ERROR on DELETE format. Must be:\n"); |
||
133 | System.out.println("> DELETE <file_path>"); |
||
134 | break;
|
||
135 | |||
136 | case "RECLAIM": |
||
137 | System.out.println("ERROR on RECLAIM format. Must be:\n"); |
||
138 | System.out.println("> RECLAIM <disk_space>"); |
||
139 | break;
|
||
140 | |||
141 | case "STATE": |
||
142 | System.out.println("ERROR on STATE format. Must be:\n"); |
||
143 | System.out.println("> RECLAIM <disk_space>"); |
||
144 | break;
|
||
145 | |||
146 | default:
|
||
147 | System.out.println("ERROR. Unkown op"); |
||
148 | } |
||
149 | } |
||
150 | } |