Skip to content

Java02. ДЗ 02, Васильев Роман#8

Open
nizshee wants to merge 2 commits intomasterfrom
task-ftp
Open

Java02. ДЗ 02, Васильев Роман#8
nizshee wants to merge 2 commits intomasterfrom
task-ftp

Conversation

@nizshee
Copy link
Copy Markdown
Owner

@nizshee nizshee commented Oct 14, 2016

Copy link
Copy Markdown

@sproshev sproshev left a comment

Choose a reason for hiding this comment

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

надо исправлять

System.out.print(b);
}
System.out.println();
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

а если аргументы неверные? тихонько выйдем и ничего не скажем?)

System.out.println(bytes.length);
for (byte b: bytes) {
System.out.print(b);
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

  1. файл может не влезть в память -1
  2. бинарные файлы тоже в консоль будут выводиться? может все-таки сохранить?

DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
DataInputStream dis = new DataInputStream(socket.getInputStream());
dos.writeInt(code);
method.writeValue(dos, value);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

странно, почему разные сущности что-то пишут в dos? почему value дается снаружи, а пишет его method? может лучше method все запишет и все прочитает?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

method - удаленная процедура. Она знает как сериализовывать/десериализовывать объекты.
Это разумно. С другой стороны она не знает какой у нее идентификатор на конкретном сервере, что тоже, вроде, разумно.

DataInputStream dis = new DataInputStream(socket.getInputStream());
dos.writeInt(code);
method.writeValue(dos, value);
return method.readResult(dis);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

сокет не закрывается -1

return getValue(new Socket(host, port), 2, new GetMethod(), name);
}

public static <Value, Result> Result getValue(Socket socket, int code, Method<Value, Result> method, Value value)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

используется только внутри класса и тестах, это значит, что:

должен стать приватным
тесты должны быть написаны без него

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Изначальная задумка была в том, чтобы разнести межсетевое взаимодействие и работу с данными, и тестировать их раздельно. Этот метод способствует задумки. Я перенес его в интерфейс Method.

void writeResult(DataOutputStream dos, Result result) throws IOException;

Result readResult(DataInputStream dis) throws IOException;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

еще и названия сбивают с толку, как понять, что делает apply? может лучше его сигнатуру заменить на Result execute(Request request)?

то же и с write/readvalue

dos.write(bytes, 0, bytes.length);
for (Byte aByte : bytes) {
dos.writeByte(aByte);
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

файл два раза пишется? -0.5


@Override
public byte[] readResult(DataInputStream dis) throws IOException {
int length = dis.readInt();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

по заданию -- лонг

int length = dis.readInt();
byte[] bytes = new byte[length];
int read = dis.read(bytes, 0, length);
if (read != length) throw new IOException("Bad read,");
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

??? вполне естественная ситуация, надо ее поддерживать -1. см readFully у dis


@Override
public String toString() {
return name;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

а как пользователю увидеть папка это или файл? -0.25

@nizshee
Copy link
Copy Markdown
Owner Author

nizshee commented Nov 15, 2016

Я слегка просчитался. Идея класса Method была в том, чтобы передавать небольшие объекты.
Для get пришлось добавить абстракцию над операцией на сервере, и в Client написать напрямую работу с байтами.

@nizshee
Copy link
Copy Markdown
Owner Author

nizshee commented Jan 11, 2017

Я на всякий случай напишу, что готово к проверке.

Copy link
Copy Markdown

@sproshev sproshev left a comment

Choose a reason for hiding this comment

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

6.25

import java.net.Socket;
import java.util.List;

@SuppressWarnings("all")
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

лучше подавлять конкретные варнинги и как можно ближе к месту подавления, иначе какой-н. важный варнинг в будущем будет подавлен

try (Socket socket = new Socket(host, port)) {
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
dos.writeInt(2);
dos.writeUTF(name);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

flush

import java.io.IOException;
import java.net.Socket;

public interface Method<Request, Response> {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

по сути выродился в ListMethod

dos.writeInt(code);
method.writeRequest(dos, request);
Response response = method.readResult(dis);
socket.close();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

до этого места не всегда дойдет


private final static int code = 0;
private final static AtomicBoolean trigger = new AtomicBoolean(false);
public static final Method<String, String> method = new Method<String, String>() {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

caps

Server server = new Server(InetAddress.getByName("localhost"), 8080);
server.start(handlers);
Thread.sleep(500);
Client client = new Client("localhost", 8080);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

не используется

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants