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
|
package org.openslx.imagemaster.serverconnection;
import java.io.File;
import java.io.FileNotFoundException;
import java.nio.ByteBuffer;
import java.sql.SQLException;
import java.util.List;
import java.util.UUID;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
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.DbImage;
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 Logger LOGGER = LogManager.getLogger( IncomingTransfer.class );
private static final long MIN_FREE_SPACE_BYTES = FileChunk.CHUNK_SIZE * 10;
private final String imageVersionId;
private final TransferInformation transferInfo;
public IncomingTransfer( ImagePublishData img, List<ByteBuffer> blockHashes, File absDestination, int plainPort, int sslPort )
throws TInvocationException, FileNotFoundException
{
super( UUID.randomUUID().toString(), absDestination, img.fileSize, ThriftUtil.unwrapByteBufferList( blockHashes ), null );
this.imageVersionId = img.imageVersionId;
this.transferInfo = new TransferInformation( getId(), plainPort, sslPort );
// If the file already exists, see if any chunks are already complete
if ( absDestination.exists() && absDestination.length() > 0 ) {
try {
List<Boolean> statusList = DbImageBlock.getMissingStatusList( img.imageVersionId );
if ( !statusList.isEmpty() ) {
getChunks().resumeFromStatusList( statusList, absDestination.length() );
for ( int i = 0; i < 3; ++i ) {
queueUnhashedChunk( false );
}
}
} catch ( SQLException e ) {
}
}
LOGGER.info( "Incoming transfer started" );
}
@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()
{
if ( Globals.isReadOnlyMode() )
return false;
long space = Globals.getImagePath().getUsableSpace();
return space > MIN_FREE_SPACE_BYTES;
}
@Override
protected boolean finishIncomingTransfer()
{
potentialFinishTime.set( System.currentTimeMillis() );
try {
DbImage.markValid( this.imageVersionId, true );
} catch ( SQLException e ) {
// Nothing to do
}
LOGGER.info( "Incoming transfer ended" );
return true;
}
@Override
public TransferInformation getTransferInfo()
{
return transferInfo;
}
@Override
protected void chunkStatusChanged( FileChunk chunk )
{
if ( chunk.getFailCount() > 6 ) {
cancel();
LOGGER.warn( "Server is cancelling upload of Version " + imageVersionId
+ ": Hash check for block " + chunk.getChunkIndex()
+ " failed " + chunk.getFailCount()
+ " times." );
}
ChunkStatus status = chunk.getStatus();
if ( status == ChunkStatus.MISSING || status == ChunkStatus.COMPLETE ) {
try {
DbImageBlock.asyncUpdate( imageVersionId, chunk );
} catch ( InterruptedException e ) {
e.printStackTrace();
}
}
}
public Object getImageVersionId()
{
return imageVersionId;
}
}
|