Project

General

Profile

Statistics
| Revision:

root / project / src / main / java / testapp / TestApp.java @ 1

History | View | Annotate | Download (5.41 KB)

1 1 up20120064
package main.java.testapp;
2
3
import main.java.service.RMI;
4
5
6
import java.io.File;
7
import java.rmi.NotBoundException;
8
import java.rmi.RemoteException;
9
import java.rmi.registry.LocateRegistry;
10
import java.rmi.registry.Registry;
11
12
import static main.java.utils.Utilities.validateAccessPoint;
13
14
//TODO: RENAME TO TestApp
15
public class TestApp {
16
17
18
    public static String AP;
19
    public static String subProtocol;
20
    public static String filePath;
21
    public static int replicationDegree;
22
    private static String rmiObjectAddress;
23
    private static String rmiObjectName;
24
    private static int amount;
25
26
    private static RMI peer;
27
28
    /*
29
    java DBS <peer_ap> <sub_protocol> <opnd_1> <opnd_2>
30
    java DBS 1923 BACKUP test1.pdf 3
31
     */
32
33
    /*
34
    If you choose to use RMI in the communication between the test application and the peer,
35
    you should use as access point the name of the remote object providing the "testing" service.
36
     */
37
38
    public static void main(String[] args) {
39
40
41
42
        parseAccessPoint(args);
43
        parseSubProtocol(args);
44
45
        try {
46
            Registry registry = LocateRegistry.getRegistry(rmiObjectAddress);
47
48
            peer = (RMI)registry.lookup(rmiObjectName);
49
50
51
        } catch (RemoteException | NotBoundException e) {
52
            System.out.println("Invalid RMI object name");
53
            return;
54
        }
55
56
57
58
        if(subProtocol.equals("BACKUP")) {
59
            System.out.println("Initializing Backup...");
60
            parseBackupArguments(args);
61
62
            try {
63
                File file = new File(filePath);
64
                peer.backup(file, replicationDegree);
65
                System.out.println("\t File Path: " + filePath + "\n");
66
            } catch (RemoteException e) {
67
                e.printStackTrace();
68
            }
69
        } else if(subProtocol.equals("RESTORE")) {
70
            System.out.println("Initializing Restore...");
71
            parseRestoreArguments(args);
72
73
            try {
74
                File file = new File(filePath);
75
                peer.restore(file);
76
            } catch (RemoteException e) {
77
                e.printStackTrace();
78
            }
79
        } else if(subProtocol.equals("DELETE")) {
80
            System.out.println("Initializing DELETE");
81
            parseDeleteArguments(args);
82
            try {
83
                peer.delete(filePath);
84
            } catch (RemoteException e) {
85
                e.printStackTrace();
86
            }
87
88
        }
89
        else if(subProtocol.equals("RECLAIM")) {
90
            System.out.println("Initializing Reclaim...");
91
            parseReclaimArguments(args);
92
93
            try {
94
                peer.reclaim(amount);
95
            } catch (RemoteException e) {
96
                e.printStackTrace();
97
            }
98
        }
99
        else if(subProtocol.equals("STATE")){
100
            System.out.println("Retrieving testapp state...");
101
102
            try {
103
                peer.state();
104
            } catch (RemoteException e) {
105
                e.printStackTrace();
106
            }
107
        }
108
109
110
111
112
113
114
115
    }
116
117
    private static void parseDeleteArguments(String[] args) {
118
119
        filePath = args[2];
120
        System.out.println("DELETE FILE PATH: " + filePath);
121
122
    }
123
124
    private static void parseSubProtocol(String[] args) {
125
        subProtocol = args[1];
126
    }
127
128
    private static boolean parseBackupArguments(String[] args) {
129
130
        if(args.length != 4) {
131
            System.out.println("\t Usage: peer <accessPoint> BACKUP <filePath> <replicationDegree> \n");
132
            return false;
133
        } else {
134
            filePath = args[2];
135
            replicationDegree = Integer.parseInt(args[3]);
136
        }
137
138
        System.out.println("\t \t \t TestApp Arguments\n\n" +
139
                "\t Access Point: " + AP + "\n" +
140
                "\t Sub Protocol : " + subProtocol + "\n" +
141
                "\t File Path: " + filePath + "\n" + "\n" +
142
                "\t Replication Degree: " + replicationDegree + "\n");
143
144
        return true;
145
146
    }
147
148
    private static boolean parseRestoreArguments(String[] args) {
149
        if(args.length != 3) {
150
            System.out.println("\t Usage: peer <accessPoint> RESTORE <filePath> <replicationDegree> \n");
151
            return false;
152
        } else {
153
            filePath = args[2];
154
155
        }
156
157
        System.out.println("\t \t \t \t TestApp Arguments\n\n" +
158
                "\t Access Point: " + AP + "\n" +
159
                "\t Sub Protocol : " + subProtocol + "\n" +
160
                "\t File Path: " + filePath + "\n" + "\n");
161
162
        return true;
163
    }
164
165
    private static boolean parseReclaimArguments(String[] args){
166
        if(args.length != 3) {
167
            System.out.println("\t Usage: \n" + "\t On src/main/java run: \n" + "\t \t java Peer.peer <access_point> " +
168
                    "RECLAIM <amount>  \n");
169
            return false;
170
        } else {
171
            amount = Integer.parseInt(args[2]);
172
173
        }
174
175
        System.out.println("\t \t \t \t TestApp Arguments\n\n" +
176
                "\t Access Point: " + AP + "\n" +
177
                "\t Sub Protocol : " + subProtocol + "\n" +
178
                "\t Amount: " + amount + "\n" + "\n" );
179
180
        return true;
181
    }
182
183
    public static void parseAccessPoint(String[] args) {
184
185
        AP = args[0];
186
187
188
        if(validateAccessPoint(AP)) {
189
            String[] splittedAP = AP.split("/");
190
            rmiObjectName = splittedAP[1];
191
            rmiObjectAddress = splittedAP[0];
192
        } else {
193
            System.out.println("\t Wrong Access Point format! \n" + "\t Usage: IP/remoteObjectName");
194
        }
195
196
197
    }
198
199
200
}