diff --git a/Lab5Client/build/libs/Lab5Client-2.0.jar b/Lab5Client/build/libs/Lab5Client-2.0.jar index 9bb6618..06ad261 100644 Binary files a/Lab5Client/build/libs/Lab5Client-2.0.jar and b/Lab5Client/build/libs/Lab5Client-2.0.jar differ diff --git a/Lab5Client/build/libs/connection.properties b/Lab5Client/build/libs/connection.properties index 15fc23f..f5630d4 100644 --- a/Lab5Client/build/libs/connection.properties +++ b/Lab5Client/build/libs/connection.properties @@ -1,2 +1,2 @@ -hostname=helios.se.ifmo.ru +hostname=localhost port=1234 \ No newline at end of file diff --git a/Lab5Client/build/libs/connection.properties.bak b/Lab5Client/build/libs/connection.properties.bak index 193632e..168e0c6 100644 --- a/Lab5Client/build/libs/connection.properties.bak +++ b/Lab5Client/build/libs/connection.properties.bak @@ -1,2 +1,2 @@ -hostname=s316304@helios.se.ifmo.ru -port=1234 \ No newline at end of file +hostname=localhost +port=2222 \ No newline at end of file diff --git a/Lab5Client/src/main/java/client/commandline/CommandLineHandler.java b/Lab5Client/src/main/java/client/commandline/CommandLineHandler.java index a484b30..88bf7ee 100644 --- a/Lab5Client/src/main/java/client/commandline/CommandLineHandler.java +++ b/Lab5Client/src/main/java/client/commandline/CommandLineHandler.java @@ -1,8 +1,10 @@ package client.commandline; import client.net.UDPClient; +import common.commandline.Executables; import common.commandline.response.CommandResult; import common.commandline.response.DefaultResponse; +import common.commandline.response.Response; import common.parser.ConnectionProperties; import common.util.UtilFunctions; @@ -68,7 +70,7 @@ public final class CommandLineHandler { System.out.flush(); } } catch (IOException | InterruptedException e) { - e.printStackTrace(); + System.err.println("Что-то пошло не так"); } } @@ -92,11 +94,10 @@ public final class CommandLineHandler { } boolean argsValid = command.validate(args); if (!argsValid) return; - CommandResult result = command.isClientOnly() || clientMode ? - command.executeOnClient() : executeOnServer(udp, command); + boolean isClient = command.isClientOnly() || clientMode; + CommandResult result = isClient ? command.executeOnClient() : executeOnServer(udp, command); PrintStream ps = result.getResponse() == DefaultResponse.OK ? System.out : System.err; - if (result.getValue() == null) ps.println(result.getResponse().getMsg()); - else ps.println(result.getValue()); + ps.println(result.getValue()); updateHistory(alias); } @@ -243,18 +244,21 @@ public final class CommandLineHandler { args -> { String fileName = (String) args[0]; File file = new File(fileName); - if (!file.exists() || file.isDirectory()) - return new CommandResult(null, DefaultResponse.FILE_NOT_FOUND); + if (!file.exists() || file.isDirectory()) { + Response response = DefaultResponse.FILE_NOT_FOUND; + return new CommandResult(response.getMsg(), response); + } Reader streamReader; try { streamReader = new InputStreamReader(new FileInputStream(file)); } catch (FileNotFoundException e) { - e.printStackTrace(); - return new CommandResult(null, DefaultResponse.UNKNOWN); + Response response = DefaultResponse.UNKNOWN; + return new CommandResult(response.getMsg(), response); } instance.addNewInput(streamReader, fileName); - return new CommandResult(null, DefaultResponse.OK); + Response response = DefaultResponse.OK; + return new CommandResult(response.getMsg(), response); }); } diff --git a/Lab5Client/src/main/java/client/net/UDPClient.java b/Lab5Client/src/main/java/client/net/UDPClient.java index 62a8901..a71271d 100644 --- a/Lab5Client/src/main/java/client/net/UDPClient.java +++ b/Lab5Client/src/main/java/client/net/UDPClient.java @@ -45,10 +45,8 @@ public class UDPClient { public CommandResult send(Executable executable, Object[] args) { if (!available) return new CommandResult(null, DefaultResponse.HOST_NOT_FOUND); byte[] buffer; - try ( - ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); - ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteOutputStream) - ) { + try (ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream()) { + ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteOutputStream); objectOutputStream.writeObject(executable); objectOutputStream.writeObject(args); buffer = byteOutputStream.toByteArray(); diff --git a/Lab5Core/build/classes/java/main/common/parser/ConnectionProperties.class b/Lab5Core/build/classes/java/main/common/parser/ConnectionProperties.class index 71b4f31..5f86f73 100644 Binary files a/Lab5Core/build/classes/java/main/common/parser/ConnectionProperties.class and b/Lab5Core/build/classes/java/main/common/parser/ConnectionProperties.class differ diff --git a/Lab5Core/build/docs/javadoc/common/data/package-tree.html b/Lab5Core/build/docs/javadoc/common/data/package-tree.html index af23486..d6beec8 100644 --- a/Lab5Core/build/docs/javadoc/common/data/package-tree.html +++ b/Lab5Core/build/docs/javadoc/common/data/package-tree.html @@ -91,8 +91,8 @@ diff --git a/Lab5Core/build/libs/Lab5Core-2.0.jar b/Lab5Core/build/libs/Lab5Core-2.0.jar index 5437021..b00027e 100644 Binary files a/Lab5Core/build/libs/Lab5Core-2.0.jar and b/Lab5Core/build/libs/Lab5Core-2.0.jar differ diff --git a/Lab5Core/build/tmp/compileJava/previous-compilation-data.bin b/Lab5Core/build/tmp/compileJava/previous-compilation-data.bin index af2aa2c..88486f4 100644 Binary files a/Lab5Core/build/tmp/compileJava/previous-compilation-data.bin and b/Lab5Core/build/tmp/compileJava/previous-compilation-data.bin differ diff --git a/Lab5Core/src/main/java/common/commandline/Executables.java b/Lab5Core/src/main/java/common/commandline/Executables.java index 0d99a53..9f410cb 100644 --- a/Lab5Core/src/main/java/common/commandline/Executables.java +++ b/Lab5Core/src/main/java/common/commandline/Executables.java @@ -5,6 +5,7 @@ import common.collection.PeopleDatabase; import common.commandline.response.CommandResult; import common.commandline.response.DefaultResponse; import common.commandline.response.PeopleDatabaseResponse; +import common.commandline.response.Response; import common.data.Person; import java.util.Collections; @@ -15,26 +16,30 @@ public enum Executables { PeopleDatabase peopleDatabase = (PeopleDatabase) args[0]; Person person = (Person) args[1]; boolean success = peopleDatabase.getCollection().add(person); - return new CommandResult(null, success ? DefaultResponse.OK : PeopleDatabaseResponse.FAILED_TO_ADD); + Response response = success ? DefaultResponse.OK : PeopleDatabaseResponse.FAILED_TO_ADD; + return new CommandResult(response.getMsg(), response); }), ADD_IF_MAX(args -> { PeopleDatabase peopleDatabase = (PeopleDatabase) args[0]; Person person = (Person) args[1]; Person last = peopleDatabase.getCollection().last(); if (person.compareTo(last) > 0) return ADD.executable.execute(args); - return new CommandResult(null, PeopleDatabaseResponse.FAILED_TO_ADD); + Response response = PeopleDatabaseResponse.FAILED_TO_ADD; + return new CommandResult(response.getMsg(), response); }), ADD_IF_MIN(args -> { PeopleDatabase peopleDatabase = (PeopleDatabase) args[0]; Person person = (Person) args[1]; Person first = peopleDatabase.getCollection().first(); if (person.compareTo(first) < 0) return ADD.executable.execute(args); - return new CommandResult(null, PeopleDatabaseResponse.FAILED_TO_ADD); + Response response = PeopleDatabaseResponse.FAILED_TO_ADD; + return new CommandResult(response.getMsg(), response); }), CLEAR(args -> { PeopleDatabase peopleDatabase = (PeopleDatabase) args[0]; peopleDatabase.getCollection().clear(); - return new CommandResult(null, DefaultResponse.OK); + Response response = DefaultResponse.OK; + return new CommandResult(response.getMsg(), response); }), FILTER_CONTAINS_NAME(args -> { PeopleDatabase peopleDatabase = (PeopleDatabase) args[0]; @@ -64,13 +69,15 @@ public enum Executables { PeopleDatabase peopleDatabase = (PeopleDatabase) args[0]; long id = (long) args[1]; boolean success = peopleDatabase.getCollection().removeIf(p -> p.getId().equals(id)); - return new CommandResult(null, success ? DefaultResponse.OK : PeopleDatabaseResponse.ELEMENT_NOT_FOUND); + Response response = success ? DefaultResponse.OK : PeopleDatabaseResponse.ELEMENT_NOT_FOUND; + return new CommandResult(response.getMsg(), response); }), SAVE(args -> { PeopleDatabase peopleDatabase = (PeopleDatabase) args[0]; try { peopleDatabase.save(); - return new CommandResult(null, DefaultResponse.OK); + Response response = DefaultResponse.OK; + return new CommandResult(response.getMsg(), response); } catch (Database.DatabaseSaveFailedException e) { return new CommandResult(e.getMessage(), PeopleDatabaseResponse.SAVE_FAILED); } @@ -83,11 +90,11 @@ public enum Executables { }), SUM_OF_HEIGHT(args -> { PeopleDatabase peopleDatabase = (PeopleDatabase) args[0]; - int sum = peopleDatabase.getCollection() + String sum = "Сумма ростов всех людей в коллекции - " + peopleDatabase.getCollection() .stream() .mapToInt(p -> p.getHeight() == null ? 0 : p.getHeight()) .sum(); - return new CommandResult("Сумма ростов всех людей в коллекции - " + sum, DefaultResponse.OK); + return new CommandResult(sum, DefaultResponse.OK); }), UPDATE(args -> { PeopleDatabase peopleDatabase = (PeopleDatabase) args[0]; @@ -97,7 +104,8 @@ public enum Executables { .stream() .filter(p -> p.getId().equals(id)) .findFirst(); - if (!optionalPerson.isPresent()) return new CommandResult(null, PeopleDatabaseResponse.ELEMENT_NOT_FOUND); + Response response = PeopleDatabaseResponse.ELEMENT_NOT_FOUND; + if (!optionalPerson.isPresent()) return new CommandResult(response.getMsg(), response); Person oldPerson = optionalPerson.get(); CommandResult result = REMOVE_BY_ID.executable.execute(new Object[]{ peopleDatabase, id }); if (result.getResponse() != DefaultResponse.OK) return result; diff --git a/Lab5Server/build/libs/Lab5Server-2.0.jar b/Lab5Server/build/libs/Lab5Server-2.0.jar index 9d96d09..e2ebd8f 100644 Binary files a/Lab5Server/build/libs/Lab5Server-2.0.jar and b/Lab5Server/build/libs/Lab5Server-2.0.jar differ diff --git a/Lab5Server/build/libs/connection.properties b/Lab5Server/build/libs/connection.properties index f5630d4..193632e 100644 --- a/Lab5Server/build/libs/connection.properties +++ b/Lab5Server/build/libs/connection.properties @@ -1,2 +1,2 @@ -hostname=localhost +hostname=s316304@helios.se.ifmo.ru port=1234 \ No newline at end of file diff --git a/Lab5Server/src/main/java/server/net/UDPServer.java b/Lab5Server/src/main/java/server/net/UDPServer.java index ee09deb..66b22c7 100644 --- a/Lab5Server/src/main/java/server/net/UDPServer.java +++ b/Lab5Server/src/main/java/server/net/UDPServer.java @@ -2,8 +2,10 @@ package server.net; import common.collection.PeopleDatabase; import common.commandline.Executable; +import common.commandline.Executables; import common.commandline.response.CommandResult; import common.commandline.response.DefaultResponse; +import common.commandline.response.Response; import java.io.*; import java.net.*; @@ -51,10 +53,8 @@ public class UDPServer { public void send(CommandResult result, InetAddress address, int port) { byte[] buffer; - try ( - ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); - ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteOutputStream) - ) { + try (ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream()) { + ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteOutputStream); objectOutputStream.writeObject(result); buffer = byteOutputStream.toByteArray(); DatagramPacket request = new DatagramPacket(buffer, buffer.length, address, port); @@ -80,17 +80,21 @@ public class UDPServer { if (args[0] == null) args[0] = peopleDatabase; result = command.execute(args); } catch (IOException e) { - logger.info("Не удалось преобразовать полученные данные, данные были повреждены во время передачи"); - result = new CommandResult(null, DefaultResponse.SERVER_ERROR); + logger.severe("Не удалось преобразовать полученные данные, данные были повреждены во время передачи"); + Response response = DefaultResponse.SERVER_ERROR; + result = new CommandResult(response.getMsg(), response); } catch (ClassNotFoundException e) { e.printStackTrace(); - logger.info("Не удалось преобразовать полученные данные, классов полученных объектов не существует"); - result = new CommandResult(null, DefaultResponse.CLASS_NOT_FOUND); + logger.severe("Не удалось преобразовать полученные данные, классов полученных объектов не существует"); + Response response = DefaultResponse.CLASS_NOT_FOUND; + result = new CommandResult(response.getMsg(), response); } catch (ClassCastException e) { - logger.info("Не удалось преобразовать полученные данные, ожидались объекты другого типа"); - result = new CommandResult(null, DefaultResponse.TYPE_ERROR); + logger.severe("Не удалось преобразовать полученные данные, ожидались объекты другого типа"); + Response response = DefaultResponse.TYPE_ERROR; + result = new CommandResult(response.getMsg(), response); } - logger.info("Команда выполнена, отправляем результат клиенту..."); + logger.info("Команда выполнена, сохраняем результат и отправляем результат клиенту..."); + Executables.SAVE.getExecutable().execute(new Object[]{ peopleDatabase }); send(result, request.getAddress(), request.getPort()); } }