summaryrefslogtreecommitdiff
path: root/NET/worlds/network/ServerTracker.java
blob: 7de2fbc24d261c5585e6188fb5dbd4dcadb398ed (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package NET.worlds.network;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

class ServerTracker {
   private Hashtable<String, WorldServer> _serverHash = new Hashtable<String, WorldServer>();
   private Vector<WorldServer> _pendingOpenServers = new Vector<WorldServer>();
   private Vector<WorldServer> _activeServers = new Vector<WorldServer>();
   private Vector<WorldServer> _pendingCloseServers = new Vector<WorldServer>();
   private Galaxy _galaxy;
   private ServerURL _serverURL;

   protected ServerTracker(Galaxy parent) {
      this._galaxy = parent;
      this._serverURL = this._galaxy.getServerURL();
   }

   public synchronized WorldServer getServer(String StrServerURL) throws InvalidServerURLException {
      if ((Galaxy.getDebugLevel() & 32) > 0) {
         System.out.println("Galaxy.getServer(" + StrServerURL + ") from " + this._galaxy);
      }

      ServerURL serverURL = new ServerURL(StrServerURL);
      WorldServer ws = null;
      if (this._serverURL.getHost().equals(serverURL.getHost())) {
         ws = this.getServer(this._activeServers, this);
         if (ws == null) {
            ws = this.getServer(this._pendingOpenServers, this);
         }
      }

      if (ws == null) {
         ws = this.findOrMake(serverURL, this);
      }

      ws.tmpRefCnt(this);
      return ws;
   }

   protected WorldServer getActive(Object referrer) {
      return this.getServer(this._activeServers, referrer);
   }

   protected synchronized WorldServer getServer(Vector<WorldServer> list, Object referrer) {
      int lastServer = list.size() - 1;
      if (lastServer >= 0) {
         WorldServer ws = list.elementAt(lastServer);
         ws.incRefCnt(referrer);
         return ws;
      } else {
         return null;
      }
   }

   private synchronized WorldServer findOrMake(ServerURL serverURL, Object referrer) throws InvalidServerURLException {
      WorldServer ws = this._serverHash.get(serverURL.getHost());
      if (ws == null) {
         int _debugLevel = 0;
         _debugLevel = Galaxy.getDebugLevel();
         if ((_debugLevel & 32) > 0) {
            System.out.println("    Creating new server of type=" + serverURL.getType() + ".");
         }

         try {
            Class<?> wsClass = Class.forName("NET.worlds.network." + serverURL.getType());
            ws = (WorldServer)wsClass.newInstance();
         } catch (Exception var8) {
            Exception e = var8;
            if ((_debugLevel & 32) > 0) {
               synchronized (System.out) {
                  System.out.println("    Exception during class creation.");
                  e.printStackTrace(System.out);
               }
            }

            throw new InvalidServerURLException("Bad class: " + serverURL.getType());
         }

         this._serverHash.put(serverURL.getHost(), ws);
         ws.initInstance(this._galaxy, serverURL);
      } else if ((Galaxy.getDebugLevel() & 32) > 0) {
         System.out.println("    Found old server.");
      }

      ws.incRefCnt(referrer);
      return ws;
   }

   protected synchronized void swapServer(WorldServer oldServ, WorldServer newServ) {
      String oldURL = oldServ.getServerURL().getHost();

      assert oldURL.equals(newServ.getServerURL().getHost());

      oldURL = oldServ.getServerURL().getHost();
      this._serverHash.put(oldURL, newServ);

      assert !this._activeServers.contains(oldServ);
   }

   protected synchronized boolean isActive() {
      return this._activeServers.size() > 0 || this._pendingOpenServers.size() > 0 || this._pendingCloseServers.size() > 0;
   }

   protected synchronized boolean addPendingServer(WorldServer ws) {
      if ((Galaxy.getDebugLevel() & 8) > 0) {
         System.out.println(this._galaxy + ": addPendingServer(" + ws + ")");
         System.out.println("\t_pendingOpen = " + this._pendingOpenServers);
         System.out.println("\t_activeServers = " + this._activeServers);
         System.out.println("\t_pendingClose = " + this._pendingCloseServers);
      }

      if (this._pendingOpenServers.indexOf(ws) < 0) {
         this._pendingOpenServers.addElement(ws);
      }

      return this._pendingOpenServers.size() == 1 && this._activeServers.size() == 0 && this._pendingCloseServers.size() == 0;
   }

   protected synchronized boolean addActiveServer(WorldServer ws) {
      if ((Galaxy.getDebugLevel() & 8) > 0) {
         System.out.println(this._galaxy + ": addActiveServer(" + ws + ")");
         System.out.println("\t_pendingOpen = " + this._pendingOpenServers);
         System.out.println("\t_activeServers = " + this._activeServers);
         System.out.println("\t_pendingClose = " + this._pendingCloseServers);
      }

      this._pendingOpenServers.removeElement(ws);
      if (this._activeServers.indexOf(ws) < 0) {
         this._activeServers.addElement(ws);
      }

      return this._activeServers.size() == 1 && this._pendingCloseServers.size() == 0;
   }

   protected synchronized boolean addClosingServer(WorldServer ws) {
      if ((Galaxy.getDebugLevel() & 8) > 0) {
         System.out.println(this._galaxy + ": addClosingServer(" + ws + ")");
         System.out.println("\t_pendingOpen = " + this._pendingOpenServers);
         System.out.println("\t_activeServers = " + this._activeServers);
         System.out.println("\t_pendingClose = " + this._pendingCloseServers);
      }

      this._pendingOpenServers.removeElement(ws);
      boolean wasActive = this._activeServers.removeElement(ws);
      if (this._pendingCloseServers.indexOf(ws) < 0) {
         this._pendingCloseServers.addElement(ws);
      }

      return wasActive && this._activeServers.size() == 0;
   }

   protected synchronized void markClosedServer(WorldServer ws) {
      if ((Galaxy.getDebugLevel() & 8) > 0) {
         System.out.println(this._galaxy + ": markClosedServer(" + ws + ")");
         System.out.println("\t_pendingOpen = " + this._pendingOpenServers);
         System.out.println("\t_activeServers = " + this._activeServers);
         System.out.println("\t_pendingClose = " + this._pendingCloseServers);
      }

      this._pendingOpenServers.removeElement(ws);
      this._pendingCloseServers.removeElement(ws);
      this._activeServers.removeElement(ws);
   }

   protected synchronized void killServer(WorldServer ws) {
      if ((Galaxy.getDebugLevel() & 8) > 0) {
         System.out.println(this._galaxy + ": killServer(" + ws + ")");
         System.out.println("\t_pendingOpen = " + this._pendingOpenServers);
         System.out.println("\t_activeServers = " + this._activeServers);
         System.out.println("\t_pendingClose = " + this._pendingCloseServers);
      }

      this._pendingOpenServers.removeElement(ws);
      this._pendingCloseServers.removeElement(ws);
      this._activeServers.removeElement(ws);
      if (this._serverHash.get(ws.getServerURL().getHost()) == ws) {
         this._serverHash.remove(ws.getServerURL().getHost());
      } else {
         System.out.println("DEBUG -- a server tried to murder another!");
         System.out.println("Caller = " + ws);
         System.out.println("Victim = " + this._serverHash.get(ws.getServerURL().getHost()));
         new Exception().printStackTrace();
      }
   }

   protected Enumeration<WorldServer> getAllServers() {
      return this._serverHash.elements();
   }

   @Override
   public String toString() {
      return "ServerTracker[\n\t_pendingOpen = "
         + this._pendingOpenServers
         + "\n\t_activeServers = "
         + this._activeServers
         + "\n\t_pendingClose = "
         + this._pendingCloseServers
         + "\n]";
   }
}