public class StorageServer extends java.lang.Object implements Storage, Command
Storage servers respond to client file access requests. The files accessible through a storage server are those accessible under a given directory of the local filesystem.
Modifier and Type | Field and Description |
---|---|
(package private) Skeleton<Command> |
commandSkeleton |
private java.io.File |
root |
(package private) Skeleton<Storage> |
storageSkeleton |
Constructor and Description |
---|
StorageServer(java.io.File root)
Creates a storage server, given a directory on the local filesystem.
|
Modifier and Type | Method and Description |
---|---|
boolean |
copy(Path file,
Storage server)
Copies a file from another storage server.
|
boolean |
create(Path file)
Creates a file on the storage server.
|
boolean |
delete(Path path)
Deletes a file or directory on the storage server.
|
private java.lang.Boolean |
deleteHelper(java.io.File f) |
private void |
directoryCleanup(java.io.File root) |
byte[] |
read(Path file,
long offset,
int length)
Reads a sequence of bytes from a file.
|
long |
size(Path file)
Returns the length of a file, in bytes.
|
void |
start(java.lang.String hostname,
Registration naming_server)
Starts the storage server and registers it with the given naming
server.
|
void |
stop()
Stops the storage server.
|
protected void |
stopped(java.lang.Throwable cause)
Called when the storage server has shut down.
|
void |
write(Path file,
long offset,
byte[] data)
Writes bytes to a file.
|
public StorageServer(java.io.File root)
root
- Directory on the local filesystem. The contents of this
directory will be accessible through the storage server.java.lang.NullPointerException
- If root
is null
.public void start(java.lang.String hostname, Registration naming_server) throws RMIException, java.net.UnknownHostException, java.io.FileNotFoundException
hostname
- The externally-routable hostname of the local host on
which the storage server is running. This is used to
ensure that the stub which is provided to the naming
server by the start
method carries the
externally visible hostname or address of this storage
server.naming_server
- Remote interface for the naming server with which
the storage server is to register.java.net.UnknownHostException
- If a stub cannot be created for the storage
server because a valid address has not been
assigned.java.io.FileNotFoundException
- If the directory with which the server was
created does not exist or is in fact a
file.RMIException
- If the storage server cannot be started, or if it
cannot be registered.private void directoryCleanup(java.io.File root)
public void stop()
The server should not be restarted.
protected void stopped(java.lang.Throwable cause)
cause
- The cause for the shutdown, if any, or null
if
the server was shut down by the user's request.public long size(Path file) throws java.io.FileNotFoundException, RMIException
Storage
size
in interface Storage
file
- Path to the file.java.io.FileNotFoundException
- If the file cannot be found or the path
refers to a directory.RMIException
- If the call cannot be completed due to a network
error.public byte[] read(Path file, long offset, int length) throws java.io.FileNotFoundException, java.io.IOException, RMIException
Storage
read
in interface Storage
file
- Path to the file.offset
- Offset into the file to the beginning of the sequence.length
- The number of bytes to be read.java.io.FileNotFoundException
- If the file cannot be found or the path
refers to a directory.java.io.IOException
- If the file read cannot be completed on the server.RMIException
- If the call cannot be completed due to a network
error.public void write(Path file, long offset, byte[] data) throws java.io.FileNotFoundException, java.io.IOException
Storage
write
in interface Storage
file
- Path to the file.offset
- Offset into the file where data is to be written.data
- Array of bytes to be written.java.io.FileNotFoundException
- If the file cannot be found or the path
refers to a directory.java.io.IOException
- If the file write cannot be completed on the server.public boolean create(Path file)
Command
private java.lang.Boolean deleteHelper(java.io.File f)
public boolean delete(Path path)
Command
If the file is a directory and cannot be deleted, some, all, or none of its contents may be deleted by this operation.
public boolean copy(Path file, Storage server) throws RMIException, java.io.FileNotFoundException, java.io.IOException
Command
copy
in interface Command
file
- Path to the file to be copied.server
- Storage server from which the file is to be downloaded.true
if the file is successfully copied;
false
otherwise.RMIException
- If the call cannot be completed due to a network
error, whether between the caller and this storage
server, or between the two storage servers.java.io.FileNotFoundException
- If the file is not present on the remote
storage server, or the path refers to a
directory.java.io.IOException
- If an I/O exception occurs either on the remote or
on this storage server.