package transfer.download;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import common.ClientServerProtocol;
import common.connection.ConnectionWriterInterface;
import common.files.NamedFile;
import connection.server.ServerConnection;
import transfer.AbstractFileTransfer;
public class DownloadRequest extends AbstractFileTransfer implements Serializable
{
private static final long serialVersionUID = -3698241191695815523L;
private static class RequestStatus extends AbstractStatus
{
public static final RequestStatus IDLE = new RequestStatus("idle");
public static final RequestStatus QUERYING = new RequestStatus("querying");
private RequestStatus(String name)
{
super(name);
}
}
public DownloadRequest(NamedFile file)
{
super(file);
transitTo(RequestStatus.IDLE);
}
public void query(ServerConnection serverConnection)
{
int protocolId = ClientServerProtocol.PROTOCOL_FILES;
int opcode = ClientServerProtocol.Files.OP_GET_INFO;
serverConnection.sendOverlapped(protocolId, opcode,
new ConnectionWriterInterface()
{
public void run(ObjectOutputStream os) throws IOException
{
os.writeObject(file);
}
});
transitTo(RequestStatus.QUERYING);
}
public void notifyQueryFailed()
{
transitTo(RequestStatus.IDLE);
}
private void readObject(ObjectInputStream in) throws IOException,
ClassNotFoundException
{
doReadObject(in);
transitTo(RequestStatus.IDLE);
}
private void writeObject(ObjectOutputStream out) throws IOException
{
doWriteObject(out);
}
}