Reformat code
This commit is contained in:
parent
630e76198e
commit
bdffb7a457
@ -28,8 +28,10 @@ public class CheckSum {
|
||||
sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
|
||||
}
|
||||
|
||||
} catch (Exception e){System.out.println("Generate Checksum Failed: "+e.getMessage());}
|
||||
} catch (Exception e) {
|
||||
System.out.println("Generate Checksum Failed: " + e.getMessage());
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,8 +2,12 @@ package ru.redguy.tftpserver;
|
||||
|
||||
public interface ErrorEvent {
|
||||
public void onPacketReceiveException(Exception exception);
|
||||
|
||||
public void onPacketReadException(Exception exception);
|
||||
|
||||
public void onPacketWriteException(Exception exception);
|
||||
|
||||
public void onClientReadException(Exception exception, TFTPread tftPread);
|
||||
|
||||
public void onClientWriteException(Exception exception, TFTPwrite tftPwrite);
|
||||
}
|
||||
|
@ -1,7 +1,10 @@
|
||||
package ru.redguy.tftpserver;
|
||||
|
||||
import java.net.*;
|
||||
import java.io.*;
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.SocketException;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
public class TFTPServer {
|
||||
|
||||
@ -16,15 +19,15 @@ public class TFTPServer {
|
||||
|
||||
public void start(int port) throws SocketException {
|
||||
socket = new DatagramSocket(port);
|
||||
runner = new Runner(socket,this);
|
||||
runner = new Runner(socket, this);
|
||||
thread = new Thread(runner);
|
||||
thread.setDaemon(true);
|
||||
thread.start();
|
||||
}
|
||||
|
||||
public void start(String host, int port) throws UnknownHostException, SocketException {
|
||||
socket = new DatagramSocket(port,InetAddress.getByName(host));
|
||||
runner = new Runner(socket,this);
|
||||
socket = new DatagramSocket(port, InetAddress.getByName(host));
|
||||
runner = new Runner(socket, this);
|
||||
thread.setDaemon(true);
|
||||
thread.start();
|
||||
}
|
||||
@ -47,7 +50,7 @@ public class TFTPServer {
|
||||
TFTPServer server;
|
||||
boolean run = true;
|
||||
|
||||
public Runner(DatagramSocket socket,TFTPServer server) {
|
||||
public Runner(DatagramSocket socket, TFTPServer server) {
|
||||
this.datagramSocket = socket;
|
||||
this.server = server;
|
||||
}
|
||||
@ -72,9 +75,7 @@ public class TFTPServer {
|
||||
} catch (TftpException e) {
|
||||
server.errorEvent.onPacketReadException(e);
|
||||
}
|
||||
}
|
||||
|
||||
else if (in instanceof TFTPwrite) {
|
||||
} else if (in instanceof TFTPwrite) {
|
||||
try {
|
||||
TFTPserverWRQ w = new TFTPserverWRQ((TFTPwrite) in, server.errorEvent);
|
||||
} catch (TftpException e) {
|
||||
|
@ -1,16 +1,22 @@
|
||||
package ru.redguy.tftpserver;
|
||||
|
||||
import java.net.*;
|
||||
import java.io.*;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
|
||||
class TftpException extends Exception {
|
||||
public TftpException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public TftpException(String s) {
|
||||
super(s);
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//GENERAL packet: define the packet structure, necessary members and methods//
|
||||
//of TFTP packet. To be extended by other specific packet(read, write, etc) //
|
||||
@ -19,29 +25,29 @@ public class TFTPpacket {
|
||||
|
||||
// TFTP constants
|
||||
public static int tftpPort = 69;
|
||||
public static int maxTftpPakLen=516;
|
||||
public static int maxTftpData=512;
|
||||
public static int maxTftpPakLen = 516;
|
||||
public static int maxTftpData = 512;
|
||||
|
||||
// Tftp opcodes
|
||||
protected static final short tftpRRQ=1;
|
||||
protected static final short tftpWRQ=2;
|
||||
protected static final short tftpDATA=3;
|
||||
protected static final short tftpACK=4;
|
||||
protected static final short tftpERROR=5;
|
||||
protected static final short tftpRRQ = 1;
|
||||
protected static final short tftpWRQ = 2;
|
||||
protected static final short tftpDATA = 3;
|
||||
protected static final short tftpACK = 4;
|
||||
protected static final short tftpERROR = 5;
|
||||
|
||||
// Packet Offsets
|
||||
protected static final int opOffset=0;
|
||||
protected static final int opOffset = 0;
|
||||
|
||||
protected static final int fileOffset=2;
|
||||
protected static final int fileOffset = 2;
|
||||
|
||||
protected static final int blkOffset=2;
|
||||
protected static final int dataOffset=4;
|
||||
protected static final int blkOffset = 2;
|
||||
protected static final int dataOffset = 4;
|
||||
|
||||
protected static final int numOffset=2;
|
||||
protected static final int msgOffset=4;
|
||||
protected static final int numOffset = 2;
|
||||
protected static final int msgOffset = 4;
|
||||
|
||||
// The actual packet for UDP transfer
|
||||
protected byte [] message;
|
||||
protected byte[] message;
|
||||
protected int length;
|
||||
|
||||
// Address info (required for replies)
|
||||
@ -50,46 +56,46 @@ public class TFTPpacket {
|
||||
|
||||
// Constructor
|
||||
public TFTPpacket() {
|
||||
message=new byte[maxTftpPakLen];
|
||||
length=maxTftpPakLen;
|
||||
message = new byte[maxTftpPakLen];
|
||||
length = maxTftpPakLen;
|
||||
}
|
||||
|
||||
// Methods to receive packet and convert it to yhe right type(data/ack/read/...)
|
||||
public static TFTPpacket receive(DatagramSocket sock) throws IOException {
|
||||
TFTPpacket in=new TFTPpacket(), retPak=new TFTPpacket();
|
||||
TFTPpacket in = new TFTPpacket(), retPak = new TFTPpacket();
|
||||
//receive data and put them into in.message
|
||||
DatagramPacket inPak = new DatagramPacket(in.message,in.length);
|
||||
DatagramPacket inPak = new DatagramPacket(in.message, in.length);
|
||||
sock.receive(inPak);
|
||||
|
||||
//Check the opcode in message, then cast the message into the corresponding type
|
||||
switch (in.get(0)) {
|
||||
case tftpRRQ:
|
||||
retPak=new TFTPread();
|
||||
retPak = new TFTPread();
|
||||
break;
|
||||
case tftpWRQ:
|
||||
retPak=new TFTPwrite();
|
||||
retPak = new TFTPwrite();
|
||||
break;
|
||||
case tftpDATA:
|
||||
retPak=new TFTPdata();
|
||||
retPak = new TFTPdata();
|
||||
break;
|
||||
case tftpACK:
|
||||
retPak=new TFTPack();
|
||||
retPak = new TFTPack();
|
||||
break;
|
||||
case tftpERROR:
|
||||
retPak=new TFTPerror();
|
||||
retPak = new TFTPerror();
|
||||
break;
|
||||
}
|
||||
retPak.message=in.message;
|
||||
retPak.length=inPak.getLength();
|
||||
retPak.host=inPak.getAddress();
|
||||
retPak.port=inPak.getPort();
|
||||
retPak.message = in.message;
|
||||
retPak.length = inPak.getLength();
|
||||
retPak.host = inPak.getAddress();
|
||||
retPak.port = inPak.getPort();
|
||||
|
||||
return retPak;
|
||||
}
|
||||
|
||||
//Method to send packet
|
||||
public void send(InetAddress ip, int port, DatagramSocket s) throws IOException {
|
||||
s.send(new DatagramPacket(message,length,ip,port));
|
||||
s.send(new DatagramPacket(message, length, ip, port));
|
||||
}
|
||||
|
||||
// DatagramPacket like methods
|
||||
@ -107,8 +113,8 @@ public class TFTPpacket {
|
||||
|
||||
// Methods to put opcode, blkNum, error code into the byte array 'message'.
|
||||
protected void put(int at, short value) {
|
||||
message[at++] = (byte)(value >>> 8); // first byte
|
||||
message[at] = (byte)(value % 256); // last byte
|
||||
message[at++] = (byte) (value >>> 8); // first byte
|
||||
message[at] = (byte) (value % 256); // last byte
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@ -119,12 +125,12 @@ public class TFTPpacket {
|
||||
}
|
||||
|
||||
protected int get(int at) {
|
||||
return (message[at] & 0xff) << 8 | message[at+1] & 0xff;
|
||||
return (message[at] & 0xff) << 8 | message[at + 1] & 0xff;
|
||||
}
|
||||
|
||||
protected String get (int at, byte del) {
|
||||
protected String get(int at, byte del) {
|
||||
StringBuffer result = new StringBuffer();
|
||||
while (message[at] != del) result.append((char)message[at++]);
|
||||
while (message[at] != del) result.append((char) message[at++]);
|
||||
return result.toString();
|
||||
}
|
||||
}
|
||||
@ -136,7 +142,9 @@ public class TFTPpacket {
|
||||
final class TFTPdata extends TFTPpacket {
|
||||
|
||||
// Constructors
|
||||
protected TFTPdata() {}
|
||||
protected TFTPdata() {
|
||||
}
|
||||
|
||||
public TFTPdata(int blockNumber, FileInputStream in) throws IOException {
|
||||
this.message = new byte[maxTftpPakLen];
|
||||
// manipulate message
|
||||
@ -175,6 +183,7 @@ class TFTPerror extends TFTPpacket {
|
||||
// Constructors
|
||||
protected TFTPerror() {
|
||||
}
|
||||
|
||||
//Generate error packet
|
||||
public TFTPerror(int number, String message) {
|
||||
length = 4 + message.length() + 1;
|
||||
@ -188,6 +197,7 @@ class TFTPerror extends TFTPpacket {
|
||||
public int number() {
|
||||
return this.get(numOffset);
|
||||
}
|
||||
|
||||
public String message() {
|
||||
return this.get(msgOffset, (byte) 0);
|
||||
}
|
||||
@ -202,6 +212,7 @@ final class TFTPack extends TFTPpacket {
|
||||
// Constructors
|
||||
protected TFTPack() {
|
||||
}
|
||||
|
||||
//Generate ack packet
|
||||
public TFTPack(int blockNumber) {
|
||||
length = 4;
|
||||
|
@ -1,7 +1,10 @@
|
||||
package ru.redguy.tftpserver;
|
||||
|
||||
import java.net.*;
|
||||
import java.io.*;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.SocketTimeoutException;
|
||||
|
||||
|
||||
class TFTPserverRRQ extends Thread {
|
||||
@ -11,7 +14,7 @@ class TFTPserverRRQ extends Thread {
|
||||
protected int port;
|
||||
protected FileInputStream source;
|
||||
protected TFTPpacket req;
|
||||
protected int timeoutLimit=5;
|
||||
protected int timeoutLimit = 5;
|
||||
protected String fileName;
|
||||
|
||||
// initialize read request
|
||||
@ -43,9 +46,10 @@ class TFTPserverRRQ extends Thread {
|
||||
} catch (Exception f) {
|
||||
}
|
||||
|
||||
event.onClientReadException(e,request);
|
||||
event.onClientReadException(e, request);
|
||||
}
|
||||
}
|
||||
|
||||
//everything is fine, open new thread to transfer file
|
||||
public void run() {
|
||||
int bytesRead = TFTPpacket.maxTftpPakLen;
|
||||
@ -61,27 +65,31 @@ class TFTPserverRRQ extends Thread {
|
||||
/*System.out.println("current op code " + outPak.get(0)); */
|
||||
|
||||
//wait for the correct ack. if incorrect, retry up to 5 times
|
||||
while (timeoutLimit!=0) {
|
||||
while (timeoutLimit != 0) {
|
||||
try {
|
||||
TFTPpacket ack = TFTPpacket.receive(sock);
|
||||
if (!(ack instanceof TFTPack)){throw new Exception("Client failed");}
|
||||
if (!(ack instanceof TFTPack)) {
|
||||
throw new Exception("Client failed");
|
||||
}
|
||||
TFTPack a = (TFTPack) ack;
|
||||
|
||||
if(a.blockNumber()!=blkNum){ //check ack
|
||||
throw new SocketTimeoutException("last packet lost, resend packet");}
|
||||
if (a.blockNumber() != blkNum) { //check ack
|
||||
throw new SocketTimeoutException("last packet lost, resend packet");
|
||||
}
|
||||
/*System.out.println("confirm blk num " + a.blockNumber()+" from "+a.getPort());*/
|
||||
break;
|
||||
}
|
||||
catch (SocketTimeoutException t) {//resend last packet
|
||||
} catch (SocketTimeoutException t) {//resend last packet
|
||||
System.out.println("Resent blk " + blkNum);
|
||||
timeoutLimit--;
|
||||
outPak.send(host, port, sock);
|
||||
}
|
||||
} // end of while
|
||||
if(timeoutLimit==0){throw new Exception("connection failed");}
|
||||
if (timeoutLimit == 0) {
|
||||
throw new Exception("connection failed");
|
||||
}
|
||||
System.out.println("Transfer completed.(Client " +host +")" );
|
||||
System.out.println("Filename: "+fileName + "\nSHA1 checksum: "+CheckSum.getChecksum(fileName)+"\n");
|
||||
}
|
||||
System.out.println("Transfer completed.(Client " + host + ")");
|
||||
System.out.println("Filename: " + fileName + "\nSHA1 checksum: " + CheckSum.getChecksum(fileName) + "\n");
|
||||
} catch (Exception e) {
|
||||
TFTPerror ePak = new TFTPerror(1, e.getMessage());
|
||||
|
||||
|
@ -1,7 +1,10 @@
|
||||
package ru.redguy.tftpserver;
|
||||
|
||||
import java.net.*;
|
||||
import java.io.*;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.SocketTimeoutException;
|
||||
|
||||
|
||||
class TFTPserverWRQ extends Thread {
|
||||
@ -45,7 +48,7 @@ class TFTPserverWRQ extends Thread {
|
||||
} catch (Exception f) {
|
||||
}
|
||||
|
||||
event.onClientWriteException(e,request);
|
||||
event.onClientWriteException(e, request);
|
||||
}
|
||||
}
|
||||
|
||||
@ -85,11 +88,13 @@ class TFTPserverWRQ extends Thread {
|
||||
timeoutLimit--;
|
||||
}
|
||||
}
|
||||
if(timeoutLimit==0){throw new Exception("Connection failed");}
|
||||
if (timeoutLimit == 0) {
|
||||
throw new Exception("Connection failed");
|
||||
}
|
||||
}
|
||||
outFile.close();
|
||||
System.out.println("Transfer completed.(Client " +host +")" );
|
||||
System.out.println("Filename: "+fileName + "\nSHA1 checksum: "+CheckSum.getChecksum(fileName)+"\n");
|
||||
System.out.println("Transfer completed.(Client " + host + ")");
|
||||
System.out.println("Filename: " + fileName + "\nSHA1 checksum: " + CheckSum.getChecksum(fileName) + "\n");
|
||||
|
||||
} catch (Exception e) {
|
||||
TFTPerror ePak = new TFTPerror(1, e.getMessage());
|
||||
|
@ -1,7 +1,5 @@
|
||||
package ru.redguy.tftpserver.datasource;
|
||||
|
||||
import ru.redguy.tftpserver.IDataSource;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
package ru.redguy.tftpserver;
|
||||
package ru.redguy.tftpserver.datasource;
|
||||
|
||||
public interface IDataSource {
|
||||
public boolean isFileExists(String localPath);
|
Reference in New Issue
Block a user