mirror of
https://github.com/tonikelope/megabasterd.git
synced 2025-04-29 22:24:32 +02:00
JOIN CHUNKS LOCK (one file at time)
This commit is contained in:
parent
91dafbce45
commit
5353110c3e
@ -19,6 +19,7 @@ import java.nio.file.Paths;
|
|||||||
import java.security.InvalidAlgorithmParameterException;
|
import java.security.InvalidAlgorithmParameterException;
|
||||||
import java.security.InvalidKeyException;
|
import java.security.InvalidKeyException;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
import javax.crypto.CipherInputStream;
|
import javax.crypto.CipherInputStream;
|
||||||
@ -32,6 +33,8 @@ public class ChunkWriterManager implements Runnable, SecureSingleThreadNotifiabl
|
|||||||
|
|
||||||
private static final Logger LOG = Logger.getLogger(ChunkWriterManager.class.getName());
|
private static final Logger LOG = Logger.getLogger(ChunkWriterManager.class.getName());
|
||||||
|
|
||||||
|
private static final ReentrantLock JOIN_CHUNKS_LOCK = new ReentrantLock();
|
||||||
|
|
||||||
public static long calculateChunkOffset(long chunk_id, int size_multi) {
|
public static long calculateChunkOffset(long chunk_id, int size_multi) {
|
||||||
long[] offs = {0, 128, 384, 768, 1280, 1920, 2688};
|
long[] offs = {0, 128, 384, 768, 1280, 1920, 2688};
|
||||||
|
|
||||||
@ -179,8 +182,16 @@ public class ChunkWriterManager implements Runnable, SecureSingleThreadNotifiabl
|
|||||||
LOG.log(Level.INFO, "{0} ChunkWriterManager LAST CHUNK WRITTEN -> [{1}] {2} {3}...", new Object[]{Thread.currentThread().getName(), _last_chunk_id_written, _bytes_written, _download.getFile_name()});
|
LOG.log(Level.INFO, "{0} ChunkWriterManager LAST CHUNK WRITTEN -> [{1}] {2} {3}...", new Object[]{Thread.currentThread().getName(), _last_chunk_id_written, _bytes_written, _download.getFile_name()});
|
||||||
boolean download_finished = false;
|
boolean download_finished = false;
|
||||||
if (_file_size > 0) {
|
if (_file_size > 0) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
while (!_exit && (!_download.isStopped() || !_download.getChunkworkers().isEmpty()) && _bytes_written < _file_size) {
|
while (!_exit && (!_download.isStopped() || !_download.getChunkworkers().isEmpty()) && _bytes_written < _file_size) {
|
||||||
|
|
||||||
|
if (!JOIN_CHUNKS_LOCK.isHeldByCurrentThread()) {
|
||||||
|
LOG.log(Level.INFO, "{0} ChunkWriterManager: JOIN LOCK LOCKED FOR {1}", new Object[]{Thread.currentThread().getName(), _download.getFile_name()});
|
||||||
|
JOIN_CHUNKS_LOCK.lock();
|
||||||
|
}
|
||||||
|
|
||||||
if (!download_finished && _download.getProgress() == _file_size) {
|
if (!download_finished && _download.getProgress() == _file_size) {
|
||||||
|
|
||||||
finishDownload();
|
finishDownload();
|
||||||
@ -226,8 +237,8 @@ public class ChunkWriterManager implements Runnable, SecureSingleThreadNotifiabl
|
|||||||
chunk_file.delete();
|
chunk_file.delete();
|
||||||
|
|
||||||
chunk_file = new File(getChunks_dir() + "/" + MiscTools.HashString("sha1", _download.getUrl()) + ".chunk" + String.valueOf(_last_chunk_id_written + 1));
|
chunk_file = new File(getChunks_dir() + "/" + MiscTools.HashString("sha1", _download.getUrl()) + ".chunk" + String.valueOf(_last_chunk_id_written + 1));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
chunk_io_error = true;
|
chunk_io_error = true;
|
||||||
LOG.log(Level.WARNING, ex.getMessage());
|
LOG.log(Level.WARNING, ex.getMessage());
|
||||||
@ -240,10 +251,20 @@ public class ChunkWriterManager implements Runnable, SecureSingleThreadNotifiabl
|
|||||||
|
|
||||||
LOG.log(Level.INFO, "{0} ChunkWriterManager waiting for chunk [{1}] {2}...", new Object[]{Thread.currentThread().getName(), _last_chunk_id_written + 1, _download.getFile_name()});
|
LOG.log(Level.INFO, "{0} ChunkWriterManager waiting for chunk [{1}] {2}...", new Object[]{Thread.currentThread().getName(), _last_chunk_id_written + 1, _download.getFile_name()});
|
||||||
|
|
||||||
secureWait();
|
if (JOIN_CHUNKS_LOCK.isHeldByCurrentThread() && JOIN_CHUNKS_LOCK.isLocked()) {
|
||||||
|
LOG.log(Level.INFO, "{0} ChunkWriterManager: JOIN LOCK RELEASED FOR {1}", new Object[]{Thread.currentThread().getName(), _download.getFile_name()});
|
||||||
|
JOIN_CHUNKS_LOCK.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
secureWait();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
if (JOIN_CHUNKS_LOCK.isHeldByCurrentThread() && JOIN_CHUNKS_LOCK.isLocked()) {
|
||||||
|
LOG.log(Level.INFO, "{0} ChunkWriterManager: JOIN LOCK RELEASED FOR {1}", new Object[]{Thread.currentThread().getName(), _download.getFile_name()});
|
||||||
|
JOIN_CHUNKS_LOCK.unlock();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_bytes_written == _file_size && MiscTools.isDirEmpty(Paths.get(getChunks_dir()))) {
|
if (_bytes_written == _file_size && MiscTools.isDirEmpty(Paths.get(getChunks_dir()))) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user