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