diff options
Diffstat (limited to 'dozentenmodul/src/main/java/org/openslx/dozmod/filetransfer/AsyncHashGenerator.java')
-rw-r--r-- | dozentenmodul/src/main/java/org/openslx/dozmod/filetransfer/AsyncHashGenerator.java | 57 |
1 files changed, 39 insertions, 18 deletions
diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/filetransfer/AsyncHashGenerator.java b/dozentenmodul/src/main/java/org/openslx/dozmod/filetransfer/AsyncHashGenerator.java index f53d5d0b..83fbb212 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/filetransfer/AsyncHashGenerator.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/filetransfer/AsyncHashGenerator.java @@ -23,7 +23,7 @@ public class AsyncHashGenerator extends Thread { private static final Logger LOGGER = Logger.getLogger(AsyncHashGenerator.class); - private final String uploadToken; + private String uploadToken = null; private final RandomAccessFile file; private final List<ByteBuffer> blockHashes; private final MessageDigest digester; @@ -32,9 +32,7 @@ public class AsyncHashGenerator extends Thread { private volatile boolean isCanceled = false; - public AsyncHashGenerator(String uploadToken, File uploadFile) throws FileNotFoundException, - NoSuchAlgorithmException { - this.uploadToken = uploadToken; + public AsyncHashGenerator(File uploadFile) throws FileNotFoundException, NoSuchAlgorithmException { try { digester = MessageDigest.getInstance("SHA-1"); } catch (NoSuchAlgorithmException e1) { @@ -54,6 +52,13 @@ public class AsyncHashGenerator extends Thread { setName("HashGenerator"); } + public void setUploadToken(String token) { + if (!isCanceled && this.uploadToken == null) { + this.uploadToken = token; + submitHashes(); + } + } + @Override public void run() { Checker checker = new Checker(); @@ -92,7 +97,6 @@ public class AsyncHashGenerator extends Thread { } } finally { Util.safeClose(file); - LOGGER.debug("AsyncHashGenerator done"); } } @@ -147,20 +151,16 @@ public class AsyncHashGenerator extends Thread { isCanceled = true; break; } - blockHashes.add(ByteBuffer.wrap(hash)); - LOGGER.debug("blockIndex=" + blockIndex + ", list.size()=" + list.size()); - if (blockIndex <= 5 || blockIndex % 20 == 0 || blockIndex + 1 == list.size()) { - try { - ThriftManager.getSatClient().updateBlockHashes(uploadToken, blockHashes); - } catch (TInvalidTokenException e) { - LOGGER.warn("Cannot send hashList to satellite: Sat claims uploadToken is invalid!"); - isCanceled = true; + synchronized (blockHashes) { + blockHashes.add(ByteBuffer.wrap(hash)); + } + if (blockIndex % 20 == 0 || blockIndex + 1 == list.size()) { + if (!submitHashes()) break; - } catch (TException e) { - LOGGER.warn("Unknown exception when submitting hashList to sat", e); - } - if (blockIndex + 1 == list.size()) + if (blockIndex + 1 == list.size()) { + LOGGER.debug("Hashing done"); break; + } } } } catch (InterruptedException e) { @@ -168,9 +168,30 @@ public class AsyncHashGenerator extends Thread { interrupt(); } finally { thisGenerator.interrupt(); - LOGGER.debug("Hasher done"); } } } + /** + * Submit current list of hashes. + * + * @return false if the token is not known to the server + */ + private boolean submitHashes() { + synchronized (blockHashes) { + if (uploadToken == null) // No token yet, cannot submit + return true; + try { + ThriftManager.getSatClient().updateBlockHashes(uploadToken, blockHashes); + } catch (TInvalidTokenException e) { + LOGGER.warn("Cannot send hashList to satellite: Sat claims uploadToken is invalid!"); + isCanceled = true; + return false; + } catch (TException e) { + LOGGER.warn("Unknown exception when submitting hashList to sat", e); + } + return true; + } + } + } |