PackageControllerQueue:

-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
This commit is contained in:
jiaz 2024-09-27 11:33:36 +00:00
parent 24958d3837
commit d47e9aa03b
5 changed files with 112 additions and 45 deletions

View File

@ -43,6 +43,19 @@ import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream; import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream; 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.controlling.SingleReachableState;
import org.appwork.exceptions.WTFException; import org.appwork.exceptions.WTFException;
import org.appwork.scheduler.DelayedRunnable; import org.appwork.scheduler.DelayedRunnable;
@ -55,6 +68,7 @@ import org.appwork.storage.TypeRef;
import org.appwork.storage.config.JsonConfig; import org.appwork.storage.config.JsonConfig;
import org.appwork.storage.simplejson.JSonFactory; import org.appwork.storage.simplejson.JSonFactory;
import org.appwork.utils.Application; import org.appwork.utils.Application;
import org.appwork.utils.DebugMode;
import org.appwork.utils.IO; import org.appwork.utils.IO;
import org.appwork.utils.StringUtils; import org.appwork.utils.StringUtils;
import org.appwork.utils.event.Eventsender; import org.appwork.utils.event.Eventsender;
@ -86,18 +100,6 @@ import org.jdownloader.settings.GeneralSettings;
import org.jdownloader.settings.GeneralSettings.CreateFolderTrigger; import org.jdownloader.settings.GeneralSettings.CreateFolderTrigger;
import org.jdownloader.settings.staticreferences.CFG_GENERAL; 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> { public class DownloadController extends PackageController<FilePackage, DownloadLink> {
private final transient DownloadControllerEventSender eventSender = new DownloadControllerEventSender(); private final transient DownloadControllerEventSender eventSender = new DownloadControllerEventSender();
private final DelayedRunnable downloadSaver; private final DelayedRunnable downloadSaver;
@ -178,7 +180,7 @@ public class DownloadController extends PackageController<FilePackage, DownloadL
} }
}); });
final DownloadControllerConfig cfg = JsonConfig.create(DownloadControllerConfig.class); 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(); long maximumDelay = cfg.getMaximumSaveDelay();
if (maximumDelay <= 0) { if (maximumDelay <= 0) {
maximumDelay = -1; maximumDelay = -1;
@ -601,15 +603,15 @@ public class DownloadController extends PackageController<FilePackage, DownloadL
private final ArrayList<IndexedDownloadLink> downloadLinks = new ArrayList<IndexedDownloadLink>(); private final ArrayList<IndexedDownloadLink> downloadLinks = new ArrayList<IndexedDownloadLink>();
private final static Comparator<IndexedDownloadLink> COMPARATOR = new Comparator<IndexedDownloadLink>() { private final static Comparator<IndexedDownloadLink> COMPARATOR = new Comparator<IndexedDownloadLink>() {
private final int compare(int x, int y) { private final int compare(int x, int y) {
return (x < y) ? -1 : ((x == y) ? 0 : 1); return (x < y) ? -1 : ((x == y) ? 0 : 1);
} }
@Override @Override
public int compare(IndexedDownloadLink o1, IndexedDownloadLink o2) { public int compare(IndexedDownloadLink o1, IndexedDownloadLink o2) {
return compare(o1.getIndex(), o2.getIndex()); return compare(o1.getIndex(), o2.getIndex());
} }
}; };
private FilePackage getLoadedPackage() { private FilePackage getLoadedPackage() {
final FilePackage filePackage = this.filePackage; final FilePackage filePackage = this.filePackage;
@ -1013,6 +1015,7 @@ public class DownloadController extends PackageController<FilePackage, DownloadL
* @param file * @param file
*/ */
private boolean save(java.util.List<FilePackage> packages, File file) throws IOException { private boolean save(java.util.List<FilePackage> packages, File file) throws IOException {
final boolean isShuttingDown = ShutdownController.getInstance().isShuttingDown();
synchronized (SAVELOADLOCK) { synchronized (SAVELOADLOCK) {
if (file == null) { if (file == null) {
if (downloadLists.size() > 0) { if (downloadLists.size() > 0) {
@ -1142,6 +1145,12 @@ public class DownloadController extends PackageController<FilePackage, DownloadL
} }
int childIndex = 0; int childIndex = 0;
for (final DownloadLink link : pkg.getChildren()) { 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 DownloadLinkStorable linkStorable = new DownloadLinkStorable(link);
final String childEntryID = String.format(childFormat, childIndex++); final String childEntryID = String.format(childFormat, childIndex++);
final ZipEntry linkEntry = new ZipEntry(packageEntryID + "_" + childEntryID); final ZipEntry linkEntry = new ZipEntry(packageEntryID + "_" + childEntryID);

View File

@ -15,9 +15,8 @@ import java.util.WeakHashMap;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicLong; 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.ModifyLock;
import org.appwork.utils.Regex; import org.appwork.utils.Regex;
import org.appwork.utils.StringUtils; import org.appwork.utils.StringUtils;
@ -104,22 +103,7 @@ public abstract class PackageController<PackageType extends AbstractPackageNode<
return mapLock; return mapLock;
} }
protected final Queue QUEUE = new Queue(getClass().getName()) { protected final PackageControllerQueue QUEUE = new PackageControllerQueue(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);
};
};
/** /**
* add a Package at given position position in this PackageController. in case the Package is already controlled by this * add a Package at given position position in this PackageController. in case the Package is already controlled by this
@ -132,7 +116,7 @@ public abstract class PackageController<PackageType extends AbstractPackageNode<
addmovePackageAt(pkg, index, false); addmovePackageAt(pkg, index, false);
} }
public Queue getQueue() { public PackageControllerQueue getQueue() {
return QUEUE; return QUEUE;
} }
@ -828,7 +812,7 @@ public abstract class PackageController<PackageType extends AbstractPackageNode<
/* remove all */ /* remove all */
/* /*
* TODO: speed optimization, we have to correct the index to match changes in children structure * 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 * TODO: optimize this loop. only process existing links in this package
*/ */
for (final ChildType child : elementsToMove) { for (final ChildType child : elementsToMove) {
@ -1161,7 +1145,7 @@ public abstract class PackageController<PackageType extends AbstractPackageNode<
if (lSelectionInfo != null && lSelectionInfo.getBackendVersion() == version) { if (lSelectionInfo != null && lSelectionInfo.getBackendVersion() == version) {
return lSelectionInfo; 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 @Override
protected SelectionInfo<PackageType, ChildType> run() throws RuntimeException { protected SelectionInfo<PackageType, ChildType> run() throws RuntimeException {
final long version = getBackendChanged(); final long version = getBackendChanged();

View File

@ -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();
}
}

View File

@ -18,13 +18,13 @@ import jd.controlling.packagecontroller.AbstractNode;
import jd.controlling.packagecontroller.AbstractPackageChildrenNode; import jd.controlling.packagecontroller.AbstractPackageChildrenNode;
import jd.controlling.packagecontroller.AbstractPackageNode; import jd.controlling.packagecontroller.AbstractPackageNode;
import jd.controlling.packagecontroller.PackageController; import jd.controlling.packagecontroller.PackageController;
import jd.controlling.packagecontroller.PackageControllerQueue.ReadOnlyQueueAction;
import jd.plugins.DownloadLink; import jd.plugins.DownloadLink;
import jd.plugins.FilePackage; import jd.plugins.FilePackage;
import jd.plugins.PluginForHost; import jd.plugins.PluginForHost;
import org.appwork.utils.event.queue.Queue; import org.appwork.utils.event.queue.Queue;
import org.appwork.utils.event.queue.Queue.QueuePriority; import org.appwork.utils.event.queue.Queue.QueuePriority;
import org.appwork.utils.event.queue.QueueAction;
import org.jdownloader.controlling.UniqueAlltimeID; import org.jdownloader.controlling.UniqueAlltimeID;
public class SelectionInfo<PackageType extends AbstractPackageNode<ChildrenType, PackageType>, ChildrenType extends AbstractPackageChildrenNode<PackageType>> { public class SelectionInfo<PackageType extends AbstractPackageNode<ChildrenType, PackageType>, ChildrenType extends AbstractPackageChildrenNode<PackageType>> {
@ -114,7 +114,7 @@ public class SelectionInfo<PackageType extends AbstractPackageNode<ChildrenType,
protected void aggregate(Queue queue) { protected void aggregate(Queue queue) {
if (queue != null) { if (queue != null) {
queue.addWait(new QueueAction<Void, RuntimeException>(QueuePriority.HIGH) { queue.addWait(new ReadOnlyQueueAction<Void, RuntimeException>(QueuePriority.HIGH) {
@Override @Override
protected Void run() throws RuntimeException { protected Void run() throws RuntimeException {
aggregate(); aggregate();

View File

@ -31,6 +31,7 @@ import jd.controlling.packagecontroller.AbstractNode;
import jd.controlling.packagecontroller.AbstractPackageChildrenNode; import jd.controlling.packagecontroller.AbstractPackageChildrenNode;
import jd.controlling.packagecontroller.AbstractPackageNode; import jd.controlling.packagecontroller.AbstractPackageNode;
import jd.controlling.packagecontroller.PackageController; import jd.controlling.packagecontroller.PackageController;
import jd.controlling.packagecontroller.PackageControllerQueue.ReadOnlyQueueAction;
import jd.gui.swing.jdgui.BasicJDTable; import jd.gui.swing.jdgui.BasicJDTable;
import org.appwork.exceptions.WTFException; import org.appwork.exceptions.WTFException;
@ -344,7 +345,7 @@ public abstract class PackageControllerTable<ParentType extends AbstractPackageN
} }
}.start(invokeLater); }.start(invokeLater);
} else { } else {
getModel().getController().getQueue().add(new QueueAction<Void, RuntimeException>(Queue.QueuePriority.HIGH) { getModel().getController().getQueue().add(new ReadOnlyQueueAction<Void, RuntimeException>(Queue.QueuePriority.HIGH) {
@Override @Override
protected Void run() throws RuntimeException { protected Void run() throws RuntimeException {
final SelectionInfo<ParentType, ChildrenType> selectionInfo = getModel().getController().getSelectionInfo(); final SelectionInfo<ParentType, ChildrenType> selectionInfo = getModel().getController().getSelectionInfo();