root / src / TestApp.java @ 1
History | View | Annotate | Download (5.15 KB)
1 | 1 | up20140508 | import java.io.IOException; |
---|---|---|---|
2 | import java.net.*; |
||
3 | import java.util.Scanner; |
||
4 | |||
5 | import static utils.Utils.BUFFER_SIZE; |
||
6 | |||
7 | //TODO: parse args for reclaim and delete not fully functional
|
||
8 | //grupo 11
|
||
9 | |||
10 | public class TestApp { |
||
11 | |||
12 | private static String hostname = "localhost"; |
||
13 | private static int portNumber; |
||
14 | private static String protocolRequest; |
||
15 | private static String filePath; |
||
16 | private static int replicationDegree; |
||
17 | |||
18 | |||
19 | public static void main(String[] args) throws SocketException, UnknownHostException { |
||
20 | mainMenu(); |
||
21 | } |
||
22 | |||
23 | private static void mainMenu() throws SocketException, UnknownHostException { |
||
24 | |||
25 | System.out.println("1- Backup a file"); |
||
26 | System.out.println("2- Restore a file"); |
||
27 | System.out.println("0- Exit"); |
||
28 | |||
29 | |||
30 | Scanner in = new Scanner(System.in); |
||
31 | int option = in.nextInt();
|
||
32 | |||
33 | switch(option){
|
||
34 | case 0: |
||
35 | System.out.println("exiting application..."); |
||
36 | break;
|
||
37 | case 1: |
||
38 | backupMenu(); |
||
39 | break;
|
||
40 | case 2: |
||
41 | restoreMenu(); |
||
42 | break;
|
||
43 | |||
44 | } |
||
45 | } |
||
46 | |||
47 | private static void restoreMenu() throws SocketException, UnknownHostException { |
||
48 | String[] args = new String[4]; |
||
49 | |||
50 | System.out.println("Please insert the peer port number which you want to send the request"); |
||
51 | |||
52 | Scanner in = new Scanner(System.in); |
||
53 | String portnumber = in.nextLine();
|
||
54 | |||
55 | args[0] = portnumber;
|
||
56 | |||
57 | args[1] = "RESTORE"; |
||
58 | |||
59 | System.out.println("Specify the file name you want to restore "); |
||
60 | |||
61 | String filename = in.nextLine();
|
||
62 | |||
63 | args[2] = filename;
|
||
64 | |||
65 | if(!parseArgs(args)) return; |
||
66 | |||
67 | sendRequest(args); |
||
68 | |||
69 | |||
70 | } |
||
71 | |||
72 | private static void backupMenu() throws SocketException, UnknownHostException { |
||
73 | String[] args = new String[4]; |
||
74 | |||
75 | System.out.println("Please insert the peer port number which you want to send the request"); |
||
76 | |||
77 | Scanner in = new Scanner(System.in); |
||
78 | String portnumber = in.nextLine();
|
||
79 | |||
80 | args[0] = portnumber;
|
||
81 | |||
82 | args[1] = "BACKUP"; |
||
83 | |||
84 | System.out.println("Specify the file name you want to backup (must be inside test folder) "); |
||
85 | |||
86 | String filename = in.nextLine();
|
||
87 | |||
88 | args[2] = filename;
|
||
89 | |||
90 | System.out.println("Now enter the replication degree"); |
||
91 | String replicD = in.nextLine();
|
||
92 | |||
93 | args[3] = replicD;
|
||
94 | |||
95 | if(!parseArgs(args)) return; |
||
96 | |||
97 | sendRequest(args); |
||
98 | |||
99 | } |
||
100 | |||
101 | private static void sendRequest(String[] args) throws SocketException, UnknownHostException { |
||
102 | DatagramSocket socket = new DatagramSocket(); |
||
103 | byte[] buffer; |
||
104 | String message = ""; |
||
105 | |||
106 | for(int i = 1; i < args.length; i++){ |
||
107 | if(i == args.length -1){ |
||
108 | message += args[i]; |
||
109 | break;
|
||
110 | } |
||
111 | message += args[i] + " ";
|
||
112 | } |
||
113 | |||
114 | buffer = message.getBytes(); |
||
115 | |||
116 | InetAddress address = InetAddress.getByName(hostname); |
||
117 | DatagramPacket packet = new DatagramPacket(buffer, buffer.length, address, portNumber); |
||
118 | |||
119 | try {
|
||
120 | socket.send(packet); |
||
121 | } catch (IOException e) { |
||
122 | e.printStackTrace(); |
||
123 | } |
||
124 | |||
125 | buffer = new byte[BUFFER_SIZE]; |
||
126 | packet = new DatagramPacket(buffer, buffer.length); |
||
127 | |||
128 | //
|
||
129 | try {
|
||
130 | socket.receive(packet); |
||
131 | } catch (IOException e) { |
||
132 | e.printStackTrace(); |
||
133 | } |
||
134 | |||
135 | String answer = new String(packet.getData(), 0, packet.getLength()); |
||
136 | System.out.println("Peer said: " + answer); |
||
137 | socket.close(); |
||
138 | |||
139 | //after request sent and done, come back to main menu
|
||
140 | mainMenu(); |
||
141 | } |
||
142 | |||
143 | private static boolean parseArgs(String[] args) { |
||
144 | if(args == null || args.length < 3) { |
||
145 | System.out.println("Usage: java TestApp <peer_ap> <sub_protocol> <opnd_1> <opnd_2>"); |
||
146 | return false; |
||
147 | } |
||
148 | String[] peer_ap = args[0].split(":"); |
||
149 | |||
150 | if(peer_ap.length == 1) { //user just gave the port number |
||
151 | portNumber = Integer.parseInt(peer_ap[0]); |
||
152 | } |
||
153 | else if(peer_ap.length == 2) { |
||
154 | hostname = peer_ap[0];
|
||
155 | portNumber = Integer.parseInt(peer_ap[1]); |
||
156 | } |
||
157 | else {
|
||
158 | System.out.println("Usage: java TestApp <peer_ap> <sub_protocol> <opnd_1> <opnd_2>"); |
||
159 | return false; |
||
160 | } |
||
161 | |||
162 | switch(args[1]) { |
||
163 | case "BACKUP": |
||
164 | protocolRequest = "BACKUP";
|
||
165 | filePath = args[2];
|
||
166 | if(args[3] == null){ |
||
167 | System.out.println("In backup protocol, you have to specify also the replication degree"); |
||
168 | return false; |
||
169 | } |
||
170 | replicationDegree = Integer.parseInt(args[3]); |
||
171 | break;
|
||
172 | case "RESTORE": |
||
173 | protocolRequest = "RESTORE";
|
||
174 | filePath = args[2];
|
||
175 | break;
|
||
176 | case "DELETE": |
||
177 | protocolRequest = "DELETE";
|
||
178 | filePath = args[2];
|
||
179 | break;
|
||
180 | case "RECLAIM": |
||
181 | protocolRequest = "RECLAIM";
|
||
182 | break;
|
||
183 | default: return false; |
||
184 | } |
||
185 | |||
186 | |||
187 | |||
188 | |||
189 | return true; |
||
190 | } |
||
191 | |||
192 | } |