View Javadoc

1   /***
2    * Jetrix TetriNET Server
3    * Copyright (C) 2008  Emmanuel Bourg
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18   */
19  
20  package net.jetrix.listeners;
21  
22  import java.io.IOException;
23  import java.net.DatagramPacket;
24  import java.net.DatagramSocket;
25  import java.net.InetAddress;
26  import java.net.InetSocketAddress;
27  import java.util.logging.Level;
28  
29  import net.jetrix.Listener;
30  import net.jetrix.Server;
31  import net.jetrix.services.AbstractService;
32  
33  /***
34   * Listens for shutdown commands from the localhost. This is used to stop
35   * the server from the shell on Unix.
36   *
37   * @author Emmanuel Bourg
38   * @version $Revision: 786 $, $Date: 2009-02-13 12:11:50 +0100 (Fri, 13 Feb 2009) $
39   */
40  public class ShutdownListener extends AbstractService implements Listener
41  {
42      private static final String SHUTDOWN_COMMAND = "shutdown";
43  
44      public String getName()
45      {
46          return "shutdown";
47      }
48  
49      public int getPort()
50      {
51          return 31457;
52      }
53  
54      public void setPort(int port)
55      {
56      }
57  
58      public void start()
59      {
60          Thread t = new Thread(this, "listener: " + getName());
61          t.setDaemon(true);
62          t.start();
63      }
64  
65      public void stop()
66      {
67      }
68  
69      public void run()
70      {
71          try
72          {
73              DatagramSocket socket = new DatagramSocket(new InetSocketAddress(InetAddress.getLocalHost(), getPort()));
74  
75              while (true)
76              {
77                  int length = SHUTDOWN_COMMAND.length();
78                  DatagramPacket packet = new DatagramPacket(new byte[length], length);
79                  socket.receive(packet);
80  
81                  if (new String(packet.getData(), "UTF8").equals(SHUTDOWN_COMMAND))
82                  {
83                      log.info("Shutdown command received");
84                      Server.getInstance().stop();
85                      break;
86                  }
87              }
88          }
89          catch (IOException e)
90          {
91              log.log(Level.WARNING, "ShutdownListener error", e);
92          }
93      }
94  
95      public boolean isRunning()
96      {
97          return true;
98      }
99  }