Skip to content

Commit

Permalink
PackageControllerQueue:
Browse files Browse the repository at this point in the history
-extends Queue
-allows peek/executeQueuedAction
PackageController/PackageControllerTable/SelectionInfo:
-updated to make use of ReadOnlyQueueAction
LinkCollector/DownloadController:
-updated save to make use of new PackageControllerQueue.peek and PackageControllerQueue.executeQueuedAction methods

refs thread 96430 

git-svn-id: svn://svn.jdownloader.org/jdownloader/trunk@49880 ebf7c1c2-ba36-0410-9fe8-c592906822b4

Former-commit-id: 2074a51dd9aed84af6ed31fb194060c5f0901079
  • Loading branch information
jiaz committed Sep 27, 2024
1 parent 24958d3 commit d47e9aa
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 45 deletions.
51 changes: 30 additions & 21 deletions src/jd/controlling/downloadcontroller/DownloadController.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import jd.controlling.packagecontroller.AbstractNode;
import jd.controlling.packagecontroller.AbstractPackageChildrenNodeFilter;
import jd.controlling.packagecontroller.PackageController;
import jd.controlling.packagecontroller.PackageControllerQueue.ReadOnlyQueueAction;
import jd.parser.Regex;
import jd.plugins.DownloadLink;
import jd.plugins.DownloadLinkProperty;
import jd.plugins.DownloadLinkStorable;
import jd.plugins.FilePackage;
import jd.plugins.FilePackageProperty;
import jd.plugins.PluginForHost;
import jd.utils.JDUtilities;

import org.appwork.controlling.SingleReachableState;
import org.appwork.exceptions.WTFException;
import org.appwork.scheduler.DelayedRunnable;
Expand All @@ -55,6 +68,7 @@
import org.appwork.storage.config.JsonConfig;
import org.appwork.storage.simplejson.JSonFactory;
import org.appwork.utils.Application;
import org.appwork.utils.DebugMode;
import org.appwork.utils.IO;
import org.appwork.utils.StringUtils;
import org.appwork.utils.event.Eventsender;
Expand Down Expand Up @@ -86,18 +100,6 @@
import org.jdownloader.settings.GeneralSettings.CreateFolderTrigger;
import org.jdownloader.settings.staticreferences.CFG_GENERAL;

import jd.controlling.packagecontroller.AbstractNode;
import jd.controlling.packagecontroller.AbstractPackageChildrenNodeFilter;
import jd.controlling.packagecontroller.PackageController;
import jd.parser.Regex;
import jd.plugins.DownloadLink;
import jd.plugins.DownloadLinkProperty;
import jd.plugins.DownloadLinkStorable;
import jd.plugins.FilePackage;
import jd.plugins.FilePackageProperty;
import jd.plugins.PluginForHost;
import jd.utils.JDUtilities;

public class DownloadController extends PackageController<FilePackage, DownloadLink> {
private final transient DownloadControllerEventSender eventSender = new DownloadControllerEventSender();
private final DelayedRunnable downloadSaver;
Expand Down Expand Up @@ -178,7 +180,7 @@ public String toString() {
}
});
final DownloadControllerConfig cfg = JsonConfig.create(DownloadControllerConfig.class);
final long minimumDelay = Math.max(5000, cfg.getMinimumSaveDelay());
final long minimumDelay = Math.max(DebugMode.TRUE_IN_IDE_ELSE_FALSE ? 1 : 5000, cfg.getMinimumSaveDelay());
long maximumDelay = cfg.getMaximumSaveDelay();
if (maximumDelay <= 0) {
maximumDelay = -1;
Expand Down Expand Up @@ -601,15 +603,15 @@ private final void setFilePackage(FilePackage filePackage) {

private final ArrayList<IndexedDownloadLink> downloadLinks = new ArrayList<IndexedDownloadLink>();
private final static Comparator<IndexedDownloadLink> COMPARATOR = new Comparator<IndexedDownloadLink>() {
private final int compare(int x, int y) {
return (x < y) ? -1 : ((x == y) ? 0 : 1);
}
private final int compare(int x, int y) {
return (x < y) ? -1 : ((x == y) ? 0 : 1);
}

@Override
public int compare(IndexedDownloadLink o1, IndexedDownloadLink o2) {
return compare(o1.getIndex(), o2.getIndex());
}
};
@Override
public int compare(IndexedDownloadLink o1, IndexedDownloadLink o2) {
return compare(o1.getIndex(), o2.getIndex());
}
};

private FilePackage getLoadedPackage() {
final FilePackage filePackage = this.filePackage;
Expand Down Expand Up @@ -1013,6 +1015,7 @@ public void removeListener(final DownloadControllerListener l) {
* @param file
*/
private boolean save(java.util.List<FilePackage> packages, File file) throws IOException {
final boolean isShuttingDown = ShutdownController.getInstance().isShuttingDown();
synchronized (SAVELOADLOCK) {
if (file == null) {
if (downloadLists.size() > 0) {
Expand Down Expand Up @@ -1142,6 +1145,12 @@ public void flush() throws IOException {
}
int childIndex = 0;
for (final DownloadLink link : pkg.getChildren()) {
if (!isShuttingDown) {
final QueueAction<?, ? extends Throwable> waiting = DownloadController.this.getQueue().peek();
if (waiting instanceof ReadOnlyQueueAction) {
DownloadController.this.getQueue().executeQueuedAction(waiting);
}
}
final DownloadLinkStorable linkStorable = new DownloadLinkStorable(link);
final String childEntryID = String.format(childFormat, childIndex++);
final ZipEntry linkEntry = new ZipEntry(packageEntryID + "_" + childEntryID);
Expand Down
26 changes: 5 additions & 21 deletions src/jd/controlling/packagecontroller/PackageController.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicLong;

import javax.swing.SwingUtilities;
import jd.controlling.packagecontroller.PackageControllerQueue.ReadOnlyQueueAction;

import org.appwork.utils.DebugMode;
import org.appwork.utils.ModifyLock;
import org.appwork.utils.Regex;
import org.appwork.utils.StringUtils;
Expand Down Expand Up @@ -104,22 +103,7 @@ protected ModifyLock getMapLock() {
return mapLock;
}

protected final Queue QUEUE = new Queue(getClass().getName()) {
@Override
public void killQueue() {
org.appwork.utils.logging2.extmanager.LoggerFactory.getDefaultLogger().log(new Throwable("YOU CANNOT KILL ME!"));
/*
* this queue can't be killed
*/
}

public <E, T extends Throwable> E addWait(QueueAction<E, T> item) throws T {
if (DebugMode.TRUE_IN_IDE_ELSE_FALSE && SwingUtilities.isEventDispatchThread()) {
new Exception("This should be done via callback to avoid queue<->edt deadlocks").printStackTrace();
}
return super.addWait(item);
};
};
protected final PackageControllerQueue QUEUE = new PackageControllerQueue(getClass().getName());

/**
* add a Package at given position position in this PackageController. in case the Package is already controlled by this
Expand All @@ -132,7 +116,7 @@ protected void addmovePackageAt(final PackageType pkg, final int index) {
addmovePackageAt(pkg, index, false);
}

public Queue getQueue() {
public PackageControllerQueue getQueue() {
return QUEUE;
}

Expand Down Expand Up @@ -828,7 +812,7 @@ protected Void run() throws RuntimeException {
/* remove all */
/*
* TODO: speed optimization, we have to correct the index to match changes in children structure
*
*
* TODO: optimize this loop. only process existing links in this package
*/
for (final ChildType child : elementsToMove) {
Expand Down Expand Up @@ -1161,7 +1145,7 @@ public SelectionInfo<PackageType, ChildType> getSelectionInfo() {
if (lSelectionInfo != null && lSelectionInfo.getBackendVersion() == version) {
return lSelectionInfo;
}
return getQueue().addWait(new QueueAction<SelectionInfo<PackageType, ChildType>, RuntimeException>(Queue.QueuePriority.HIGH) {
return getQueue().addWait(new ReadOnlyQueueAction<SelectionInfo<PackageType, ChildType>, RuntimeException>(Queue.QueuePriority.HIGH) {
@Override
protected SelectionInfo<PackageType, ChildType> run() throws RuntimeException {
final long version = getBackendChanged();
Expand Down
73 changes: 73 additions & 0 deletions src/jd/controlling/packagecontroller/PackageControllerQueue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package jd.controlling.packagecontroller;

import javax.swing.SwingUtilities;

import org.appwork.utils.DebugMode;
import org.appwork.utils.event.queue.Queue;
import org.appwork.utils.event.queue.QueueAction;
import org.appwork.utils.logging2.LogInterface;

public class PackageControllerQueue extends Queue {

public static abstract class ReadOnlyQueueAction<T, E extends Throwable> extends QueueAction<T, E> {

public ReadOnlyQueueAction() {
super();
}

public ReadOnlyQueueAction(QueuePriority prio) {
super(prio);
}
}

protected PackageControllerQueue(String id) {
super(id);
}

@Override
public void killQueue() {
getLogger().log(new Throwable("YOU CANNOT KILL ME!"));
/*
* this queue can't be killed
*/
}

public <E, T extends Throwable> E addWait(QueueAction<E, T> item) throws T {
if (DebugMode.TRUE_IN_IDE_ELSE_FALSE && SwingUtilities.isEventDispatchThread()) {
getLogger().log(new Exception("This should be done via callback to avoid queue<->edt deadlocks"));
}
return super.addWait(item);
};

@Override
public QueueAction<?, ? extends Throwable> peek() {
if (!DebugMode.TRUE_IN_IDE_ELSE_FALSE) {
return null;
}
try {
return super.peek();
} catch (Throwable e) {
// compatibility with incompatible core
return null;
}
}

public boolean executeQueuedAction(final QueueAction<?, ?> item) {
if (item != null && !item.gotStarted() && isQueueThread(item) && isQueued(item)) {
try {
super.startItem(item, true);
} catch (Throwable e) {
getLogger().log(e);
}
remove(item);
return true;
} else {
return false;
}
}

protected LogInterface getLogger() {
return org.appwork.utils.logging2.extmanager.LoggerFactory.getDefaultLogger();
}

}
4 changes: 2 additions & 2 deletions src/org/jdownloader/gui/views/SelectionInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
import jd.controlling.packagecontroller.AbstractPackageChildrenNode;
import jd.controlling.packagecontroller.AbstractPackageNode;
import jd.controlling.packagecontroller.PackageController;
import jd.controlling.packagecontroller.PackageControllerQueue.ReadOnlyQueueAction;
import jd.plugins.DownloadLink;
import jd.plugins.FilePackage;
import jd.plugins.PluginForHost;

import org.appwork.utils.event.queue.Queue;
import org.appwork.utils.event.queue.Queue.QueuePriority;
import org.appwork.utils.event.queue.QueueAction;
import org.jdownloader.controlling.UniqueAlltimeID;

public class SelectionInfo<PackageType extends AbstractPackageNode<ChildrenType, PackageType>, ChildrenType extends AbstractPackageChildrenNode<PackageType>> {
Expand Down Expand Up @@ -114,7 +114,7 @@ public SelectionInfo(final AbstractNode contextObject, final List<? extends Abst

protected void aggregate(Queue queue) {
if (queue != null) {
queue.addWait(new QueueAction<Void, RuntimeException>(QueuePriority.HIGH) {
queue.addWait(new ReadOnlyQueueAction<Void, RuntimeException>(QueuePriority.HIGH) {
@Override
protected Void run() throws RuntimeException {
aggregate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import jd.controlling.packagecontroller.AbstractPackageChildrenNode;
import jd.controlling.packagecontroller.AbstractPackageNode;
import jd.controlling.packagecontroller.PackageController;
import jd.controlling.packagecontroller.PackageControllerQueue.ReadOnlyQueueAction;
import jd.gui.swing.jdgui.BasicJDTable;

import org.appwork.exceptions.WTFException;
Expand Down Expand Up @@ -344,7 +345,7 @@ public Void edtRun() {
}
}.start(invokeLater);
} else {
getModel().getController().getQueue().add(new QueueAction<Void, RuntimeException>(Queue.QueuePriority.HIGH) {
getModel().getController().getQueue().add(new ReadOnlyQueueAction<Void, RuntimeException>(Queue.QueuePriority.HIGH) {
@Override
protected Void run() throws RuntimeException {
final SelectionInfo<ParentType, ChildrenType> selectionInfo = getModel().getController().getSelectionInfo();
Expand Down

0 comments on commit d47e9aa

Please sign in to comment.