Shutdown.java 15 KB

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