All pastes #776135 Raw Edit

Miscellany

public text v1 · immutable
·published 2007-11-15 21:52 UTC
rendered paste body
import java.sql.*;

public class server implements Runnable {

	public server() {

	}
	public static final int cycleTime = 500;
	public static boolean updateServer = false;
	public static int updateSeconds = 180;
	public static long startTime;

	public static void main(java.lang.String args[]) {
		clientHandler = new server();
		(new Thread(clientHandler)).start();
		playerHandler = new PlayerHandler();
		ObjectHandler = new ObjectHandler();
		QuestHandler = new QuestHandler();
		BonusHandler = new BonusHandler();
		npcHandler = new NPCHandler();
		itemHandler = new ItemHandler();
		shopHandler = new ShopHandler();
		int waitFails = 0;
		long lastTicks = System.currentTimeMillis();
		long totalTimeSpentProcessing = 0;
		int cycle = 0;
		while(!shutdownServer) {
		if(updateServer)
			calcTime();
			playerHandler.process();			// updates all player related stuff
			npcHandler.process();
			itemHandler.process();
			shopHandler.process();
			ObjectHandler.process();
			long timeSpent = System.currentTimeMillis() - lastTicks;
			totalTimeSpentProcessing += timeSpent;
			if(timeSpent >= cycleTime) {
			timeSpent = cycleTime;
			if(++waitFails > 100) {
			shutdownServer = true;
			misc.println("[KERNEL]: machine is too slow to run this server!");
			}}
			try {
			Thread.sleep(cycleTime-timeSpent);
			} catch(java.lang.Exception _ex) { }
			lastTicks = System.currentTimeMillis();
			cycle++;
			if(cycle % 100 == 0) {
			float time = ((float)totalTimeSpentProcessing)/cycle;
			}
			if (ShutDown == true) {
			if (ShutDownCounter >= 100) {
			shutdownServer = true;
			}
			ShutDownCounter++;
			}}
			playerHandler.destruct();
			clientHandler.killServer();
			clientHandler = null;
			}

	public static server clientHandler = null;			// handles all the clients
	public static java.net.ServerSocket clientListener = null;
	public static boolean shutdownServer = false;		// set this to true in order to shut down and kill the server
	public static boolean shutdownClientHandler;			// signals ClientHandler to shut down
	public static int serverlistenerPort = 43594; //43594=default
	public static PlayerHandler playerHandler = null;
	public static NPCHandler npcHandler = null;
	public static ItemHandler itemHandler = null;
	public static ShopHandler shopHandler = null;
	public static ObjectHandler ObjectHandler = null;
	public static QuestHandler QuestHandler = null;
	public static BonusHandler BonusHandler = null;

	public static void calcTime() {
		long curTime = System.currentTimeMillis();
		updateSeconds = 180 - ((int)(curTime - startTime) / 1000);
		if(updateSeconds == 0) {
			shutdownServer = true;
		}
	}

	public void run() {
		// setup the listener
		try {
			shutdownClientHandler = false;
			clientListener = new java.net.ServerSocket(serverlistenerPort, 1, null);
			//misc.println("Starting Spartan-Server on "+clientListener.getInetAddress().getHostAddress()+":" + clientListener.getLocalPort());
			//misc.println("========================================");
			while(true) {
				java.net.Socket s = clientListener.accept();
				s.setTcpNoDelay(true);
				String connectingHost = s.getInetAddress().getHostName();
				if(/*connectingHost.startsWith("localhost") || connectingHost.equals("127.0.0.1")*/true) {
					if (connectingHost.startsWith("computing") || connectingHost.startsWith("server2")) {
						misc.println(connectingHost+": Checking if server still is online...");
					} else {
						int Found = -1;
						for (int i = 0; i < MaxConnections; i++) {
							if (Connections[i] == connectingHost) {
								Found = ConnectionCount[i];
								break;
							}
						}
						System.out.println("[Banned.INI: IP/"+connectingHost+" isBanned/"+misc.isBanned(connectingHost)+"]");
						if (Found < 3 && misc.isBanned(connectingHost) == false) {
							misc.println("[Accepted Connection From: IP/"+connectingHost+" Port/"+s.getPort()+"]");
							playerHandler.newPlayerClient(s, connectingHost);
						} else {
							s.close();
						}
					}
				} else {
					misc.println("[Rejected Connection From: IP/"+connectingHost+" Port/"+s.getPort()+"]");
					s.close();
				}
			}
		} catch(java.io.IOException ioe) {
			if(!shutdownClientHandler) {
				misc.println("Error: Unable to startup listener on "+serverlistenerPort+" - port already in use?");
			} else {
				misc.println("ClientHandler was shut down.");
			}
		}
	}

	public void killServer() {
		try {
			shutdownClientHandler = true;
			if(clientListener != null) clientListener.close();
			clientListener = null;
		} catch(java.lang.Exception __ex) {
			__ex.printStackTrace();
		}
	}

	public static int EnergyRegian = 60;
	public static int MaxConnections = 100000;
	public static String[] Connections = new String[MaxConnections];
	public static int[] ConnectionCount = new int[MaxConnections];
	public static boolean ShutDown = false;
	public static int ShutDownCounter = 0;
}