diff options
| author | Fuwn <[email protected]> | 2026-02-12 22:33:32 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2026-02-12 22:33:32 -0800 |
| commit | c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9 (patch) | |
| tree | df9f48bf128a6c0186a8e91857d6ff30fe0e9f18 /NET/worlds/network/ServerTracker.java | |
| download | worldsplayer-c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9.tar.xz worldsplayer-c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9.zip | |
Initial commit
Diffstat (limited to 'NET/worlds/network/ServerTracker.java')
| -rw-r--r-- | NET/worlds/network/ServerTracker.java | 202 |
1 files changed, 202 insertions, 0 deletions
diff --git a/NET/worlds/network/ServerTracker.java b/NET/worlds/network/ServerTracker.java new file mode 100644 index 0000000..7de2fbc --- /dev/null +++ b/NET/worlds/network/ServerTracker.java @@ -0,0 +1,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]"; + } +} |