Finished up wait() and now deal with interrupts instead of waits and polling.
This commit is contained in:
parent
fae480946d
commit
00d09b342e
|
@ -1,50 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2011 moparisthebest
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Official forums are http://www.moparscape.org/smf/
|
||||
* Email me at admin@moparisthebest.com , I read it but don't usually respond.
|
||||
*/
|
||||
|
||||
package org.moparscape.res;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ALTest {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
final List<Object> downloadItems = new ArrayList<Object>(5);
|
||||
int uid = 0;
|
||||
downloadItems.add(new ALTest(uid));
|
||||
System.out.println("downloads size: "+downloadItems.size());
|
||||
System.out.println("downloads contains(uid): "+downloadItems.contains(uid));
|
||||
System.out.println("downloads contains(Integer(uid)): "+downloadItems.contains(new Integer(uid)));
|
||||
System.out.println("downloads contains(ALTest): "+downloadItems.contains(new ALTest(uid)));
|
||||
}
|
||||
|
||||
private int uid;
|
||||
|
||||
public ALTest(int uid) {
|
||||
this.uid = uid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
System.out.println("ALTest equals: " + other);
|
||||
return ((other instanceof ALTest) && ((ALTest) other).uid == this.uid) ||
|
||||
((other instanceof Integer) && other.equals(this.uid));
|
||||
}
|
||||
}
|
|
@ -78,26 +78,42 @@ public class ResourceGrabber {
|
|||
//System.out.println("filename: " + new URL("http://moparisthebest.com/bob/tom/cache.zip").getFile());
|
||||
ResourceGrabber rg = new ResourceGrabber();
|
||||
System.out.println("before downloads...");
|
||||
//rg.download("http://www.moparisthebest.com/downloads/cedegaSRC.tar.gz", "/home/mopar/tests/extest", true);
|
||||
rg.download("http://www.moparisthebest.com/downloads/cedegaSRC.tar.gz", "/home/mopar/tests/extest", true);
|
||||
//rg.download("http://mirror01.th.ifl.net/releases//maverick/ubuntu-10.10-desktop-i386.iso", "/home/mopar/tests/extest", false);
|
||||
//Thread.sleep(30000);
|
||||
int clientZipUID = rg.download("https://www.moparscape.org/libs/client.zip.gz", "/home/mopar/tests/extest", true);
|
||||
rg.wait(clientZipUID);
|
||||
System.out.println("after downloads...");
|
||||
System.out.println("returned: '"+rg.wait(clientZipUID)+"' after downloads...");
|
||||
|
||||
}
|
||||
|
||||
public void wait(int uid) throws InterruptedException {
|
||||
//synchronized (downloadItems) {
|
||||
DownloadHandle dh = new DownloadHandle(uid);
|
||||
while (downloadItems.contains(dh))
|
||||
Thread.sleep(delay);
|
||||
while(true){
|
||||
synchronized (downloadItems) {
|
||||
for (final DlListener dll : downloadItems)
|
||||
dll
|
||||
public boolean wait(int uid) {
|
||||
DlListener dll = null;
|
||||
// grab the listener for this uid
|
||||
synchronized (downloadItems) {
|
||||
for (final DlListener d : downloadItems) {
|
||||
if (d.uid == uid) {
|
||||
dll = d;
|
||||
break;
|
||||
}
|
||||
}
|
||||
//}
|
||||
}
|
||||
// if we couldn't find one, the download is finished, return
|
||||
if (dll == null)
|
||||
return true; // we don't really know how it ended, just return true I guess...
|
||||
AbstractDownloadListener.Status status = dll.getStatus();
|
||||
while (status != AbstractDownloadListener.Status.FINISHED
|
||||
&& status != AbstractDownloadListener.Status.STOPPED
|
||||
&& status != AbstractDownloadListener.Status.ERROR) {
|
||||
try {
|
||||
synchronized (dll) {
|
||||
dll.wait();
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
// just ignore it, let the loop go around again
|
||||
}
|
||||
status = dll.getStatus();
|
||||
}
|
||||
return status != AbstractDownloadListener.Status.ERROR;
|
||||
}
|
||||
|
||||
public int download(String url, String savePath, boolean extract) throws MalformedURLException {
|
||||
|
@ -181,8 +197,6 @@ public class ResourceGrabber {
|
|||
}
|
||||
break;
|
||||
case FINISHED:
|
||||
if(dll.waiter != null)
|
||||
dll.waiter.interrupt();
|
||||
break;
|
||||
case EXTRACTING:
|
||||
dll.setRunning();
|
||||
|
@ -226,26 +240,11 @@ public class ResourceGrabber {
|
|||
|
||||
}
|
||||
|
||||
private class DownloadHandle {
|
||||
int uid;
|
||||
|
||||
private DownloadHandle(int uid) {
|
||||
this.uid = uid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
return ((other instanceof DlListener) && ((DlListener) other).uid == this.uid) ||
|
||||
((other instanceof DownloadHandle) && ((DownloadHandle) other).uid == this.uid);
|
||||
}
|
||||
}
|
||||
|
||||
private class DlListener extends AbstractDownloadListener {
|
||||
|
||||
int uid;
|
||||
boolean extract;
|
||||
DownloadItemPanel dip = null;
|
||||
Thread waiter = null;
|
||||
|
||||
public DlListener(int uid, boolean extract) {
|
||||
this.uid = uid;
|
||||
|
@ -265,6 +264,9 @@ public class ResourceGrabber {
|
|||
for (String file : filesDownloaded)
|
||||
Downloader.extractFile(file, savePath, this);
|
||||
super.finished(savePath, filesDownloaded);
|
||||
synchronized (this) {
|
||||
this.notify();//waiter.interrupt();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -276,8 +278,7 @@ public class ResourceGrabber {
|
|||
@Override
|
||||
public boolean equals(Object other) {
|
||||
//System.out.println("DlListener equals: " + other);
|
||||
return ((other instanceof DlListener) && ((DlListener) other).uid == this.uid) ||
|
||||
((other instanceof DownloadHandle) && ((DownloadHandle) other).uid == this.uid);
|
||||
return ((other != null) && (other instanceof DlListener) && (((DlListener) other).uid == this.uid));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue