|
From: <cr...@us...> - 2008-05-11 09:16:49
|
Revision: 4083
http://jnode.svn.sourceforge.net/jnode/?rev=4083&view=rev
Author: crawley
Date: 2008-05-11 02:16:47 -0700 (Sun, 11 May 2008)
Log Message:
-----------
Renamed / converted TcpInoutCommand
Modified Paths:
--------------
trunk/net/descriptors/org.jnode.net.command.xml
Added Paths:
-----------
trunk/net/src/net/org/jnode/net/command/TcpInoutCommand.java
Removed Paths:
-------------
trunk/net/src/net/org/jnode/net/command/NetCommand.java
Modified: trunk/net/descriptors/org.jnode.net.command.xml
===================================================================
--- trunk/net/descriptors/org.jnode.net.command.xml 2008-05-11 03:00:18 UTC (rev 4082)
+++ trunk/net/descriptors/org.jnode.net.command.xml 2008-05-11 09:16:47 UTC (rev 4083)
@@ -36,7 +36,7 @@
<alias name="resolve" class="org.jnode.net.command.ResolveCommand"/>
<alias name="tftp" class="org.jnode.net.command.TftpCommand"/>
<alias name="wlanctl" class="org.jnode.net.command.WLanCtlCommand"/>
- <alias name="net" class="org.jnode.net.command.NetCommand"/>
+ <alias name="tcpinout" class="org.jnode.net.command.TcpInoutCommand"/>
<alias name="rpcinfo" class="org.jnode.net.command.RpcInfoCommand"/>
</extension>
@@ -60,6 +60,13 @@
<optional><argument argLabel="subnetMask"/></optional>
</sequence>
</syntax>
+ <syntax alias="tcpinout">
+ <argument argLabel="localPort" description="Run tcpinout in server mode"/>
+ <sequence description="Run tcpinout in client mode">
+ <argument argLabel="host"/>
+ <argument argLabel="port"/>
+ </sequence>
+ </syntax>
</extension>
<extension point="org.jnode.security.permissions">
Deleted: trunk/net/src/net/org/jnode/net/command/NetCommand.java
===================================================================
--- trunk/net/src/net/org/jnode/net/command/NetCommand.java 2008-05-11 03:00:18 UTC (rev 4082)
+++ trunk/net/src/net/org/jnode/net/command/NetCommand.java 2008-05-11 09:16:47 UTC (rev 4083)
@@ -1,140 +0,0 @@
-package org.jnode.net.command;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.io.PrintStream;
-import java.net.InetAddress;
-import java.net.ServerSocket;
-import java.net.Socket;
-
-import javax.net.ServerSocketFactory;
-import javax.net.SocketFactory;
-
-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.Syntax;
-import org.jnode.shell.help.argument.InetAddressArgument;
-import org.jnode.shell.help.argument.IntegerArgument;
-
-public class NetCommand extends AbstractCommand {
-
- private static final InetAddressArgument ARG_HOST = new InetAddressArgument(
- "ip-address", "the IP-address of the server to contact");
-
- private static final IntegerArgument ARG_PORT = new IntegerArgument("port",
- "the port the server is listening to");
-
- private static final IntegerArgument ARG_LISTENPORT = new IntegerArgument(
- "port", "the Port the server should listen to");
-
- private static final Parameter PARAM_LISTEN = new Parameter(ARG_LISTENPORT);
-
- public static Help.Info HELP_INFO = new Help.Info("tcp", new Syntax(
- "Connect to a remote server", new Parameter(ARG_HOST),
- new Parameter(ARG_PORT)), new Syntax("Listens to a specified port",
- PARAM_LISTEN));
-
- /**
- * @param args
- */
- public static void main(String[] args) throws Exception {
- new NetCommand().execute(args);
- }
-
-
- public void execute(CommandLine commandLine, InputStream in, PrintStream out, PrintStream err) throws Exception {
- ParsedArguments args = HELP_INFO.parse(commandLine);
-
- Socket socket = null;
- if (PARAM_LISTEN.isSet(args)) {
- int port = ARG_LISTENPORT.getInteger(args);
- ServerSocket ss = ServerSocketFactory.getDefault()
- .createServerSocket(port);
- socket = ss.accept();
- } else {
- InetAddress host = ARG_HOST.getAddress(args);
- int port = ARG_PORT.getInteger(args);
- socket = SocketFactory.getDefault().createSocket(host, port);
- }
-
- new NetConnection(socket).start();
- }
-
- private static class NetConnection {
- private Socket socket;
-
- private CopyThread toThread;
-
- private CopyThread fromThread;
-
- private NetConnection(Socket socket) {
- this.socket = socket;
- }
-
- private void start() throws IOException {
- toThread = new CopyThread(System.in, socket.getOutputStream());
- fromThread = new CopyThread(socket.getInputStream(), System.out);
- toThread.start();
- fromThread.start();
- synchronized(this) {
- try {
- wait();
- } catch (Exception e) {
- }
- }
- }
-
- private void close(CopyThread source) {
- if (socket != null) {
- try {
- socket.close();
- } catch (IOException e) {
- }
- socket = null;
- synchronized(this) {
- notifyAll();
- }
- }
- if (source == fromThread)
- toThread.breakout();
- else
- fromThread.breakout();
- }
-
- private class CopyThread extends Thread {
-
- private final InputStream in;
-
- private final OutputStream out;
-
- public CopyThread(InputStream in, OutputStream out) {
- this.in = in;
- this.out = out;
- }
-
- void breakout() {
- interrupt();
- }
-
- public void run() {
- try {
- while (socket != null) {
- int b = in.read();
- if (b == -1)
- break;
-
- out.write(b);
- }
- } catch (Exception iex) {
- } finally {
- close(this);
- }
- }
-
- }
- }
-}
Added: trunk/net/src/net/org/jnode/net/command/TcpInoutCommand.java
===================================================================
--- trunk/net/src/net/org/jnode/net/command/TcpInoutCommand.java (rev 0)
+++ trunk/net/src/net/org/jnode/net/command/TcpInoutCommand.java 2008-05-11 09:16:47 UTC (rev 4083)
@@ -0,0 +1,150 @@
+package org.jnode.net.command;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.PrintStream;
+import java.net.InetAddress;
+import java.net.ServerSocket;
+import java.net.Socket;
+
+import javax.net.ServerSocketFactory;
+import javax.net.SocketFactory;
+
+import org.jnode.shell.AbstractCommand;
+import org.jnode.shell.CommandLine;
+import org.jnode.shell.syntax.*;
+
+/**
+ * This command establishes a TCP connection to a remote machine, either by
+ * connecting to it or accepting a remote connection. Once the connection has
+ * been set up, it sends the command's standard input to the remote connection and
+ * sends the output from the connection to the command's standard output.
+ *
+ * @author quades
+ * @author cr...@jn...
+ */
+public class TcpInoutCommand extends AbstractCommand {
+ // FIXME this command is only useful for testing. What we need is
+ // implementations of TELNET, RSH and SSH protocols (client and server-side).
+
+ private final HostNameArgument ARG_HOST =
+ new HostNameArgument("host", Argument.OPTIONAL,
+ "the hostname of the server to contact");
+
+ private final PortNumberArgument ARG_PORT =
+ new PortNumberArgument("port", Argument.OPTIONAL,
+ "the port the server is listening to");
+
+ private final PortNumberArgument ARG_LOCAL_PORT =
+ new PortNumberArgument("localPort", Argument.OPTIONAL,
+ "the local port we should listen to");
+
+ private Socket socket;
+ private CopyThread toThread;
+ private CopyThread fromThread;
+
+ public TcpInoutCommand() {
+ super("Set up an interactive TCP connection to a remote machine");
+ registerArguments(ARG_HOST, ARG_LOCAL_PORT, ARG_PORT);
+ }
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) throws Exception {
+ new TcpInoutCommand().execute(args);
+ }
+
+
+ public void execute(CommandLine commandLine, InputStream in, PrintStream out, PrintStream err)
+ throws IOException {
+ Socket socket;
+ if (ARG_LOCAL_PORT.isSet()) {
+ int port = ARG_LOCAL_PORT.getValue();
+ ServerSocket ss =
+ ServerSocketFactory.getDefault().createServerSocket(port);
+ socket = ss.accept();
+ }
+ else {
+ InetAddress host = ARG_HOST.getAsInetAddress();
+ int port = ARG_PORT.getValue();
+ socket = SocketFactory.getDefault().createSocket(host, port);
+ }
+
+ toThread = new CopyThread(in, socket.getOutputStream(), err);
+ fromThread = new CopyThread(socket.getInputStream(), out, err);
+
+ synchronized (this) {
+ toThread.start();
+ fromThread.start();
+ try {
+ wait();
+ }
+ catch (InterruptedException e) {
+ close(null);
+ }
+ }
+ }
+
+ private synchronized void close(CopyThread source) {
+ if (socket != null) {
+ try {
+ socket.close();
+ }
+ catch (IOException e) {
+ // We don't care ...
+ }
+ socket = null;
+ notifyAll();
+ }
+ if (source != toThread) {
+ toThread.terminate();
+ }
+ if (source != fromThread) {
+ fromThread.terminate();
+ }
+ }
+
+ private class CopyThread extends Thread {
+ private final InputStream in;
+ private final OutputStream out;
+ private final PrintStream err;
+ private boolean terminated;
+
+ CopyThread(InputStream in, OutputStream out, PrintStream err) {
+ this.in = in;
+ this.out = out;
+ this.err = err;
+ }
+
+ synchronized void terminate() {
+ if (!this.terminated) {
+ interrupt();
+ this.terminated = true;
+ }
+ }
+
+ public void run() {
+ try {
+ while (socket != null) {
+ int b = in.read();
+ if (b == -1) {
+ break;
+ }
+ out.write(b);
+ }
+ }
+ catch (IOException ex) {
+ synchronized(this) {
+ if (!terminated) {
+ err.println(ex.getLocalizedMessage());
+ }
+ }
+ }
+ finally {
+ close(this);
+ }
+ }
+ }
+}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|