Shutdown.java 15 KB

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