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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea/
target/
/*.iml
43 changes: 43 additions & 0 deletions src/main/java/ru/spbau/mit/hw3/Client.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package ru.spbau.mit.hw3;

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

/**
* Created by YuryKravchenko on 14/03/16.
*/
public class Client{

Socket socket;
DataInputStream in;
DataOutputStream out;

Choose a reason for hiding this comment

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

модификаторы


public Client() throws IOException {
socket = new Socket("127.0.0.1", 12345);
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
}

public String List(String path) throws IOException {

Choose a reason for hiding this comment

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

методы именуются с маленкой буквы, -0.5

out.writeInt(1);
out.writeUTF(path);
int count = in.readInt();
StringBuilder builder = new StringBuilder();
for (int i = 0; i < count; ++i) {
builder.append(in.readUTF()).append(' ').append(in.readBoolean()).append('\n');
}
return builder.toString();
}

public String Get(String s) throws IOException {

Choose a reason for hiding this comment

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

методы именуются с маленкой буквы

out.writeInt(2);
out.writeUTF(s);
Long size = in.readLong();

Choose a reason for hiding this comment

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

long

byte[] file = new byte[size.intValue()];
in.read(file, 0, size.intValue());

Choose a reason for hiding this comment

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

по этим двум строчкам аналогичные замечания: файл может не влезть в память, read может прочитать не все

return file.toString();

Choose a reason for hiding this comment

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

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

}

}
48 changes: 48 additions & 0 deletions src/main/java/ru/spbau/mit/hw3/MainClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package ru.spbau.mit.hw3;

import java.io.IOException;
import java.util.Scanner;

/**
* Created by YuryKravchenko on 14/03/16.
*/
public class MainClient {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Client client = null;
while (true) {
String cmd = in.nextLine();
switch (cmd) {
case "connect":
try {
client = new Client();
} catch (IOException e) {
System.out.println("starting client failed, try again");
}
break;
case "executeList":
cmd = in.nextLine();

Choose a reason for hiding this comment

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

cmd -> path

try {
System.out.println(client.List(cmd));
} catch (IOException e) {
System.out.println("connection failed");
} catch (NullPointerException e) {

Choose a reason for hiding this comment

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

это надо проверять перед исполнением запроса

System.out.println("no connection");
}
break;
case "executeGet":
cmd = in.nextLine();

Choose a reason for hiding this comment

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

cmd -> path

try {
System.out.println(client.Get(cmd));
} catch (IOException e) {
System.out.println("connection failed");
} catch (NullPointerException e) {

Choose a reason for hiding this comment

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

это надо проверять перед исполнением запроса

System.out.println("no connection");
}
break;
case "disconnect":
return;

Choose a reason for hiding this comment

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

клиент должен закрывать подключение, -0.5

}
}
}

Choose a reason for hiding this comment

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

делай также, как с сервером, запускай клиент сразу, а потом в цикле обрабатывай команды

}
30 changes: 30 additions & 0 deletions src/main/java/ru/spbau/mit/hw3/MainServer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package ru.spbau.mit.hw3;

import java.io.IOException;
import java.util.Scanner;

/**
* Created by YuryKravchenko on 14/03/16.
*/
public class MainServer {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (true) {
String cmd = in.nextLine();
try {
switch (cmd) {
case "start":
Thread newThread = new Thread(new Server());
newThread.setDaemon(true);
newThread.start();
break;
case "stop":
return;
}
} catch (IOException e) {
System.out.println("starting server failed, try again");
return;
}
}
}

Choose a reason for hiding this comment

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

больше одного сервера все равно не запустить, можно было запустить сервер, а потом ждать команды stop

}
67 changes: 67 additions & 0 deletions src/main/java/ru/spbau/mit/hw3/Server.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package ru.spbau.mit.hw3;

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

/**
* Created by YuryKravchenko on 14/03/16.
*/
public class Server implements Runnable {

ServerSocket socket;
DataInputStream in;
DataOutputStream out;

Choose a reason for hiding this comment

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

модификаторы, -1


public Server() throws IOException {
socket = new ServerSocket(12345);
}

@Override
public void run() {
try {
Socket socket = this.socket.accept();

Choose a reason for hiding this comment

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

в единицу времени приложение может обрабатывать только один запрос, было бы лучше, если бы это было не так. Здесь исправлять не надо, в следующем дз надо к этому стремиться

in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
} catch (IOException e) {
System.out.println("connection error");
return;
}
try {
while (true) {
int cmd = in.readInt();
String path = in.readUTF();
switch (cmd) {
case 1:

Choose a reason for hiding this comment

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

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

if (!new File(path).exists()) {

Choose a reason for hiding this comment

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

new FIle(path) должно быть выделено в отдельную переменную, оно используется 6!!! раз, -1

out.writeInt(0);
break;
}
out.writeInt(new File(path).listFiles().length);

Choose a reason for hiding this comment

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

надо listFiles() проверять на null, см документацию

for (File f : new File(path).listFiles()) {
out.writeUTF(f.getName());
out.writeBoolean(f.isDirectory());
}
out.flush();
break;
case 2:
if (!new File(path).exists() || new File(path).isDirectory()) {
out.writeInt(0);
break;
}
DataInputStream file = new DataInputStream(new FileInputStream(path));
Long length = new File(path).length();

Choose a reason for hiding this comment

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

long, -0.5

out.writeLong(length);
byte[] b = new byte[length.intValue()];

Choose a reason for hiding this comment

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

файл может не влезть в память, -0.5

file.read(b);

Choose a reason for hiding this comment

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

см документацию read, туда могут записаться не все данные, -0.5

out.write(b);
out.flush();
break;
}
}
} catch (IOException e) {
System.out.println("connection failed");
return;
}
}
}