Implement FileSystem DataSource
This commit is contained in:
parent
296290f5dd
commit
4caa2f55a6
@ -1,5 +1,8 @@
|
|||||||
package ru.redguy.tftpserver;
|
package ru.redguy.tftpserver;
|
||||||
|
|
||||||
|
import ru.redguy.tftpserver.datasource.FileSystem;
|
||||||
|
import ru.redguy.tftpserver.datasource.IDataSource;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.DatagramSocket;
|
import java.net.DatagramSocket;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
@ -12,6 +15,7 @@ public class TFTPServer {
|
|||||||
private Runner runner;
|
private Runner runner;
|
||||||
private Thread thread;
|
private Thread thread;
|
||||||
private ErrorEvent errorEvent;
|
private ErrorEvent errorEvent;
|
||||||
|
private IDataSource dataSource;
|
||||||
|
|
||||||
public void start() throws SocketException {
|
public void start() throws SocketException {
|
||||||
start(69,true);
|
start(69,true);
|
||||||
@ -22,6 +26,11 @@ public class TFTPServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void start(int port, boolean isDaemon) throws SocketException {
|
public void start(int port, boolean isDaemon) throws SocketException {
|
||||||
|
start(port, isDaemon, new FileSystem("."));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void start(int port, boolean isDaemon, IDataSource dataSource) throws SocketException {
|
||||||
|
this.dataSource = dataSource;
|
||||||
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);
|
||||||
@ -30,6 +39,11 @@ public class TFTPServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void start(String host, int port) throws UnknownHostException, SocketException {
|
public void start(String host, int port) throws UnknownHostException, SocketException {
|
||||||
|
start(host, port, new FileSystem("."));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void start(String host, int port, IDataSource dataSource) throws UnknownHostException, SocketException {
|
||||||
|
this.dataSource = dataSource;
|
||||||
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);
|
||||||
@ -75,13 +89,13 @@ public class TFTPServer {
|
|||||||
|
|
||||||
if (in instanceof TFTPread) {
|
if (in instanceof TFTPread) {
|
||||||
try {
|
try {
|
||||||
TFTPserverRRQ r = new TFTPserverRRQ((TFTPread) in, server.errorEvent);
|
TFTPserverRRQ r = new TFTPserverRRQ((TFTPread) in, server.errorEvent, server.dataSource);
|
||||||
} 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, server.dataSource);
|
||||||
} catch (TftpException e) {
|
} catch (TftpException e) {
|
||||||
server.errorEvent.onPacketWriteException(e);
|
server.errorEvent.onPacketWriteException(e);
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
package ru.redguy.tftpserver;
|
package ru.redguy.tftpserver;
|
||||||
|
|
||||||
import java.io.FileInputStream;
|
import java.io.*;
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.net.DatagramPacket;
|
import java.net.DatagramPacket;
|
||||||
import java.net.DatagramSocket;
|
import java.net.DatagramSocket;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
@ -145,7 +143,7 @@ final class TFTPdata extends TFTPpacket {
|
|||||||
protected TFTPdata() {
|
protected TFTPdata() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public TFTPdata(int blockNumber, FileInputStream in) throws IOException {
|
public TFTPdata(int blockNumber, InputStream in) throws IOException {
|
||||||
this.message = new byte[maxTftpPakLen];
|
this.message = new byte[maxTftpPakLen];
|
||||||
// manipulate message
|
// manipulate message
|
||||||
this.put(opOffset, tftpDATA);
|
this.put(opOffset, tftpDATA);
|
||||||
@ -167,7 +165,7 @@ final class TFTPdata extends TFTPpacket {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
// File output
|
// File output
|
||||||
public int write(FileOutputStream out) throws IOException {
|
public int write(OutputStream out) throws IOException {
|
||||||
out.write(message, dataOffset, length - 4);
|
out.write(message, dataOffset, length - 4);
|
||||||
|
|
||||||
return (length - 4);
|
return (length - 4);
|
||||||
@ -186,6 +184,7 @@ class TFTPerror extends TFTPpacket {
|
|||||||
|
|
||||||
//Generate error packet
|
//Generate error packet
|
||||||
public TFTPerror(int number, String message) {
|
public TFTPerror(int number, String message) {
|
||||||
|
if(message == null) message = "Error";
|
||||||
length = 4 + message.length() + 1;
|
length = 4 + message.length() + 1;
|
||||||
this.message = new byte[length];
|
this.message = new byte[length];
|
||||||
put(opOffset, tftpERROR);
|
put(opOffset, tftpERROR);
|
||||||
|
@ -1,7 +1,11 @@
|
|||||||
package ru.redguy.tftpserver;
|
package ru.redguy.tftpserver;
|
||||||
|
|
||||||
|
import ru.redguy.tftpserver.datasource.IDataSource;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.net.DatagramSocket;
|
import java.net.DatagramSocket;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.SocketTimeoutException;
|
import java.net.SocketTimeoutException;
|
||||||
@ -12,14 +16,16 @@ class TFTPserverRRQ extends Thread {
|
|||||||
protected DatagramSocket sock;
|
protected DatagramSocket sock;
|
||||||
protected InetAddress host;
|
protected InetAddress host;
|
||||||
protected int port;
|
protected int port;
|
||||||
protected FileInputStream source;
|
protected InputStream source;
|
||||||
protected TFTPpacket req;
|
protected TFTPpacket req;
|
||||||
protected int timeoutLimit = 5;
|
protected int timeoutLimit = 5;
|
||||||
protected String fileName;
|
protected String fileName;
|
||||||
|
protected IDataSource dataSource;
|
||||||
|
|
||||||
// initialize read request
|
// initialize read request
|
||||||
public TFTPserverRRQ(TFTPread request, ErrorEvent event) throws TftpException {
|
public TFTPserverRRQ(TFTPread request, ErrorEvent event, IDataSource dataSource) throws TftpException {
|
||||||
try {
|
try {
|
||||||
|
this.dataSource = dataSource;
|
||||||
req = request;
|
req = request;
|
||||||
//open new socket with random port num for tranfer
|
//open new socket with random port num for tranfer
|
||||||
sock = new DatagramSocket();
|
sock = new DatagramSocket();
|
||||||
@ -29,12 +35,9 @@ class TFTPserverRRQ extends Thread {
|
|||||||
host = request.getAddress();
|
host = request.getAddress();
|
||||||
port = request.getPort();
|
port = request.getPort();
|
||||||
|
|
||||||
//create file object in parent folder
|
|
||||||
File srcFile = new File(fileName);
|
|
||||||
/*System.out.println("procce checking");*/
|
|
||||||
//check file
|
//check file
|
||||||
if (srcFile.exists() && srcFile.isFile() && srcFile.canRead()) {
|
if (dataSource.isFileExists(fileName) && dataSource.isFile(fileName) && dataSource.isCanRead(fileName)) {
|
||||||
source = new FileInputStream(srcFile);
|
source = dataSource.getInputStream(fileName);
|
||||||
this.start(); //open new thread for transfer
|
this.start(); //open new thread for transfer
|
||||||
} else
|
} else
|
||||||
throw new TftpException("access violation");
|
throw new TftpException("access violation");
|
||||||
|
@ -1,7 +1,10 @@
|
|||||||
package ru.redguy.tftpserver;
|
package ru.redguy.tftpserver;
|
||||||
|
|
||||||
|
import ru.redguy.tftpserver.datasource.IDataSource;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
import java.net.DatagramSocket;
|
import java.net.DatagramSocket;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.SocketTimeoutException;
|
import java.net.SocketTimeoutException;
|
||||||
@ -12,16 +15,17 @@ class TFTPserverWRQ extends Thread {
|
|||||||
protected DatagramSocket sock;
|
protected DatagramSocket sock;
|
||||||
protected InetAddress host;
|
protected InetAddress host;
|
||||||
protected int port;
|
protected int port;
|
||||||
protected FileOutputStream outFile;
|
protected OutputStream outFile;
|
||||||
protected TFTPpacket req;
|
protected TFTPpacket req;
|
||||||
protected int timeoutLimit = 5;
|
protected int timeoutLimit = 5;
|
||||||
//protected int testloss=0;
|
//protected int testloss=0;
|
||||||
protected File saveFile;
|
|
||||||
protected String fileName;
|
protected String fileName;
|
||||||
|
protected IDataSource dataSource;
|
||||||
|
|
||||||
// Initialize read request
|
// Initialize read request
|
||||||
public TFTPserverWRQ(TFTPwrite request, ErrorEvent event) throws TftpException {
|
public TFTPserverWRQ(TFTPwrite request, ErrorEvent event, IDataSource dataSource) throws TftpException {
|
||||||
try {
|
try {
|
||||||
|
this.dataSource = dataSource;
|
||||||
req = request;
|
req = request;
|
||||||
sock = new DatagramSocket(); // new port for transfer
|
sock = new DatagramSocket(); // new port for transfer
|
||||||
sock.setSoTimeout(1000);
|
sock.setSoTimeout(1000);
|
||||||
@ -29,11 +33,9 @@ class TFTPserverWRQ extends Thread {
|
|||||||
host = request.getAddress();
|
host = request.getAddress();
|
||||||
port = request.getPort();
|
port = request.getPort();
|
||||||
fileName = request.fileName();
|
fileName = request.fileName();
|
||||||
//create file object in parent folder
|
|
||||||
saveFile = new File(fileName);
|
|
||||||
|
|
||||||
if (!saveFile.exists()) {
|
if (!dataSource.isFileExists(fileName)) {
|
||||||
outFile = new FileOutputStream(saveFile);
|
outFile = dataSource.getOutputStream(fileName);
|
||||||
TFTPack a = new TFTPack(0);
|
TFTPack a = new TFTPack(0);
|
||||||
a.send(host, port, sock); // send ack 0 at first, ready to
|
a.send(host, port, sock); // send ack 0 at first, ready to
|
||||||
// receive
|
// receive
|
||||||
@ -104,7 +106,7 @@ class TFTPserverWRQ extends Thread {
|
|||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("Client failed: " + e.getMessage());
|
System.out.println("Client failed: " + e.getMessage());
|
||||||
saveFile.delete();
|
dataSource.delete(fileName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package ru.redguy.tftpserver.datasource;
|
package ru.redguy.tftpserver.datasource;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.*;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
|
||||||
public class FileSystem implements IDataSource {
|
public class FileSystem implements IDataSource {
|
||||||
@ -26,4 +26,49 @@ public class FileSystem implements IDataSource {
|
|||||||
public boolean isFileExists(String localPath) {
|
public boolean isFileExists(String localPath) {
|
||||||
return new File(this.path,localPath).exists();
|
return new File(this.path,localPath).exists();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isFile(String file) {
|
||||||
|
return new File(this.path,file).isFile();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isCanRead(String file) {
|
||||||
|
return new File(this.path,file).canRead();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public OutputStream getOutputStream(String file) {
|
||||||
|
try {
|
||||||
|
return new FileOutputStream(new File(this.path,file));
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
try {
|
||||||
|
new File(this.path,file).createNewFile();
|
||||||
|
return new FileOutputStream(new File(this.path,file));
|
||||||
|
} catch (IOException ioException) {
|
||||||
|
new File(this.path,file).getParentFile().mkdirs();
|
||||||
|
try {
|
||||||
|
new File(this.path,file).createNewFile();
|
||||||
|
return new FileOutputStream(new File(this.path,file));
|
||||||
|
} catch (IOException exception) {
|
||||||
|
exception.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InputStream getInputStream(String file) {
|
||||||
|
try {
|
||||||
|
return new FileInputStream(new File(this.path,file));
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void delete(String file) {
|
||||||
|
new File(this.path,file).delete();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,16 @@
|
|||||||
package ru.redguy.tftpserver.datasource;
|
package ru.redguy.tftpserver.datasource;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
|
||||||
public interface IDataSource {
|
public interface IDataSource {
|
||||||
public boolean isFileExists(String localPath);
|
public boolean isFileExists(String file);
|
||||||
|
public boolean isFile(String file);
|
||||||
|
public boolean isCanRead(String file);
|
||||||
|
|
||||||
|
public OutputStream getOutputStream(String file);
|
||||||
|
|
||||||
|
public InputStream getInputStream(String file);
|
||||||
|
|
||||||
|
public void delete(String file);
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ import ru.redguy.tftpserver.ErrorEvent;
|
|||||||
import ru.redguy.tftpserver.TFTPServer;
|
import ru.redguy.tftpserver.TFTPServer;
|
||||||
import ru.redguy.tftpserver.TFTPread;
|
import ru.redguy.tftpserver.TFTPread;
|
||||||
import ru.redguy.tftpserver.TFTPwrite;
|
import ru.redguy.tftpserver.TFTPwrite;
|
||||||
|
import ru.redguy.tftpserver.datasource.FileSystem;
|
||||||
|
|
||||||
import java.net.SocketException;
|
import java.net.SocketException;
|
||||||
|
|
||||||
@ -9,7 +10,7 @@ public class Main {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
TFTPServer tftpServer = new TFTPServer();
|
TFTPServer tftpServer = new TFTPServer();
|
||||||
try {
|
try {
|
||||||
tftpServer.start(1000);
|
tftpServer.start(1000,false,new FileSystem("gradle"));
|
||||||
} catch (SocketException e) {
|
} catch (SocketException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user