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
30 changes: 30 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/
.idea

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store
12 changes: 12 additions & 0 deletions client/client.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module" module-name="common" />
</component>
</module>
121 changes: 103 additions & 18 deletions client/src/com/soroko/client/Client.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.soroko.client;

import com.soroko.common.common.SendReceive;
import com.soroko.common.common.Message;
import com.soroko.common.FileMessage;
import com.soroko.common.SendReceive;
import com.soroko.common.Message;

import java.io.IOException;
import java.net.InetSocketAddress;
Expand All @@ -12,40 +13,124 @@ public class Client {
private InetSocketAddress address;
private String username;
private Scanner scanner;
private SendReceive connectionHandler;
private boolean FilesAreEmpty;

public Client(InetSocketAddress address) {
this.address = address;
scanner = new Scanner(System.in);
}

public void saveCommand() throws InterruptedException {
String filepath = "";
String description = "";
if (!FilesAreEmpty) {
System.out.println("Укажите папку, в которую необходимо загрузить файл из сервера");
filepath = scanner.nextLine();
System.out.println("Введите название файла из списка доступных файлов:");
description = scanner.nextLine();
}
FileMessage fileMessage = new FileMessage(description, filepath);
fileMessage.setFilePath(filepath);
try {
connectionHandler.sendFileDescription(fileMessage);
} catch (IOException e) {
connectionHandler.close();
}
}

public void loadCommand() {
System.out.println("Введите путь, по которому необходимо загрузить файл на сервер");
String filepath = scanner.nextLine();
System.out.println("Введите описание файла:");
String description = scanner.nextLine();
System.out.println("Введите размер файла в мегабайтах:");
int size = scanner.nextInt();
FileMessage fileMessage = new FileMessage(description, size);
fileMessage.setFilePath(filepath);
try {
connectionHandler.sendFileDescription(fileMessage);
} catch (IOException e) {
connectionHandler.close();
}
}

public void startClient() {
System.out.println("Введите имя");
username = scanner.nextLine();
while (true) {
System.out.println("Введите текст сообщения");
String text = scanner.nextLine();
if (text.equals("/exit")) break;
try (SendReceive connectionHandler
= new SendReceive(new Socket(
address.getHostName(),
address.getPort()
))) {
private class Writer extends Thread {
public void run() {
boolean isLoadCommand = false;
boolean isSaveCommand = false;
while (true) {
if (isLoadCommand) {
loadCommand();
} else if (isSaveCommand) {
try {
saveCommand();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
} else System.out.println("Введите текст сообщения");
isLoadCommand = false;
isSaveCommand = false;
String text = scanner.nextLine();
if (text.equalsIgnoreCase("/loadfile")) isLoadCommand = true;
if (text.equalsIgnoreCase("/savefile")) isSaveCommand = true;
if (text.equalsIgnoreCase("/exit")) {
System.out.println("Соединение прекращено");
connectionHandler.close();
break;
}
Message message = new Message(username);
message.setText(text);
try {
connectionHandler.send(message);
Message fromServer = connectionHandler.receive();
System.out.println(fromServer.getText());
} catch (IOException e) {
} catch (IOException ignored) {
connectionHandler.close();
}
}
}
}

} catch (Exception e) {
private class Reader extends Thread {
public void run() {
while (true) {
Message message;
try {
message = connectionHandler.receive();
FilesAreEmpty = message.getFilesAreEmpty();
if (message.getText().equalsIgnoreCase("/exit")) {
connectionHandler.close();
break;
}
} catch (IOException ignored) {
connectionHandler.close();
break;
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
System.out.println(message.getText());
}
}
}

public void createConnection() throws IOException {
connectionHandler = new SendReceive(
new Socket(address.getHostName(), address.getPort()));
}

public void startClient() {
System.out.println("Введите имя");
username = scanner.nextLine();
try {
createConnection();
} catch (IOException e) {
throw new RuntimeException(e);
}
new Writer().start();
new Reader().start();
}
}





11 changes: 11 additions & 0 deletions common/common.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
45 changes: 45 additions & 0 deletions common/src/com/soroko/common/FileMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.soroko.common;

import java.io.Serializable;
import java.nio.file.Paths;

public class FileMessage implements Serializable {
private String description;
private int size;
private String filePath;


public FileMessage(String description, String filepath) {
this.description = description;
this.filePath = filepath;
}

public FileMessage(String description, int size) {
this.description = description;
this.size = size;
}

public String getFilePath() {
return filePath;
}

public void setFilePath(String filePath) {
this.filePath = filePath;
}

public String getDescription() {
return description;
}

public int getSize() {
return size;
}

@Override
public String toString() {
String fileName = Paths.get(filePath).getFileName().toString();
return "\n" + "название: " + fileName +
", описание: " + description +
", размер в мб: " + size;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.soroko.common.common;
package com.soroko.common;

import java.io.Serializable;
import java.time.LocalDateTime;

public class Message implements Serializable {
String sender;
String text;
LocalDateTime sentAt;
private String sender;
private String text;
private String sentAt;
private boolean FilesAreEmpty;

public Message(String sender) {
this.sender = sender;
Expand All @@ -16,10 +16,6 @@ public String getSender() {
return sender;
}

public void setSender(String sender) {
this.sender = sender;
}

public String getText() {
return text;
}
Expand All @@ -28,11 +24,19 @@ public void setText(String text) {
this.text = text;
}

public LocalDateTime getSentAt() {
public String getSentAt() {
return sentAt;
}

public void setSentAt(LocalDateTime sentAt) {
public void setSentAt(String sentAt) {
this.sentAt = sentAt;
}

public boolean getFilesAreEmpty() {
return FilesAreEmpty;
}

public void setFilesAreEmpty(boolean filesAreEmpty) {
FilesAreEmpty = filesAreEmpty;
}
}
63 changes: 63 additions & 0 deletions common/src/com/soroko/common/SendReceive.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.soroko.common;

import java.io.*;
import java.net.Socket;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Objects;

public class SendReceive implements AutoCloseable {
private ObjectOutputStream outputStream;
private ObjectInputStream inputStream;
private Socket socket;

public SendReceive(Socket socket) throws IOException {
this.socket = Objects.requireNonNull(socket);
outputStream = new ObjectOutputStream(socket.getOutputStream());
inputStream = new ObjectInputStream(socket.getInputStream());
}

public void send(Message message) throws IOException {
DateTimeFormatter formatTime = DateTimeFormatter.ofPattern("HH:mm:ss");
LocalTime localTime = LocalTime.now();
message.setSentAt(localTime.format(formatTime));
outputStream.writeObject(message);
outputStream.flush();
}

public void sendFileDescription(FileMessage fileMessage) throws IOException {
outputStream.writeObject(fileMessage);
outputStream.flush();
}

public Message receive() throws IOException, ClassNotFoundException {
try {
return (Message) inputStream.readObject();
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}

public FileMessage receiveFileDescription() throws IOException, ClassNotFoundException {
try {
return (FileMessage) inputStream.readObject();
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}

@Override
public void close() {
try {
if (!socket.isClosed()) {
inputStream.close();
outputStream.close();
socket.close();
}
} catch (IOException ignored) {
System.out.println("Проблема при закрытии потоков");
}

}
}
Loading