Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .travis.yml
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
31 changes: 31 additions & 0 deletions 03.FTP/pom.xml
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>
78 changes: 78 additions & 0 deletions 03.FTP/src/main/java/ru/spbau/mit/alyokhina/Client.java
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;
}

}
77 changes: 77 additions & 0 deletions 03.FTP/src/main/java/ru/spbau/mit/alyokhina/Main.java
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);
}
}

6 changes: 6 additions & 0 deletions 03.FTP/src/main/java/ru/spbau/mit/alyokhina/Request.java
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 03.FTP/src/main/java/ru/spbau/mit/alyokhina/Server.java
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 {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тесты это хорошо, но хотелось бы ещё консольный UI, чтобы запустить клиент и сервер и поперекачивать файлы

/** 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);
}
}
}





Loading