Shutdown.java 15 KB

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