summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/serverconnection/IncomingTransfer.java
blob: bfc65e154691c3e74f0a1be1f5449db0c28f235e (plain) (blame)
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
package org.openslx.imagemaster.serverconnection;

import java.io.File;
import java.io.FileNotFoundException;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.UUID;

import org.openslx.bwlp.thrift.iface.ImagePublishData;
import org.openslx.bwlp.thrift.iface.TInvocationException;
import org.openslx.bwlp.thrift.iface.TransferInformation;
import org.openslx.filetransfer.util.ChunkStatus;
import org.openslx.filetransfer.util.FileChunk;
import org.openslx.filetransfer.util.IncomingTransferBase;
import org.openslx.imagemaster.Globals;
import org.openslx.imagemaster.db.mappers.DbImageBlock;
import org.openslx.imagemaster.util.Util;
import org.openslx.util.ThriftUtil;

public class IncomingTransfer extends IncomingTransferBase
{

	private static final long MIN_FREE_SPACE_BYTES = FileChunk.CHUNK_SIZE * 10;

	private final String imageVersionId;

	public IncomingTransfer( ImagePublishData img, List<ByteBuffer> blockHashes )
			throws TInvocationException, FileNotFoundException
	{
		super( UUID.randomUUID().toString(), new File( new File( Globals.getImageDir(), img.imageBaseId ), img.imageVersionId ),
				img.fileSize, ThriftUtil.unwrapByteBufferList( blockHashes ) );
		this.imageVersionId = img.imageVersionId;
	}

	@Override
	public String getRelativePath()
	{
		return Util.getRelativePath( getTmpFileName(), new File( Globals.getImageDir() ) );
	}

	@Override
	public synchronized void cancel()
	{
		super.cancel();
		getTmpFileName().delete();
	}

	@Override
	protected boolean hasEnoughFreeSpace()
	{
		long space = Globals.getImagePath().getUsableSpace();
		return space > MIN_FREE_SPACE_BYTES;
	}

	@Override
	protected boolean finishIncomingTransfer()
	{
		potentialFinishTime.set( System.currentTimeMillis() );
		return true;
	}

	@Override
	public TransferInformation getTransferInfo()
	{
		return new TransferInformation( getId(), Globals.getFiletransferPortPlain(), Globals.getFiletransferPortSsl() );
	}

	@Override
	protected void chunkStatusChanged( FileChunk chunk )
	{
		ChunkStatus status = chunk.getStatus();
		if ( status == ChunkStatus.MISSING || status == ChunkStatus.COMPLETE ) {
			try {
				DbImageBlock.asyncUpdate( imageVersionId, chunk );
			} catch ( InterruptedException e ) {
				e.printStackTrace();
			}
		}
	}

}