From: <cr...@us...> - 2008-05-02 14:34:13
|
Revision: 4040 http://jnode.svn.sourceforge.net/jnode/?rev=4040&view=rev Author: crawley Date: 2008-05-02 07:34:11 -0700 (Fri, 02 May 2008) Log Message: ----------- Overhaul 'udpout' (now 'remoteout') to support TCP and to work without messing with System.out,err ... which can be problematic. Modified Paths: -------------- trunk/shell/descriptors/org.jnode.shell.command.debug.xml Added Paths: ----------- trunk/shell/src/shell/org/jnode/shell/command/debug/RemoteOutputCommand.java Removed Paths: ------------- trunk/shell/src/shell/org/jnode/shell/command/debug/UDPOutputCommand.java Modified: trunk/shell/descriptors/org.jnode.shell.command.debug.xml =================================================================== --- trunk/shell/descriptors/org.jnode.shell.command.debug.xml 2008-05-02 14:32:13 UTC (rev 4039) +++ trunk/shell/descriptors/org.jnode.shell.command.debug.xml 2008-05-02 14:34:11 UTC (rev 4040) @@ -29,12 +29,19 @@ <option argLabel="port" shortName="p" description="The port for the debugger to connect to"/> </optional> </syntax> + <syntax alias="remoteout"> + <optionSet description="Redirect output and logging to a remote receiver"> + <option argLabel="host" shortName="h" longName="host" description="The host for the remote receiver"/> + <option argLabel="port" shortName="p" longName="port" description="The port for the remote receiver"/> + <option argLabel="udp" shortName="u" longName="udp" description="If set use UDP, otherwise use TCP"/> + </optionSet> + </syntax> </extension> <extension point="org.jnode.security.permissions"> <permission class="java.lang.RuntimePermission" name="modifyThreadGroup"/> <permission class="java.lang.RuntimePermission" name="setIO"/> - <permission class="java.net.SocketPermission" name="*:1-" actions="resolve,listen"/> + <permission class="java.net.SocketPermission" name="*:1-" actions="resolve,listen,connect"/> </extension> </plugin> \ No newline at end of file Added: trunk/shell/src/shell/org/jnode/shell/command/debug/RemoteOutputCommand.java =================================================================== --- trunk/shell/src/shell/org/jnode/shell/command/debug/RemoteOutputCommand.java (rev 0) +++ trunk/shell/src/shell/org/jnode/shell/command/debug/RemoteOutputCommand.java 2008-05-02 14:34:11 UTC (rev 4040) @@ -0,0 +1,110 @@ +/* + * $Id: UDPOutputCommand.java 4032 2008-04-27 13:53:11Z crawley $ + * + * JNode.org + * Copyright (C) 2003-2006 JNode.org + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation; either version 2.1 of the License, or + * (at your option) any later version. + * + * This library 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 Lesser General Public + * License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; If not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +package org.jnode.shell.command.debug; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.PrintStream; +import java.net.ConnectException; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.Socket; +import java.net.SocketAddress; +import java.net.UnknownHostException; + +import org.apache.log4j.Logger; +import org.jnode.debug.RemoteReceiver; +import org.jnode.debug.RemoteAppender; +import org.jnode.debug.UDPOutputStream; +import org.jnode.shell.AbstractCommand; +import org.jnode.shell.CommandLine; +import org.jnode.shell.ShellUtils; +import org.jnode.shell.syntax.Argument; +import org.jnode.shell.syntax.FlagArgument; +import org.jnode.shell.syntax.HostNameArgument; +import org.jnode.shell.syntax.IntegerArgument; + +/** + * @author Ewout Prangsma (ep...@us...) + * @author Martin Husted Hartvig (ha...@jn...) + * @author cr...@jn... + */ +public class RemoteOutputCommand extends AbstractCommand { + public static int DEFAULT_PORT = RemoteReceiver.DEFAULT_PORT; + + private final HostNameArgument ARG_ADDRESS = + new HostNameArgument("host", Argument.MANDATORY, "remote host running the receiver"); + + private final IntegerArgument ARG_PORT = + new IntegerArgument("port", Argument.OPTIONAL, "remote port for the receiver"); + + private final FlagArgument FLAG_UDP = + new FlagArgument("udp", Argument.OPTIONAL, "if set use udp, otherwise use tcp"); + + public RemoteOutputCommand() { + super("send data from System.out, System.err and the logger to a remote receiver"); + registerArguments(ARG_ADDRESS, ARG_PORT, FLAG_UDP); + } + + public void execute(CommandLine commandLine, InputStream in, + PrintStream out, PrintStream err) + throws Exception { + try { + final int port = ARG_PORT.isSet() ? ARG_PORT.getValue() : DEFAULT_PORT; + final InetAddress addr = ARG_ADDRESS.getAsInetAddress(); + final SocketAddress sockAddr = new InetSocketAddress(addr, port); + final boolean udp = FLAG_UDP.isSet(); + OutputStream remoteOut = udp ? new UDPOutputStream(sockAddr) : + createTCPOutputStream(addr, port); + + try { + ShellUtils.getCurrentShell().addConsoleOuputRecorder(remoteOut); + } + catch (UnsupportedOperationException ex) { + err.println("Cannot capture output from the current shell"); + remoteOut.close(); + exit(2); + } + // FIXME ... we cannot do this for TCP because it triggers a + // kernel panic. I suspect that this is something to do with + // the TCP protocol stack itself trying to log debug messages. + if (udp) { + final Logger root = Logger.getRootLogger(); + root.addAppender(new RemoteAppender(remoteOut, null)); + } + } + catch (ConnectException ex) { + err.println("Connection failed: " + ex.getMessage()); + exit(1); + } + catch (UnknownHostException ex) { + err.println("Unknown host: " + ex.getMessage()); + exit(1); + } + } + + private OutputStream createTCPOutputStream(InetAddress addr, int port) throws IOException { + Socket socket = new Socket(addr, port); + return socket.getOutputStream(); + } +} Deleted: trunk/shell/src/shell/org/jnode/shell/command/debug/UDPOutputCommand.java =================================================================== --- trunk/shell/src/shell/org/jnode/shell/command/debug/UDPOutputCommand.java 2008-05-02 14:32:13 UTC (rev 4039) +++ trunk/shell/src/shell/org/jnode/shell/command/debug/UDPOutputCommand.java 2008-05-02 14:34:11 UTC (rev 4040) @@ -1,155 +0,0 @@ -/* - * $Id$ - * - * JNode.org - * Copyright (C) 2003-2006 JNode.org - * - * This library is free software; you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License as published - * by the Free Software Foundation; either version 2.1 of the License, or - * (at your option) any later version. - * - * This library 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 Lesser General Public - * License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this library; If not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -package org.jnode.shell.command.debug; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.io.PrintStream; -import java.net.InetSocketAddress; -import java.net.SocketAddress; - -import org.apache.log4j.Logger; -import org.jnode.debug.UDPAppender; -import org.jnode.debug.UDPOutputStream; -import org.jnode.shell.AbstractCommand; -import org.jnode.shell.CommandLine; -import org.jnode.shell.help.Help; -import org.jnode.shell.help.Parameter; -import org.jnode.shell.help.ParsedArguments; -import org.jnode.shell.help.argument.InetAddressArgument; -import org.jnode.shell.help.argument.IntegerArgument; - -/** - * @author Ewout Prangsma (ep...@us...) - * @author Martin Husted Hartvig (ha...@jn...) - */ -public class UDPOutputCommand extends AbstractCommand { - - private static final InetAddressArgument ARG_ADDRESS = - new InetAddressArgument("host", "connect host to the udpreceiver"); - - private static final IntegerArgument ARG_PORT = - new IntegerArgument("port", "port to connect on"); - - private static final Parameter PARAM_ADDRESS = - new Parameter(ARG_ADDRESS, Parameter.MANDATORY); - - private static final Parameter PARAM_PORT = - new Parameter(ARG_PORT, Parameter.OPTIONAL); - - public static Help.Info HELP_INFO = - new Help.Info("udpout", - "send data from System.out and System.err to remote host", - new Parameter[] { PARAM_ADDRESS, PARAM_PORT }); - - public void execute(CommandLine commandLine, InputStream in, - PrintStream out, PrintStream err) throws Exception { - - final ParsedArguments args = HELP_INFO.parse(commandLine); - - final int port; - - if (PARAM_PORT.isSet(args)) { - port = ARG_PORT.getInteger(args); - } else { - port = 5612; - } - - final SocketAddress address = - new InetSocketAddress(ARG_ADDRESS.getAddress(args), port); - UDPOutputStream udpOut = new UDPOutputStream(address); - - DupOutputStream dupOut = new DupOutputStream(System.out, udpOut); - PrintStream ps = new PrintStream(dupOut); - System.setOut(ps); - System.setErr(ps); - - final Logger root = Logger.getRootLogger(); - root.addAppender(new UDPAppender(udpOut, null)); - } - - static class DupOutputStream extends OutputStream { - - private final OutputStream os1; - private final OutputStream os2; - - public DupOutputStream(OutputStream os1, OutputStream os2) { - this.os1 = os1; - this.os2 = os2; - } - - /** - * @see java.io.OutputStream#close() - * @throws IOException - */ - public void close() throws IOException { - os1.close(); - os2.close(); - } - - /** - * @see java.io.OutputStream#flush() - * @throws IOException - */ - public void flush() throws IOException { - os1.flush(); - os2.flush(); - } - - /** - * @param b - * @param off - * @param len - * @see java.io.OutputStream#write(byte[], int, int) - * @throws IOException - * @throws NullPointerException - * @throws IndexOutOfBoundsException - */ - public void write(byte[] b, int off, int len) throws IOException, - NullPointerException, IndexOutOfBoundsException { - os1.write(b, off, len); - os2.write(b, off, len); - } - - /** - * @param b - * @see java.io.OutputStream#write(byte[]) - * @throws IOException - * @throws NullPointerException - */ - public void write(byte[] b) throws IOException, NullPointerException { - os1.write(b); - os2.write(b); - } - - /** - * @param b - * @see java.io.OutputStream#write(int) - * @throws IOException - */ - public void write(int b) throws IOException { - os1.write(b); - os2.write(b); - } - } -} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |