-
Notifications
You must be signed in to change notification settings - Fork 0
Hw3.ftp #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ilkivoo
wants to merge
10
commits into
master
Choose a base branch
from
hw3.FTP
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Hw3.ftp #5
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
97fa99d
first commit
03d8a5c
add buildscript for travis
e8a5d29
fix compare List
4e3a50e
check travis
d21c50e
check travis
ac88ce0
add directory
1a8977b
delete files
87db2c6
add TemporaryFolder and add enum for request
35f3165
add JavaDoc for Main
44c8cf5
remove try/catch from test
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| language: java | ||
| jdk: | ||
| - oraclejdk8 | ||
| os: | ||
| - linux | ||
| script: | ||
| - chmod +x buildscript.sh && ./buildscript.sh |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <groupId>03</groupId> | ||
| <artifactId>FTP</artifactId> | ||
| <version>1.0-SNAPSHOT</version> | ||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-compiler-plugin</artifactId> | ||
| <configuration> | ||
| <source>1.8</source> | ||
| <target>1.8</target> | ||
| </configuration> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| <dependencies> | ||
| <dependency> | ||
| <groupId>junit</groupId> | ||
| <artifactId>junit</artifactId> | ||
| <version>4.12</version> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
|
|
||
| </project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| package ru.spbau.mit.alyokhina; | ||
|
|
||
| import javafx.util.Pair; | ||
|
|
||
| import java.io.*; | ||
| import java.net.Socket; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| /** Client, which allows you to execute the requests list and get */ | ||
| public class Client { | ||
| /** OutputStream from Socket */ | ||
| private DataOutputStream dataOutputStream; | ||
| /**InputStream from Socket*/ | ||
| private DataInputStream dataInputStream; | ||
|
|
||
| /** | ||
| * Constructor | ||
| * | ||
| * @throws IOException if Socket or Stream can't be created | ||
| */ | ||
| public Client(String host, int port) throws IOException { | ||
| Socket clientSocket = new Socket(host, port); | ||
| dataInputStream = new DataInputStream(clientSocket.getInputStream()); | ||
| dataOutputStream = new DataOutputStream(clientSocket.getOutputStream()); | ||
|
|
||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Listing files in the directory on the server | ||
| * | ||
| * @param path directory path | ||
| * @return list of Pair. Fist value - name of file. Second value - type of file ( if file is directory - true, else false) | ||
| * Count files in directory = length of list | ||
| * @throws IOException if we can't read/write in InputStream/OutputStream | ||
| */ | ||
| public List<Pair<String, Boolean>> list(String path) throws IOException { | ||
| List<Pair<String, Boolean>> listFiles = new ArrayList<>(); | ||
| dataOutputStream.writeInt(Request.LIST_REQUEST.ordinal()); | ||
| dataOutputStream.writeUTF(path); | ||
| dataOutputStream.flush(); | ||
| int count = dataInputStream.readInt(); | ||
| for (int i = 0; i < count; i++) { | ||
| String fileName = dataInputStream.readUTF(); | ||
| Boolean isDirectory = dataInputStream.readBoolean(); | ||
| listFiles.add(new Pair<>(fileName, isDirectory)); | ||
| } | ||
| return listFiles; | ||
| } | ||
|
|
||
| /** | ||
| * Сopy the file from the server to the file | ||
| * | ||
| * @param path path of the file from server | ||
| * @param nameFileForSave the name of the file into which the content will be copied | ||
| * @return file into which the content will be copied | ||
| * @throws IOException if we can't read/write in InputStream/OutputStream | ||
| */ | ||
| public File get(String path, String nameFileForSave) throws IOException { | ||
| dataOutputStream.writeInt(Request.GET_REQUEST.ordinal()); | ||
| dataOutputStream.writeUTF(path); | ||
| dataOutputStream.flush(); | ||
| File fileForSave = new File(nameFileForSave); | ||
| int count = dataInputStream.readInt(); | ||
| if (count != 0) { | ||
| byte[] bytes = new byte[count]; | ||
| int countReadBytes = dataInputStream.read(bytes); | ||
| if (countReadBytes != count) { | ||
| throw new IOException("Impossible to read all data"); | ||
| } | ||
| DataOutputStream dataOutputStreamForSave = new DataOutputStream(new FileOutputStream(fileForSave)); | ||
| dataOutputStreamForSave.write(bytes); | ||
| } | ||
| return fileForSave; | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| package ru.spbau.mit.alyokhina; | ||
|
|
||
|
|
||
| import javafx.util.Pair; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.util.List; | ||
| import java.util.Scanner; | ||
|
|
||
| /** Console UI (list files on server and download files) */ | ||
| public class Main { | ||
| public static void main(String[] args) { | ||
| int type; | ||
| Scanner in = new Scanner(System.in); | ||
| do { | ||
| System.out.println("1 - запустить сервер"); | ||
| System.out.println("2 - запустить клиента"); | ||
| System.out.println("3 - выйти"); | ||
| type = in.nextInt(); | ||
| if (type == 1) { | ||
| System.out.println("Введите порт"); | ||
| int port = in.nextInt(); | ||
| try { | ||
| final Server server = new Server(port); | ||
| Thread thread = new Thread(server::start); | ||
| thread.start(); | ||
| System.out.println("Сервер создан"); | ||
| } catch (IOException e) { | ||
| System.out.println(e.getMessage()); | ||
| } | ||
| } | ||
| if (type == 2) { | ||
| try { | ||
| System.out.println("Введите порт"); | ||
| int port = in.nextInt(); | ||
| Client client = new Client("localhost", port); | ||
| int typeRequest = 0; | ||
| do { | ||
| System.out.println("1 - list"); | ||
| System.out.println("2 - get"); | ||
| System.out.println("3 - назад"); | ||
| typeRequest = in.nextInt(); | ||
| if (typeRequest == 1) { | ||
| System.out.println("Введите путь к директории"); | ||
| String path = in.next(); | ||
| List<Pair<String, Boolean>> files = client.list(path); | ||
| for (Pair<String, Boolean> file : files) { | ||
| System.out.print(file.getKey()); | ||
| if (file.getValue()) { | ||
| System.out.println(" is directory"); | ||
| } else { | ||
| System.out.println(" is file"); | ||
| } | ||
| } | ||
| } | ||
| if (typeRequest == 2) { | ||
| System.out.println("Введите путь к файлу"); | ||
| String path = in.next(); | ||
| System.out.println("Введите путь для сохранения"); | ||
| String fileName = in.next(); | ||
| File file = client.get(path, fileName); | ||
| System.out.print("Размер файла = "); | ||
| System.out.println(file.length()); | ||
| } | ||
| } while (typeRequest != 3); | ||
|
|
||
|
|
||
| } catch (IOException e) { | ||
| System.out.println(e.getMessage()); | ||
| } | ||
| } | ||
| } while (type != 3); | ||
| System.exit(0); | ||
| } | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package ru.spbau.mit.alyokhina; | ||
|
|
||
| public enum Request { | ||
| LIST_REQUEST, | ||
| GET_REQUEST | ||
| } |
100 changes: 100 additions & 0 deletions
100
03.FTP/src/main/java/ru/spbau/mit/alyokhina/Server.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| package ru.spbau.mit.alyokhina; | ||
|
|
||
| import java.io.*; | ||
| import java.net.ServerSocket; | ||
| import java.net.Socket; | ||
|
|
||
| /** A server that processes two list requests and receives */ | ||
| public class Server { | ||
| /** Socket for connection with this server */ | ||
| private ServerSocket serverSocket; | ||
|
|
||
| /** | ||
| * Constructor | ||
| * | ||
| * @param port port of connection | ||
| * @throws IOException if Socket can't be created | ||
| */ | ||
| public Server(int port) throws IOException { | ||
| serverSocket = new ServerSocket(port); | ||
|
|
||
| } | ||
|
|
||
| /** Start of the server */ | ||
| public void start() { | ||
| while (true) { | ||
| try { | ||
| final Socket socket = serverSocket.accept(); | ||
| Thread thread = new Thread(() -> { | ||
| try (DataInputStream dataInputStream = new DataInputStream(socket.getInputStream()); | ||
| DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream())) { | ||
| while (!Thread.interrupted()) { | ||
| int requestType = dataInputStream.readInt(); | ||
| String path = dataInputStream.readUTF(); | ||
| if (requestType == Request.LIST_REQUEST.ordinal()) { | ||
| list(path, dataOutputStream); | ||
| } else if (requestType == Request.GET_REQUEST.ordinal()) { | ||
| get(path, dataOutputStream); | ||
| } | ||
| } | ||
| } catch (IOException e) { | ||
| System.out.println(e.getMessage()); | ||
| } | ||
| }); | ||
| thread.start(); | ||
| } catch (IOException ignored) { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Write count files, names of files and their types from input directory to dataOutputStream | ||
| * | ||
| * @param path directory path | ||
| * @param dataOutputStream stream for write result | ||
| * @throws IOException if it is impossible to write in dataOutputStream | ||
| */ | ||
| private void list(String path, DataOutputStream dataOutputStream) throws IOException { | ||
| File directory = new File(path); | ||
| File[] files = directory.listFiles(); | ||
| dataOutputStream.writeInt(files == null ? 0 : files.length); | ||
| if (files != null) { | ||
| for (File file : files) { | ||
| dataOutputStream.writeUTF(file.getName()); | ||
| dataOutputStream.writeBoolean(file.isDirectory()); | ||
| } | ||
| } | ||
| dataOutputStream.flush(); | ||
| } | ||
|
|
||
| /** | ||
| * Write file contents in dataOutputStream | ||
| * @param path name of file | ||
| * @param dataOutputStream OutputStream for write result | ||
| * @throws IOException if it is impossible to write in dataOutputStream | ||
| */ | ||
| private void get(String path, DataOutputStream dataOutputStream) throws IOException { | ||
| File file = new File(path); | ||
| int length = (int) file.length(); | ||
| if (length != 0) { | ||
| DataInputStream dataInputStreamForRequest = new DataInputStream(new FileInputStream(file)); | ||
| byte[] bytes = new byte[length]; | ||
|
|
||
| if (dataInputStreamForRequest.read(bytes) == length) { | ||
| dataOutputStream.writeInt(length); | ||
| dataOutputStream.write(bytes); | ||
| } else { | ||
| throw new IOException("Impossible to read all data"); | ||
| } | ||
| dataInputStreamForRequest.close(); | ||
| } else { | ||
| dataOutputStream.writeInt(length); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
тесты это хорошо, но хотелось бы ещё консольный UI, чтобы запустить клиент и сервер и поперекачивать файлы