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 (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++; } } } }