Shutdown.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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. }
  197. catch (Throwable t)
  198. {
  199. // ignore
  200. }
  201. // stop all threadpolls
  202. try
  203. {
  204. ThreadPoolManager.getInstance().shutdown();
  205. }
  206. catch (Throwable t)
  207. {
  208. // ignore
  209. }
  210. // last byebye, save all data and quit this server
  211. // logging doesnt work here :(
  212. saveData();
  213. try
  214. {
  215. LoginServerThread.getInstance().interrupt();
  216. }
  217. catch (Throwable t)
  218. {
  219. // ignore
  220. }
  221. // saveData sends messages to exit players, so sgutdown selector after it
  222. try
  223. {
  224. GameServer.gameServer.getSelectorThread().shutdown();
  225. GameServer.gameServer.getSelectorThread().setDaemon(true);
  226. }
  227. catch (Throwable t)
  228. {
  229. // ignore
  230. }
  231. // commit data, last chance
  232. try
  233. {
  234. L2DatabaseFactory.getInstance().shutdown();
  235. }
  236. catch (Throwable t)
  237. {
  238. }
  239. // server will quit, when this function ends.
  240. if (_instance._shutdownMode == GM_RESTART)
  241. {
  242. Runtime.getRuntime().halt(2);
  243. }
  244. else
  245. {
  246. Runtime.getRuntime().halt(0);
  247. }
  248. }
  249. else
  250. {
  251. // gm shutdown: send warnings and then call exit to start shutdown sequence
  252. countdown();
  253. // last point where logging is operational :(
  254. _log.warning("GM shutdown countdown is over. " + MODE_TEXT[_shutdownMode] + " NOW!");
  255. switch (_shutdownMode) {
  256. case GM_SHUTDOWN:
  257. _instance.setMode(GM_SHUTDOWN);
  258. System.exit(0);
  259. break;
  260. case GM_RESTART:
  261. _instance.setMode(GM_RESTART);
  262. System.exit(2);
  263. break;
  264. }
  265. }
  266. }
  267. /**
  268. * This functions starts a shutdown countdown
  269. *
  270. * @param activeChar GM who issued the shutdown command
  271. * @param seconds seconds until shutdown
  272. * @param restart true if the server will restart after shutdown
  273. */
  274. public void startShutdown(L2PcInstance activeChar, int seconds, boolean restart) {
  275. _log.warning("GM: "+activeChar.getName()+"("+activeChar.getObjectId()+") issued shutdown command. " + MODE_TEXT[_shutdownMode] + " in "+seconds+ " seconds!");
  276. if (restart) {
  277. _shutdownMode = GM_RESTART;
  278. } else {
  279. _shutdownMode = GM_SHUTDOWN;
  280. }
  281. if(_shutdownMode > 0)
  282. {
  283. switch (seconds)
  284. {
  285. case 540:
  286. case 480:
  287. case 420:
  288. case 360:
  289. case 300:
  290. case 240:
  291. case 180:
  292. case 120:
  293. case 60:
  294. case 30:
  295. case 10:
  296. case 5:
  297. case 4:
  298. case 3:
  299. case 2:
  300. case 1:
  301. break;
  302. default:
  303. SendServerQuit(seconds);
  304. }
  305. }
  306. if (_counterInstance != null) {
  307. _counterInstance._abort();
  308. }
  309. // the main instance should only run for shutdown hook, so we start a new instance
  310. _counterInstance = new Shutdown(seconds, restart);
  311. _counterInstance.start();
  312. }
  313. /**
  314. * This function aborts a running countdown
  315. *
  316. * @param activeChar GM who issued the abort command
  317. */
  318. public void abort(L2PcInstance activeChar) {
  319. _log.warning("GM: "+activeChar.getName()+"("+activeChar.getObjectId()+") issued shutdown ABORT. " + MODE_TEXT[_shutdownMode] + " has been stopped!");
  320. if (_counterInstance != null) {
  321. _counterInstance._abort();
  322. Announcements _an = Announcements.getInstance();
  323. _an.announceToAll("Server aborts " + MODE_TEXT[_shutdownMode] + " and continues normal operation!");
  324. }
  325. }
  326. /**
  327. * set the shutdown mode
  328. * @param mode what mode shall be set
  329. */
  330. private void setMode(int mode) {
  331. _shutdownMode = mode;
  332. }
  333. /**
  334. * set shutdown mode to ABORT
  335. *
  336. */
  337. private void _abort() {
  338. _shutdownMode = ABORT;
  339. }
  340. /**
  341. * this counts the countdown and reports it to all players
  342. * countdown is aborted if mode changes to ABORT
  343. */
  344. private void countdown() {
  345. try {
  346. while (_secondsShut > 0) {
  347. switch (_secondsShut)
  348. {
  349. case 540:SendServerQuit(540);break;
  350. case 480:SendServerQuit(480);break;
  351. case 420:SendServerQuit(420);break;
  352. case 360:SendServerQuit(360);break;
  353. case 300:SendServerQuit(300);break;
  354. case 240:SendServerQuit(240);break;
  355. case 180:SendServerQuit(180);break;
  356. case 120:SendServerQuit(120);break;
  357. case 60:
  358. LoginServerThread.getInstance().setServerStatus(ServerStatus.STATUS_DOWN); //avoids new players from logging in
  359. SendServerQuit(60);break;
  360. case 30:SendServerQuit(30);break;
  361. case 10:SendServerQuit(10);break;
  362. case 5:SendServerQuit(5);break;
  363. case 4:SendServerQuit(4);break;
  364. case 3:SendServerQuit(3);break;
  365. case 2:SendServerQuit(2);break;
  366. case 1:SendServerQuit(1);break;
  367. }
  368. _secondsShut--;
  369. int delay = 1000; //milliseconds
  370. Thread.sleep(delay);
  371. if(_shutdownMode == ABORT) break;
  372. }
  373. } catch (InterruptedException e) {
  374. //this will never happen
  375. }
  376. }
  377. /**
  378. * this sends a last byebye, disconnects all players and saves data
  379. *
  380. */
  381. private void saveData() {
  382. switch (_shutdownMode)
  383. {
  384. case SIGTERM:
  385. System.err.println("SIGTERM received. Shutting down NOW!");
  386. break;
  387. case GM_SHUTDOWN:
  388. System.err.println("GM shutdown received. Shutting down NOW!");
  389. break;
  390. case GM_RESTART:
  391. System.err.println("GM restart received. Restarting NOW!");
  392. break;
  393. }
  394. if (Config.ACTIVATE_POSITION_RECORDER)
  395. Universe.getInstance().implode(true);
  396. try
  397. {
  398. } catch (Throwable t) {
  399. _log.log(Level.INFO, "", t);
  400. }
  401. // we cannt abort shutdown anymore, so i removed the "if"
  402. disconnectAllCharacters();
  403. // Seven Signs data is now saved along with Festival data.
  404. if (!SevenSigns.getInstance().isSealValidationPeriod())
  405. SevenSignsFestival.getInstance().saveFestivalData(false);
  406. // Save Seven Signs data before closing. :)
  407. SevenSigns.getInstance().saveSevenSignsData(null, true);
  408. // Save all raidboss and GrandBoss status ^_^
  409. RaidBossSpawnManager.getInstance().cleanUp();
  410. System.err.println("RaidBossSpawnManager: All raidboss info saved!!");
  411. GrandBossManager.getInstance().cleanUp();
  412. System.err.println("GrandBossManager: All Grand Boss info saved!!");
  413. TradeController.getInstance().dataCountStore();
  414. System.err.println("TradeController: All count Item Saved");
  415. try
  416. {
  417. Olympiad.getInstance().save();
  418. }
  419. catch(Exception e){e.printStackTrace();}
  420. System.err.println("Olympiad System: Data saved!!");
  421. // Save Cursed Weapons data before closing.
  422. CursedWeaponsManager.getInstance().saveData();
  423. // Save all manor data
  424. CastleManorManager.getInstance().save();
  425. // Save all global (non-player specific) Quest data that needs to persist after reboot
  426. QuestManager.getInstance().save();
  427. //Save items on ground before closing
  428. if(Config.SAVE_DROPPED_ITEM){
  429. ItemsOnGroundManager.getInstance().saveInDb();
  430. ItemsOnGroundManager.getInstance().cleanUp();
  431. System.err.println("ItemsOnGroundManager: All items on ground saved!!");
  432. }
  433. System.err.println("Data saved. All players disconnected, shutting down.");
  434. try {
  435. int delay = 5000;
  436. Thread.sleep(delay);
  437. }
  438. catch (InterruptedException e) {
  439. //never happens :p
  440. }
  441. }
  442. /**
  443. * this disconnects all clients from the server
  444. *
  445. */
  446. @SuppressWarnings("deprecation")
  447. private void disconnectAllCharacters()
  448. {
  449. SystemMessage sysm = new SystemMessage(0);
  450. ServerClose ql = new ServerClose();
  451. Collection<L2PcInstance> pls = L2World.getInstance().getAllPlayers().values();
  452. //synchronized (L2World.getInstance().getAllPlayers())
  453. {
  454. for (L2PcInstance player : pls)
  455. {
  456. player.sendPacket(sysm);
  457. //Logout Character
  458. try {
  459. L2GameClient.saveCharToDisk(player);
  460. //SystemMessage sm = new SystemMessage(SystemMessage.YOU_HAVE_WON_THE_WAR_OVER_THE_S1_CLAN);
  461. //player.sendPacket(sm);
  462. player.sendPacket(ql);
  463. } catch (Throwable t) {}
  464. }
  465. }
  466. try { Thread.sleep(1000); } catch (Throwable t) {_log.log(Level.INFO, "", t);}
  467. pls = L2World.getInstance().getAllPlayers().values();
  468. //synchronized (L2World.getInstance().getAllPlayers())
  469. {
  470. for (L2PcInstance player : pls)
  471. {
  472. try {
  473. player.closeNetConnection();
  474. } catch (Throwable t) {
  475. // just to make sure we try to kill the connection
  476. }
  477. }
  478. }
  479. }
  480. }