|
From: <cr...@us...> - 2008-05-13 14:14:27
|
Revision: 4096
http://jnode.svn.sourceforge.net/jnode/?rev=4096&view=rev
Author: crawley
Date: 2008-05-13 07:13:57 -0700 (Tue, 13 May 2008)
Log Message:
-----------
Converted TftpCommand, refactored a bit and fixed some bugs
Modified Paths:
--------------
trunk/net/descriptors/org.jnode.net.command.xml
trunk/net/descriptors/org.jnode.net.ipv4.xml
trunk/net/src/net/org/jnode/net/command/TftpCommand.java
trunk/net/src/net/org/jnode/net/ipv4/tftp/TFTPClient.java
Modified: trunk/net/descriptors/org.jnode.net.command.xml
===================================================================
--- trunk/net/descriptors/org.jnode.net.command.xml 2008-05-13 12:02:17 UTC (rev 4095)
+++ trunk/net/descriptors/org.jnode.net.command.xml 2008-05-13 14:13:57 UTC (rev 4096)
@@ -99,6 +99,17 @@
<argument argLabel="port"/>
</sequence>
</syntax>
+ <syntax alias="tftp">
+ <optional description="Run an interactive TFTP client"><argument argLabel="host"/></optional>
+ <sequence description="Do a non-interactive TFTP 'get' or 'put'">
+ <alternatives>
+ <option argLabel="get" longName="get"/>
+ <option argLabel="put" longName="put"/>
+ </alternatives>
+ <argument argLabel="host"/>
+ <argument argLabel="filename"/>
+ </sequence>
+ </syntax>
</extension>
<extension point="org.jnode.security.permissions">
@@ -108,6 +119,7 @@
<permission class="java.net.SocketPermission" name="*:80" actions="resolve,listen,connect"/>
<permission class="java.util.PropertyPermission" name="dns.server" actions="read"/>
<permission class="java.util.PropertyPermission" name="dns.search" actions="read"/>
+ <permission class="java.util.PropertyPermission" name="user.dir" actions="read"/>
<permission class="org.jnode.net.NetPermission" name="bootpClient"/>
<permission class="org.jnode.net.NetPermission" name="dhcpClient"/>
<permission class="java.io.FilePermission" name="<<ALL FILES>>" actions="read,write"/>
Modified: trunk/net/descriptors/org.jnode.net.ipv4.xml
===================================================================
--- trunk/net/descriptors/org.jnode.net.ipv4.xml 2008-05-13 12:02:17 UTC (rev 4095)
+++ trunk/net/descriptors/org.jnode.net.ipv4.xml 2008-05-13 14:13:57 UTC (rev 4096)
@@ -48,6 +48,12 @@
<permission class="java.net.SocketPermission" name="*:53" actions="connect,resolve,listen"/>
<permission class="java.util.PropertyPermission" name="dns.server" actions="read,write"/>
<permission class="java.util.PropertyPermission" name="dns.search" actions="read,write"/>
+
+ <!-- TFTP !?! -->
+ <permission class="java.io.FilePermission" name="<<ALL FILES>>" actions="read,write"/>
+ <permission class="java.util.PropertyPermission" name="user.dir" actions="read"/>
+ <permission class="java.net.SocketPermission" name="*:69" actions="connect,resolve"/>
+
</extension>
<extension point="org.jnode.net.networkLayers">
Modified: trunk/net/src/net/org/jnode/net/command/TftpCommand.java
===================================================================
--- trunk/net/src/net/org/jnode/net/command/TftpCommand.java 2008-05-13 12:02:17 UTC (rev 4095)
+++ trunk/net/src/net/org/jnode/net/command/TftpCommand.java 2008-05-13 14:13:57 UTC (rev 4096)
@@ -21,60 +21,80 @@
package org.jnode.net.command;
+import java.io.File;
import java.io.InputStream;
import java.io.PrintStream;
-import org.jnode.net.help.argument.HostArgument;
import org.jnode.net.ipv4.tftp.TFTPClient;
import org.jnode.shell.AbstractCommand;
import org.jnode.shell.CommandLine;
-import org.jnode.shell.help.Argument;
-import org.jnode.shell.help.Help;
-import org.jnode.shell.help.Parameter;
-import org.jnode.shell.help.Syntax;
-import org.jnode.shell.help.argument.OptionArgument;
+import org.jnode.shell.syntax.Argument;
+import org.jnode.shell.syntax.FileArgument;
+import org.jnode.shell.syntax.FlagArgument;
+import org.jnode.shell.syntax.HostNameArgument;
/**
+ * This Command class does a batch mode TFTP get or put, or starts a simple TFTP client.
+ *
* @author markhale
+ * @author crawley
*/
public class TftpCommand extends AbstractCommand {
- private static final OptionArgument.Option[] COMMAND_OPTIONS = new OptionArgument.Option[] {
- new OptionArgument.Option("put", "transfer a file to a server"),
- new OptionArgument.Option("get", "transfer a file from a server")
- };
- private static final HostArgument ARG_SERVER = new HostArgument("hostname", "the hostname of the TFTP server");
- private static final OptionArgument ARG_COMMAND = new OptionArgument("command", "must be either PUT or GET", COMMAND_OPTIONS);
- private static final Argument ARG_FILENAME = new Argument("filename", "the file to transfer");
+ private final FlagArgument FLAG_PUT =
+ new FlagArgument("put", Argument.OPTIONAL, "if set, transfer a file to the TFTP server");
- public static Help.Info HELP_INFO = new Help.Info(
- "tftp",
- new Syntax[] {
- new Syntax(
- "Start the TFTP client as an interactive session",
- new Parameter[] {
- new Parameter(ARG_SERVER, Parameter.OPTIONAL)
- }
- ),
- new Syntax(
- "Execute the TFTP client non-interactively",
- new Parameter[] {
- new Parameter(ARG_SERVER, Parameter.MANDATORY),
- new Parameter(ARG_COMMAND, Parameter.MANDATORY),
- new Parameter(ARG_FILENAME, Parameter.MANDATORY)
- }
- )
- }
- );
+ private final FlagArgument FLAG_GET =
+ new FlagArgument("get", Argument.OPTIONAL, "if set, fetch a file from the TFTP server");
+
+ private final HostNameArgument ARG_SERVER =
+ new HostNameArgument("host", Argument.OPTIONAL, "the hostname of the TFTP server");
+
+ private final FileArgument ARG_FILENAME =
+ new FileArgument("filename", Argument.OPTIONAL, "the file to transfer");
+ public TftpCommand() {
+ super("Do a TFTP get or put, or run an interactive TFTP client");
+ registerArguments(FLAG_GET, FLAG_PUT, ARG_FILENAME, ARG_SERVER);
+ }
+
public static void main(String[] args) throws Exception {
new TftpCommand().execute(args);
}
public void execute(CommandLine commandLine, InputStream in, PrintStream out, PrintStream err)
throws Exception {
- TFTPClient.main(commandLine.getArguments());
- System.out.println();
+ TFTPClient client = new TFTPClient(out);
+ String host = ARG_SERVER.getValue();
+ File file = ARG_FILENAME.getValue();
+ if (FLAG_PUT.isSet()) {
+ if (client.executeCommand(new String[] {TFTPClient.CONNECT_CMD, host})) {
+ if (!client.executeCommand(new String[] {TFTPClient.PUT_CMD, file.toString()})) {
+ exit(1);
+ }
+ }
+ else {
+ exit(2);
+ }
+ }
+ else if (FLAG_GET.isSet()) {
+ if (client.executeCommand(new String[] {TFTPClient.CONNECT_CMD, host})) {
+ if (!client.executeCommand(new String[] {TFTPClient.GET_CMD, file.toString()})) {
+ exit(1);
+ }
+ }
+ else {
+ exit(2);
+ }
+ }
+ else {
+ if (host != null) {
+ if (!client.executeCommand(new String[] {TFTPClient.CONNECT_CMD, host})) {
+ exit(2);
+ }
+ }
+ client.run(in);
+ }
}
}
Modified: trunk/net/src/net/org/jnode/net/ipv4/tftp/TFTPClient.java
===================================================================
--- trunk/net/src/net/org/jnode/net/ipv4/tftp/TFTPClient.java 2008-05-13 12:02:17 UTC (rev 4095)
+++ trunk/net/src/net/org/jnode/net/ipv4/tftp/TFTPClient.java 2008-05-13 14:13:57 UTC (rev 4096)
@@ -21,16 +21,22 @@
package org.jnode.net.ipv4.tftp;
+import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.PrintStream;
+import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* Console TFTP client.
* Usage: TFTPClient [hostname [PUT/GET filename]]
+ *
* @author markhale
*/
public class TFTPClient extends org.apache.commons.net.tftp.TFTPClient {
@@ -46,81 +52,34 @@
public final static String HELP_CMD = "help";
public final static String QUIT_CMD = "quit";
- // BufferedReader does not currently function correctly (GNU classpath bug 5558)
- //private final BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
- // use DataInputStream instead
- private final DataInputStream in = new DataInputStream(System.in);
+ private BufferedReader br;
+ private PrintStream out;
private InetAddress serverAddress;
private int mode = BINARY_MODE;
private boolean quit;
-
- public static void main(String[] args) {
- TFTPClient client = new TFTPClient();
- if (args.length == 3) { // non-interactive mode
- if (args[1].equalsIgnoreCase(PUT_CMD)) {
- if (client.executeCommand(new String[] {CONNECT_CMD, args[0]})) {
- client.executeCommand(new String[] {PUT_CMD, args[2]});
- }
- }
- else if (args[1].equalsIgnoreCase(GET_CMD)) {
- if (client.executeCommand(new String[] {CONNECT_CMD, args[0]})) {
- client.executeCommand(new String[] {GET_CMD, args[2]});
- }
- }
- else {
- System.out.println("Unrecognised command line.");
- }
- }
- else { // interactive mode
- if (args.length == 1) {
- client.executeCommand(new String[] {CONNECT_CMD, args[0]});
- }
- client.run();
- }
+
+ public TFTPClient(PrintStream out) {
+ this.out = out;
}
@SuppressWarnings("deprecation")
- private void run() {
- System.out.println("JNode TFTP Client");
+ public void run(InputStream in) throws IOException {
+ // FIXME ... figure out to how to use JNode command argument parsing
+ // (and completion) for our little TFTP interactive command syntax.
+ this.br = new BufferedReader(new InputStreamReader(in));
+ out.println("JNode TFTP Client");
do {
- try {
- System.out.print("tftp> ");
- String line = in.readLine();
- String[] args = parseLine(line);
- executeCommand(args);
- } catch (IOException ex) {
+ out.print("tftp> ");
+ String line = br.readLine();
+ if (line == null) {
+ // EOF
+ break;
}
+ executeCommand(line.trim().split("\\s+"));
} while (!quit);
}
-
- private final static String[] parseLine(String line) {
- // count arguments
- int count = 0;
- int pos = -1;
- do {
- count++;
- pos = line.indexOf(' ', pos+1);
- } while (pos != -1);
-
- // parse
- String[] args = new String[count];
- count = 0;
- pos = -1;
- do {
- int startPos = pos + 1;
- pos = line.indexOf(' ', startPos);
- if (pos != -1) {
- args[count] = line.substring(startPos, pos);
- }
- else {
- args[count] = line.substring(startPos, line.length());
- }
- count++;
- } while (pos != -1);
- return args;
- }
/**
* High-level command API.
* @return true on success.
@@ -134,25 +93,26 @@
final String cmd = args[0];
if (cmd.equals(CONNECT_CMD)) { // connect
if (args.length < 2) {
- System.out.println("Please specify a host name.");
+ out.println("Please specify a host name.");
}
else {
try {
+ // FIXME ... this is not "connecting"!!
serverAddress = InetAddress.getByName(args[1]);
serverAddress.getHostName(); // do DNS lookup
success = true;
}
catch (UnknownHostException ex) {
- System.out.println("Unknown host " + args[1] + ".");
+ out.println("Unknown host " + args[1] + ".");
}
}
}
else if (cmd.equals(GET_CMD)) { // get
if (serverAddress == null) {
- System.out.println("Not connected.");
+ out.println("Not connected.");
}
else if (args.length < 2) {
- System.out.println("Please specify a file name.");
+ out.println("Please specify a file name.");
}
else {
String filename = args[1];
@@ -162,7 +122,7 @@
open();
try {
int bytesTransferred = receiveFile(filename, mode, fileOut, serverAddress);
- System.out.println(bytesTransferred + " bytes transferred.");
+ out.println(bytesTransferred + " bytes transferred.");
}
finally {
close();
@@ -174,16 +134,16 @@
success = true;
}
catch (IOException ex) {
- System.out.println("Error transferring file: " + ex.getMessage());
+ diagnose(ex, "Error transferring file");
}
}
}
else if (cmd.equals(PUT_CMD)) { // put
if (serverAddress == null) {
- System.out.println("Not connected.");
+ out.println("Not connected.");
}
else if (args.length < 2) {
- System.out.println("Please specify a file name.");
+ out.println("Please specify a file name.");
}
else {
String filename = args[1];
@@ -204,7 +164,7 @@
success = true;
}
catch (IOException ex) {
- System.out.println("Error transferring file: " + ex.getMessage());
+ diagnose(ex, "Error transferring file");
}
}
}
@@ -218,58 +178,58 @@
}
else if (cmd.equals(TIMEOUT_CMD)) { // timeout
if (args.length < 2) {
- System.out.println("Please specify a timeout value.");
+ out.println("Please specify a timeout value.");
}
else {
try {
setDefaultTimeout(Integer.parseInt(args[1]));
success = true;
- } catch(NumberFormatException ex) {
- System.out.println("Invalid timeout value.");
+ } catch (NumberFormatException ex) {
+ out.println("Invalid timeout value.");
}
}
}
else if (cmd.equals(RETRIES_CMD)) { // retries
if (args.length < 2) {
- System.out.println("Please specify a retries value.");
+ out.println("Please specify a retries value.");
}
else {
try {
setMaxTimeouts(Integer.parseInt(args[1]));
success = true;
- } catch(NumberFormatException ex) {
- System.out.println("Invalid retries value.");
+ } catch (NumberFormatException ex) {
+ out.println("Invalid retries value.");
}
}
}
else if (cmd.equals(STATUS_CMD)) { // status
if (serverAddress != null) {
- System.out.println("Connected to "+serverAddress.getHostName() + ".");
+ out.println("Connected to " + serverAddress.getHostName() + ".");
}
else {
- System.out.println("Not connected.");
+ out.println("Not connected.");
}
if (mode == ASCII_MODE) {
- System.out.print("mode: ASCII");
+ out.print("mode: ASCII");
}
else if (mode == BINARY_MODE) {
- System.out.print("mode: BINARY");
+ out.print("mode: BINARY");
}
- System.out.print(" timeout: " + getDefaultTimeout());
- System.out.println(" retries: " + getMaxTimeouts());
+ out.print(" timeout: " + getDefaultTimeout());
+ out.println(" retries: " + getMaxTimeouts());
success = true;
}
else if (cmd.equals(HELP_CMD)) { // help
- System.out.println(ASCII_CMD + " - set mode to ASCII");
- System.out.println(CONNECT_CMD + " - connect to a tftp server");
- System.out.println(BINARY_CMD + " - set mode to binary");
- System.out.println(GET_CMD + " - receive file");
- System.out.println(HELP_CMD + " - display this help");
- System.out.println(PUT_CMD + " - send file");
- System.out.println(QUIT_CMD + " - exit");
- System.out.println(RETRIES_CMD + " - set retries");
- System.out.println(STATUS_CMD + " - display current status");
- System.out.println(TIMEOUT_CMD + " - set timeout");
+ out.println(ASCII_CMD + " - set mode to ASCII");
+ out.println(CONNECT_CMD + " - connect to a tftp server");
+ out.println(BINARY_CMD + " - set mode to binary");
+ out.println(GET_CMD + " - receive file");
+ out.println(HELP_CMD + " - display this help");
+ out.println(PUT_CMD + " - send file");
+ out.println(QUIT_CMD + " - exit");
+ out.println(RETRIES_CMD + " - set retries");
+ out.println(STATUS_CMD + " - display current status");
+ out.println(TIMEOUT_CMD + " - set timeout");
success = true;
}
else if (cmd.equals(QUIT_CMD)) { // quit
@@ -277,8 +237,14 @@
success = true;
}
else {
- System.out.println("Unrecognised command.");
+ out.println("Unrecognised command.");
}
return success;
}
+
+ private void diagnose(IOException ex, String message) {
+ String exMessage = ex.getClass().getSimpleName() + " - " + ex.getLocalizedMessage();
+ out.println(message + ": " + exMessage);
+ }
+
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|