Reformat code

This commit is contained in:
Ilya 2020-11-20 19:01:11 +03:00
parent 630e76198e
commit bdffb7a457
8 changed files with 458 additions and 429 deletions

View File

@ -28,8 +28,10 @@ public class CheckSum {
sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1)); 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(); return sb.toString();
} }
} }

View File

@ -2,8 +2,12 @@ package ru.redguy.tftpserver;
public interface ErrorEvent { public interface ErrorEvent {
public void onPacketReceiveException(Exception exception); public void onPacketReceiveException(Exception exception);
public void onPacketReadException(Exception exception); public void onPacketReadException(Exception exception);
public void onPacketWriteException(Exception exception); public void onPacketWriteException(Exception exception);
public void onClientReadException(Exception exception, TFTPread tftPread); public void onClientReadException(Exception exception, TFTPread tftPread);
public void onClientWriteException(Exception exception, TFTPwrite tftPwrite); public void onClientWriteException(Exception exception, TFTPwrite tftPwrite);
} }

View File

@ -1,7 +1,10 @@
package ru.redguy.tftpserver; package ru.redguy.tftpserver;
import java.net.*; import java.io.IOException;
import java.io.*; import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
public class TFTPServer { public class TFTPServer {
@ -16,15 +19,15 @@ public class TFTPServer {
public void start(int port) throws SocketException { public void start(int port) throws SocketException {
socket = new DatagramSocket(port); socket = new DatagramSocket(port);
runner = new Runner(socket,this); runner = new Runner(socket, this);
thread = new Thread(runner); thread = new Thread(runner);
thread.setDaemon(true); thread.setDaemon(true);
thread.start(); thread.start();
} }
public void start(String host, int port) throws UnknownHostException, SocketException { public void start(String host, int port) throws UnknownHostException, SocketException {
socket = new DatagramSocket(port,InetAddress.getByName(host)); socket = new DatagramSocket(port, InetAddress.getByName(host));
runner = new Runner(socket,this); runner = new Runner(socket, this);
thread.setDaemon(true); thread.setDaemon(true);
thread.start(); thread.start();
} }
@ -47,7 +50,7 @@ public class TFTPServer {
TFTPServer server; TFTPServer server;
boolean run = true; boolean run = true;
public Runner(DatagramSocket socket,TFTPServer server) { public Runner(DatagramSocket socket, TFTPServer server) {
this.datagramSocket = socket; this.datagramSocket = socket;
this.server = server; this.server = server;
} }
@ -72,9 +75,7 @@ public class TFTPServer {
} catch (TftpException e) { } catch (TftpException e) {
server.errorEvent.onPacketReadException(e); server.errorEvent.onPacketReadException(e);
} }
} } else if (in instanceof TFTPwrite) {
else if (in instanceof TFTPwrite) {
try { try {
TFTPserverWRQ w = new TFTPserverWRQ((TFTPwrite) in, server.errorEvent); TFTPserverWRQ w = new TFTPserverWRQ((TFTPwrite) in, server.errorEvent);
} catch (TftpException e) { } catch (TftpException e) {

View File

@ -1,16 +1,22 @@
package ru.redguy.tftpserver; package ru.redguy.tftpserver;
import java.net.*; import java.io.FileInputStream;
import java.io.*; import java.io.FileOutputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
class TftpException extends Exception { class TftpException extends Exception {
public TftpException() { public TftpException() {
super(); super();
} }
public TftpException(String s) { public TftpException(String s) {
super(s); super(s);
} }
} }
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
//GENERAL packet: define the packet structure, necessary members and methods// //GENERAL packet: define the packet structure, necessary members and methods//
//of TFTP packet. To be extended by other specific packet(read, write, etc) // //of TFTP packet. To be extended by other specific packet(read, write, etc) //
@ -19,29 +25,29 @@ public class TFTPpacket {
// TFTP constants // TFTP constants
public static int tftpPort = 69; public static int tftpPort = 69;
public static int maxTftpPakLen=516; public static int maxTftpPakLen = 516;
public static int maxTftpData=512; public static int maxTftpData = 512;
// Tftp opcodes // Tftp opcodes
protected static final short tftpRRQ=1; protected static final short tftpRRQ = 1;
protected static final short tftpWRQ=2; protected static final short tftpWRQ = 2;
protected static final short tftpDATA=3; protected static final short tftpDATA = 3;
protected static final short tftpACK=4; protected static final short tftpACK = 4;
protected static final short tftpERROR=5; protected static final short tftpERROR = 5;
// Packet Offsets // 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 blkOffset = 2;
protected static final int dataOffset=4; protected static final int dataOffset = 4;
protected static final int numOffset=2; protected static final int numOffset = 2;
protected static final int msgOffset=4; protected static final int msgOffset = 4;
// The actual packet for UDP transfer // The actual packet for UDP transfer
protected byte [] message; protected byte[] message;
protected int length; protected int length;
// Address info (required for replies) // Address info (required for replies)
@ -50,46 +56,46 @@ public class TFTPpacket {
// Constructor // Constructor
public TFTPpacket() { public TFTPpacket() {
message=new byte[maxTftpPakLen]; message = new byte[maxTftpPakLen];
length=maxTftpPakLen; length = maxTftpPakLen;
} }
// Methods to receive packet and convert it to yhe right type(data/ack/read/...) // Methods to receive packet and convert it to yhe right type(data/ack/read/...)
public static TFTPpacket receive(DatagramSocket sock) throws IOException { 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 //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); sock.receive(inPak);
//Check the opcode in message, then cast the message into the corresponding type //Check the opcode in message, then cast the message into the corresponding type
switch (in.get(0)) { switch (in.get(0)) {
case tftpRRQ: case tftpRRQ:
retPak=new TFTPread(); retPak = new TFTPread();
break; break;
case tftpWRQ: case tftpWRQ:
retPak=new TFTPwrite(); retPak = new TFTPwrite();
break; break;
case tftpDATA: case tftpDATA:
retPak=new TFTPdata(); retPak = new TFTPdata();
break; break;
case tftpACK: case tftpACK:
retPak=new TFTPack(); retPak = new TFTPack();
break; break;
case tftpERROR: case tftpERROR:
retPak=new TFTPerror(); retPak = new TFTPerror();
break; break;
} }
retPak.message=in.message; retPak.message = in.message;
retPak.length=inPak.getLength(); retPak.length = inPak.getLength();
retPak.host=inPak.getAddress(); retPak.host = inPak.getAddress();
retPak.port=inPak.getPort(); retPak.port = inPak.getPort();
return retPak; return retPak;
} }
//Method to send packet //Method to send packet
public void send(InetAddress ip, int port, DatagramSocket s) throws IOException { 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 // DatagramPacket like methods
@ -107,8 +113,8 @@ public class TFTPpacket {
// Methods to put opcode, blkNum, error code into the byte array 'message'. // Methods to put opcode, blkNum, error code into the byte array 'message'.
protected void put(int at, short value) { protected void put(int at, short value) {
message[at++] = (byte)(value >>> 8); // first byte message[at++] = (byte) (value >>> 8); // first byte
message[at] = (byte)(value % 256); // last byte message[at] = (byte) (value % 256); // last byte
} }
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@ -119,12 +125,12 @@ public class TFTPpacket {
} }
protected int get(int at) { 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(); 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(); return result.toString();
} }
} }
@ -136,7 +142,9 @@ public class TFTPpacket {
final class TFTPdata extends TFTPpacket { final class TFTPdata extends TFTPpacket {
// Constructors // Constructors
protected TFTPdata() {} protected TFTPdata() {
}
public TFTPdata(int blockNumber, FileInputStream in) throws IOException { public TFTPdata(int blockNumber, FileInputStream in) throws IOException {
this.message = new byte[maxTftpPakLen]; this.message = new byte[maxTftpPakLen];
// manipulate message // manipulate message
@ -175,6 +183,7 @@ class TFTPerror extends TFTPpacket {
// Constructors // Constructors
protected TFTPerror() { protected TFTPerror() {
} }
//Generate error packet //Generate error packet
public TFTPerror(int number, String message) { public TFTPerror(int number, String message) {
length = 4 + message.length() + 1; length = 4 + message.length() + 1;
@ -188,6 +197,7 @@ class TFTPerror extends TFTPpacket {
public int number() { public int number() {
return this.get(numOffset); return this.get(numOffset);
} }
public String message() { public String message() {
return this.get(msgOffset, (byte) 0); return this.get(msgOffset, (byte) 0);
} }
@ -202,6 +212,7 @@ final class TFTPack extends TFTPpacket {
// Constructors // Constructors
protected TFTPack() { protected TFTPack() {
} }
//Generate ack packet //Generate ack packet
public TFTPack(int blockNumber) { public TFTPack(int blockNumber) {
length = 4; length = 4;

View File

@ -1,7 +1,10 @@
package ru.redguy.tftpserver; package ru.redguy.tftpserver;
import java.net.*; import java.io.File;
import java.io.*; import java.io.FileInputStream;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketTimeoutException;
class TFTPserverRRQ extends Thread { class TFTPserverRRQ extends Thread {
@ -11,7 +14,7 @@ class TFTPserverRRQ extends Thread {
protected int port; protected int port;
protected FileInputStream source; protected FileInputStream source;
protected TFTPpacket req; protected TFTPpacket req;
protected int timeoutLimit=5; protected int timeoutLimit = 5;
protected String fileName; protected String fileName;
// initialize read request // initialize read request
@ -43,9 +46,10 @@ class TFTPserverRRQ extends Thread {
} catch (Exception f) { } catch (Exception f) {
} }
event.onClientReadException(e,request); event.onClientReadException(e, request);
} }
} }
//everything is fine, open new thread to transfer file //everything is fine, open new thread to transfer file
public void run() { public void run() {
int bytesRead = TFTPpacket.maxTftpPakLen; int bytesRead = TFTPpacket.maxTftpPakLen;
@ -61,27 +65,31 @@ class TFTPserverRRQ extends Thread {
/*System.out.println("current op code " + outPak.get(0)); */ /*System.out.println("current op code " + outPak.get(0)); */
//wait for the correct ack. if incorrect, retry up to 5 times //wait for the correct ack. if incorrect, retry up to 5 times
while (timeoutLimit!=0) { while (timeoutLimit != 0) {
try { try {
TFTPpacket ack = TFTPpacket.receive(sock); 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; TFTPack a = (TFTPack) ack;
if(a.blockNumber()!=blkNum){ //check ack if (a.blockNumber() != blkNum) { //check ack
throw new SocketTimeoutException("last packet lost, resend packet");} throw new SocketTimeoutException("last packet lost, resend packet");
}
/*System.out.println("confirm blk num " + a.blockNumber()+" from "+a.getPort());*/ /*System.out.println("confirm blk num " + a.blockNumber()+" from "+a.getPort());*/
break; break;
} } catch (SocketTimeoutException t) {//resend last packet
catch (SocketTimeoutException t) {//resend last packet
System.out.println("Resent blk " + blkNum); System.out.println("Resent blk " + blkNum);
timeoutLimit--; timeoutLimit--;
outPak.send(host, port, sock); outPak.send(host, port, sock);
} }
} // end of while } // 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) { } catch (Exception e) {
TFTPerror ePak = new TFTPerror(1, e.getMessage()); TFTPerror ePak = new TFTPerror(1, e.getMessage());

View File

@ -1,7 +1,10 @@
package ru.redguy.tftpserver; package ru.redguy.tftpserver;
import java.net.*; import java.io.File;
import java.io.*; import java.io.FileOutputStream;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketTimeoutException;
class TFTPserverWRQ extends Thread { class TFTPserverWRQ extends Thread {
@ -45,7 +48,7 @@ class TFTPserverWRQ extends Thread {
} catch (Exception f) { } catch (Exception f) {
} }
event.onClientWriteException(e,request); event.onClientWriteException(e, request);
} }
} }
@ -85,11 +88,13 @@ class TFTPserverWRQ extends Thread {
timeoutLimit--; timeoutLimit--;
} }
} }
if(timeoutLimit==0){throw new Exception("Connection failed");} if (timeoutLimit == 0) {
throw new Exception("Connection failed");
}
} }
outFile.close(); outFile.close();
System.out.println("Transfer completed.(Client " +host +")" ); System.out.println("Transfer completed.(Client " + host + ")");
System.out.println("Filename: "+fileName + "\nSHA1 checksum: "+CheckSum.getChecksum(fileName)+"\n"); System.out.println("Filename: " + fileName + "\nSHA1 checksum: " + CheckSum.getChecksum(fileName) + "\n");
} catch (Exception e) { } catch (Exception e) {
TFTPerror ePak = new TFTPerror(1, e.getMessage()); TFTPerror ePak = new TFTPerror(1, e.getMessage());

View File

@ -1,7 +1,5 @@
package ru.redguy.tftpserver.datasource; package ru.redguy.tftpserver.datasource;
import ru.redguy.tftpserver.IDataSource;
import java.io.File; import java.io.File;
import java.nio.file.Path; import java.nio.file.Path;

View File

@ -1,4 +1,4 @@
package ru.redguy.tftpserver; package ru.redguy.tftpserver.datasource;
public interface IDataSource { public interface IDataSource {
public boolean isFileExists(String localPath); public boolean isFileExists(String localPath);