[jetrix-cvs] SF.net SVN: jetrix:[757] jetrix/trunk/src
Brought to you by:
smanux
From: <sm...@us...> - 2008-09-02 09:49:37
|
Revision: 757 http://jetrix.svn.sourceforge.net/jetrix/?rev=757&view=rev Author: smanux Date: 2008-09-02 09:49:34 +0000 (Tue, 02 Sep 2008) Log Message: ----------- Jetrix can now be stopped from the command line with "jetrix stop" Modified Paths: -------------- jetrix/trunk/src/bin/jetrix jetrix/trunk/src/bin/jetrix.bat jetrix/trunk/src/java/net/jetrix/Launcher.java jetrix/trunk/src/java/net/jetrix/Server.java Added Paths: ----------- jetrix/trunk/src/java/net/jetrix/listeners/ShutdownListener.java Removed Paths: ------------- jetrix/trunk/src/etc/log/.cvsignore Modified: jetrix/trunk/src/bin/jetrix =================================================================== --- jetrix/trunk/src/bin/jetrix 2008-09-02 09:45:45 UTC (rev 756) +++ jetrix/trunk/src/bin/jetrix 2008-09-02 09:49:34 UTC (rev 757) @@ -7,4 +7,4 @@ JAVA_PATH="java"; fi -$JAVA_PATH -jar lib/jetrix-launcher-@version@.jar +$JAVA_PATH -jar lib/jetrix-launcher-@version@.jar $1 Modified: jetrix/trunk/src/bin/jetrix.bat =================================================================== --- jetrix/trunk/src/bin/jetrix.bat 2008-09-02 09:45:45 UTC (rev 756) +++ jetrix/trunk/src/bin/jetrix.bat 2008-09-02 09:49:34 UTC (rev 757) @@ -5,4 +5,4 @@ IF NOT JAVA_HOME == "" SET JAVA_EXE="%JAVA_HOME%\bin\java" IF JAVA_HOME == "" SET JAVA_EXE=java -%JAVA_EXE% -Djava.library.path=lib -jar lib/jetrix-launcher-@version@.jar +%JAVA_EXE% -Djava.library.path=lib -jar lib/jetrix-launcher-@version@.jar %1 Deleted: jetrix/trunk/src/etc/log/.cvsignore =================================================================== --- jetrix/trunk/src/etc/log/.cvsignore 2008-09-02 09:45:45 UTC (rev 756) +++ jetrix/trunk/src/etc/log/.cvsignore 2008-09-02 09:49:34 UTC (rev 757) @@ -1 +0,0 @@ -*.log Modified: jetrix/trunk/src/java/net/jetrix/Launcher.java =================================================================== --- jetrix/trunk/src/java/net/jetrix/Launcher.java 2008-09-02 09:45:45 UTC (rev 756) +++ jetrix/trunk/src/java/net/jetrix/Launcher.java 2008-09-02 09:49:34 UTC (rev 757) @@ -21,11 +21,16 @@ import java.io.File; import java.io.FileOutputStream; -import java.util.List; -import java.util.ArrayList; +import java.io.IOException; import java.net.URL; import java.net.URLClassLoader; -import java.util.jar.*; +import java.net.DatagramPacket; +import java.net.DatagramSocket; +import java.net.InetAddress; +import java.util.ArrayList; +import java.util.List; +import java.util.jar.JarOutputStream; +import java.util.jar.Pack200; /** * An application launcher executing a specified class and building dynamically @@ -46,11 +51,42 @@ */ public static void main(String[] args) throws Exception { + if (args.length == 1 && "stop".equals(args[0])) + { + stop(); + } + else + { + start(args); + } + } + + private static void start(String[] args) throws Exception + { // get the files in the lib directory File repository = new File("lib/"); - File[] files = repository.listFiles(); // decompress the pack200 files + unpack(repository); + + ClassLoader loader = createClassLoader(repository, new File("lang/")); + + Thread.currentThread().setContextClassLoader(loader); + + // run the main method of the specified class + Class serverClass = loader.loadClass(MAIN_CLASS); + serverClass.getMethod("main", String[].class).invoke(null, new Object[] { args }); + } + + /** + * Unpack the pack200 files in the specified directory + * + * @param directory + */ + private static void unpack(File directory) throws IOException + { + File[] files = directory.listFiles(); + for (File file : files) { String filename = file.getAbsolutePath(); @@ -69,40 +105,57 @@ file.delete(); } } + } - // build the list of JARs in the ./lib directory - files = repository.listFiles(); - List<URL> jars = new ArrayList<URL>(); + /** + * Build a classloader including the jar and zip files in the specified + * directories. The directories are also included in the classpath. + * + * @param directories + */ + private static ClassLoader createClassLoader(File... directories) throws Exception + { + List<URL> urls = new ArrayList<URL>(); - for (int i = 0; i < files.length; i++) + for (File directory : directories) { - String filename = files[i].getAbsolutePath(); - if (filename.endsWith(".jar") || filename.endsWith(".zip")) + // add the jar and zip files in the directory to the classpath + File[] files = directory.listFiles(); + + for (int i = 0; i < files.length; i++) { - jars.add(files[i].toURL()); + String filename = files[i].getAbsolutePath(); + if (filename.endsWith(".jar") || filename.endsWith(".zip")) + { + urls.add(files[i].toURI().toURL()); + } } + + // add the directory to the classpath + urls.add(directory.toURI().toURL()); } - // add the lib directory to the classpath - jars.add(repository.toURL()); + // create the classloader + return new URLClassLoader(urls.toArray(new URL[urls.size()]), null); + } - // add the lang directory to the classpath - jars.add(new File("lang/").toURL()); + private static void stop() throws Exception + { + // send the "shutdown" to the UDP port 31457 + byte[] msg = "shutdown".getBytes("UTF8"); + DatagramPacket packet = new DatagramPacket(msg, msg.length); + packet.setAddress(InetAddress.getLocalHost()); + packet.setPort(31457); - // build the list of URLs - URL[] urls = new URL[jars.size()]; - for (int i = 0; i < jars.size(); i++) + DatagramSocket socket = new DatagramSocket(); + try { - urls[i] = jars.get(i); + socket.send(packet); } - - // create the classloader - ClassLoader loader = new URLClassLoader(urls, null); - Thread.currentThread().setContextClassLoader(loader); - - // run the main method of the specified class - Class serverClass = loader.loadClass(MAIN_CLASS); - serverClass.getMethod("main", args.getClass()).invoke(null, new Object[] { args }); + finally + { + socket.close(); + } } } Modified: jetrix/trunk/src/java/net/jetrix/Server.java =================================================================== --- jetrix/trunk/src/java/net/jetrix/Server.java 2008-09-02 09:45:45 UTC (rev 756) +++ jetrix/trunk/src/java/net/jetrix/Server.java 2008-09-02 09:49:34 UTC (rev 757) @@ -27,6 +27,7 @@ import net.jetrix.config.*; import net.jetrix.messages.*; import net.jetrix.services.VersionService; +import net.jetrix.listeners.ShutdownListener; /** * Main class, starts server components. @@ -111,6 +112,9 @@ } } + // start the shutdown listener + new ShutdownListener().start(); + // start the services for (Service service : config.getServices()) { @@ -281,7 +285,7 @@ */ public static void main(String[] args) { - System.out.println("Jetrix TetriNET Server " + ServerConfig.VERSION + ", Copyright (C) 2001-2005 Emmanuel Bourg\n"); + System.out.println("Jetrix TetriNET Server " + ServerConfig.VERSION + ", Copyright (C) 2001-2008 Emmanuel Bourg\n"); Server server = Server.getInstance(); server.start(); } Added: jetrix/trunk/src/java/net/jetrix/listeners/ShutdownListener.java =================================================================== --- jetrix/trunk/src/java/net/jetrix/listeners/ShutdownListener.java (rev 0) +++ jetrix/trunk/src/java/net/jetrix/listeners/ShutdownListener.java 2008-09-02 09:49:34 UTC (rev 757) @@ -0,0 +1,99 @@ +/** + * Jetrix TetriNET Server + * Copyright (C) 2008 Emmanuel Bourg + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +package net.jetrix.listeners; + +import java.io.IOException; +import java.net.DatagramPacket; +import java.net.DatagramSocket; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.util.logging.Level; + +import net.jetrix.Listener; +import net.jetrix.Server; +import net.jetrix.services.AbstractService; + +/** + * Listens for shutdown commands from the localhost. This is used to stop + * the server from the shell on Unix. + * + * @author Emmanuel Bourg + * @version $Revision$, $Date$ + */ +public class ShutdownListener extends AbstractService implements Listener +{ + private static final String SHUTDOWN_COMMAND = "shutdown"; + + public String getName() + { + return "shutdown"; + } + + public int getPort() + { + return 31457; + } + + public void setPort(int port) + { + } + + public void start() + { + Thread t = new Thread(this, "listener: " + getName()); + t.setDaemon(true); + t.start(); + } + + public void stop() + { + } + + public void run() + { + try + { + DatagramSocket socket = new DatagramSocket(new InetSocketAddress(InetAddress.getLocalHost(), getPort())); + + while (true) + { + int length = SHUTDOWN_COMMAND.length(); + DatagramPacket packet = new DatagramPacket(new byte[length], length); + socket.receive(packet); + + if (packet != null && new String(packet.getData(), "UTF8").equals(SHUTDOWN_COMMAND)) + { + log.info("Shutdown command received"); + Server.getInstance().stop(); + break; + } + } + } + catch (IOException e) + { + log.log(Level.WARNING, "ShutdownListener error", e); + } + } + + public boolean isRunning() + { + return true; + } +} Property changes on: jetrix/trunk/src/java/net/jetrix/listeners/ShutdownListener.java ___________________________________________________________________ Added: svn:keywords + Date Author Id Revision HeadURL Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |