Shutdown.java 15 KB

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