Reformat code
This commit is contained in:
parent
630e76198e
commit
bdffb7a457
@ -28,7 +28,9 @@ 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();
|
||||||
}
|
}
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
|
@ -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 {
|
||||||
|
|
||||||
@ -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) {
|
||||||
|
@ -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) //
|
||||||
@ -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;
|
||||||
|
@ -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 {
|
||||||
@ -46,6 +49,7 @@ class TFTPserverRRQ extends Thread {
|
|||||||
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;
|
||||||
@ -64,21 +68,25 @@ class TFTPserverRRQ extends Thread {
|
|||||||
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("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");
|
||||||
|
@ -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 {
|
||||||
@ -85,7 +88,9 @@ 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 + ")");
|
||||||
|
@ -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;
|
||||||
|
|
||||||
|
@ -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);
|
Reference in New Issue
Block a user