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 04.FTP_GUI/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>04</groupId>
<artifactId>FTP_GUI</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>
26 changes: 26 additions & 0 deletions 04.FTP_GUI/src/main/java/ru/spbau/mit/alyokhina/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ru.spbau.mit.alyokhina;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import ru.spbau.mit.alyokhina.ui.*;

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Крестики - Нолики");
Copy link

Choose a reason for hiding this comment

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

:)

GridPane gridPane = new GridPane();
gridPane.setAlignment(Pos.CENTER);
CreateNewElements.createMainActivity(gridPane);
primaryStage.setScene(new Scene(gridPane, 600, 400));
primaryStage.show();

}

public static void main(String[] args) {
launch(args);
}
}
99 changes: 99 additions & 0 deletions 04.FTP_GUI/src/main/java/ru/spbau/mit/alyokhina/client/Client.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package ru.spbau.mit.alyokhina.client;

import javafx.util.Pair;
import ru.spbau.mit.alyokhina.server.Server;


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(Server.LIST_REQUEST);
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(Server.GET_REQUEST);
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;
}

/**
* check file in server for existence
*
* @param path path of the file, that we want to check
* @return true - is exists, false - else
* @throws IOException if we can't write in dataOutputStream
*/
public boolean isExists(String path) throws IOException {
dataOutputStream.writeInt(Server.IS_EXISTS_REQUEST);
dataOutputStream.writeUTF(path);
dataOutputStream.flush();
return dataInputStream.readBoolean();
}
}
131 changes: 131 additions & 0 deletions 04.FTP_GUI/src/main/java/ru/spbau/mit/alyokhina/server/Server.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package ru.spbau.mit.alyokhina.server;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
* A server that processes two list requests and receives
*/
public class Server {
/**
* Value for request list
*/
public static int LIST_REQUEST = 1;
/**
* Value for request get
*/
public static int GET_REQUEST = 2;
/**
* Value for request is exist file
*/
public static int IS_EXISTS_REQUEST = 3;
/**
* 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 == LIST_REQUEST) {
list(path, dataOutputStream);
} else if (requestType == GET_REQUEST) {
get(path, dataOutputStream);
} else if (requestType == IS_EXISTS_REQUEST) {
isExist(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);
}
}

/**
* check file for existence
*
* @param path path of the file, that we want to check
* @throws IOException if we can't write in dataOutputStream or create
*/
private void isExist(String path, DataOutputStream dataOutputStream) throws IOException {
File file = new File(path);
dataOutputStream.writeBoolean(file.exists());
}
}




Loading