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,7 +28,9 @@ 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();
}

View File

@ -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);
}

View File

@ -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 {
@ -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) {

View File

@ -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) //
@ -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;

View File

@ -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 {
@ -46,6 +49,7 @@ class TFTPserverRRQ extends Thread {
event.onClientReadException(e, request);
}
}
//everything is fine, open new thread to transfer file
public void run() {
int bytesRead = TFTPpacket.maxTftpPakLen;
@ -64,21 +68,25 @@ class TFTPserverRRQ extends Thread {
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");}
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");

View File

@ -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 {
@ -85,7 +88,9 @@ 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 + ")");

View File

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

View File

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