bwLehrpool Masterserver
Manages authentication and sharing of virtual machines between participating institutions
IncomingTransfer.java
Go to the documentation of this file.
1 package org.openslx.imagemaster.serverconnection;
2 
3 import java.io.File;
4 import java.io.FileNotFoundException;
5 import java.nio.ByteBuffer;
6 import java.sql.SQLException;
7 import java.util.List;
8 import java.util.UUID;
9 
10 import org.apache.logging.log4j.LogManager;
11 import org.apache.logging.log4j.Logger;
12 import org.openslx.bwlp.thrift.iface.ImagePublishData;
13 import org.openslx.bwlp.thrift.iface.TInvocationException;
14 import org.openslx.bwlp.thrift.iface.TransferInformation;
15 import org.openslx.filetransfer.util.ChunkStatus;
16 import org.openslx.filetransfer.util.FileChunk;
17 import org.openslx.filetransfer.util.IncomingTransferBase;
22 import org.openslx.util.ThriftUtil;
23 
24 public class IncomingTransfer extends IncomingTransferBase
25 {
26 
27  private static final Logger LOGGER = LogManager.getLogger( IncomingTransfer.class );
28 
29  private static final long MIN_FREE_SPACE_BYTES = FileChunk.CHUNK_SIZE * 10;
30 
31  private final String imageVersionId;
32 
33  private final TransferInformation transferInfo;
34 
35  public IncomingTransfer( ImagePublishData img, List<ByteBuffer> blockHashes, File absDestination, int plainPort, int sslPort )
36  throws TInvocationException, FileNotFoundException
37  {
38  super( UUID.randomUUID().toString(), absDestination, img.fileSize, ThriftUtil.unwrapByteBufferList( blockHashes ), null );
39  this.imageVersionId = img.imageVersionId;
40  this.transferInfo = new TransferInformation( getId(), plainPort, sslPort );
41  // If the file already exists, see if any chunks are already complete
42  if ( absDestination.exists() && absDestination.length() > 0 ) {
43  try {
44  List<Boolean> statusList = DbImageBlock.getMissingStatusList( img.imageVersionId );
45  if ( !statusList.isEmpty() ) {
46  getChunks().resumeFromStatusList( statusList, absDestination.length() );
47  for ( int i = 0; i < 3; ++i ) {
48  queueUnhashedChunk( false );
49  }
50  }
51  } catch ( SQLException e ) {
52  }
53  }
54  LOGGER.info( "Incoming transfer started" );
55  }
56 
57  @Override
58  public String getRelativePath()
59  {
60  return Util.getRelativePath( getTmpFileName(), new File( Globals.getImageDir() ) );
61  }
62 
63  @Override
64  public synchronized void cancel()
65  {
66  super.cancel();
67  getTmpFileName().delete();
68  }
69 
70  @Override
71  protected boolean hasEnoughFreeSpace()
72  {
73  if ( Globals.isReadOnlyMode() )
74  return false;
75  long space = Globals.getImagePath().getUsableSpace();
76  return space > MIN_FREE_SPACE_BYTES;
77  }
78 
79  @Override
80  protected boolean finishIncomingTransfer()
81  {
82  potentialFinishTime.set( System.currentTimeMillis() );
83  try {
84  DbImage.markValid( this.imageVersionId, true );
85  } catch ( SQLException e ) {
86  // Nothing to do
87  }
88  LOGGER.info( "Incoming transfer ended" );
89  return true;
90  }
91 
92  @Override
93  public TransferInformation getTransferInfo()
94  {
95  return transferInfo;
96  }
97 
98  @Override
99  protected void chunkStatusChanged( FileChunk chunk )
100  {
101  if ( chunk.getFailCount() > 6 ) {
102  cancel();
103  LOGGER.warn( "Server is cancelling upload of Version " + imageVersionId
104  + ": Hash check for block " + chunk.getChunkIndex()
105  + " failed " + chunk.getFailCount()
106  + " times." );
107  }
108  ChunkStatus status = chunk.getStatus();
109  if ( status == ChunkStatus.MISSING || status == ChunkStatus.COMPLETE ) {
110  try {
111  DbImageBlock.asyncUpdate( imageVersionId, chunk );
112  } catch ( InterruptedException e ) {
113  e.printStackTrace();
114  }
115  }
116  }
117 
118  public Object getImageVersionId()
119  {
120  return imageVersionId;
121  }
122 
123 }
IncomingTransfer(ImagePublishData img, List< ByteBuffer > blockHashes, File absDestination, int plainPort, int sslPort)
static List< Boolean > getMissingStatusList(String imageVersionId)
static String getRelativePath(File absolutePath, File parentDir)
Definition: Util.java:222
Representing an image in the database.
Definition: DbImage.java:32
static boolean isReadOnlyMode()
Definition: Globals.java:162
Class to hold global constants and properties from 'config/global.properties'.
Definition: Globals.java:16
static void markValid(String imageVersionId, boolean isValid)
Definition: DbImage.java:146
Some utilities to make our lives easier.
Definition: Util.java:18
static void asyncUpdate(String imageVersionId, FileChunk chunk)