summaryrefslogtreecommitdiff
path: root/NET/worlds/scape/BackgroundLoader.java
blob: 576570a4a57dba35f87634a23aaaffd3985bea86 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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 (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++;
         }
      }
   }
}