diff options
Diffstat (limited to 'NET/worlds/scape/BackgroundLoader.java')
| -rw-r--r-- | NET/worlds/scape/BackgroundLoader.java | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/NET/worlds/scape/BackgroundLoader.java b/NET/worlds/scape/BackgroundLoader.java new file mode 100644 index 0000000..8f337a0 --- /dev/null +++ b/NET/worlds/scape/BackgroundLoader.java @@ -0,0 +1,95 @@ +package NET.worlds.scape; + +import NET.worlds.console.Main; +import NET.worlds.console.MainCallback; +import NET.worlds.network.URL; +import java.util.Vector; + +public class BackgroundLoader implements MainCallback, Runnable { + private static BackgroundLoader bg = new BackgroundLoader(); + private BackgroundLoaderQueue readQueue = new BackgroundLoaderQueue(); + private BackgroundLoaderQueue asyncLoadQueue = new BackgroundLoaderQueue(); + private Vector syncLoadQueue = new Vector(); + private Thread asyncLoaderThread; + int nextSync = 0; + + public static void get(BGLoaded loader, String remoteName) { + get(loader, URL.make(remoteName), false); + } + + public static void get(BGLoaded loader, URL remoteName) { + get(loader, remoteName, false); + } + + public static void get(BGLoaded loader, URL remoteName, boolean highPri) { + new BackgroundLoaderElement(loader, remoteName, highPri); + } + + static void activeRoomChanged(Room room) { + bg.asyncLoadQueue.activeRoomChanged(room); + } + + static void asyncLoad(BackgroundLoaderElement ele) { + bg.asyncLoadQueue.add(ele); + } + + static void syncLoad(BackgroundLoaderElement ele) { + bg.syncLoadQueue.addElement(ele); + } + + private BackgroundLoader() { + this.asyncLoaderThread = new Thread(this); + this.asyncLoaderThread.setDaemon(true); + this.asyncLoaderThread.start(); + Main.register(this); + } + + @Override + public void run() { + if ($assertionsDisabled || Thread.currentThread() == this.asyncLoaderThread) { + while (true) { + BackgroundLoaderElement ele = this.asyncLoadQueue.getItem(); + ele.asyncLoad(); + synchronized (this) { + this.syncLoadQueue.addElement(ele); + + while (this.syncLoadQueue.size() > 0) { + try { + this.wait(); + } catch (InterruptedException var4) { + } + + if (this.asyncLoadQueue.hasHighPriorityItems()) { + break; + } + } + } + } + } + + throw new AssertionError(); + } + + @Override + public synchronized void mainCallback() { + if (this.syncLoadQueue.size() != 0) { + if (this.nextSync >= this.syncLoadQueue.size()) { + this.nextSync = 0; + } + + BackgroundLoaderElement ele = (BackgroundLoaderElement)this.syncLoadQueue.elementAt(this.nextSync); + if (!ele.syncLoad()) { + this.syncLoadQueue.removeElementAt(this.nextSync); + if (this.syncLoadQueue.size() == 0) { + this.notify(); + } + } else { + if (this.asyncLoadQueue.hasHighPriorityItems()) { + this.notify(); + } + + this.nextSync++; + } + } + } +} |