-
Notifications
You must be signed in to change notification settings - Fork 1
/
DfsClient.java
243 lines (211 loc) · 8 KB
/
DfsClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
* @file DfsClient.java
* @brief Implements the client for a distributed file system modeled after
* AFS.
* @author Brendan Sweeney, ID #1161836
* @date December 13, 2012
*/
import java.io.*;
import java.util.*;
import java.rmi.*;
import java.rmi.server.*;
public class DfsClient extends UnicastRemoteObject
implements ClientInterface {
private final static int ERROR = 0;
private final static int INVALID = 0;
private final static int READ_SHARED = 1;
private final static int WRITE_OWNED = 2;
private final static int RELEASE_OWN = 3;
private final static int READ_MODE = 10;
private final static int WRITE_MODE = 11;
private final static String CACHE_NAME = "/tmp/bps7.txt";
private final static String EDITOR = "/usr/bin/emacs";
private String fileName;
private Boolean write;
private Boolean owner;
private int state;
private FileContents local;
private ServerInterface server;
public DfsClient() throws RemoteException {
fileName = "";
write = false;
owner = false;
state = INVALID;
} // end constructor
public static void main(String args[]) {
String address, port; // connection fields
int mode;
if (args.length != 2) {
System.err.println("usage: java DfsClient server port#");
System.exit(-1);
} // end if (args.length != 2)
address = args[0];
port = args[1];
try {
DfsClient client = new DfsClient();
Naming.rebind("rmi://localhost:" + port + "/dfsclient", client);
client.connect(address, port);
do {
mode = client.readInput();
if (mode == READ_MODE) {
client.readFile();
} // end if (mode.compareTo(READ_MODE) == 0)
else if (mode == WRITE_MODE) {
client.writeFile();
} // end else if (mode.compareTo(WRITE_MODE) == 0)
} while (mode > 0);// end while(runner.readInput())
System.exit(0);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
} // end try new DfsClient();
} // end main(String[])
public boolean invalidate() { // set the DFS client’s file state to “Invalid”
if (state == READ_SHARED) {
state = INVALID;
} // end if (state == READ_SHARED)
return true;
} // end invalidate()
public boolean writeback() { // request the DFS client to upload its current cache.
if (state == WRITE_OWNED) {
state = RELEASE_OWN;
} // end if (state == WRITE_OWNED)
return true;
} // end writeback()
public void connect(String address, String port) {
try {
server = (ServerInterface)Naming.lookup(
"rmi://" + address + ":" + port + "/dfsserver");
} catch (Exception e) {
e.printStackTrace();
} // end try Naming.lookup(...)
} // end connect(String, String)
private int readInput() {
String getName = "", getMode = "";
BufferedReader input =
new BufferedReader(new InputStreamReader(System.in));
System.out.println("FileClient: Next file to open (~ to exit)");
while (getName.compareTo("") == 0) {
System.out.print("File name: ");
try {
getName = input.readLine();
} catch (IOException e) {
e.printStackTrace();
} // end try fileName = input.readLine();
} // end while(fileName.compareTo("") == 0)
if (getName.compareTo("~") == 0) {
return 0;
} // end if (getName.compareTo("~") == 0)
while (getMode.compareToIgnoreCase("r") != 0 &&
getMode.compareToIgnoreCase("w") != 0) {
System.out.print("How(r/w): ");
try {
getMode = input.readLine();
} catch (IOException e) {
e.printStackTrace();
} // end try fileMode = input.readLine();
} // end while(fileMode.compareTo(READ_MODE) != 0...)
fileName = getName;
if (getMode.compareToIgnoreCase("r") == 0) {
return READ_MODE;
} // end if (getMode.compareToIgnoreCase("r") == 0)
if (getMode.compareToIgnoreCase("w") == 0) {
return WRITE_MODE;
} // end if (getMode.compareToIgnoreCase("w") == 0)
return ERROR;
} // end readInput(String)
private boolean checkCache() {
return true;
} // end checkCache()
private int readFile() {
FileOutputStream file = null;
switch (state) {
case INVALID:
try {
local = server.download(getClientHost(), fileName, "r");
file = new FileOutputStream(CACHE_NAME);
file.write(local.get());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (file != null) {
try {
file.close();
} catch (Exception e) {
e.printStackTrace();
} // end try file.close();
} // end if (file != null)
} // end try file = new FileOutputStream(CACHE_NAME);
write = false;
owner = false;
state = READ_SHARED;
break;
case READ_SHARED:
if (!checkCache()) {
state = INVALID;
} // end if()
break;
case WRITE_OWNED:
break;
} // end switch (state)
edit("400");
return 0;
} // end openFile()
private int writeFile() {
FileOutputStream file = null;
switch (state) {
case INVALID:
try {
local = server.download(getClientHost(), fileName, "w");
file = new FileOutputStream(CACHE_NAME);
file.write(local.get());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (file != null) {
try {
file.close();
} catch (Exception e) {
e.printStackTrace();
} // end try file.close();
} // end if (file != null)
} // end try file = new FileOutputStream(CACHE_NAME);
write = false;
owner = false;
state = WRITE_OWNED;
break;
case READ_SHARED:
return state;
case WRITE_OWNED:
return state;
} // end switch (state)
edit("600");
return 0;
} // end writeFile()
private void edit(String mode) {
String[] cmdarray = new String[3];
cmdarray[0] = "chmod";
cmdarray[1] = mode;
cmdarray[2] = CACHE_NAME;
try {
Runtime runtime = Runtime.getRuntime( );
Process process = runtime.exec( cmdarray );
int retval = process.waitFor( );
} catch ( Exception e ) {
e.printStackTrace( );
} // end try Runtime runtime = Runtime.getRuntime( );
cmdarray = new String[2];
cmdarray[0] = EDITOR;
cmdarray[1] = CACHE_NAME;
try {
Runtime runtime = Runtime.getRuntime( );
Process process = runtime.exec( cmdarray );
int retval = process.waitFor( );
} catch ( Exception e ) {
e.printStackTrace( );
} // end try Runtime runtime = Runtime.getRuntime( );
if (state == RELEASE_OWN) {
state = READ_SHARED;
} // end if (state == RELEASE_OWN)
} // end edit(String)
} // end class DfsClient