-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileClient.java
More file actions
508 lines (465 loc) · 15.1 KB
/
Copy pathFileClient.java
File metadata and controls
508 lines (465 loc) · 15.1 KB
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
import java.io.*; // IOException
import java.net.*; // InetAddress
import java.rmi.*; // Naming
import java.rmi.server.*; // UnicastRemoteObject
import java.rmi.registry.*; // rmiregistry
/**
* Is a DFS client program that downloads and uploads a file requested by the
* client. Its implementation is based on the session semantics and the write
* invalidation.
*/
public class FileClient extends UnicastRemoteObject
implements ClientInterface {
private BufferedReader input = null; // standard input
private final static String ramDiskFile // a cached file in /tmp: CHANGE!!
= "/tmp/munehiro.txt";
private ServerInterface server = null; // a DFS server interface
private File file = null; // a cached file in memory
private boolean emacs_option = false; // invoke emacs if true, otherwise vim
/**
* Is a constructor that connects to a given DFS server, creates a cache
* file entry and sets up the standard input.
*
* @param machinename the server's ip name
* @param port the server's port
*/
public FileClient( String machinename, String port, boolean emacs_option )
throws RemoteException {
// server search
try {
server = ( ServerInterface )Naming.lookup( "rmi://" + machinename +
":" + port +
"/fileserver" );
} catch ( Exception e ) {
e.printStackTrace( );
}
// file cache creation;
file = new File( );
// Standard input
input = new BufferedReader( new InputStreamReader( System.in ) );
// Enable emacs or not. If not, vim is invoked
this.emacs_option = emacs_option;
}
/**
* Is the main loop where the client program reads a file name and its
* acces mode from a client, downloads the file if it is not cached, and
* finally opens it with emacs or vim.
*/
public void loop( ) {
// go into the main loop
while( true ) {
// If a client doesn't type any inputs immediately, FileClient's main
// program won't do anything immediately. Therefore, let's start
// a writeback thread.
// This thread works in background so that it will uploads
// the cached file to the server as soon as it calls writeback( ).
WritebackThread writebackThread = new WritebackThread( );
writebackThread.start( );
// user input: file name and open mode
String filename = null;
String mode = null;
try {
System.out.println( "FileClient: Next file to open:" );
System.out.print( "\tFile name: " );
filename = input.readLine( );
if ( filename.equals( "quit" ) || filename.equals( "exit" ) ) {
if ( file.isStateWriteOwned( ) )
file.upload( );
System.exit( 0 );
} else if ( filename.equals( "" ) ) {
System.err.println( "Do it again" );
writebackThread.kill( );
continue;
}
System.out.print( "\tHow(r/w): " );
mode = input.readLine( );
if ( !mode.equals( "r" ) && !mode.equals( "w" ) ) {
System.err.println( "Do it again" );
writebackThread.kill( );
continue;
}
} catch ( IOException e ) {
e.printStackTrace( );
}
// Now, the main thread manipulates the cached file. It terminates
// the background thread that takes care of uploading the cached
// file to the server.
writebackThread.kill( );
// look through the cache
if ( file.hit( filename, mode ) != true ) {
// cache miss
if ( file.isStateWriteOwned( ) ) {
// replacement
file.upload( );
}
// download a file from the server
if ( file.download( filename, mode ) == false ) {
System.out.println( "File downloaded failed" );
continue;
}
}
// open an editor
file.launchEditor( mode );
}
}
/**
* Is an RMI function called by the DFS server to invalidate the current
* cached file.
*
* @return true if invalidation takes place in success
*/
public boolean invalidate( ) {
return file.invalidate( );
}
/**
* Is an RMI function called by the DFS server to write back the current
* cached file.
*
* @return true if writeback is scheduled in success
*/
public boolean writeback( ) {
return file.writeback( );
}
/**
* Is the main function that receives a DFS server's ip name and port#.
* It then instantiates a DFS client object and calls the loop( )
* function where it repeats downloading and uploading a file requested
* by the client user.
* @param args the array that includes server_ip in its 0th element,
* the server port in the 1st element, and the e option
* in the 2nd element. If e is added, emacs is poppoed out
* for editing a file. Otherwise, vim is invoked.
*/
public static void main( String[] args ) {
// checking arguments
if ( args.length != 2 && args.length != 3 ) {
System.err.println( "usage: java FileClient server_ip port# [e]" );
System.exit( -1 );
}
try {
// create a client object
FileClient client
= new FileClient( args[0], args[1],
( args.length == 3 && args[2].equals( "e" ) )
);
// registering myself
startRegistry( Integer.parseInt( args[1] ) );
Naming.rebind( "rmi://localhost:" + args[1] + "/fileclient",
client );
System.out.println( "rmi://localhost: " + args[1] + "/fileclient" +
" invokded" );
// goes into the main loop of file operations
client.loop( );
} catch ( Exception e ) {
e.printStackTrace( );
System.exit( 1 );
}
}
/**
* Starts an RMI registry in background, which relieves a user from
* manually starting the registry and thus prevents her/him from
* forgetting its termination upon a logout.
*/
private static void startRegistry( int port ) throws RemoteException {
try {
Registry registry =
LocateRegistry.getRegistry( port );
registry.list( );
}
catch ( RemoteException e ) {
Registry registry =
LocateRegistry.createRegistry( port );
}
}
/**
* Is a background thread that uploads the cached file back to the DFS
* server in response to its writeback request.
*/
private class WritebackThread extends Thread {
private boolean active = false; // reset when the main thread kills me
public WritebackThread( ) { // the constructor simply sets the
active = true; // active variable
}
// If the DFS server calls writeback( ) that changes the file state to
// state_back2readshared, I will write back the cached file to the
// server.
public void run( ) {
while ( isActive( ) ) {
if ( file.isStateBackToReadShared( ) )
file.upload( );
}
}
// This function is called by the main thread when it is about to
// manipulate the cached file with emacs/vim. I must be terminated.
synchronized void kill ( ) {
active = false;
try {
this.join( );
} catch ( InterruptedException e ) {
e.printStackTrace( );
}
}
// Is called by run( ) to check if I can be still alive.
synchronized boolean isActive( ) {
return active;
}
}
/**
* Is FileClient's private File class. The file client has only one file
* cache. Therefore this class instantiates only one object. It has all
* necessary file caching logics:
* invalidate( ), writeback( ), hit( ), download( ), upload( ),
* and launchEditor( )
*/
private class File {
private final static int state_invalid = 0;
private final static int state_readshared = 1;
private final static int state_writeowned = 2;
private final static int state_back2readshared = 3;
private int state = state_invalid;
private boolean inEdit = false;
private String name = "";
private boolean ownership = false;
private byte[] bytes = null;
private String myIpName = null;
public File( ) {
// My IP name
try {
InetAddress inetaddr = InetAddress.getLocalHost( );
myIpName = inetaddr.getHostName( );
} catch ( java.net.UnknownHostException e ) {
e.printStackTrace( );
}
}
public synchronized boolean isStateInvalid( ) {
return ( state == state_invalid );
}
public synchronized boolean isStateReadShared( ) {
return ( state == state_readshared );
}
public synchronized boolean isStateWriteOwned( ) {
System.out.println( "name = " + name +
" state = " + state +
" ownership = " + ownership );
return ( state == state_writeowned );
}
public synchronized boolean isStateBackToReadShared( ) {
return ( state == state_back2readshared );
}
/**
* If the cached file is in readshared, it will be invalidated.
* Otherwise, the file is owned and thus can't be invalidated.
*
* @return true if the current file cache can be invalidated
*/
public synchronized boolean invalidate( ) {
if ( state == state_readshared ) {
state = state_invalid;
System.out.println( "file( " + name + ") invalidated...state "
+ state );
return true;
} else
return false;
}
/**
* If the cached file is in writeowned, it will transit to
* readshared, so that the cached file can be uploaded to the
* server later.
*
* @return true if the current file cache can be uploaded later.
*/
public synchronized boolean writeback( ) {
if ( state == state_writeowned ) {
state = state_back2readshared;
return true;
} else
return false;
}
/**
* Checks if a given file is currently cached with a given mode (r/w).
*
* @return true if a given file is currently cached with a given mode
* r or w.
*/
public synchronized boolean hit( String name, String mode ) {
if ( !this.name.equals( name ) ) {
System.out.println( "file: " + name + " does not exist." );
return false;
}
if ( state == state_back2readshared ) {
System.out.println("file: " + name + " must be written back.");
return false;
}
if ( mode.equals( "r" ) && state != state_invalid ) {
System.out.println( "file: " + name + " exists for read." );
return true;
}
if ( mode.equals( "w" ) && state == state_writeowned ) {
System.out.println( "file: " + name + " is owned for write" );
return true;
}
System.out.println( "file: " + name + " accessed with " + mode );
return false;
}
/**
* Downloads file contents of a given filename from the server with
* mode (r/w).
* @param filename a file name the cleint wants to download
* @param mode "r" for read or "w" fro write
*
* @return true if a file download completes in success.
*/
public boolean download( String filename, String mode ) {
System.out.println( "downloading: " + filename + " with " +
mode + " mode" );
// state transition
synchronized( this ) {
switch( state ) {
case state_invalid:
if ( mode.equals( "r" ) )
state = state_readshared;
else if ( mode.equals( "w" ) ) {
state = state_writeowned;
}
break;
case state_readshared:
if ( mode.equals( "w" ) )
state = state_writeowned;
break;
}
}
// download file contents from the server
name = filename;
ownership = mode.equals( "w" );
try {
FileContents contents = server.download( myIpName,
name, mode );
bytes = contents.get( );
} catch ( RemoteException e ) {
e.printStackTrace( );
return false;
} catch ( NullPointerException e ) {
return false;
}
return true;
}
/**
* Uploads cached file contnets.
*
* @return true if a file upload completes in success.
*/
public boolean upload( ) {
System.out.println( "uploading: " + name + " start" );
// state transition
synchronized( this ) {
switch( state ) {
case state_writeowned:
state = state_invalid;
break;
case state_back2readshared:
state = state_readshared;
break;
}
}
// upload file contents to the server
FileContents contents = new FileContents( bytes );
try {
server.upload( myIpName, name, contents );
} catch ( RemoteException e ) {
e.printStackTrace( );
return false;
}
System.out.println( "uploading: " + name + " completed" );
return true;
}
/**
* Is called internally from launchEditor to execute a Unix command
* such as "chmod", "emacs", or "vim".
*
* @param cmd a unix command such as "chmod", "emacs", or "vim".
* @param arg1 the 1st argument passed to this Unix command. It must
* not be null
* @param arg2 the 2nd argument passed to this Unix command. It must
* not be null. If arg2 is not necessary, it must be ""
* @return true if a given command has been successfully invoked.
*/
private boolean execUnixCommand(String cmd, String arg1, String arg2) {
// create a string array that include a command and its arguments
String[] cmdarray
= arg2.equals( "" ) ? new String[2] : new String[3];
cmdarray[0] = cmd;
cmdarray[1] = arg1;
if ( !arg2.equals( "" ) )
cmdarray[2] = arg2;
try {
// Process builder with inherited IO should allow a console
// based editor to take over the terminal.
ProcessBuilder pb = new ProcessBuilder();
pb.command( cmdarray );
pb.redirectInput( ProcessBuilder.Redirect.INHERIT );
pb.redirectOutput( ProcessBuilder.Redirect.INHERIT );
pb.redirectError( ProcessBuilder.Redirect.INHERIT) ;
pb.start( ).waitFor( ); // invoke a Unix cmd and wait for it
} catch ( IOException e ) {
e.printStackTrace( );
return false;
} catch ( InterruptedException e ) {
e.printStackTrace( );
return false;
}
return true;
}
/**
* Is called by the main thread's loop( ) function. This function
* changes the file mode appropriately with chmod and thereafter
* invokes emacs or vim.
*
* @param mode the file access mode: "r" or "w"
* @return true if emacs/vim has been successuflly invoked and finished.
*/
public boolean launchEditor( String mode ) {
// chmod 600
if ( execUnixCommand( "chmod", "600", ramDiskFile ) == false )
return false;
// write the cached file contents to ramDiskFile /tmp/mfukuda.txt
try {
FileOutputStream file = new FileOutputStream( ramDiskFile );
file.write( bytes );
file.flush( );
file.close( );
} catch ( FileNotFoundException e ) {
e.printStackTrace( );
return false;
} catch ( IOException e ) {
e.printStackTrace( );
return false;
}
// chmod 400 or 600
if ( execUnixCommand( "chmod", mode.equals( "r" ) ? "400" : "600",
ramDiskFile ) == false )
return false;
// launch emacs or vim
boolean execCode = false;
if ( emacs_option )
execCode = execUnixCommand( "emacs", ramDiskFile, "" );
else
execCode = execUnixCommand( "vim", ramDiskFile, "" );
// read the updated file contents from ramDiskFile if "w" mode
if ( execCode == true && mode.equals( "w" ) ) {
try {
FileInputStream file = new FileInputStream( ramDiskFile );
bytes = new byte[file.available( )];
file.read( bytes );
file.close( );
} catch ( FileNotFoundException e ) {
e.printStackTrace( );
return false;
} catch ( IOException e ) {
e.printStackTrace( );
return false;
}
}
return true;
}
}
}