-
Notifications
You must be signed in to change notification settings - Fork 0
Java02. ДЗ 02, Васильев Роман #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
sproshev
left a comment
There was a problem hiding this 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(); | ||
| } |
There was a problem hiding this comment.
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); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- файл может не влезть в память -1
- бинарные файлы тоже в консоль будут выводиться? может все-таки сохранить?
| DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); | ||
| DataInputStream dis = new DataInputStream(socket.getInputStream()); | ||
| dos.writeInt(code); | ||
| method.writeValue(dos, value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
странно, почему разные сущности что-то пишут в dos? почему value дается снаружи, а пишет его method? может лучше method все запишет и все прочитает?
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
используется только внутри класса и тестах, это значит, что:
должен стать приватным
тесты должны быть написаны без него
There was a problem hiding this comment.
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; | ||
| } |
There was a problem hiding this comment.
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); | ||
| } |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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,"); |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
а как пользователю увидеть папка это или файл? -0.25
|
Я слегка просчитался. Идея класса Method была в том, чтобы передавать небольшие объекты. |
|
Я на всякий случай напишу, что готово к проверке. |
sproshev
left a comment
There was a problem hiding this 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") |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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> { |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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>() { |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
не используется
@sproshev