Project

General

Profile

Revision 3

Update

View differences:

TestApp.java
1
package client;

2

  
3
import utils.Utils;

4

  
5
import java.lang.Integer;

6
import java.rmi.NotBoundException;

7
import java.rmi.RemoteException;

8
import java.rmi.registry.LocateRegistry;

9
import java.rmi.registry.Registry;

10

  
11
import peer.PeerInterface;

12

  
13
import java.lang.Float;

14

  
15
/**

16
 * TestApp

17
 */

18
public class TestApp {

19

  
20
    private String peer_ap;

21
    private String operation;

22
    private String filePath;

23
    private float diskSpace;

24
    private int replicationDeg;

25
    private PeerInterface pInterface;

26

  
27
    public static void main(String[] args){

28
        TestApp app = new TestApp(args);

29
        app.processRequest();

30
    }

31

  
32
    public TestApp(String[] args) {

33
        if (!handleInputs(args)) {

34
            usage();

35
            return;

36
        }

37

  
38
        connect();

39
    }

40

  
41
    public void processRequest(){

42
        switch (operation) {

43
            case "BACKUP":

44
                System.out.println("BACKUP:\n\tFile: " + filePath + "\n\tReplication Degree: " + replicationDeg);

45
                try {

46
                    pInterface.backup(filePath, replicationDeg);

47
                } catch (RemoteException e) {

48
                    System.out.println("BACKUP ERROR!");

49
                    e.printStackTrace();

50
                }

51
                System.out.println("BACKUP SUCCESSFUL!");

52
                break;

53
            case "RESTORE":

54
                System.out.println("RESTORE:\n\tFile: " + filePath);

55
                try {

56
                    pInterface.restore(filePath);

57
                } catch (RemoteException e) {

58
                    System.out.println("RESTORE ERROR!");

59
                    e.printStackTrace();

60
                }

61
                System.out.println("RESTORE SUCCESSFUL!");

62
                break;

63
            case "DELETE":

64
                System.out.println("DELETE:\n\tFile: " + filePath);

65
                try {

66
                    pInterface.delete(filePath);

67
                } catch (RemoteException e) {

68
                    System.out.println("DELETE ERROR!");

69
                    e.printStackTrace();

70
                }

71
                System.out.println("DELETE SUCCESSFUL!");

72
                break;

73
            case "RECLAIM":

74
                System.out.println("RECLAIM:\n\tDisk Space: " + diskSpace);

75
                try {

76
                    pInterface.reclaim(diskSpace);

77
                } catch (RemoteException e) {

78
                    System.out.println("RECLAIM ERROR!");

79
                    e.printStackTrace();

80
                }

81
                System.out.println("RECLAIM SUCCESSFUL!");

82
                break;

83
            case "STATE":

84
                System.out.println("STATE:");

85
                try {

86
                    pInterface.state();

87
                } catch (RemoteException e) {

88
                    System.out.println("STATE ERROR!");

89
                    e.printStackTrace();

90
                }

91
                System.out.println("STATE SUCCESSFUL!");

92
                    break;

93
            default:

94
                break;

95
            }

96
    }

97
        
98
    
99

  
100
    private boolean handleInputs(String[] args) {

101
        if (args.length < 2) {

102
            System.out.println("Error: Invalid number of arguments!");

103
            return false;

104
        }

105

  
106
        this.peer_ap = args[0];

107
        
108
        this.operation = args[1].toUpperCase();

109
        switch (operation) {

110
        case "BACKUP":

111
            if (args.length != 4) {

112
                System.out.println(operation + " error: Invalid number of arguments!");

113
                return false;

114
            }

115
            if (!Utils.fileExists(args[2])) {

116
                System.out.println(operation + " error: File doesn't exist!");

117
                return false;

118
            }

119
            this.filePath = args[2];

120
            if (!Utils.isInteger(args[3])) {

121
                System.out.println(operation + " error: Replication Degree invalid!");

122
                return false;

123
            }

124
            this.replicationDeg = Integer.parseInt(args[3]);

125
            break;

126
        case "RESTORE":

127
            if (args.length != 3) {

128
                System.out.println(operation + " error: Invalid number of arguments!");

129
                return false;

130
            }

131
            if (!Utils.fileExists(args[2])) {

132
                System.out.println(operation + " error: File doesn't exist!");

133
                return false;

134
            }

135
            this.filePath = args[2];

136
            break;

137
        case "DELETE":

138
            if (args.length != 3) {

139
                System.out.println(operation + " error: Invalid number of arguments!");

140
                return false;

141
            }

142
            if (!Utils.fileExists(args[2])) {

143
                System.out.println(operation + " error: File doesn't exist!");

144
                return false;

145
            }

146
            this.filePath = args[2];

147
            break;

148
        case "RECLAIM":

149
            if (args.length != 3) {

150
                System.out.println(operation + " error: Invalid number of arguments!");

151
                return false;

152
            }

153
            if (!Utils.isFloat(args[2])) {

154
                System.out.println(operation + " error: Maximum amount of disk space invalid!");

155
                return false;

156
            }

157
            this.diskSpace = Float.parseFloat(args[2]);

158
            break;

159
        case "STATE":

160
            if (args.length != 2) {

161
                System.out.println(operation + " error: Invalid number of arguments!");

162
                return false;

163
            }

164
            break;

165
        default:

166
            System.out.println("Error: Invalid operation!");

167
            return false;

168
        }

169
        return true;

170
    }

171

  
172
    private static void usage() {

173
        System.out.println(

174
                "Usage:\n\tjava TestApp <peer_ap> <operation> <opnd_1> <opnd2>\n\t\t<peer_app>: Peer's access point;\n\t\t<operation>: Operation the peer of backup service must execute. It must be one of: BACKUP, RESTORE, DELETE, RECLAIM, STATE;\n\t\t<opnd_1>: Path Name of the file in case of operations BACKUP, RESTORE or DELETE or maximum amount of disk space (in KByte) in case of operation RECLAIM\n\t\t<opnd_2>: Integer that specifies the desired replication degree for operation BACKUP");

175
        return;

176
    }

177

  
178
    public void connect() {

179
        try {

180
            Registry rmiReg = LocateRegistry.getRegistry(Registry.REGISTRY_PORT);

181
            this.pInterface = (PeerInterface) rmiReg.lookup(this.peer_ap);

182
        } catch (RemoteException | NotBoundException e) {

183
            e.printStackTrace();

184
        }

185
    }

1
package client;
2

  
3
import utils.Utils;
4

  
5
import java.lang.Integer;
6
import java.rmi.NotBoundException;
7
import java.rmi.RemoteException;
8
import java.rmi.registry.LocateRegistry;
9
import java.rmi.registry.Registry;
10

  
11
import peer.PeerInterface;
12

  
13
import java.lang.Float;
14

  
15
/**
16
 * TestApp
17
 */
18
public class TestApp {
19

  
20
    private String peer_ap;
21
    private String operation;
22
    private String filePath;
23
    private float diskSpace;
24
    private int replicationDeg;
25
    private PeerInterface pInterface;
26

  
27
    public static void main(String[] args){
28
        TestApp app = new TestApp(args);
29
        app.processRequest();
30
    }
31

  
32
    public TestApp(String[] args) {
33
        if (!handleInputs(args)) {
34
            usage();
35
            return;
36
        }
37

  
38
        connect();
39
    }
40

  
41
    public void processRequest(){
42
        switch (operation) {
43
            case "BACKUP":
44
                System.out.println("BACKUP:\n\tFile: " + filePath + "\n\tReplication Degree: " + replicationDeg);
45
                try {
46
                    pInterface.backup(filePath, replicationDeg);
47
                } catch (RemoteException e) {
48
                    System.out.println("BACKUP ERROR!");
49
                    e.printStackTrace();
50
                }
51
                System.out.println("BACKUP SUCCESSFUL!");
52
                break;
53
            case "RESTORE":
54
                System.out.println("RESTORE:\n\tFile: " + filePath);
55
                try {
56
                    pInterface.restore(filePath);
57
                } catch (RemoteException e) {
58
                    System.out.println("RESTORE ERROR!");
59
                    e.printStackTrace();
60
                }
61
                System.out.println("RESTORE SUCCESSFUL!");
62
                break;
63
            case "DELETE":
64
                System.out.println("DELETE:\n\tFile: " + filePath);
65
                try {
66
                    pInterface.delete(filePath);
67
                } catch (RemoteException e) {
68
                    System.out.println("DELETE ERROR!");
69
                    e.printStackTrace();
70
                }
71
                System.out.println("DELETE SUCCESSFUL!");
72
                break;
73
            case "RECLAIM":
74
                System.out.println("RECLAIM:\n\tDisk Space: " + diskSpace);
75
                try {
76
                    pInterface.reclaim(diskSpace);
77
                } catch (RemoteException e) {
78
                    System.out.println("RECLAIM ERROR!");
79
                    e.printStackTrace();
80
                }
81
                System.out.println("RECLAIM SUCCESSFUL!");
82
                break;
83
            case "STATE":
84
                System.out.println("STATE:");
85
                try {
86
                    pInterface.state();
87
                } catch (RemoteException e) {
88
                    System.out.println("STATE ERROR!");
89
                    e.printStackTrace();
90
                }
91
                System.out.println("STATE SUCCESSFUL!");
92
                    break;
93
            default:
94
                break;
95
            }
96
    }
97
        
98
    
99

  
100
    private boolean handleInputs(String[] args) {
101
        if (args.length < 2) {
102
            System.out.println("Error: Invalid number of arguments!");
103
            return false;
104
        }
105

  
106
        this.peer_ap = args[0];
107
        
108
        this.operation = args[1].toUpperCase();
109
        switch (operation) {
110
        case "BACKUP":
111
            if (args.length != 4) {
112
                System.out.println(operation + " error: Invalid number of arguments!");
113
                return false;
114
            }
115
            if (!Utils.fileExists(args[2])) {
116
                System.out.println(operation + " error: File doesn't exist!");
117
                return false;
118
            }
119
            this.filePath = args[2];
120
            if (!Utils.isInteger(args[3])) {
121
                System.out.println(operation + " error: Replication Degree invalid!");
122
                return false;
123
            }
124
            this.replicationDeg = Integer.parseInt(args[3]);
125
            break;
126
        case "RESTORE":
127
            if (args.length != 3) {
128
                System.out.println(operation + " error: Invalid number of arguments!");
129
                return false;
130
            }
131
            if (!Utils.fileExists(args[2])) {
132
                System.out.println(operation + " error: File doesn't exist!");
133
                return false;
134
            }
135
            this.filePath = args[2];
136
            break;
137
        case "DELETE":
138
            if (args.length != 3) {
139
                System.out.println(operation + " error: Invalid number of arguments!");
140
                return false;
141
            }
142
            if (!Utils.fileExists(args[2])) {
143
                System.out.println(operation + " error: File doesn't exist!");
144
                return false;
145
            }
146
            this.filePath = args[2];
147
            break;
148
        case "RECLAIM":
149
            if (args.length != 3) {
150
                System.out.println(operation + " error: Invalid number of arguments!");
151
                return false;
152
            }
153
            if (!Utils.isFloat(args[2])) {
154
                System.out.println(operation + " error: Maximum amount of disk space invalid!");
155
                return false;
156
            }
157
            this.diskSpace = Float.parseFloat(args[2]);
158
            break;
159
        case "STATE":
160
            if (args.length != 2) {
161
                System.out.println(operation + " error: Invalid number of arguments!");
162
                return false;
163
            }
164
            break;
165
        default:
166
            System.out.println("Error: Invalid operation!");
167
            return false;
168
        }
169
        return true;
170
    }
171

  
172
    private static void usage() {
173
        System.out.println(
174
                "Usage:\n\tjava TestApp <peer_ap> <operation> <opnd_1> <opnd2>\n\t\t<peer_app>: Peer's access point;\n\t\t<operation>: Operation the peer of backup service must execute. It must be one of: BACKUP, RESTORE, DELETE, RECLAIM, STATE;\n\t\t<opnd_1>: Path Name of the file in case of operations BACKUP, RESTORE or DELETE or maximum amount of disk space (in KByte) in case of operation RECLAIM\n\t\t<opnd_2>: Integer that specifies the desired replication degree for operation BACKUP");
175
        return;
176
    }
177

  
178
    public void connect() {
179
        try {
180
            Registry rmiReg = LocateRegistry.getRegistry(Registry.REGISTRY_PORT);
181
            this.pInterface = (PeerInterface) rmiReg.lookup(this.peer_ap);
182
        } catch (RemoteException | NotBoundException e) {
183
            e.printStackTrace();
184
        }
185
    }
186 186
}

Also available in: Unified diff