|
From: <cr...@us...> - 2008-05-13 12:02:21
|
Revision: 4095
http://jnode.svn.sourceforge.net/jnode/?rev=4095&view=rev
Author: crawley
Date: 2008-05-13 05:02:17 -0700 (Tue, 13 May 2008)
Log Message:
-----------
Preliminary code-style changes
Modified Paths:
--------------
trunk/net/src/net/org/jnode/net/command/TftpCommand.java
trunk/net/src/net/org/jnode/net/ipv4/tftp/TFTPClient.java
Modified: trunk/net/src/net/org/jnode/net/command/TftpCommand.java
===================================================================
--- trunk/net/src/net/org/jnode/net/command/TftpCommand.java 2008-05-13 11:42:45 UTC (rev 4094)
+++ trunk/net/src/net/org/jnode/net/command/TftpCommand.java 2008-05-13 12:02:17 UTC (rev 4095)
@@ -18,7 +18,7 @@
* 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.net.command;
import java.io.InputStream;
@@ -38,42 +38,43 @@
* @author markhale
*/
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");
-
- 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)
- }
- )
- }
- );
-
- 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();
- }
+ 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");
+
+ 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)
+ }
+ )
+ }
+ );
+
+ 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();
+ }
}
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 11:42:45 UTC (rev 4094)
+++ trunk/net/src/net/org/jnode/net/ipv4/tftp/TFTPClient.java 2008-05-13 12:02:17 UTC (rev 4095)
@@ -34,211 +34,251 @@
* @author markhale
*/
public class TFTPClient extends org.apache.commons.net.tftp.TFTPClient {
-
- public final static String CONNECT_CMD = "connect";
- public final static String PUT_CMD = "put";
- public final static String GET_CMD = "get";
- public final static String ASCII_CMD = "ascii";
- public final static String BINARY_CMD = "binary";
- public final static String TIMEOUT_CMD = "timeout";
- public final static String RETRIES_CMD = "retries";
- public final static String STATUS_CMD = "status";
- 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 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 final static String CONNECT_CMD = "connect";
+ public final static String PUT_CMD = "put";
+ public final static String GET_CMD = "get";
+ public final static String ASCII_CMD = "ascii";
+ public final static String BINARY_CMD = "binary";
+ public final static String TIMEOUT_CMD = "timeout";
+ public final static String RETRIES_CMD = "retries";
+ public final static String STATUS_CMD = "status";
+ 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 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();
+ }
+ }
+
@SuppressWarnings("deprecation")
- private void run() {
- System.out.println("JNode TFTP Client");
- do {
- try {
- System.out.print("tftp> ");
- String line = in.readLine();
- String[] args = parseLine(line);
- executeCommand(args);
- } catch(IOException ex) {
- }
- } 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.
- */
- public boolean executeCommand(String[] args) {
- if(args == null || args.length < 1)
- return false;
-
- boolean success = false;
- final String cmd = args[0];
- if(cmd.equals(CONNECT_CMD)) { // connect
- if(args.length < 2) {
- System.out.println("Please specify a host name.");
- } else {
- try {
- serverAddress = InetAddress.getByName(args[1]);
- serverAddress.getHostName(); // do DNS lookup
- success = true;
- } catch(UnknownHostException ex) {
- System.out.println("Unknown host "+args[1]+".");
- }
- }
- } else if(cmd.equals(GET_CMD)) { // get
- if(serverAddress == null) {
- System.out.println("Not connected.");
- } else if(args.length < 2) {
- System.out.println("Please specify a file name.");
- } else {
- String filename = args[1];
- try {
- FileOutputStream fileOut = new FileOutputStream(filename);
- try {
- open();
- try {
- int bytesTransferred = receiveFile(filename, mode, fileOut, serverAddress);
- System.out.println(bytesTransferred+" bytes transferred.");
- } finally {
- close();
- }
- } finally {
- fileOut.close();
- }
- success = true;
- } catch(IOException ex) {
- System.out.println("Error transferring file: "+ex.getMessage());
- }
- }
- } else if(cmd.equals(PUT_CMD)) { // put
- if(serverAddress == null) {
- System.out.println("Not connected.");
- } else if(args.length < 2) {
- System.out.println("Please specify a file name.");
- } else {
- String filename = args[1];
- try {
- FileInputStream fileIn = new FileInputStream(filename);
- try {
- open();
- try {
- sendFile(filename, mode, fileIn, serverAddress);
- } finally {
- close();
- }
- } finally {
- fileIn.close();
- }
- success = true;
- } catch(IOException ex) {
- System.out.println("Error transferring file: "+ex.getMessage());
- }
- }
- } else if(cmd.equals(ASCII_CMD)) { // ascii
- mode = ASCII_MODE;
- success = true;
- } else if(cmd.equals(BINARY_CMD)) { // binary
- mode = BINARY_MODE;
- success = true;
- } else if(cmd.equals(TIMEOUT_CMD)) { // timeout
- if(args.length < 2) {
- System.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.");
- }
- }
- } else if(cmd.equals(RETRIES_CMD)) { // retries
- if(args.length < 2) {
- System.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.");
- }
- }
- } else if(cmd.equals(STATUS_CMD)) { // status
- if(serverAddress != null)
- System.out.println("Connected to "+serverAddress.getHostName()+".");
- else
- System.out.println("Not connected.");
- if(mode == ASCII_MODE)
- System.out.print("mode: ASCII");
- else if(mode == BINARY_MODE)
- System.out.print("mode: BINARY");
- System.out.print(" timeout: "+getDefaultTimeout());
- System.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");
- success = true;
- } else if(cmd.equals(QUIT_CMD)) { // quit
- quit = true;
- success = true;
- } else {
- System.out.println("Unrecognised command.");
- }
- return success;
- }
+ private void run() {
+ System.out.println("JNode TFTP Client");
+ do {
+ try {
+ System.out.print("tftp> ");
+ String line = in.readLine();
+ String[] args = parseLine(line);
+ executeCommand(args);
+ } catch (IOException ex) {
+ }
+ } 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.
+ */
+ public boolean executeCommand(String[] args) {
+ if (args == null || args.length < 1) {
+ return false;
+ }
+
+ boolean success = false;
+ final String cmd = args[0];
+ if (cmd.equals(CONNECT_CMD)) { // connect
+ if (args.length < 2) {
+ System.out.println("Please specify a host name.");
+ }
+ else {
+ try {
+ serverAddress = InetAddress.getByName(args[1]);
+ serverAddress.getHostName(); // do DNS lookup
+ success = true;
+ }
+ catch (UnknownHostException ex) {
+ System.out.println("Unknown host " + args[1] + ".");
+ }
+ }
+ }
+ else if (cmd.equals(GET_CMD)) { // get
+ if (serverAddress == null) {
+ System.out.println("Not connected.");
+ }
+ else if (args.length < 2) {
+ System.out.println("Please specify a file name.");
+ }
+ else {
+ String filename = args[1];
+ try {
+ FileOutputStream fileOut = new FileOutputStream(filename);
+ try {
+ open();
+ try {
+ int bytesTransferred = receiveFile(filename, mode, fileOut, serverAddress);
+ System.out.println(bytesTransferred + " bytes transferred.");
+ }
+ finally {
+ close();
+ }
+ }
+ finally {
+ fileOut.close();
+ }
+ success = true;
+ }
+ catch (IOException ex) {
+ System.out.println("Error transferring file: " + ex.getMessage());
+ }
+ }
+ }
+ else if (cmd.equals(PUT_CMD)) { // put
+ if (serverAddress == null) {
+ System.out.println("Not connected.");
+ }
+ else if (args.length < 2) {
+ System.out.println("Please specify a file name.");
+ }
+ else {
+ String filename = args[1];
+ try {
+ FileInputStream fileIn = new FileInputStream(filename);
+ try {
+ open();
+ try {
+ sendFile(filename, mode, fileIn, serverAddress);
+ }
+ finally {
+ close();
+ }
+ }
+ finally {
+ fileIn.close();
+ }
+ success = true;
+ }
+ catch (IOException ex) {
+ System.out.println("Error transferring file: " + ex.getMessage());
+ }
+ }
+ }
+ else if (cmd.equals(ASCII_CMD)) { // ascii
+ mode = ASCII_MODE;
+ success = true;
+ }
+ else if (cmd.equals(BINARY_CMD)) { // binary
+ mode = BINARY_MODE;
+ success = true;
+ }
+ else if (cmd.equals(TIMEOUT_CMD)) { // timeout
+ if (args.length < 2) {
+ System.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.");
+ }
+ }
+ }
+ else if (cmd.equals(RETRIES_CMD)) { // retries
+ if (args.length < 2) {
+ System.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.");
+ }
+ }
+ }
+ else if (cmd.equals(STATUS_CMD)) { // status
+ if (serverAddress != null) {
+ System.out.println("Connected to "+serverAddress.getHostName() + ".");
+ }
+ else {
+ System.out.println("Not connected.");
+ }
+ if (mode == ASCII_MODE) {
+ System.out.print("mode: ASCII");
+ }
+ else if (mode == BINARY_MODE) {
+ System.out.print("mode: BINARY");
+ }
+ System.out.print(" timeout: " + getDefaultTimeout());
+ System.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");
+ success = true;
+ }
+ else if (cmd.equals(QUIT_CMD)) { // quit
+ quit = true;
+ success = true;
+ }
+ else {
+ System.out.println("Unrecognised command.");
+ }
+ return success;
+ }
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|