package transfer.upload;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import main.PeerProtocol;
import common.connection.ConnectionWriterInterface;
import common.files.SerializableFile;
import common.handlers.AbstractHandlerSet;
import common.handlers.EmptyHandlerSet;
import connection.peer.GenericPeerConnection;
import static main.ClientMain.*;
public class FileUploadConnection extends GenericPeerConnection
{
private static final EmptyHandlerSet HANDLER_SET = new EmptyHandlerSet();
private FileUpload upload;
private InputStream fis;
public FileUploadConnection(GenericPeerConnection source, SerializableFile file)
throws IOException
{
super(source);
upload = new FileUpload(file, this);
fis = new BufferedInputStream(getSharedFilesTable().openFile(upload.getFile()));
}
public void initializeConnection() throws IOException
{
upload.addToManager();
}
public void finalizeConnection()
{
upload.removeFromManager();
}
protected AbstractHandlerSet getHandlerSet()
{
return HANDLER_SET;
}
public void upload(long offset) throws IOException
{
upload.setCompletedSize(offset);
int protocolId = PeerProtocol.PROTOCOL_TRANSFER;
int opcode = PeerProtocol.Transfer.OP_FILE;
send(protocolId, opcode, new ConnectionWriterInterface()
{
public void run(ObjectOutputStream os) throws IOException
{
upload.transfer(fis, os);
}
});
}
}