Shutdown.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package net.sf.l2j.gameserver;
  16. import java.util.logging.Level;
  17. import java.util.logging.Logger;
  18. import net.sf.l2j.Config;
  19. import net.sf.l2j.L2DatabaseFactory;
  20. import net.sf.l2j.gameserver.gameserverpackets.ServerStatus;
  21. import net.sf.l2j.gameserver.instancemanager.CastleManorManager;
  22. import net.sf.l2j.gameserver.instancemanager.CursedWeaponsManager;
  23. import net.sf.l2j.gameserver.instancemanager.GrandBossManager;
  24. import net.sf.l2j.gameserver.instancemanager.ItemsOnGroundManager;
  25. import net.sf.l2j.gameserver.instancemanager.QuestManager;
  26. import net.sf.l2j.gameserver.instancemanager.RaidBossSpawnManager;
  27. import net.sf.l2j.gameserver.model.L2World;
  28. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  29. import net.sf.l2j.gameserver.network.L2GameClient;
  30. import net.sf.l2j.gameserver.serverpackets.ServerClose;
  31. import net.sf.l2j.gameserver.serverpackets.SystemMessage;
  32. /**
  33. *
  34. * This class provides the functions for shutting down and restarting the server
  35. * It closes all open clientconnections and saves all data.
  36. *
  37. * @version $Revision: 1.2.4.5 $ $Date: 2005/03/27 15:29:09 $
  38. */
  39. public class Shutdown extends Thread
  40. {
  41. private static Logger _log = Logger.getLogger(Shutdown.class.getName());
  42. private static Shutdown _instance;
  43. private static Shutdown _counterInstance = null;
  44. private int _secondsShut;
  45. private int _shutdownMode;
  46. public static final int SIGTERM = 0;
  47. public static final int GM_SHUTDOWN = 1;
  48. public static final int GM_RESTART = 2;
  49. public static final int ABORT = 3;
  50. private static final String[] MODE_TEXT = {"SIGTERM", "shutting down", "restarting", "aborting"};
  51. /**
  52. * This function starts a shutdown countdown from Telnet (Copied from Function startShutdown())
  53. *
  54. * @param ip IP Which Issued shutdown command
  55. * @param seconds seconds untill shutdown
  56. * @param restart true if the server will restart after shutdown
  57. */
  58. @SuppressWarnings("deprecation")
  59. private void SendServerQuit(int seconds)
  60. {
  61. for (L2PcInstance player : L2World.getInstance().getAllPlayers())
  62. {
  63. SystemMessage sysm = new SystemMessage(1);
  64. sysm.addNumber(seconds);
  65. player.sendPacket(sysm);
  66. }
  67. }
  68. public void startTelnetShutdown(String IP, int seconds, boolean restart)
  69. {
  70. _log.warning("IP: " + IP + " issued shutdown command. " + MODE_TEXT[_shutdownMode] + " in "+seconds+ " seconds!");
  71. //_an.announceToAll("Server is " + _modeText[shutdownMode] + " in "+seconds+ " seconds!");
  72. if (restart) {
  73. _shutdownMode = GM_RESTART;
  74. } else {
  75. _shutdownMode = GM_SHUTDOWN;
  76. }
  77. if(_shutdownMode > 0)
  78. {
  79. switch (seconds)
  80. {
  81. case 540:
  82. case 480:
  83. case 420:
  84. case 360:
  85. case 300:
  86. case 240:
  87. case 180:
  88. case 120:
  89. case 60:
  90. case 30:
  91. case 10:
  92. case 5:
  93. case 4:
  94. case 3:
  95. case 2:
  96. case 1:
  97. break;
  98. default:
  99. SendServerQuit(seconds);
  100. }
  101. }
  102. if (_counterInstance != null) {
  103. _counterInstance._abort();
  104. }
  105. _counterInstance = new Shutdown(seconds, restart);
  106. _counterInstance.start();
  107. }
  108. /**
  109. * This function aborts a running countdown
  110. *
  111. * @param IP IP Which Issued shutdown command
  112. */
  113. public void telnetAbort(String IP) {
  114. _log.warning("IP: " + IP + " issued shutdown ABORT. " + MODE_TEXT[_shutdownMode] + " has been stopped!");
  115. if (_counterInstance != null) {
  116. _counterInstance._abort();
  117. Announcements _an = Announcements.getInstance();
  118. _an.announceToAll("Server aborts " + MODE_TEXT[_shutdownMode] + " and continues normal operation!");
  119. }
  120. }
  121. /**
  122. * Default constucter is only used internal to create the shutdown-hook instance
  123. *
  124. */
  125. public Shutdown() {
  126. _secondsShut = -1;
  127. _shutdownMode = SIGTERM;
  128. }
  129. /**
  130. * This creates a countdown instance of Shutdown.
  131. *
  132. * @param seconds how many seconds until shutdown
  133. * @param restart true is the server shall restart after shutdown
  134. *
  135. */
  136. public Shutdown(int seconds, boolean restart) {
  137. if (seconds < 0) {
  138. seconds = 0;
  139. }
  140. _secondsShut = seconds;
  141. if (restart) {
  142. _shutdownMode = GM_RESTART;
  143. } else {
  144. _shutdownMode = GM_SHUTDOWN;
  145. }
  146. }
  147. /**
  148. * get the shutdown-hook instance
  149. * the shutdown-hook instance is created by the first call of this function,
  150. * but it has to be registrered externaly.
  151. *
  152. * @return instance of Shutdown, to be used as shutdown hook
  153. */
  154. public static Shutdown getInstance()
  155. {
  156. if (_instance == null)
  157. {
  158. _instance = new Shutdown();
  159. }
  160. return _instance;
  161. }
  162. /**
  163. * this function is called, when a new thread starts
  164. *
  165. * if this thread is the thread of getInstance, then this is the shutdown hook
  166. * and we save all data and disconnect all clients.
  167. *
  168. * after this thread ends, the server will completely exit
  169. *
  170. * if this is not the thread of getInstance, then this is a countdown thread.
  171. * we start the countdown, and when we finished it, and it was not aborted,
  172. * we tell the shutdown-hook why we call exit, and then call exit
  173. *
  174. * when the exit status of the server is 1, startServer.sh / startServer.bat
  175. * will restart the server.
  176. *
  177. */
  178. @Override
  179. public void run()
  180. {
  181. // disallow new logins
  182. try
  183. {
  184. //Doesnt actually do anything
  185. //Server.gameServer.getLoginController().setMaxAllowedOnlinePlayers(0);
  186. }
  187. catch (Throwable t)
  188. {
  189. // ignore
  190. }
  191. if (this == _instance)
  192. {
  193. // ensure all services are stopped
  194. try
  195. {
  196. GameTimeController.getInstance().stopTimer();
  197. }
  198. catch (Throwable t)
  199. {
  200. // ignore
  201. }
  202. // stop all threadpolls
  203. try
  204. {
  205. ThreadPoolManager.getInstance().shutdown();
  206. }
  207. catch (Throwable t)
  208. {
  209. // ignore
  210. }
  211. // last byebye, save all data and quit this server
  212. // logging doesnt work here :(
  213. saveData();
  214. try
  215. {
  216. LoginServerThread.getInstance().interrupt();
  217. }
  218. catch (Throwable t)
  219. {
  220. // ignore
  221. }
  222. // saveData sends messages to exit players, so sgutdown selector after it
  223. try
  224. {
  225. GameServer.gameServer.getSelectorThread().shutdown();
  226. GameServer.gameServer.getSelectorThread().setDaemon(true);
  227. }
  228. catch (Throwable t)
  229. {
  230. // ignore
  231. }
  232. // commit data, last chance
  233. try
  234. {
  235. L2DatabaseFactory.getInstance().shutdown();
  236. }
  237. catch (Throwable t)
  238. {
  239. }
  240. // server will quit, when this function ends.
  241. if (_instance._shutdownMode == GM_RESTART)
  242. {
  243. Runtime.getRuntime().halt(2);
  244. }
  245. else
  246. {
  247. Runtime.getRuntime().halt(0);
  248. }
  249. }
  250. else
  251. {
  252. // gm shutdown: send warnings and then call exit to start shutdown sequence
  253. countdown();
  254. // last point where logging is operational :(
  255. _log.warning("GM shutdown countdown is over. " + MODE_TEXT[_shutdownMode] + " NOW!");
  256. switch (_shutdownMode) {
  257. case GM_SHUTDOWN:
  258. _instance.setMode(GM_SHUTDOWN);
  259. System.exit(0);
  260. break;
  261. case GM_RESTART:
  262. _instance.setMode(GM_RESTART);
  263. System.exit(2);
  264. break;
  265. }
  266. }
  267. }
  268. /**
  269. * This functions starts a shutdown countdown
  270. *
  271. * @param activeChar GM who issued the shutdown command
  272. * @param seconds seconds until shutdown
  273. * @param restart true if the server will restart after shutdown
  274. */
  275. public void startShutdown(L2PcInstance activeChar, int seconds, boolean restart) {
  276. _log.warning("GM: "+activeChar.getName()+"("+activeChar.getObjectId()+") issued shutdown command. " + MODE_TEXT[_shutdownMode] + " in "+seconds+ " seconds!");
  277. if (restart) {
  278. _shutdownMode = GM_RESTART;
  279. } else {
  280. _shutdownMode = GM_SHUTDOWN;
  281. }
  282. if(_shutdownMode > 0)
  283. {
  284. switch (seconds)
  285. {
  286. case 540:
  287. case 480:
  288. case 420:
  289. case 360:
  290. case 300:
  291. case 240:
  292. case 180:
  293. case 120:
  294. case 60:
  295. case 30:
  296. case 10:
  297. case 5:
  298. case 4:
  299. case 3:
  300. case 2:
  301. case 1:
  302. break;
  303. default:
  304. SendServerQuit(seconds);
  305. }
  306. }
  307. if (_counterInstance != null) {
  308. _counterInstance._abort();
  309. }
  310. // the main instance should only run for shutdown hook, so we start a new instance
  311. _counterInstance = new Shutdown(seconds, restart);
  312. _counterInstance.start();
  313. }
  314. /**
  315. * This function aborts a running countdown
  316. *
  317. * @param activeChar GM who issued the abort command
  318. */
  319. public void abort(L2PcInstance activeChar) {
  320. _log.warning("GM: "+activeChar.getName()+"("+activeChar.getObjectId()+") issued shutdown ABORT. " + MODE_TEXT[_shutdownMode] + " has been stopped!");
  321. if (_counterInstance != null) {
  322. _counterInstance._abort();
  323. Announcements _an = Announcements.getInstance();
  324. _an.announceToAll("Server aborts " + MODE_TEXT[_shutdownMode] + " and continues normal operation!");
  325. }
  326. }
  327. /**
  328. * set the shutdown mode
  329. * @param mode what mode shall be set
  330. */
  331. private void setMode(int mode) {
  332. _shutdownMode = mode;
  333. }
  334. /**
  335. * set shutdown mode to ABORT
  336. *
  337. */
  338. private void _abort() {
  339. _shutdownMode = ABORT;
  340. }
  341. /**
  342. * this counts the countdown and reports it to all players
  343. * countdown is aborted if mode changes to ABORT
  344. */
  345. private void countdown() {
  346. try {
  347. while (_secondsShut > 0) {
  348. switch (_secondsShut)
  349. {
  350. case 540:SendServerQuit(540);break;
  351. case 480:SendServerQuit(480);break;
  352. case 420:SendServerQuit(420);break;
  353. case 360:SendServerQuit(360);break;
  354. case 300:SendServerQuit(300);break;
  355. case 240:SendServerQuit(240);break;
  356. case 180:SendServerQuit(180);break;
  357. case 120:SendServerQuit(120);break;
  358. case 60:
  359. LoginServerThread.getInstance().setServerStatus(ServerStatus.STATUS_DOWN); //avoids new players from logging in
  360. SendServerQuit(60);break;
  361. case 30:SendServerQuit(30);break;
  362. case 10:SendServerQuit(10);break;
  363. case 5:SendServerQuit(5);break;
  364. case 4:SendServerQuit(4);break;
  365. case 3:SendServerQuit(3);break;
  366. case 2:SendServerQuit(2);break;
  367. case 1:SendServerQuit(1);break;
  368. }
  369. _secondsShut--;
  370. int delay = 1000; //milliseconds
  371. Thread.sleep(delay);
  372. if(_shutdownMode == ABORT) break;
  373. }
  374. } catch (InterruptedException e) {
  375. //this will never happen
  376. }
  377. }
  378. /**
  379. * this sends a last byebye, disconnects all players and saves data
  380. *
  381. */
  382. private void saveData() {
  383. switch (_shutdownMode)
  384. {
  385. case SIGTERM:
  386. System.err.println("SIGTERM received. Shutting down NOW!");
  387. break;
  388. case GM_SHUTDOWN:
  389. System.err.println("GM shutdown received. Shutting down NOW!");
  390. break;
  391. case GM_RESTART:
  392. System.err.println("GM restart received. Restarting NOW!");
  393. break;
  394. }
  395. if (Config.ACTIVATE_POSITION_RECORDER)
  396. Universe.getInstance().implode(true);
  397. try
  398. {
  399. } catch (Throwable t) {
  400. _log.log(Level.INFO, "", t);
  401. }
  402. // we cannt abort shutdown anymore, so i removed the "if"
  403. disconnectAllCharacters();
  404. // Seven Signs data is now saved along with Festival data.
  405. if (!SevenSigns.getInstance().isSealValidationPeriod())
  406. SevenSignsFestival.getInstance().saveFestivalData(false);
  407. // Save Seven Signs data before closing. :)
  408. SevenSigns.getInstance().saveSevenSignsData(null, true);
  409. // Save all raidboss and GrandBoss status ^_^
  410. RaidBossSpawnManager.getInstance().cleanUp();
  411. System.err.println("RaidBossSpawnManager: All raidboss info saved!!");
  412. GrandBossManager.getInstance().cleanUp();
  413. System.err.println("GrandBossManager: All Grand Boss info saved!!");
  414. TradeController.getInstance().dataCountStore();
  415. System.err.println("TradeController: All count Item Saved");
  416. try
  417. {
  418. Olympiad.getInstance().save();
  419. }
  420. catch(Exception e){e.printStackTrace();}
  421. System.err.println("Olympiad System: Data saved!!");
  422. // Save Cursed Weapons data before closing.
  423. CursedWeaponsManager.getInstance().saveData();
  424. // Save all manor data
  425. CastleManorManager.getInstance().save();
  426. // Save all global (non-player specific) Quest data that needs to persist after reboot
  427. QuestManager.getInstance().save();
  428. //Save items on ground before closing
  429. if(Config.SAVE_DROPPED_ITEM){
  430. ItemsOnGroundManager.getInstance().saveInDb();
  431. ItemsOnGroundManager.getInstance().cleanUp();
  432. System.err.println("ItemsOnGroundManager: All items on ground saved!!");
  433. }
  434. System.err.println("Data saved. All players disconnected, shutting down.");
  435. try {
  436. int delay = 5000;
  437. Thread.sleep(delay);
  438. }
  439. catch (InterruptedException e) {
  440. //never happens :p
  441. }
  442. }
  443. /**
  444. * this disconnects all clients from the server
  445. *
  446. */
  447. @SuppressWarnings("deprecation")
  448. private void disconnectAllCharacters()
  449. {
  450. for (L2PcInstance player : L2World.getInstance().getAllPlayers())
  451. {
  452. SystemMessage sysm = new SystemMessage(0);
  453. player.sendPacket(sysm);
  454. //Logout Character
  455. try {
  456. L2GameClient.saveCharToDisk(player);
  457. //SystemMessage sm = new SystemMessage(SystemMessage.YOU_HAVE_WON_THE_WAR_OVER_THE_S1_CLAN);
  458. //player.sendPacket(sm);
  459. ServerClose ql = new ServerClose();
  460. player.sendPacket(ql);
  461. } catch (Throwable t) {}
  462. }
  463. try { Thread.sleep(1000); } catch (Throwable t) {_log.log(Level.INFO, "", t);}
  464. for (L2PcInstance player : L2World.getInstance().getAllPlayers())
  465. {
  466. try {
  467. player.closeNetConnection();
  468. } catch (Throwable t) {
  469. // just to make sure we try to kill the connection
  470. }
  471. }
  472. }
  473. }