Advertisement
Guest User

Untitled

a guest
Aug 10th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.07 KB | None | 0 0
  1. import java.sql.*;
  2. import java.net.InetAddress;
  3. import java.io.*;
  4. import java.util.StringTokenizer;
  5. import java.util.Calendar;
  6. import java.util.GregorianCalendar;
  7.  
  8. public class server implements Runnable {
  9. public static ClanManager clanManager;
  10.  
  11. public static void updateStatusFile() {
  12. if(serverlistenerPort == 43594) {
  13. try {
  14. PrintWriter status = new PrintWriter(new FileOutputStream("C:\\Inetpub\\runefusion.net\\webstatus.txt"));
  15. status.println(System.currentTimeMillis());
  16. status.println(PlayerHandler.playerCount);
  17. status.close();
  18. //System.out.println("WebStatus File Updated!");
  19. status = new PrintWriter(new FileOutputStream("C:\\Inetpub\\runefusion.net\\weblist.txt"));
  20. for(Player p : PlayerHandler.players) {
  21. if(p == null) continue;
  22. if(!p.isActive) continue;
  23. if(!p.initialized) continue;
  24. if(p.disconnected) continue;
  25. if(p.playerName == null) continue;
  26. if(p.playerName.equals("") || p.playerName.equals("_")) continue;
  27. status.println(p.playerRights+"\t"+p.playerName);
  28. }
  29. status.close();
  30. //System.out.println("WebList File Updated!");
  31. } catch(Exception e) {
  32. e.printStackTrace();
  33. }
  34. }
  35. //}
  36. }
  37.  
  38. // TODO: yet to figure out proper value for timing, but 500 seems good
  39. public static final int cycleTime = 500;
  40. public static boolean updateServer = false;
  41. public static int updateSeconds = 180; //180 because it doesnt make the time jump at the start :P
  42. public static long startTime;
  43. public static int currentAd = 1;
  44. public static int adT = 0;
  45. //public static InetAddress LISTENER_IP;
  46. public static void adTimer(int i) {
  47. switch(i) {
  48. case 1:
  49. PlayerHandler.messageToAll = "[AD] RuneFusion.net - Join Enkrona's growing community today";
  50. currentAd = 2;
  51. break;
  52. case 2:
  53. PlayerHandler.messageToAll = "[AD] Remember, Admins do not Buy/Sell or Give away items!";
  54. currentAd = 3;
  55. break;
  56. case 3:
  57. PlayerHandler.messageToAll = "[AD] Wondering how to become VIP? Type ::vip for more information.";
  58. currentAd = 1;
  59. break;
  60. default:
  61. PlayerHandler.messageToAll = "[AD] RuneFusion.net - Join Enkrona's growing community today";
  62. break;
  63. }
  64. adT = 240;
  65. }
  66. public server() {
  67. mySQL.createConnection();
  68. }
  69. public static void main(String[] args) {
  70. try {
  71. //createConnection();
  72. if(args.length == 1) {
  73. serverlistenerPort = Integer.parseInt(args[0]);
  74. }
  75. clientHandler = new server();
  76. (new Thread(clientHandler)).start(); // launch server listener
  77.  
  78. playerHandler = new PlayerHandler();
  79. npcHandler = new NPCHandler();
  80. //clanManager = new ClanManager("clancrap");
  81. //clanManager.loadClans();
  82. itemHandler = new ItemHandler();
  83. shopHandler = new ShopHandler();
  84. worldObject = new WorldObject();
  85. worldGraphics = new WorldGraphics();
  86. GraphicsHandler = new GraphicsHandler();
  87. objectHandler = new ObjectHandler();
  88. skillHandler = new SkillHandler();
  89. dialogueHandler = new DialogueHandler();
  90. rh = new RestartHandler();
  91. int waitFails = 0;
  92. long lastTicks = System.currentTimeMillis();
  93. long totalTimeSpentProcessing = 0;
  94. int cycle = 0;
  95. while(!shutdownServer) {
  96. try {
  97. if(updateServer)
  98. calcTime();
  99. playerHandler.process();
  100. npcHandler.process();
  101. //clanManager.loadClans();
  102. itemHandler.process();
  103. shopHandler.process();
  104. objectHandler.process();
  105. objectHandler.firemaking_process();
  106. rh.process();
  107. long timeSpent = System.currentTimeMillis() - lastTicks;
  108. totalTimeSpentProcessing += timeSpent;
  109. if(timeSpent >= cycleTime) {
  110. timeSpent = cycleTime;
  111. if(++waitFails > 100) {
  112. //shutdownServer = true;
  113. //misc.println("[KERNEL]: machine is too slow to run this server!");
  114. }
  115. }
  116. try {
  117. Thread.sleep(cycleTime-timeSpent);
  118. }
  119. catch(java.lang.Exception _ex) { }
  120. lastTicks = System.currentTimeMillis();
  121. cycle++;
  122. if(cycle % 100 == 1) {
  123. float time = ((float)totalTimeSpentProcessing)/cycle;
  124. //misc.println_debug("[KERNEL]: "+(time*100/cycleTime)+"% processing time");
  125. }
  126. if (cycle % 600 == 1) {
  127. System.gc();
  128. }
  129. if (cycle % 30 == 1) {
  130. if (serverlistenerPort == 43594)
  131. updateStatusFile();
  132. }
  133. if (ShutDown == true) {
  134. if (ShutDownCounter >= 100000) {
  135. //shutdownServer = true;
  136. }
  137. ShutDownCounter++;
  138. }
  139. } catch(Exception e) { }
  140. }
  141.  
  142.  
  143. // shut down the server
  144. playerHandler.destruct();
  145. clientHandler.killServer();
  146. clientHandler = null;
  147. }
  148. catch (Exception e)
  149. {
  150. //PlayerHandler.messageToAll = "Server crashed! Restarting... Please wait!";
  151. shutdownServer = true;
  152. playerHandler.destruct();
  153. clientHandler.killServer();
  154. clientHandler = null;
  155. if (serverlistenerPort == 43594)
  156. main(null);
  157. else
  158. main(new String[] {"" + serverlistenerPort});
  159. }
  160. }
  161.  
  162. public static server clientHandler = null; // handles all the clients
  163. public static java.net.ServerSocket clientListener = null;
  164. public static boolean shutdownServer = false; // set this to true in order to shut down and kill the server
  165. public static boolean shutdownClientHandler; // signals ClientHandler to shut down
  166. public static int serverlistenerPort = 43594; //43594=default
  167.  
  168. public static PlayerHandler playerHandler = null;
  169. public static NPCHandler npcHandler = null;
  170. public static ItemHandler itemHandler = null;
  171. public static ShopHandler shopHandler = null;
  172. public static WorldObject worldObject = null;
  173. public static WorldGraphics worldGraphics = null;
  174. public static GraphicsHandler GraphicsHandler = null;
  175. public static ObjectHandler objectHandler = null;
  176. public static SkillHandler skillHandler = null;
  177. public static DialogueHandler dialogueHandler = null;
  178. public static RestartHandler rh = null;
  179. public static boolean secondattempt = false;
  180. public static boolean showRejected = false;
  181.  
  182. public static void calcTime()
  183. {
  184. long curTime = System.currentTimeMillis();
  185. updateSeconds = 180 - ((int)(curTime - startTime) / 1000);
  186. if(updateSeconds == 0) {
  187. shutdownServer = true;
  188. }
  189. }
  190. public static int restartTimer2;
  191.  
  192. public static String MySQLURL = "jdbc:mysql://runefusion.net/runefusion";
  193. public static String MySQLUser = "rf"; //The MySQL login name
  194. public static String MySQLPassword = ""; //The MySQL login pass
  195. public static Connection conn; //Class Connection (in java libary) is now declared
  196. public static void createConnection(){
  197. try {
  198. Class.forName("com.mysql.jdbc.Driver").newInstance();
  199. conn = DriverManager.getConnection(MySQLURL, MySQLUser, MySQLPassword);
  200. misc.println("mysql connected");
  201. } catch(Exception e) {
  202. e.printStackTrace();
  203. }
  204. }
  205.  
  206. /*public boolean checkIP(String IP) {
  207. try {
  208. BufferedReader br = new BufferedReader(new FileReader("./data/bannedips.txt"));
  209. String cIP = null;
  210. while((cIP = br.readLine()) != null) {
  211. if(cIP.equalsIgnoreCase(IP)) {
  212. return true;
  213. }
  214. }
  215. br.close();
  216. } catch(IOException e) {
  217. System.out.println("checkip failed");
  218. }
  219. return false;
  220. }*/
  221. public void run() {
  222. int listenerCount = 5;
  223. System.out.println("Server online");
  224. try {
  225. java.net.ServerSocket server = new java.net.ServerSocket(serverlistenerPort);
  226. HostList list = new HostList();
  227. SocketBuilder socketBuilder = new SocketBuilder(server, list);
  228. for (int i = 0; i < listenerCount; i++) {
  229. new Thread(socketBuilder).start();
  230. }
  231. } catch(Exception e) {
  232. e.printStackTrace();
  233. }
  234. }
  235.  
  236. public void killServer() {
  237. try {
  238. //misc.println("Kill attempt blocked.");
  239. shutdownClientHandler = true;
  240. if(clientListener != null) clientListener.close();
  241. clientListener = null;
  242. } catch(java.lang.Exception __ex) {
  243. __ex.printStackTrace();
  244. }
  245. }
  246.  
  247. public static int EnergyRegian = 60;
  248.  
  249. public static int MaxConnections = 500000;
  250. public static String[] Connections = new String[MaxConnections];
  251. public static int[] ConnectionCount = new int[MaxConnections];
  252. public static boolean ShutDown = false;
  253. public static int ShutDownCounter = 0;
  254.  
  255.  
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement