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