diff options
4 files changed, 99 insertions, 50 deletions
diff --git a/src/main/java/org/openslx/imagemaster/server/ApiServer.java b/src/main/java/org/openslx/imagemaster/server/ApiServer.java index 104d98d..ee22493 100644 --- a/src/main/java/org/openslx/imagemaster/server/ApiServer.java +++ b/src/main/java/org/openslx/imagemaster/server/ApiServer.java @@ -96,7 +96,7 @@ public class ApiServer return ImageProcessor.getUploadInfos( serverSessionId, imageDescription, crcSums ); } - public static DownloadInfos getImage( String uuid, String serverSessionId, List<Integer> requestedBlocks ) throws AuthorizationException, ImageDataException + public static DownloadInfos getImage( String uuid, String serverSessionId ) throws AuthorizationException, ImageDataException { // first check session of server if ( ServerSessionManager.getSession( serverSessionId ) == null ) { @@ -108,7 +108,7 @@ public class ApiServer } // then let the image processor decide what to do - return ImageProcessor.getDownloadInfos( serverSessionId, uuid, requestedBlocks ); + return ImageProcessor.getDownloadInfos( serverSessionId, uuid, null ); } /** diff --git a/src/main/java/org/openslx/imagemaster/serverconnection/ConnectionHandler.java b/src/main/java/org/openslx/imagemaster/serverconnection/ConnectionHandler.java index 2faa9b9..0fb52f5 100644 --- a/src/main/java/org/openslx/imagemaster/serverconnection/ConnectionHandler.java +++ b/src/main/java/org/openslx/imagemaster/serverconnection/ConnectionHandler.java @@ -20,9 +20,11 @@ import javax.net.ssl.SSLContext; import org.apache.log4j.Logger; import org.openslx.filetransfer.Downloader; +import org.openslx.filetransfer.FileRange; import org.openslx.filetransfer.IncomingEvent; import org.openslx.filetransfer.Listener; import org.openslx.filetransfer.Uploader; +import org.openslx.filetransfer.WantRangeCallback; import org.openslx.imagemaster.Globals; /** @@ -116,12 +118,15 @@ public class ConnectionHandler implements IncomingEvent @Override public void incomingUploader( Uploader uploader ) throws IOException { - // try to read meta data - if ( !uploader.readMetaData() ) - return; String token = uploader.getToken(); log.debug( "Got token :'" + token + "'" ); // check token to identify the client + if (token == null) + { + uploader.sendErrorCode( "No token available." ); + uploader.close(null); + return; + } if ( !connections.containsKey( token ) ) { uploader.sendErrorCode( "Token not accepted." ); uploader.close(null); @@ -135,17 +140,8 @@ public class ConnectionHandler implements IncomingEvent return; } - List<Integer> blocks = connections.get( token ).client.getLastRequestedBlocks( token ); String fileName = connections.get( token ).filepath; - long fileSize = new File( fileName ).length(); - long actual; - for ( Integer block : blocks ) { - actual = block * Globals.blockSize; - uploader.prepareSendRange( actual, ( ( fileSize - actual ) < Globals.blockSize ) ? fileSize : ( block + 1 ) * Globals.blockSize ); - uploader.sendFile( fileName ); - } - uploader.close(null); - + uploader.upload(fileName); } /** @@ -155,40 +151,83 @@ public class ConnectionHandler implements IncomingEvent public void incomingDownloader( Downloader downloader ) throws IOException { log.debug( "Client wants to upload" ); - long startOfRange = 0; - String token = ""; - // try to read meta data - while ( downloader.readMetaData() ) { - // check token to identify the client - token = downloader.getToken(); - if ( !connections.containsKey( token ) ) { - downloader.sendErrorCode( "Token not accepted." ); - downloader.close(null); - return; - } - - startOfRange = downloader.getStartOfRange(); - - if ( downloader.getDiffOfRange() <= 0 ) { - return; - } - - // check if he was a uploading client - if ( connections.get( token ).type == Connection.DOWNLOADING ) { - downloader.sendErrorCode( "You can not upload, if you are downloading." ); - downloader.close(null); - return; - } - - int blockNumber = (int) ( startOfRange / Globals.blockSize ); - UploadingImage image = connections.get( token ).image; - image.setNeedsCheck( blockNumber ); - image.increaseTransmittedTimes( blockNumber ); - log.debug( "Block " + blockNumber + " was transmitted " + image.getTimesTransmitted( blockNumber ) + " time(s)." ); - - downloader.setOutputFilename( connections.get( token ).filepath ); - downloader.receiveBinary(); + String token = downloader.getToken(); + if (token == null) + { + downloader.sendErrorCode( "No token available." ); + downloader.close(null); + return; + } + // Check token to identify the client. + if ( !connections.containsKey( token ) ) { + downloader.sendErrorCode( "Token not accepted." ); + downloader.close(null); + return; } + + // check if he was a uploading client + if ( connections.get( token ).type == Connection.DOWNLOADING ) { + downloader.sendErrorCode( "You can not upload, if you are downloading." ); + downloader.close(null); + return; + } + + String destinationFileName = connections.get( token ).filepath; + final UploadingImage image = connections.get( token ).image; + downloader.download( destinationFileName, new WantRangeCallback() { + + @Override + public FileRange get() { + // get start of range. + int blockNumber = image.getNextMissingBlock(); + if (blockNumber == -1) + return null; + + image.setNeedsCheck( blockNumber ); + image.increaseTransmittedTimes( blockNumber ); + log.debug( "Block " + blockNumber + " was transmitted " + image.getTimesTransmitted( blockNumber ) + " time(s)." ); + + long startOfRange = image.getNextMissingBlock() * Globals.blockSize; + long endOfRange = Math.min(startOfRange + Globals.blockSize, image.getImageFile().length()); + FileRange range = new FileRange(startOfRange, endOfRange); + return range; + } + }); +// long startOfRange = 0; +// String token = ""; +// // try to read meta data +// while ( downloader.readMetaData() ) { +// // check token to identify the client +// token = downloader.getToken(); +// if ( !connections.containsKey( token ) ) { +// downloader.sendErrorCode( "Token not accepted." ); +// downloader.close(null); +// return; +// } +// +// startOfRange = downloader.getStartOfRange(); +// +// if ( downloader.getDiffOfRange() <= 0 ) { +// return; +// } +// +// // check if he was a uploading client +// if ( connections.get( token ).type == Connection.DOWNLOADING ) { +// downloader.sendErrorCode( "You can not upload, if you are downloading." ); +// downloader.close(null); +// return; +// } +// +// +// int blockNumber = (int) ( startOfRange / Globals.blockSize ); +// UploadingImage image = connections.get( token ).image; +// image.setNeedsCheck( blockNumber ); +// image.increaseTransmittedTimes( blockNumber ); +// log.debug( "Block " + blockNumber + " was transmitted " + image.getTimesTransmitted( blockNumber ) + " time(s)." ); +// +// downloader.setOutputFilename( connections.get( token ).filepath ); +// downloader.receiveBinary(); +// } downloader.close(null); } } diff --git a/src/main/java/org/openslx/imagemaster/serverconnection/UploadingImage.java b/src/main/java/org/openslx/imagemaster/serverconnection/UploadingImage.java index b0b035b..169877c 100644 --- a/src/main/java/org/openslx/imagemaster/serverconnection/UploadingImage.java +++ b/src/main/java/org/openslx/imagemaster/serverconnection/UploadingImage.java @@ -149,6 +149,16 @@ public class UploadingImage /////////////////////////////////////////////////////////////////// return blockStatus.length; } + + protected int getNextMissingBlock() + { + // TODO: handle intern status of current uploading block. + for (int i = 0; i < blockStatus.length; i++) { + if (blockStatus[i] == 0) + return i; + } + return -1; + } protected long getTimestamp() { diff --git a/src/main/java/org/openslx/imagemaster/thrift/server/ImageServerHandler.java b/src/main/java/org/openslx/imagemaster/thrift/server/ImageServerHandler.java index ad3a57d..6ca2384 100644 --- a/src/main/java/org/openslx/imagemaster/thrift/server/ImageServerHandler.java +++ b/src/main/java/org/openslx/imagemaster/thrift/server/ImageServerHandler.java @@ -69,9 +69,9 @@ public class ImageServerHandler implements ImageServer.Iface } @Override - public DownloadInfos getImage( String uuid, String serverSessionId, List<Integer> requestedBlocks ) throws AuthorizationException, ImageDataException, TException + public DownloadInfos getImage( String uuid, String serverSessionId ) throws AuthorizationException, ImageDataException, TException { - return ApiServer.getImage( uuid, serverSessionId, requestedBlocks ); + return ApiServer.getImage( uuid, serverSessionId ); } @Override |