diff --git a/SimpleFTP/.idea/compiler.xml b/SimpleFTP/.idea/compiler.xml
new file mode 100644
index 0000000..bfbbf39
--- /dev/null
+++ b/SimpleFTP/.idea/compiler.xml
@@ -0,0 +1,17 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/SimpleFTP/.idea/misc.xml b/SimpleFTP/.idea/misc.xml
new file mode 100644
index 0000000..bc8d0a3
--- /dev/null
+++ b/SimpleFTP/.idea/misc.xml
@@ -0,0 +1,7 @@
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/SimpleFTP/.idea/modules.xml b/SimpleFTP/.idea/modules.xml
new file mode 100644
index 0000000..8bef07e
--- /dev/null
+++ b/SimpleFTP/.idea/modules.xml
@@ -0,0 +1,18 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/SimpleFTP/.idea/modules/ClientGUI/ClientGUI.iml b/SimpleFTP/.idea/modules/ClientGUI/ClientGUI.iml
new file mode 100644
index 0000000..727babf
--- /dev/null
+++ b/SimpleFTP/.idea/modules/ClientGUI/ClientGUI.iml
@@ -0,0 +1,12 @@
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/SimpleFTP/.idea/modules/FTPClient/FTPClient.iml b/SimpleFTP/.idea/modules/FTPClient/FTPClient.iml
new file mode 100644
index 0000000..608393c
--- /dev/null
+++ b/SimpleFTP/.idea/modules/FTPClient/FTPClient.iml
@@ -0,0 +1,13 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/SimpleFTP/.idea/modules/FTPClient/FTPClient_main.iml b/SimpleFTP/.idea/modules/FTPClient/FTPClient_main.iml
new file mode 100644
index 0000000..957e157
--- /dev/null
+++ b/SimpleFTP/.idea/modules/FTPClient/FTPClient_main.iml
@@ -0,0 +1,14 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/SimpleFTP/.idea/modules/FTPClient/FTPClient_test.iml b/SimpleFTP/.idea/modules/FTPClient/FTPClient_test.iml
new file mode 100644
index 0000000..f1fb24b
--- /dev/null
+++ b/SimpleFTP/.idea/modules/FTPClient/FTPClient_test.iml
@@ -0,0 +1,17 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/SimpleFTP/.idea/modules/FTPServer/FTPServer.iml b/SimpleFTP/.idea/modules/FTPServer/FTPServer.iml
new file mode 100644
index 0000000..a882b10
--- /dev/null
+++ b/SimpleFTP/.idea/modules/FTPServer/FTPServer.iml
@@ -0,0 +1,13 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/SimpleFTP/.idea/modules/FTPServer/FTPServer_main.iml b/SimpleFTP/.idea/modules/FTPServer/FTPServer_main.iml
new file mode 100644
index 0000000..e96af3e
--- /dev/null
+++ b/SimpleFTP/.idea/modules/FTPServer/FTPServer_main.iml
@@ -0,0 +1,14 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/SimpleFTP/.idea/modules/FTPServer/FTPServer_test.iml b/SimpleFTP/.idea/modules/FTPServer/FTPServer_test.iml
new file mode 100644
index 0000000..e408f50
--- /dev/null
+++ b/SimpleFTP/.idea/modules/FTPServer/FTPServer_test.iml
@@ -0,0 +1,17 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/SimpleFTP/.idea/modules/SimpleFTP.iml b/SimpleFTP/.idea/modules/SimpleFTP.iml
new file mode 100644
index 0000000..6dc9039
--- /dev/null
+++ b/SimpleFTP/.idea/modules/SimpleFTP.iml
@@ -0,0 +1,13 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/SimpleFTP/.idea/modules/SimpleFTP_main.iml b/SimpleFTP/.idea/modules/SimpleFTP_main.iml
new file mode 100644
index 0000000..b57c78d
--- /dev/null
+++ b/SimpleFTP/.idea/modules/SimpleFTP_main.iml
@@ -0,0 +1,14 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/SimpleFTP/.idea/modules/SimpleFTP_test.iml b/SimpleFTP/.idea/modules/SimpleFTP_test.iml
new file mode 100644
index 0000000..fa66cbb
--- /dev/null
+++ b/SimpleFTP/.idea/modules/SimpleFTP_test.iml
@@ -0,0 +1,18 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/SimpleFTP/.idea/vcs.xml b/SimpleFTP/.idea/vcs.xml
new file mode 100644
index 0000000..6c0b863
--- /dev/null
+++ b/SimpleFTP/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/SimpleFTP/FTPClient/build.gradle b/SimpleFTP/FTPClient/build.gradle
new file mode 100644
index 0000000..c73fa91
--- /dev/null
+++ b/SimpleFTP/FTPClient/build.gradle
@@ -0,0 +1,14 @@
+group 'ru.spbau.dkaznacheev'
+version '1.0-SNAPSHOT'
+
+apply plugin: 'java'
+
+sourceCompatibility = 1.8
+
+repositories {
+ mavenCentral()
+}
+
+dependencies {
+ testCompile group: 'junit', name: 'junit', version: '4.12'
+}
diff --git a/SimpleFTP/FTPClient/src/main/java/ru/spbau/dkaznacheev/simpleftp/Client.java b/SimpleFTP/FTPClient/src/main/java/ru/spbau/dkaznacheev/simpleftp/Client.java
new file mode 100644
index 0000000..77cb2ed
--- /dev/null
+++ b/SimpleFTP/FTPClient/src/main/java/ru/spbau/dkaznacheev/simpleftp/Client.java
@@ -0,0 +1,89 @@
+package ru.spbau.dkaznacheev.simpleftp;
+
+import java.io.*;
+import java.net.ConnectException;
+import java.net.Socket;
+import java.net.UnknownHostException;
+import java.util.Scanner;
+
+public class Client {
+
+ /**
+ * Downloads a file from sever
+ * @param in inputstream of a socket
+ * @param name name of the file
+ */
+ private static void receiveFile(DataInputStream in, String name) throws IOException{
+ long length = in.readLong();
+ if (length == 0) {
+ System.out.println("No such file");
+ return;
+ }
+ try (FileOutputStream out = new FileOutputStream(simpleName(name))) {
+ byte[] buffer = new byte[4096];
+ long remaining = length;
+ int read;
+ while ((read = in.read(buffer, 0, (int) Math.min(buffer.length, remaining))) > 0) {
+ remaining -= read;
+ out.write(buffer);
+ }
+ }
+ }
+
+ /**
+ * Returns simple file name of the filepath.
+ * @param path path to file
+ * @return filename
+ */
+ private static String simpleName(String path) {
+ return new File(path).getName();
+ }
+
+ public static void main(String[] args) {
+ try (
+ Socket socket = new Socket("127.0.0.1", 8080);
+ PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
+ DataInputStream in = new DataInputStream(socket.getInputStream());
+ Scanner stdIn = new Scanner(System.in)
+ ) {
+ ResponseCode fromServer;
+ String fromUser;
+ boolean exit = false;
+ while (!exit) {
+ fromUser = stdIn.nextLine();
+ if (fromUser != null) {
+ out.println(fromUser);
+ }
+ fromServer = ResponseCode.values()[in.readInt()];
+ switch (fromServer) {
+ case CLOSE_CONNECTION: {
+ exit = true;
+ break;
+ }
+ case INVALID_COMMAND: {
+ System.out.println("Invalid command");
+ break;
+ }
+ case FOLDER_DESCRIPTION: {
+ FolderDescription description = FolderDescription.read(in);
+ description.print();
+ break;
+ }
+ case FILE_SEND: {
+ String[] parts = fromUser.split(" ");
+ receiveFile(in, parts[1]);
+ break;
+ }
+ }
+ }
+
+ } catch (UnknownHostException e) {
+ System.err.println("Unknown host");
+ } catch (ConnectException e) {
+ System.err.println("Connection refused");
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+}
+
diff --git a/SimpleFTP/FTPServer/build.gradle b/SimpleFTP/FTPServer/build.gradle
new file mode 100644
index 0000000..c73fa91
--- /dev/null
+++ b/SimpleFTP/FTPServer/build.gradle
@@ -0,0 +1,14 @@
+group 'ru.spbau.dkaznacheev'
+version '1.0-SNAPSHOT'
+
+apply plugin: 'java'
+
+sourceCompatibility = 1.8
+
+repositories {
+ mavenCentral()
+}
+
+dependencies {
+ testCompile group: 'junit', name: 'junit', version: '4.12'
+}
diff --git a/SimpleFTP/FTPServer/src/main/java/ru/spbau/dkaznacheev/simpleftp/Server.java b/SimpleFTP/FTPServer/src/main/java/ru/spbau/dkaznacheev/simpleftp/Server.java
new file mode 100644
index 0000000..d184aa8
--- /dev/null
+++ b/SimpleFTP/FTPServer/src/main/java/ru/spbau/dkaznacheev/simpleftp/Server.java
@@ -0,0 +1,111 @@
+package ru.spbau.dkaznacheev.simpleftp;
+
+import java.io.*;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.net.SocketException;
+
+import static ru.spbau.dkaznacheev.simpleftp.ResponseCode.*;
+
+/**
+ * Simple FTP server that can handle multiple clients and send files over sockets.
+ */
+public class Server {
+
+ /**
+ * Sends file over ServerSocket's DataOutputStream.
+ * @param name filename
+ * @param out output stream
+ */
+ private static void sendFile(String name, DataOutputStream out) throws IOException{
+ File file = new File(name);
+ if (!file.exists()) {
+ out.writeLong(0);
+ return;
+ }
+ out.writeLong(file.length());
+ byte[] buffer = new byte[4096];
+ int read;
+ try (FileInputStream in = new FileInputStream(file)) {
+ while ((read = in.read(buffer)) > -1) {
+ out.write(buffer, 0, read);
+ }
+ }
+
+ }
+
+ /**
+ * Client handler, runs in a separate thread as long as there is a connectin with a client.
+ */
+ private static class FTPThread extends Thread {
+
+ /**
+ * Socket f server-client connection
+ */
+ private Socket socket;
+
+ public FTPThread(Socket socket) {
+ this.socket = socket;
+ }
+
+ @Override
+ public void run() {
+ try (
+ DataOutputStream out = new DataOutputStream(socket.getOutputStream());
+ BufferedReader in = new BufferedReader(
+ new InputStreamReader(socket.getInputStream()))
+ ) {
+ String inputLine;
+ while ((inputLine = in.readLine()) != null) {
+ if (inputLine.equals("0")) {
+ out.writeInt(CLOSE_CONNECTION.ordinal());
+ break;
+ }
+
+ String[] parts = inputLine.split(" ");
+ if (parts.length != 2) {
+ out.writeInt(INVALID_COMMAND.ordinal());
+ continue;
+ }
+ String command = parts[0];
+ String path = parts[1];
+
+ if (command.equals("1")) {
+ FolderDescription description = FolderDescription.describeFolder(path);
+ out.writeInt(FOLDER_DESCRIPTION.ordinal());
+ description.write(out);
+ } else if (command.equals("2")) {
+ out.writeInt(FILE_SEND.ordinal());
+ sendFile(path, out);
+ } else {
+ out.writeInt(INVALID_COMMAND.ordinal());
+ }
+ }
+ socket.close();
+ } catch (SocketException e) {
+ System.out.println("Goodbye");
+ }
+ catch (IOException e) {
+ System.err.println("IOException on thread " + Thread.currentThread().getName());
+ }
+ }
+ }
+
+ public static void main(String[] args) {
+ ServerSocket serverSocket;
+ try {
+ serverSocket = new ServerSocket(8080);
+ } catch (IOException e) {
+ System.err.println("Error creating server");
+ return;
+ }
+ while (true) {
+ try {
+ Socket clientSocket = serverSocket.accept();
+ new FTPThread(clientSocket).start();
+ } catch (IOException e) {
+ System.err.println("Exception on client connecting");
+ }
+ }
+ }
+}
diff --git a/SimpleFTP/GUIClient/GUIClient.iml b/SimpleFTP/GUIClient/GUIClient.iml
new file mode 100644
index 0000000..999ca21
--- /dev/null
+++ b/SimpleFTP/GUIClient/GUIClient.iml
@@ -0,0 +1,12 @@
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/SimpleFTP/GUIClient/src/ru/spbau/dkaznacheev/simpleftp/gui/Controller.java b/SimpleFTP/GUIClient/src/ru/spbau/dkaznacheev/simpleftp/gui/Controller.java
new file mode 100644
index 0000000..10a9dc6
--- /dev/null
+++ b/SimpleFTP/GUIClient/src/ru/spbau/dkaznacheev/simpleftp/gui/Controller.java
@@ -0,0 +1,178 @@
+package ru.spbau.dkaznacheev.simpleftp.gui;
+
+import javafx.fxml.FXML;
+import javafx.geometry.Pos;
+import javafx.scene.control.Alert;
+import javafx.scene.control.Alert.AlertType;
+import javafx.scene.control.Button;
+import javafx.scene.layout.VBox;
+import ru.spbau.dkaznacheev.simpleftp.FolderDescription;
+import ru.spbau.dkaznacheev.simpleftp.FolderDescription.FileDescription;
+import ru.spbau.dkaznacheev.simpleftp.ResponseCode;
+
+import java.io.DataInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.net.Socket;
+import java.util.StringJoiner;
+
+import static ru.spbau.dkaznacheev.simpleftp.ResponseCode.FILE_SEND;
+import static ru.spbau.dkaznacheev.simpleftp.ResponseCode.FOLDER_DESCRIPTION;
+
+public class Controller {
+ private String currentPath = ".";
+
+ private PrintWriter out;
+ private DataInputStream in;
+
+ @FXML
+ VBox buttonContainer;
+
+ /**
+ * Downloads a file from sever
+ * @param in inputstream of a socket
+ * @param name name of the file
+ */
+ private static void receiveFile(DataInputStream in, String name) throws IOException{
+ long length = in.readLong();
+ if (length == 0) {
+ System.out.println("No such file");
+ return;
+ }
+ try (FileOutputStream out = new FileOutputStream(name)) {
+ byte[] buffer = new byte[4096];
+ long remaining = length;
+ int read;
+ while ((read = in.read(buffer, 0, (int) Math.min(buffer.length, remaining))) > 0) {
+ remaining -= read;
+ out.write(buffer);
+ }
+ }
+ }
+
+ private boolean initClient() {
+ try {
+ Socket socket = new Socket("127.0.0.1", 8080);
+ out = new PrintWriter(socket.getOutputStream(), true);
+ in = new DataInputStream(socket.getInputStream());
+ } catch (IOException e) {
+ return false;
+ }
+ return true;
+ }
+
+ private static String getNewPath(String path, String name) {
+ if (path.equals("./")) {
+ return name;
+ } else {
+ return path + "/" + name;
+ }
+ }
+
+ private FolderDescription requestFolderDescription(String name) {
+ System.out.println("1 " + name);
+ out.println("1 " + name);
+ try {
+ ResponseCode fromServer = ResponseCode.values()[in.readInt()];
+ if (fromServer != FOLDER_DESCRIPTION) {
+ return null;
+ }
+ return FolderDescription.read(in);
+ } catch (IOException e) {
+ return null;
+ }
+ }
+
+ private boolean getAndShowFolder(String name) {
+ FolderDescription desc = requestFolderDescription(name);
+ if (desc == null) {
+ waitAlert(AlertType.ERROR, "Wrong folder", "OK to close");
+ return false;
+ }
+ if (desc.getSize() == 0) {
+ waitAlert(AlertType.INFORMATION, "Empty folder", "OK to close");
+ return false;
+ }
+ refreshFolder(desc.getFiles());
+ return true;
+ }
+
+ private void getAndDownloadFile(String name) {
+ out.println("2 " + getNewPath(currentPath, name));
+ try {
+ ResponseCode fromServer = ResponseCode.values()[in.readInt()];
+ if (fromServer != FILE_SEND) {
+ throw new IOException();
+ }
+ receiveFile(in, name);
+ waitAlert(AlertType.INFORMATION, "File downloaded", name + " downloaded!");
+ } catch (IOException e) {
+ waitAlert(AlertType.ERROR, "Could not download file", "OK to close");
+ }
+ }
+
+ private void refreshFolder(FileDescription[] files) {
+ buttonContainer.getChildren().clear();
+ Button backButton = new Button();
+ backButton.setAlignment(Pos.BASELINE_LEFT);
+ backButton.setPrefWidth(Double.MAX_VALUE);
+ backButton.setText("..");
+ backButton.setStyle("-fx-background-color: #a5ffcf");
+ backButton.setOnAction((e) -> {
+ getAndShowFolder(backFolder());
+ currentPath = backFolder();
+ });
+ buttonContainer.getChildren().add(backButton);
+ for (FileDescription file : files) {
+ Button button = new Button();
+ button.setAlignment(Pos.BASELINE_LEFT);
+ button.setPrefWidth(Double.MAX_VALUE);
+ button.setText(file.name);
+ if (file.isDir) {
+ button.setStyle("-fx-background-color: #a5ffcf");
+ button.setOnAction((e) -> {
+ if (getAndShowFolder(getNewPath(currentPath, file.name))) {
+ currentPath = getNewPath(currentPath, file.name);
+ }
+ });
+ } else {
+ button.setStyle("-fx-background-color: #e2edff");
+ button.setOnAction((e) -> getAndDownloadFile(file.name));
+ }
+ buttonContainer.getChildren().add(button);
+ }
+ }
+
+ private String backFolder() {
+ if (currentPath.equals("./")) {
+ return currentPath;
+ }
+ String[] parts = currentPath.split("/");
+ if (parts.length == 2) {
+ return "./";
+ }
+ StringJoiner joiner = new StringJoiner("/");
+ for (int i = 0; i < parts.length - 1; i++) {
+ joiner.add(parts[i]);
+ }
+ return joiner.toString();
+ }
+
+ private void waitAlert(Alert.AlertType type, String title, String message) {
+ Alert alert = new Alert(type);
+ alert.setTitle(title);
+ alert.setHeaderText(null);
+ alert.setContentText(message);
+ alert.showAndWait();
+ }
+
+ @FXML
+ public void initialize() {
+ while (!initClient()) {
+ waitAlert(AlertType.ERROR,"Connection error", "OK to retry");
+ }
+
+ getAndShowFolder(currentPath);
+ }
+}
diff --git a/SimpleFTP/GUIClient/src/ru/spbau/dkaznacheev/simpleftp/gui/Main.java b/SimpleFTP/GUIClient/src/ru/spbau/dkaznacheev/simpleftp/gui/Main.java
new file mode 100644
index 0000000..04c1e3f
--- /dev/null
+++ b/SimpleFTP/GUIClient/src/ru/spbau/dkaznacheev/simpleftp/gui/Main.java
@@ -0,0 +1,23 @@
+package ru.spbau.dkaznacheev.simpleftp.gui;
+
+import javafx.application.Application;
+import javafx.fxml.FXMLLoader;
+import javafx.scene.Parent;
+import javafx.scene.Scene;
+import javafx.stage.Stage;
+
+public class Main extends Application {
+
+ @Override
+ public void start(Stage primaryStage) throws Exception{
+ Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
+ primaryStage.setTitle("Hello World");
+ primaryStage.setScene(new Scene(root, 400, 500));
+ primaryStage.show();
+ }
+
+
+ public static void main(String[] args) {
+ launch(args);
+ }
+}
diff --git a/SimpleFTP/GUIClient/src/ru/spbau/dkaznacheev/simpleftp/gui/sample.fxml b/SimpleFTP/GUIClient/src/ru/spbau/dkaznacheev/simpleftp/gui/sample.fxml
new file mode 100644
index 0000000..0243472
--- /dev/null
+++ b/SimpleFTP/GUIClient/src/ru/spbau/dkaznacheev/simpleftp/gui/sample.fxml
@@ -0,0 +1,15 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/SimpleFTP/build.gradle b/SimpleFTP/build.gradle
new file mode 100644
index 0000000..433439b
--- /dev/null
+++ b/SimpleFTP/build.gradle
@@ -0,0 +1,16 @@
+group 'ru.spbau.dkaznacheev'
+version '1.0-SNAPSHOT'
+
+apply plugin: 'java'
+
+sourceCompatibility = 1.8
+
+repositories {
+ mavenCentral()
+}
+
+dependencies {
+ testCompile group: 'junit', name: 'junit', version: '4.12'
+ compile group: 'org.jetbrains', name: 'annotations', version: '15.0'
+
+}
\ No newline at end of file
diff --git a/SimpleFTP/gradle/wrapper/gradle-wrapper.jar b/SimpleFTP/gradle/wrapper/gradle-wrapper.jar
new file mode 100644
index 0000000..01b8bf6
Binary files /dev/null and b/SimpleFTP/gradle/wrapper/gradle-wrapper.jar differ
diff --git a/SimpleFTP/gradle/wrapper/gradle-wrapper.properties b/SimpleFTP/gradle/wrapper/gradle-wrapper.properties
new file mode 100644
index 0000000..933b647
--- /dev/null
+++ b/SimpleFTP/gradle/wrapper/gradle-wrapper.properties
@@ -0,0 +1,5 @@
+distributionBase=GRADLE_USER_HOME
+distributionPath=wrapper/dists
+zipStoreBase=GRADLE_USER_HOME
+zipStorePath=wrapper/dists
+distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-bin.zip
diff --git a/SimpleFTP/gradlew b/SimpleFTP/gradlew
new file mode 100755
index 0000000..cccdd3d
--- /dev/null
+++ b/SimpleFTP/gradlew
@@ -0,0 +1,172 @@
+#!/usr/bin/env sh
+
+##############################################################################
+##
+## Gradle start up script for UN*X
+##
+##############################################################################
+
+# Attempt to set APP_HOME
+# Resolve links: $0 may be a link
+PRG="$0"
+# Need this for relative symlinks.
+while [ -h "$PRG" ] ; do
+ ls=`ls -ld "$PRG"`
+ link=`expr "$ls" : '.*-> \(.*\)$'`
+ if expr "$link" : '/.*' > /dev/null; then
+ PRG="$link"
+ else
+ PRG=`dirname "$PRG"`"/$link"
+ fi
+done
+SAVED="`pwd`"
+cd "`dirname \"$PRG\"`/" >/dev/null
+APP_HOME="`pwd -P`"
+cd "$SAVED" >/dev/null
+
+APP_NAME="Gradle"
+APP_BASE_NAME=`basename "$0"`
+
+# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
+DEFAULT_JVM_OPTS=""
+
+# Use the maximum available, or set MAX_FD != -1 to use that value.
+MAX_FD="maximum"
+
+warn () {
+ echo "$*"
+}
+
+die () {
+ echo
+ echo "$*"
+ echo
+ exit 1
+}
+
+# OS specific support (must be 'true' or 'false').
+cygwin=false
+msys=false
+darwin=false
+nonstop=false
+case "`uname`" in
+ CYGWIN* )
+ cygwin=true
+ ;;
+ Darwin* )
+ darwin=true
+ ;;
+ MINGW* )
+ msys=true
+ ;;
+ NONSTOP* )
+ nonstop=true
+ ;;
+esac
+
+CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
+
+# Determine the Java command to use to start the JVM.
+if [ -n "$JAVA_HOME" ] ; then
+ if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
+ # IBM's JDK on AIX uses strange locations for the executables
+ JAVACMD="$JAVA_HOME/jre/sh/java"
+ else
+ JAVACMD="$JAVA_HOME/bin/java"
+ fi
+ if [ ! -x "$JAVACMD" ] ; then
+ die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
+
+Please set the JAVA_HOME variable in your environment to match the
+location of your Java installation."
+ fi
+else
+ JAVACMD="java"
+ which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
+
+Please set the JAVA_HOME variable in your environment to match the
+location of your Java installation."
+fi
+
+# Increase the maximum file descriptors if we can.
+if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
+ MAX_FD_LIMIT=`ulimit -H -n`
+ if [ $? -eq 0 ] ; then
+ if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
+ MAX_FD="$MAX_FD_LIMIT"
+ fi
+ ulimit -n $MAX_FD
+ if [ $? -ne 0 ] ; then
+ warn "Could not set maximum file descriptor limit: $MAX_FD"
+ fi
+ else
+ warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
+ fi
+fi
+
+# For Darwin, add options to specify how the application appears in the dock
+if $darwin; then
+ GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
+fi
+
+# For Cygwin, switch paths to Windows format before running java
+if $cygwin ; then
+ APP_HOME=`cygpath --path --mixed "$APP_HOME"`
+ CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
+ JAVACMD=`cygpath --unix "$JAVACMD"`
+
+ # We build the pattern for arguments to be converted via cygpath
+ ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
+ SEP=""
+ for dir in $ROOTDIRSRAW ; do
+ ROOTDIRS="$ROOTDIRS$SEP$dir"
+ SEP="|"
+ done
+ OURCYGPATTERN="(^($ROOTDIRS))"
+ # Add a user-defined pattern to the cygpath arguments
+ if [ "$GRADLE_CYGPATTERN" != "" ] ; then
+ OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
+ fi
+ # Now convert the arguments - kludge to limit ourselves to /bin/sh
+ i=0
+ for arg in "$@" ; do
+ CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
+ CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
+
+ if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
+ eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
+ else
+ eval `echo args$i`="\"$arg\""
+ fi
+ i=$((i+1))
+ done
+ case $i in
+ (0) set -- ;;
+ (1) set -- "$args0" ;;
+ (2) set -- "$args0" "$args1" ;;
+ (3) set -- "$args0" "$args1" "$args2" ;;
+ (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
+ (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
+ (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
+ (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
+ (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
+ (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
+ esac
+fi
+
+# Escape application args
+save () {
+ for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
+ echo " "
+}
+APP_ARGS=$(save "$@")
+
+# Collect all arguments for the java command, following the shell quoting and substitution rules
+eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
+
+# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
+if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
+ cd "$(dirname "$0")"
+fi
+
+exec "$JAVACMD" "$@"
diff --git a/SimpleFTP/gradlew.bat b/SimpleFTP/gradlew.bat
new file mode 100644
index 0000000..e95643d
--- /dev/null
+++ b/SimpleFTP/gradlew.bat
@@ -0,0 +1,84 @@
+@if "%DEBUG%" == "" @echo off
+@rem ##########################################################################
+@rem
+@rem Gradle startup script for Windows
+@rem
+@rem ##########################################################################
+
+@rem Set local scope for the variables with windows NT shell
+if "%OS%"=="Windows_NT" setlocal
+
+set DIRNAME=%~dp0
+if "%DIRNAME%" == "" set DIRNAME=.
+set APP_BASE_NAME=%~n0
+set APP_HOME=%DIRNAME%
+
+@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
+set DEFAULT_JVM_OPTS=
+
+@rem Find java.exe
+if defined JAVA_HOME goto findJavaFromJavaHome
+
+set JAVA_EXE=java.exe
+%JAVA_EXE% -version >NUL 2>&1
+if "%ERRORLEVEL%" == "0" goto init
+
+echo.
+echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
+echo.
+echo Please set the JAVA_HOME variable in your environment to match the
+echo location of your Java installation.
+
+goto fail
+
+:findJavaFromJavaHome
+set JAVA_HOME=%JAVA_HOME:"=%
+set JAVA_EXE=%JAVA_HOME%/bin/java.exe
+
+if exist "%JAVA_EXE%" goto init
+
+echo.
+echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
+echo.
+echo Please set the JAVA_HOME variable in your environment to match the
+echo location of your Java installation.
+
+goto fail
+
+:init
+@rem Get command-line arguments, handling Windows variants
+
+if not "%OS%" == "Windows_NT" goto win9xME_args
+
+:win9xME_args
+@rem Slurp the command line arguments.
+set CMD_LINE_ARGS=
+set _SKIP=2
+
+:win9xME_args_slurp
+if "x%~1" == "x" goto execute
+
+set CMD_LINE_ARGS=%*
+
+:execute
+@rem Setup the command line
+
+set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
+
+@rem Execute Gradle
+"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
+
+:end
+@rem End local scope for the variables with windows NT shell
+if "%ERRORLEVEL%"=="0" goto mainEnd
+
+:fail
+rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
+rem the _cmd.exe /c_ return code!
+if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
+exit /b 1
+
+:mainEnd
+if "%OS%"=="Windows_NT" endlocal
+
+:omega
diff --git a/SimpleFTP/settings.gradle b/SimpleFTP/settings.gradle
new file mode 100644
index 0000000..0d89878
--- /dev/null
+++ b/SimpleFTP/settings.gradle
@@ -0,0 +1,5 @@
+rootProject.name = 'SimpleFTP'
+include 'FTPServer'
+include 'FTPClient'
+include 'ClientGUI'
+
diff --git a/SimpleFTP/src/main/java/ru/spbau/dkaznacheev/simpleftp/FolderDescription.java b/SimpleFTP/src/main/java/ru/spbau/dkaznacheev/simpleftp/FolderDescription.java
new file mode 100644
index 0000000..1a461df
--- /dev/null
+++ b/SimpleFTP/src/main/java/ru/spbau/dkaznacheev/simpleftp/FolderDescription.java
@@ -0,0 +1,132 @@
+package ru.spbau.dkaznacheev.simpleftp;
+
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.File;
+import java.io.IOException;
+
+/**
+ * Class describing folder contents.
+ */
+public class FolderDescription {
+
+ /**
+ * Class describing one file.
+ */
+ public static class FileDescription {
+
+ /**
+ * Filename.
+ */
+ public final String name;
+
+ /**
+ * Whether a file is a directory or not.
+ */
+ public final boolean isDir;
+
+ public FileDescription(String name, boolean isDir) {
+ this.name = name;
+ this.isDir = isDir;
+ }
+ }
+
+ public FileDescription[] getFiles() {
+ return files;
+ }
+
+ public int getSize() {
+ return size;
+ }
+
+ /**
+ * Size of the folder.
+ */
+ private int size;
+
+ /**
+ * Description of folder's contents.
+ */
+ private FileDescription[] files;
+
+ /**
+ * Writes FolderDescription to DataOutputStream.
+ * @param out stream to write to
+ */
+ public void write(DataOutputStream out) throws IOException {
+ out.writeInt(size);
+ if (size == 0) {
+ return;
+ }
+ for (FileDescription description : files) {
+ out.writeUTF(description.name);
+ out.writeBoolean(description.isDir);
+ }
+ }
+
+ /**
+ * Prints FolderDescription to console.
+ */
+ public void print() {
+ if (size == 0) {
+ System.out.println("Folder not found");
+ return;
+ }
+ System.out.println(size + " files:");
+ for (FileDescription description : files) {
+ System.out.print(description.name);
+ if (description.isDir) {
+ System.out.print(": folder");
+ }
+ System.out.println();
+ }
+ System.out.println();
+ }
+
+ /**
+ * Reads FolderDescription from InputStream.
+ * @param in input stream
+ * @return read FolderDescription
+ */
+ public static FolderDescription read(DataInputStream in) throws IOException {
+ FolderDescription description = new FolderDescription();
+ description.size = in.readInt();
+ if (description.size == 0) {
+ return description;
+ }
+ description.files = new FileDescription[description.size];
+
+ for (int i = 0; i < description.size; i++) {
+ String name = in.readUTF();
+ boolean isDir = in.readBoolean();
+ description.files[i] = new FileDescription(name, isDir);
+ }
+ return description;
+ }
+
+ /**
+ * Returns a FolderDescription from path to folder.
+ * If it is the path of non-folder file, returns a FolderDescription with size = 0.
+ * @param path path to file/folder
+ * @return FolderDescription on this path
+ */
+ public static FolderDescription describeFolder(String path) {
+ File root = new File(path);
+ FolderDescription result = new FolderDescription();
+
+ File[] contents = root.listFiles();
+ if (contents == null) {
+ result.size = 0;
+ return result;
+ }
+
+ result.files = new FileDescription[contents.length];
+ result.size = contents.length;
+ for (int i = 0; i < contents.length; i++) {
+ String name = contents[i].getName();
+ boolean isDir = contents[i].isDirectory();
+ result.files[i] = new FileDescription(name, isDir);
+ }
+ return result;
+ }
+}
diff --git a/SimpleFTP/src/main/java/ru/spbau/dkaznacheev/simpleftp/ResponseCode.java b/SimpleFTP/src/main/java/ru/spbau/dkaznacheev/simpleftp/ResponseCode.java
new file mode 100644
index 0000000..2a7015a
--- /dev/null
+++ b/SimpleFTP/src/main/java/ru/spbau/dkaznacheev/simpleftp/ResponseCode.java
@@ -0,0 +1,26 @@
+package ru.spbau.dkaznacheev.simpleftp;
+
+/**
+ * Codes that client and server use to send util information.
+ */
+public enum ResponseCode {
+ /**
+ * If a connection is closed.
+ */
+ CLOSE_CONNECTION,
+
+ /**
+ * If a client sent an invalid command.
+ */
+ INVALID_COMMAND,
+
+ /**
+ * If a server is going to send a FolderDescription.
+ */
+ FOLDER_DESCRIPTION,
+
+ /**
+ * If a server is going to send a file.
+ */
+ FILE_SEND
+}
\ No newline at end of file
diff --git a/SimpleFTP/src/test/java/ru/spbau/dkaznacheev/simpleftp/FolderDescriptionTest.java b/SimpleFTP/src/test/java/ru/spbau/dkaznacheev/simpleftp/FolderDescriptionTest.java
new file mode 100644
index 0000000..85d4b8b
--- /dev/null
+++ b/SimpleFTP/src/test/java/ru/spbau/dkaznacheev/simpleftp/FolderDescriptionTest.java
@@ -0,0 +1,11 @@
+package ru.spbau.dkaznacheev.simpleftp;
+
+import org.junit.Test;
+
+public class FolderDescriptionTest {
+ @Test
+ public void folderDescSimpleTest() {
+ FolderDescription desc = FolderDescription.describeFolder("./");
+ desc.print();
+ }
+}