mirror of https://github.com/zlatinb/muwire
revert delaying of downloads as the semaphore approach is enough. GitHub issue #90
parent
3d72497d5f
commit
0e0b30c3a6
|
@ -31,11 +31,9 @@ import com.muwire.core.collections.FileCollection
|
|||
import com.muwire.core.collections.FileCollectionItem
|
||||
import com.muwire.core.collections.UIDownloadCollectionEvent
|
||||
|
||||
import java.util.concurrent.BlockingQueue
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import java.util.concurrent.Executor
|
||||
import java.util.concurrent.Executors
|
||||
import java.util.concurrent.LinkedBlockingQueue
|
||||
import java.util.logging.Level
|
||||
|
||||
@Log
|
||||
|
@ -54,10 +52,6 @@ public class DownloadManager {
|
|||
|
||||
private final Map<InfoHash, Downloader> downloaders = new ConcurrentHashMap<>()
|
||||
|
||||
private final BlockingQueue<DelayedStart> pendingStart = new LinkedBlockingQueue<>()
|
||||
private volatile boolean shutdown
|
||||
private final Thread pendingProcessor
|
||||
|
||||
public DownloadManager(EventBus eventBus, TrustService trustService, MeshManager meshManager, MuWireSettings muSettings,
|
||||
I2PConnector connector, File home, Persona me, ChatServer chatServer, FileManager fileManager) {
|
||||
this.eventBus = eventBus
|
||||
|
@ -76,31 +70,6 @@ public class DownloadManager {
|
|||
rv.setDaemon(true)
|
||||
rv
|
||||
})
|
||||
|
||||
pendingProcessor = new Thread({processDelayed()} as Runnable)
|
||||
pendingProcessor.setName("download-delayer")
|
||||
pendingProcessor.setDaemon(true)
|
||||
pendingProcessor.start()
|
||||
}
|
||||
|
||||
private void processDelayed() {
|
||||
while(!shutdown) {
|
||||
DelayedStart ds = pendingStart.poll()
|
||||
if (ds != null) {
|
||||
Downloader downloader = ds.downloader
|
||||
if (!ds.resume) {
|
||||
if (!downloader.paused)
|
||||
executor.execute({ downloader.download() } as Runnable)
|
||||
eventBus.publish(new DownloadStartedEvent(downloader: downloader))
|
||||
} else
|
||||
executor.execute({downloader.doResume() as Runnable})
|
||||
}
|
||||
Thread.sleep(100)
|
||||
}
|
||||
}
|
||||
|
||||
void queueForResume(Downloader downloader) {
|
||||
pendingStart.offer(new DelayedStart(downloader, true))
|
||||
}
|
||||
|
||||
|
||||
|
@ -174,7 +143,8 @@ public class DownloadManager {
|
|||
}
|
||||
downloaders.put(infoHash, downloader)
|
||||
persistDownloaders()
|
||||
pendingStart.offer(new DelayedStart(downloader, false))
|
||||
executor.execute({downloader.download()} as Runnable)
|
||||
eventBus.publish(new DownloadStartedEvent(downloader: downloader))
|
||||
return downloader
|
||||
}
|
||||
|
||||
|
@ -196,10 +166,6 @@ public class DownloadManager {
|
|||
persistDownloaders()
|
||||
}
|
||||
|
||||
void resume(Downloader downloader) {
|
||||
executor.execute({downloader.download() as Runnable})
|
||||
}
|
||||
|
||||
void onUILoadedEvent(UILoadedEvent e) {
|
||||
File downloadsFile = new File(home, "downloads.json")
|
||||
if (!downloadsFile.exists())
|
||||
|
@ -263,8 +229,15 @@ public class DownloadManager {
|
|||
}
|
||||
|
||||
|
||||
try {
|
||||
if (!downloader.paused)
|
||||
downloader.download()
|
||||
downloaders.put(infoHash, downloader)
|
||||
pendingStart.offer(new DelayedStart(downloader, false))
|
||||
eventBus.publish(new DownloadStartedEvent(downloader : downloader))
|
||||
} catch (IllegalArgumentException bad) {
|
||||
log.log(Level.WARNING,"cannot start downloader, skipping", bad)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -342,8 +315,6 @@ public class DownloadManager {
|
|||
}
|
||||
|
||||
public void shutdown() {
|
||||
shutdown = true
|
||||
pendingProcessor.interrupt()
|
||||
downloaders.values().each { it.stop() }
|
||||
Downloader.executorService.shutdownNow()
|
||||
}
|
||||
|
@ -357,13 +328,4 @@ public class DownloadManager {
|
|||
downloaders.values().each { total += it.speed() }
|
||||
total
|
||||
}
|
||||
|
||||
private static class DelayedStart {
|
||||
final Downloader downloader
|
||||
final boolean resume
|
||||
DelayedStart(Downloader downloader, boolean resume) {
|
||||
this.downloader = downloader
|
||||
this.resume = resume
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -225,7 +225,7 @@ class DownloadSession {
|
|||
System.arraycopy(infoHash.getHashList(), piece * 32, expected, 0, 32)
|
||||
if (hash != expected) {
|
||||
pieces.markPartial(piece, 0)
|
||||
throw new BadHashException("bad hash on piece $piece for infoHash " + Base64.encode(infoHash.getRoot()))
|
||||
throw new BadHashException("bad hash on piece $piece")
|
||||
}
|
||||
|
||||
eventBus.publish(new SourceVerifiedEvent(infoHash : infoHash, source : endpoint.destination))
|
||||
|
|
|
@ -119,7 +119,7 @@ abstract class Downloader {
|
|||
|
||||
public void resume() {
|
||||
paused = false
|
||||
downloadManager.queueForResume(this)
|
||||
doResume()
|
||||
}
|
||||
|
||||
protected abstract void doResume();
|
||||
|
|
Loading…
Reference in New Issue