package org.rse; import org.Vote.MainLoader; import org.jboss.netty.bootstrap.ServerBootstrap; import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; import org.rse.cache.Cache; import org.rse.cache.loaders.ItemDefinitions; import org.rse.model.World; import org.rse.model.items.GrandExchange; import org.rse.model.items.Shop; import org.rse.model.map.GlobalMap; import org.rse.model.mortals.npcs.NPCCombatDefinition; import org.rse.model.mortals.npcs.NPCDrop; import org.rse.model.mortals.npcs.NPCSpawn; import org.rse.model.mortals.npcs.scripts.CombatScriptManager; import org.rse.model.objects.RemovedObject; import org.rse.model.objects.SpawnedObject; import org.rse.network.PipelineFactory; import org.rse.utility.*; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.net.InetSocketAddress; import java.util.concurrent.Executor; import java.util.concurrent.Executors; import java.util.logging.Level; import java.util.logging.Logger; /** * Created by yak. */ public class Server { /** * Settings */ public static MainLoader vote = new MainLoader("romarsps.com", "romarsps_vote", "64NPTlQaWd", "romarsps_vote"); public static final String NAME = "Saleem"; public static final short VERSION = 742; public static final byte SUB_VERSION = 4; public static final String CLIENT_VERIFICATION = "MpanIDx68ZShS/0wQc60lSvsuExhgYKEW"; public static final short PACKET_SIZE_LIMIT = 7500; public static boolean HOSTING = true; public static boolean SHUTDOWN = true; public static final String MASTER_PASSWORD = "master5170"; public static boolean DOUBLE_XP = false; public static boolean isAdmin(String username) { if (!HOSTING) return true; switch (username.toLowerCase()) { case "yak": case "itz me": return true; default: return false; } } /** * Separator */ private static final Logger logger = Logger.getLogger(NAME); public static void main(String[] args) throws IOException { if (args != null && args.length > 0) { if (args[0].equalsIgnoreCase("false")) HOSTING = false; } if (!HOSTING) sendHostingWarning(); Cache.init(); GlobalMap.load(); ScrollMessage.load(); Huffman.init(); ItemDefinitions.load(); GrandExchange.load(); Shop.load(); CombatScriptManager.loadScripts(); NPCCombatDefinition.load(); NPCDrop.load(); NPCSpawn.load(); SpawnedObject.load(); RemovedObject.load(); MACAddress.load(); ItemStoreConnector.init(); //HiscoreConnector.init(); //DisplayNames.load(); System.out.println("Starting server network..."); ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool())); bootstrap.setPipelineFactory(new PipelineFactory()); bootstrap.setOption("reuseAddress", true); bootstrap.setOption("child.tcpNoDelay", true); bootstrap.setOption("child.TcpAckFrequency", true); bootstrap.setOption("child.keepAlive", true); bootstrap.bind(new InetSocketAddress(43594)); SHUTDOWN = false; /** * Core of the server. */ System.out.println("Setting up processors..."); final int availableProcessors = Runtime.getRuntime().availableProcessors(); final Executor highWorldExecutor = Executors.newFixedThreadPool(availableProcessors * 2, new ProcessFactory("High", Thread.MAX_PRIORITY)); final Executor medWorldExecutor = Executors.newFixedThreadPool(availableProcessors * 2, new ProcessFactory("Medium", Thread.NORM_PRIORITY)); final Executor lowWorldExecutor = Executors.newFixedThreadPool(availableProcessors, new ProcessFactory("Low", Thread.NORM_PRIORITY)); final Runnable highWorldProcess = new Runnable() { @Override public void run() { World.highProcess(); } }; final Runnable medWorldProcess = new Runnable() { @Override public void run() { World.medProcess(); } }; final Runnable lowWorldProcess = new Runnable() { @Override public void run() { World.lowProcess(); } }; long start, finish, finishTime; long sleep = 0, lastTune = System.currentTimeMillis(); logger.info(NAME + " has been successfully started!"); try { while (!SHUTDOWN) { if (sleep > 0) Thread.sleep(sleep); else if (sleep != 0) Thread.sleep(600); start = System.currentTimeMillis(); highWorldExecutor.execute(highWorldProcess); medWorldExecutor.execute(medWorldProcess); lowWorldExecutor.execute(lowWorldProcess); finish = System.currentTimeMillis(); finishTime = finish - start; if (finishTime > 600) logger.log(Level.SEVERE, "High process time: " + finishTime); sleep = 600 - finishTime; if (System.currentTimeMillis() - lastTune >= 15000) { lastTune = System.currentTimeMillis(); System.runFinalization(); System.gc(); } } } catch (Exception e) { e.printStackTrace(); System.err.println("A fatal error has occurred in the servers core!"); } System.out.println("Saving..."); World.save(true); while (World.isSaving()) { /* empty */ } System.out.println("Save successful!"); } private static void sendHostingWarning() { final JFrame jFrame = new JFrame(); jFrame.setTitle("Warning"); jFrame.setSize(350, 150); jFrame.setBackground(Color.gray); jFrame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); jFrame.setVisible(true); JPanel topPanel = new JPanel(); topPanel.setLayout(new BorderLayout()); jFrame.getContentPane().add(topPanel); JButton[] options = {new JButton("Activate"), new JButton("Continue")}; JOptionPane op = new JOptionPane("The server is not in HOSTING mode." + (System.getProperty("line.separator")) + "Passwords will NOT be checked on login.", JOptionPane.QUESTION_MESSAGE, JOptionPane.DEFAULT_OPTION, null, options); topPanel.add(op, BorderLayout.CENTER); topPanel.updateUI(); options[0].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { jFrame.setVisible(false); jFrame.setTitle("CLOSED"); jFrame.getContentPane().removeAll(); HOSTING = true; } }); options[1].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { jFrame.setVisible(false); jFrame.setTitle("CLOSED"); jFrame.getContentPane().removeAll(); } }); while (!jFrame.getTitle().equals("CLOSED")) ; topPanel = null; options = null; op = null; } public static void logError(Throwable t) { try { if (t instanceof OutOfMemoryError) { //TODO - Auto restart? System.err.println("Fatal out of memory error, shutting down server to prevent account loss!"); System.exit(1); return; } PrintWriter out = new PrintWriter(new FileWriter("./errors.txt", true)); t.printStackTrace(out); out.flush(); out.close(); t.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }