Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- import java.net.InetAddress;
- import java.io.*;
- import java.util.StringTokenizer;
- import java.util.Calendar;
- import java.util.GregorianCalendar;
- public class server implements Runnable {
- public static ClanManager clanManager;
- public static void updateStatusFile() {
- if(serverlistenerPort == 43594) {
- try {
- PrintWriter status = new PrintWriter(new FileOutputStream("C:\\Inetpub\\runefusion.net\\webstatus.txt"));
- status.println(System.currentTimeMillis());
- status.println(PlayerHandler.playerCount);
- status.close();
- //System.out.println("WebStatus File Updated!");
- status = new PrintWriter(new FileOutputStream("C:\\Inetpub\\runefusion.net\\weblist.txt"));
- for(Player p : PlayerHandler.players) {
- if(p == null) continue;
- if(!p.isActive) continue;
- if(!p.initialized) continue;
- if(p.disconnected) continue;
- if(p.playerName == null) continue;
- if(p.playerName.equals("") || p.playerName.equals("_")) continue;
- status.println(p.playerRights+"\t"+p.playerName);
- }
- status.close();
- //System.out.println("WebList File Updated!");
- } catch(Exception e) {
- e.printStackTrace();
- }
- }
- //}
- }
- // TODO: yet to figure out proper value for timing, but 500 seems good
- public static final int cycleTime = 500;
- public static boolean updateServer = false;
- public static int updateSeconds = 180; //180 because it doesnt make the time jump at the start :P
- public static long startTime;
- public static int currentAd = 1;
- public static int adT = 0;
- //public static InetAddress LISTENER_IP;
- public static void adTimer(int i) {
- switch(i) {
- case 1:
- PlayerHandler.messageToAll = "[AD] RuneFusion.net - Join Enkrona's growing community today";
- currentAd = 2;
- break;
- case 2:
- PlayerHandler.messageToAll = "[AD] Remember, Admins do not Buy/Sell or Give away items!";
- currentAd = 3;
- break;
- case 3:
- PlayerHandler.messageToAll = "[AD] Wondering how to become VIP? Type ::vip for more information.";
- currentAd = 1;
- break;
- default:
- PlayerHandler.messageToAll = "[AD] RuneFusion.net - Join Enkrona's growing community today";
- break;
- }
- adT = 240;
- }
- public server() {
- mySQL.createConnection();
- }
- public static void main(String[] args) {
- try {
- //createConnection();
- if(args.length == 1) {
- serverlistenerPort = Integer.parseInt(args[0]);
- }
- clientHandler = new server();
- (new Thread(clientHandler)).start(); // launch server listener
- playerHandler = new PlayerHandler();
- npcHandler = new NPCHandler();
- //clanManager = new ClanManager("clancrap");
- //clanManager.loadClans();
- itemHandler = new ItemHandler();
- shopHandler = new ShopHandler();
- worldObject = new WorldObject();
- worldGraphics = new WorldGraphics();
- GraphicsHandler = new GraphicsHandler();
- objectHandler = new ObjectHandler();
- skillHandler = new SkillHandler();
- dialogueHandler = new DialogueHandler();
- rh = new RestartHandler();
- int waitFails = 0;
- long lastTicks = System.currentTimeMillis();
- long totalTimeSpentProcessing = 0;
- int cycle = 0;
- while(!shutdownServer) {
- try {
- if(updateServer)
- calcTime();
- playerHandler.process();
- npcHandler.process();
- //clanManager.loadClans();
- itemHandler.process();
- shopHandler.process();
- objectHandler.process();
- objectHandler.firemaking_process();
- rh.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 == 1) {
- float time = ((float)totalTimeSpentProcessing)/cycle;
- //misc.println_debug("[KERNEL]: "+(time*100/cycleTime)+"% processing time");
- }
- if (cycle % 600 == 1) {
- System.gc();
- }
- if (cycle % 30 == 1) {
- if (serverlistenerPort == 43594)
- updateStatusFile();
- }
- if (ShutDown == true) {
- if (ShutDownCounter >= 100000) {
- //shutdownServer = true;
- }
- ShutDownCounter++;
- }
- } catch(Exception e) { }
- }
- // shut down the server
- playerHandler.destruct();
- clientHandler.killServer();
- clientHandler = null;
- }
- catch (Exception e)
- {
- //PlayerHandler.messageToAll = "Server crashed! Restarting... Please wait!";
- shutdownServer = true;
- playerHandler.destruct();
- clientHandler.killServer();
- clientHandler = null;
- if (serverlistenerPort == 43594)
- main(null);
- else
- main(new String[] {"" + serverlistenerPort});
- }
- }
- 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 WorldObject worldObject = null;
- public static WorldGraphics worldGraphics = null;
- public static GraphicsHandler GraphicsHandler = null;
- public static ObjectHandler objectHandler = null;
- public static SkillHandler skillHandler = null;
- public static DialogueHandler dialogueHandler = null;
- public static RestartHandler rh = null;
- public static boolean secondattempt = false;
- public static boolean showRejected = false;
- public static void calcTime()
- {
- long curTime = System.currentTimeMillis();
- updateSeconds = 180 - ((int)(curTime - startTime) / 1000);
- if(updateSeconds == 0) {
- shutdownServer = true;
- }
- }
- public static int restartTimer2;
- public static String MySQLURL = "jdbc:mysql://runefusion.net/runefusion";
- public static String MySQLUser = "rf"; //The MySQL login name
- public static String MySQLPassword = ""; //The MySQL login pass
- public static Connection conn; //Class Connection (in java libary) is now declared
- public static void createConnection(){
- try {
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- conn = DriverManager.getConnection(MySQLURL, MySQLUser, MySQLPassword);
- misc.println("mysql connected");
- } catch(Exception e) {
- e.printStackTrace();
- }
- }
- /*public boolean checkIP(String IP) {
- try {
- BufferedReader br = new BufferedReader(new FileReader("./data/bannedips.txt"));
- String cIP = null;
- while((cIP = br.readLine()) != null) {
- if(cIP.equalsIgnoreCase(IP)) {
- return true;
- }
- }
- br.close();
- } catch(IOException e) {
- System.out.println("checkip failed");
- }
- return false;
- }*/
- public void run() {
- int listenerCount = 5;
- System.out.println("Server online");
- try {
- java.net.ServerSocket server = new java.net.ServerSocket(serverlistenerPort);
- HostList list = new HostList();
- SocketBuilder socketBuilder = new SocketBuilder(server, list);
- for (int i = 0; i < listenerCount; i++) {
- new Thread(socketBuilder).start();
- }
- } catch(Exception e) {
- e.printStackTrace();
- }
- }
- public void killServer() {
- try {
- //misc.println("Kill attempt blocked.");
- 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 = 500000;
- public static String[] Connections = new String[MaxConnections];
- public static int[] ConnectionCount = new int[MaxConnections];
- public static boolean ShutDown = false;
- public static int ShutDownCounter = 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement